Click to Open PDF: Move open PDF message handler to PDFPluginObserver.

Move the open PDF handler into PDFPluginObserver, so it can be run on
android as well.

Bug: 737787

Change-Id: I7e0b8848d408b6e497b98c30be0ee29bff562602
Reviewed-on: https://chromium-review.googlesource.com/582205
Reviewed-by: Lei Zhang <[email protected]>
Reviewed-by: Tommy Li <[email protected]>
Commit-Queue: Amber Won <[email protected]>
Cr-Commit-Position: refs/heads/master@{#490507}
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index 6cdacf36..a2fff11 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -950,6 +950,8 @@
     "platform_util_internal.h",
     "platform_util_mac.mm",
     "platform_util_win.cc",
+    "plugins/pdf_plugin_placeholder_observer.cc",
+    "plugins/pdf_plugin_placeholder_observer.h",
     "policy/chrome_browser_policy_connector.cc",
     "policy/chrome_browser_policy_connector.h",
     "policy/cloud/cloud_policy_invalidator.cc",
diff --git a/chrome/browser/plugins/pdf_plugin_placeholder_observer.cc b/chrome/browser/plugins/pdf_plugin_placeholder_observer.cc
new file mode 100644
index 0000000..65fc578
--- /dev/null
+++ b/chrome/browser/plugins/pdf_plugin_placeholder_observer.cc
@@ -0,0 +1,46 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/plugins/pdf_plugin_placeholder_observer.h"
+
+#include "chrome/common/render_messages.h"
+#include "content/public/browser/child_process_security_policy.h"
+#include "content/public/browser/render_frame_host.h"
+
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(PDFPluginPlaceholderObserver);
+
+PDFPluginPlaceholderObserver::PDFPluginPlaceholderObserver(
+    content::WebContents* web_contents)
+    : content::WebContentsObserver(web_contents) {}
+
+PDFPluginPlaceholderObserver::~PDFPluginPlaceholderObserver() {}
+
+bool PDFPluginPlaceholderObserver::OnMessageReceived(
+    const IPC::Message& message,
+    content::RenderFrameHost* render_frame_host) {
+  IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PDFPluginPlaceholderObserver, message,
+                                   render_frame_host)
+    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_OpenPDF, OnOpenPDF)
+    IPC_MESSAGE_UNHANDLED(return false)
+  IPC_END_MESSAGE_MAP()
+
+  return true;
+}
+
+void PDFPluginPlaceholderObserver::OnOpenPDF(
+    content::RenderFrameHost* render_frame_host,
+    const GURL& url) {
+  if (!content::ChildProcessSecurityPolicy::GetInstance()->CanRequestURL(
+          render_frame_host->GetRoutingID(), url)) {
+    return;
+  }
+
+  web_contents()->OpenURL(content::OpenURLParams(
+      url,
+      content::Referrer::SanitizeForRequest(
+          url, content::Referrer(web_contents()->GetURL(),
+                                 blink::kWebReferrerPolicyDefault)),
+      WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_AUTO_BOOKMARK,
+      false));
+}
diff --git a/chrome/browser/plugins/pdf_plugin_placeholder_observer.h b/chrome/browser/plugins/pdf_plugin_placeholder_observer.h
new file mode 100644
index 0000000..63b37ae3
--- /dev/null
+++ b/chrome/browser/plugins/pdf_plugin_placeholder_observer.h
@@ -0,0 +1,36 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_PLUGINS_PDF_PLUGIN_PLACEHOLDER_OBSERVER_H_
+#define CHROME_BROWSER_PLUGINS_PDF_PLUGIN_PLACEHOLDER_OBSERVER_H_
+
+#include "base/macros.h"
+#include "content/public/browser/web_contents_observer.h"
+#include "content/public/browser/web_contents_user_data.h"
+
+// Separate class from PluginObserver, since PluginObserver is created only when
+// ENABLE_PLUGINS is true. PDFPluginPlaceholderObserver should also run on
+// Android, where ENABLE_PLUGINS is false.
+class PDFPluginPlaceholderObserver
+    : public content::WebContentsObserver,
+      public content::WebContentsUserData<PDFPluginPlaceholderObserver> {
+ public:
+  ~PDFPluginPlaceholderObserver() override;
+
+  // content::WebContentsObserver:
+  bool OnMessageReceived(const IPC::Message& message,
+                         content::RenderFrameHost* render_frame_host) override;
+
+ private:
+  friend class content::WebContentsUserData<PDFPluginPlaceholderObserver>;
+
+  explicit PDFPluginPlaceholderObserver(content::WebContents* web_contents);
+
+  // Message handlers:
+  void OnOpenPDF(content::RenderFrameHost* render_frame_host, const GURL& url);
+
+  DISALLOW_COPY_AND_ASSIGN(PDFPluginPlaceholderObserver);
+};
+
+#endif  // CHROME_BROWSER_PLUGINS_PDF_PLUGIN_PLACEHOLDER_OBSERVER_H_
diff --git a/chrome/browser/plugins/plugin_observer.cc b/chrome/browser/plugins/plugin_observer.cc
index f5a46fae..0ccaaf6 100644
--- a/chrome/browser/plugins/plugin_observer.cc
+++ b/chrome/browser/plugins/plugin_observer.cc
@@ -36,7 +36,6 @@
 #include "components/infobars/core/simple_alert_infobar_delegate.h"
 #include "components/metrics_services_manager/metrics_services_manager.h"
 #include "content/public/browser/browser_thread.h"
