Move ContextMenu show/hide state tracking to WebContents

Currently, RenderWidgetHostView of the page tracks the visibility state of context
menus. However, the RWHV might go away during navigations (cross origin). On the
other hand, a context menu might outlive the RWHV and stay visible during the page
navigation which leads to a mismatch between the stored visibility state in the
new RWHV and the actual visibility of the context menu.

This CL will move this state tracking logic to WebContents which stays the same
during page navigations.

BUG=708182

Review-Url: https://codereview.chromium.org/2890143003
Cr-Commit-Position: refs/heads/master@{#477346}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 629c1f48..99cfc118 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -522,6 +522,7 @@
 #endif  // !defined(OS_ANDROID)
       mouse_lock_widget_(nullptr),
       is_overlay_content_(false),
+      showing_context_menu_(false),
       loading_weak_factory_(this),
       weak_factory_(this) {
   frame_tree_.SetFrameRemoveListener(
@@ -4394,7 +4395,7 @@
                                       const ContextMenuParams& params) {
   // If a renderer fires off a second command to show a context menu before the
   // first context menu is closed, just ignore it. https://crbug.com/707534
-  if (GetRenderWidgetHostView()->IsShowingContextMenu())
+  if (showing_context_menu_)
     return;
 
   ContextMenuParams context_menu_params(params);
@@ -4508,6 +4509,21 @@
   return frame && frame->has_focused_editable_element();
 }
 
+bool WebContentsImpl::IsShowingContextMenu() const {
+  return showing_context_menu_;
+}
+
+void WebContentsImpl::SetShowingContextMenu(bool showing) {
+  DCHECK_NE(showing_context_menu_, showing);
+  showing_context_menu_ = showing;
+
+  if (auto* view = GetRenderWidgetHostView()) {
+    // Notify the main frame's RWHV to run the platform-specific code, if any.
+    static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
+        showing);
+  }
+}
+
 void WebContentsImpl::ClearFocusedElement() {
   if (auto* frame = GetFocusedFrame())
     frame->ClearFocusedElement();
@@ -5660,6 +5676,10 @@
                          " releasing your website to the public."));
 }
 
+bool WebContentsImpl::IsShowingContextMenuOnPage() const {
+  return showing_context_menu_;
+}
+
 void WebContentsImpl::NotifyPreferencesChanged() {
   std::set<RenderViewHost*> render_view_host_set;
   for (FrameTreeNode* node : frame_tree_.Nodes()) {