Merge "window.print() should close form validation bubble." to M59

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}
(cherry picked from commit 9dbd356b0cc52911caef089b09de63572cd9e39f)

Review-Url: https://codereview.chromium.org/2833303002 .
Cr-Commit-Position: refs/branch-heads/3071@{#151}
Cr-Branched-From: a106f0abbf69dad349d4aaf4bcc4f5d376dd2377-refs/heads/master@{#464641}
diff --git a/third_party/WebKit/Source/web/ChromeClientImpl.cpp b/third_party/WebKit/Source/web/ChromeClientImpl.cpp
index ba81ee3..86f75cd 100644
--- a/third_party/WebKit/Source/web/ChromeClientImpl.cpp
+++ b/third_party/WebKit/Source/web/ChromeClientImpl.cpp
@@ -687,6 +687,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 46f636b..783f621 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 f92b657..bfe78989 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_;