window.print() should close form validation bubble.

Usually, window.open() deactivates the origin window and validation bubble on the
origin window is closed. However, if window.print() is executed, it suspends message
loop of the window, and deactivation isn't noticed until print dialog is closed.
So, we need to close validation popup explicitly for window.print().

BUG=713686

Review-Url: https://codereview.chromium.org/2834783002
Cr-Commit-Position: refs/heads/master@{#466273}
diff --git a/third_party/WebKit/Source/web/ChromeClientImpl.cpp b/third_party/WebKit/Source/web/ChromeClientImpl.cpp
index b372601e..8a65a6ac 100644
--- a/third_party/WebKit/Source/web/ChromeClientImpl.cpp
+++ b/third_party/WebKit/Source/web/ChromeClientImpl.cpp
@@ -684,6 +684,7 @@
 }
 
 void ChromeClientImpl::PrintDelegate(LocalFrame* frame) {
+  NotifyPopupOpeningObservers();
   if (web_view_->Client())
     web_view_->Client()->PrintPage(WebLocalFrameImpl::FromFrame(frame));
 }
diff --git a/third_party/WebKit/Source/web/ChromeClientImpl.h b/third_party/WebKit/Source/web/ChromeClientImpl.h
index fdff26b..9edea04 100644
--- a/third_party/WebKit/Source/web/ChromeClientImpl.h
+++ b/third_party/WebKit/Source/web/ChromeClientImpl.h
@@ -226,6 +226,8 @@
 
   double LastFrameTimeMonotonic() const override;
 
+  void RegisterPopupOpeningObserver(PopupOpeningObserver*) override;
+  void UnregisterPopupOpeningObserver(PopupOpeningObserver*) override;
   void NotifyPopupOpeningObservers() const;
 
   void InstallSupplements(LocalFrame&) override;
@@ -236,8 +238,6 @@
   explicit ChromeClientImpl(WebViewImpl*);
 
   bool IsChromeClientImpl() const override { return true; }
-  void RegisterPopupOpeningObserver(PopupOpeningObserver*) override;
-  void UnregisterPopupOpeningObserver(PopupOpeningObserver*) override;
 
   void SetCursor(const WebCursorInfo&, LocalFrame*);
 
diff --git a/third_party/WebKit/Source/web/ValidationMessageClientImpl.cpp b/third_party/WebKit/Source/web/ValidationMessageClientImpl.cpp
index 0608b48..784a1a2 100644
--- a/third_party/WebKit/Source/web/ValidationMessageClientImpl.cpp
+++ b/third_party/WebKit/Source/web/ValidationMessageClientImpl.cpp
@@ -84,6 +84,7 @@
   web_view_.Client()->ShowValidationMessage(
       anchor_in_viewport, message_, ToWebTextDirection(message_dir),
       sub_message, ToWebTextDirection(sub_message_dir));
+  web_view_.ChromeClient().RegisterPopupOpeningObserver(this);
 
   finish_time_ =
       MonotonicallyIncreasingTime() +
@@ -105,6 +106,7 @@
   message_ = String();
   finish_time_ = 0;
   web_view_.Client()->HideValidationMessage();
+  web_view_.ChromeClient().UnregisterPopupOpeningObserver(this);
 }
 
 bool ValidationMessageClientImpl::IsValidationMessageVisible(
@@ -152,6 +154,11 @@
     HideValidationMessage(*current_anchor_);
 }
 
+void ValidationMessageClientImpl::WillOpenPopup() {
+  if (current_anchor_)
+    HideValidationMessage(*current_anchor_);
+}
+
 DEFINE_TRACE(ValidationMessageClientImpl) {
   visitor->Trace(current_anchor_);
   ValidationMessageClient::Trace(visitor);
diff --git a/third_party/WebKit/Source/web/ValidationMessageClientImpl.h b/third_party/WebKit/Source/web/ValidationMessageClientImpl.h
index 7ed0c7e..1400d78 100644
--- a/third_party/WebKit/Source/web/ValidationMessageClientImpl.h
+++ b/third_party/WebKit/Source/web/ValidationMessageClientImpl.h
@@ -26,6 +26,7 @@
 #ifndef ValidationMessageClientImpl_h
 #define ValidationMessageClientImpl_h
 
+#include "core/page/PopupOpeningObserver.h"
 #include "core/page/ValidationMessageClient.h"
 #include "platform/Timer.h"
 #include "platform/geometry/IntRect.h"
@@ -39,7 +40,8 @@
 
 class ValidationMessageClientImpl final
     : public GarbageCollectedFinalized<ValidationMessageClientImpl>,
-      public ValidationMessageClient {
+      public ValidationMessageClient,
+      private PopupOpeningObserver {
   USING_GARBAGE_COLLECTED_MIXIN(ValidationMessageClientImpl);
 
  public:
@@ -64,6 +66,9 @@
   void DocumentDetached(const Document&) override;
   void WillBeDestroyed() override;
 
+  // PopupOpeningObserver function
+  void WillOpenPopup() override;
+
   WebViewImpl& web_view_;
   Member<const Element> current_anchor_;
   String message_;