-#include "content/public/browser/child_process_security_policy.h"
 #include "content/public/browser/plugin_service.h"
 #include "content/public/browser/render_frame_host.h"
 #include "content/public/browser/render_view_host.h"
@@ -257,7 +256,6 @@
 bool PluginObserver::OnMessageReceived(
       const IPC::Message& message,
       content::RenderFrameHost* render_frame_host) {
-  bool handled = true;
   IPC_BEGIN_MESSAGE_MAP(PluginObserver, message)
     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_BlockedOutdatedPlugin,
                         OnBlockedOutdatedPlugin)
@@ -269,14 +267,6 @@
                         OnShowFlashPermissionBubble)
     IPC_MESSAGE_HANDLER(ChromeViewHostMsg_CouldNotLoadPlugin,
                         OnCouldNotLoadPlugin)
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-
-  if (handled)
-    return true;
-
-  IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PluginObserver, message, render_frame_host)
-    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_OpenPDF, OnOpenPDF)
     IPC_MESSAGE_UNHANDLED(return false)
   IPC_END_MESSAGE_MAP()
 
@@ -344,18 +334,3 @@
                                  plugin_name),
       true);
 }
-
-void PluginObserver::OnOpenPDF(content::RenderFrameHost* render_frame_host,
-                               const GURL& url) {
-  if (!content::ChildProcessSecurityPolicy::GetInstance()->CanRequestURL(
-          render_frame_host->GetRoutingID(), url))
-    return;
-
-  web_contents()->OpenURL(content::OpenURLParams(
-      url,
-      content::Referrer::SanitizeForRequest(
-          url, content::Referrer(web_contents()->GetURL(),
-                                 blink::kWebReferrerPolicyDefault)),
-      WindowOpenDisposition::NEW_FOREGROUND_TAB,
-      ui::PAGE_TRANSITION_AUTO_BOOKMARK, false));
-}
diff --git a/chrome/browser/plugins/plugin_observer.h b/chrome/browser/plugins/plugin_observer.h
index e9ca2f2..e3cfec3 100644
--- a/chrome/browser/plugins/plugin_observer.h
+++ b/chrome/browser/plugins/plugin_observer.h
@@ -49,7 +49,6 @@
   void RemoveComponentObserver(int placeholder_id);
   void OnShowFlashPermissionBubble();
   void OnCouldNotLoadPlugin(const base::FilePath& plugin_path);
-  void OnOpenPDF(content::RenderFrameHost* render_frame_host, const GURL& url);
 
   // Stores all PluginPlaceholderHosts, keyed by their routing ID.
   std::map<int, std::unique_ptr<PluginPlaceholderHost>> plugin_placeholders_;
diff --git a/chrome/browser/ui/tab_helpers.cc b/chrome/browser/ui/tab_helpers.cc
index ed5cec1..3f4cea2 100644
--- a/chrome/browser/ui/tab_helpers.cc
+++ b/chrome/browser/ui/tab_helpers.cc
@@ -34,6 +34,7 @@
 #include "chrome/browser/page_load_metrics/page_load_metrics_initialize.h"
 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
 #include "chrome/browser/permissions/permission_request_manager.h"
+#include "chrome/browser/plugins/pdf_plugin_placeholder_observer.h"
 #include "chrome/browser/predictors/loading_predictor_factory.h"
 #include "chrome/browser/predictors/resource_prefetch_predictor_tab_helper.h"
 #include "chrome/browser/prerender/prerender_tab_helper.h"
@@ -213,6 +214,7 @@
   NavigationCorrectionTabObserver::CreateForWebContents(web_contents);
   NavigationMetricsRecorder::CreateForWebContents(web_contents);
   chrome::InitializePageLoadMetricsForWebContents(web_contents);
+  PDFPluginPlaceholderObserver::CreateForWebContents(web_contents);
   PermissionRequestManager::CreateForWebContents(web_contents);
   PopupBlockerTabHelper::CreateForWebContents(web_contents);
   PrefsTabHelper::CreateForWebContents(web_contents);