Chromecast: add platform hooks to inject browser/renderer message filters.

R=lcwu@chromium.org,byungchul@chromium.org
BUG=internal b/19004169

Review URL: https://codereview.chromium.org/917063003

Cr-Commit-Position: refs/heads/master@{#316886}
diff --git a/chromecast/browser/cast_content_browser_client.cc b/chromecast/browser/cast_content_browser_client.cc
index 57cbbf2..1418da01 100644
--- a/chromecast/browser/cast_content_browser_client.cc
+++ b/chromecast/browser/cast_content_browser_client.cc
@@ -73,6 +73,11 @@
       new media::CmaMessageFilterHost(host->GetID()));
   host->AddFilter(cma_message_filter.get());
 #endif  // !defined(OS_ANDROID)
+
+  auto extra_filters = PlatformGetBrowserMessageFilters();
+  for (auto const& filter : extra_filters) {
+    host->AddFilter(filter.get());
+  }
 }
 
 net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext(
diff --git a/chromecast/browser/cast_content_browser_client.h b/chromecast/browser/cast_content_browser_client.h
index 31d0eeb..fd84b728 100644
--- a/chromecast/browser/cast_content_browser_client.h
+++ b/chromecast/browser/cast_content_browser_client.h
@@ -15,19 +15,29 @@
 class CrashHandlerHostLinux;
 }
 
+namespace content {
+class BrowserMessageFilter;
+}
+
 namespace chromecast {
 namespace shell {
 
 class CastBrowserMainParts;
 class URLRequestContextFactory;
 
-void PlatformAppendExtraCommandLineSwitches(base::CommandLine* command_line);
-
 class CastContentBrowserClient: public content::ContentBrowserClient {
  public:
   CastContentBrowserClient();
   ~CastContentBrowserClient() override;
 
+  // Appends extra command line arguments before launching a new process.
+  void PlatformAppendExtraCommandLineSwitches(base::CommandLine* command_line);
+
+  // Returns any BrowserMessageFilters from the platform implementation that
+  // should be added when launching a new render process.
+  std::vector<scoped_refptr<content::BrowserMessageFilter>>
+  PlatformGetBrowserMessageFilters();
+
   // content::ContentBrowserClient implementation:
   content::BrowserMainParts* CreateBrowserMainParts(
       const content::MainFunctionParams& parameters) override;
diff --git a/chromecast/browser/cast_content_browser_client_simple.cc b/chromecast/browser/cast_content_browser_client_simple.cc
index 132a2c9..34012b4 100644
--- a/chromecast/browser/cast_content_browser_client_simple.cc
+++ b/chromecast/browser/cast_content_browser_client_simple.cc
@@ -4,10 +4,18 @@
 
 #include "chromecast/browser/cast_content_browser_client.h"
 
+#include "content/public/browser/browser_message_filter.h"
+
 namespace chromecast {
 namespace shell {
 
-void PlatformAppendExtraCommandLineSwitches(base::CommandLine* command_line) {
+void CastContentBrowserClient::PlatformAppendExtraCommandLineSwitches(
+    base::CommandLine* command_line) {
+}
+
+std::vector<scoped_refptr<content::BrowserMessageFilter>>
+CastContentBrowserClient::PlatformGetBrowserMessageFilters() {
+  return std::vector<scoped_refptr<content::BrowserMessageFilter>>();
 }
 
 }  // namespace shell
diff --git a/chromecast/renderer/cast_content_renderer_client.cc b/chromecast/renderer/cast_content_renderer_client.cc
index 1f9cc976..15f82b61 100644
--- a/chromecast/renderer/cast_content_renderer_client.cc
+++ b/chromecast/renderer/cast_content_renderer_client.cc
@@ -19,6 +19,7 @@
 #include "content/public/renderer/render_view.h"
 #include "content/public/renderer/render_view_observer.h"
 #include "crypto/nss_util.h"
+#include "ipc/message_filter.h"
 #include "third_party/WebKit/public/platform/WebColor.h"
 #include "third_party/WebKit/public/web/WebSettings.h"
 #include "third_party/WebKit/public/web/WebView.h"
@@ -105,7 +106,8 @@
   PlatformPollFreemem();
 #endif
 
-  cast_observer_.reset(new CastRenderProcessObserver());
+  cast_observer_.reset(
+      new CastRenderProcessObserver(PlatformGetRendererMessageFilters()));
 
   prescient_networking_dispatcher_.reset(
       new network_hints::PrescientNetworkingDispatcher());
diff --git a/chromecast/renderer/cast_content_renderer_client.h b/chromecast/renderer/cast_content_renderer_client.h
index 9f0dfbd..c41b98d6 100644
--- a/chromecast/renderer/cast_content_renderer_client.h
+++ b/chromecast/renderer/cast_content_renderer_client.h
@@ -10,6 +10,10 @@
 #include "base/macros.h"
 #include "content/public/renderer/content_renderer_client.h"
 
+namespace IPC {
+class MessageFilter;
+}
+
 namespace network_hints {
 class PrescientNetworkingDispatcher;
 }  // namespace network_hints
@@ -18,6 +22,7 @@
 namespace shell {
 class CastRenderProcessObserver;
 
+// Adds any platform-specific bindings to the current frame.
 void PlatformAddRendererNativeBindings(blink::WebLocalFrame* frame);
 
 class CastContentRendererClient : public content::ContentRendererClient {
@@ -25,6 +30,11 @@
   CastContentRendererClient();
   ~CastContentRendererClient() override;
 
+  // Returns any MessageFilters from the platform implementation that should
+  // be added to the render process.
+  std::vector<scoped_refptr<IPC::MessageFilter>>
+  PlatformGetRendererMessageFilters();
+
   // ContentRendererClient implementation:
   void RenderThreadStarted() override;
   void RenderViewCreated(content::RenderView* render_view) override;
diff --git a/chromecast/renderer/cast_content_renderer_client_simple.cc b/chromecast/renderer/cast_content_renderer_client_simple.cc
index e8d40c6..5ad4fccb 100644
--- a/chromecast/renderer/cast_content_renderer_client_simple.cc
+++ b/chromecast/renderer/cast_content_renderer_client_simple.cc
@@ -4,11 +4,18 @@
 
 #include "chromecast/renderer/cast_content_renderer_client.h"
 
+#include "ipc/message_filter.h"
+
 namespace chromecast {
 namespace shell {
 
 void PlatformAddRendererNativeBindings(blink::WebLocalFrame* frame) {
 }
 
+std::vector<scoped_refptr<IPC::MessageFilter>>
+CastContentRendererClient::PlatformGetRendererMessageFilters() {
+  return std::vector<scoped_refptr<IPC::MessageFilter>>();
+}
+
 }  // namespace shell
 }  // namespace chromecast
diff --git a/chromecast/renderer/cast_render_process_observer.cc b/chromecast/renderer/cast_render_process_observer.cc
index 60638935..316fd616 100644
--- a/chromecast/renderer/cast_render_process_observer.cc
+++ b/chromecast/renderer/cast_render_process_observer.cc
@@ -10,7 +10,10 @@
 namespace chromecast {
 namespace shell {
 
-CastRenderProcessObserver::CastRenderProcessObserver() {
+CastRenderProcessObserver::CastRenderProcessObserver(
+    const std::vector<scoped_refptr<IPC::MessageFilter>>&
+        platform_message_filters)
+    : platform_message_filters_(platform_message_filters) {
   content::RenderThread* thread = content::RenderThread::Get();
   thread->AddObserver(this);
   CreateCustomFilters();
@@ -22,22 +25,32 @@
 }
 
 void CastRenderProcessObserver::CreateCustomFilters() {
-#if !defined(OS_ANDROID)
   content::RenderThread* thread = content::RenderThread::Get();
+#if !defined(OS_ANDROID)
   cma_message_filter_proxy_ =
       new media::CmaMessageFilterProxy(thread->GetIOMessageLoopProxy());
   thread->AddFilter(cma_message_filter_proxy_.get());
 #endif  // !defined(OS_ANDROID)
+  for (const auto& filter : platform_message_filters_) {
+    thread->AddFilter(filter.get());
+  }
 }
 
 void CastRenderProcessObserver::OnRenderProcessShutdown() {
-#if !defined(OS_ANDROID)
   content::RenderThread* thread = content::RenderThread::Get();
+#if !defined(OS_ANDROID)
   if (cma_message_filter_proxy_.get()) {
     thread->RemoveFilter(cma_message_filter_proxy_.get());
     cma_message_filter_proxy_ = NULL;
   }
 #endif  // !defined(OS_ANDROID)
+  for (auto& filter : platform_message_filters_) {
+    if (filter.get()) {
+      thread->RemoveFilter(filter.get());
+      filter = nullptr;
+    }
+  }
+  platform_message_filters_.clear();
 }
 
 }  // namespace shell
diff --git a/chromecast/renderer/cast_render_process_observer.h b/chromecast/renderer/cast_render_process_observer.h
index ca992bc9..0c28b581 100644
--- a/chromecast/renderer/cast_render_process_observer.h
+++ b/chromecast/renderer/cast_render_process_observer.h
@@ -5,10 +5,16 @@
 #ifndef CHROMECAST_RENDERER_CAST_RENDER_PROCESS_OBSERVER_H_
 #define CHROMECAST_RENDERER_CAST_RENDER_PROCESS_OBSERVER_H_
 
+#include <vector>
+
 #include "base/macros.h"
 #include "base/memory/ref_counted.h"
 #include "content/public/renderer/render_process_observer.h"
 
+namespace IPC {
+class MessageFilter;
+}
+
 namespace chromecast {
 namespace media {
 class CmaMessageFilterProxy;
@@ -18,7 +24,9 @@
 
 class CastRenderProcessObserver : public content::RenderProcessObserver {
  public:
-  CastRenderProcessObserver();
+  CastRenderProcessObserver(
+      const std::vector<scoped_refptr<IPC::MessageFilter>>&
+          platform_message_filters);
   ~CastRenderProcessObserver() override;
 
  private:
@@ -30,6 +38,7 @@
 #if !defined(OS_ANDROID)
   scoped_refptr<media::CmaMessageFilterProxy> cma_message_filter_proxy_;
 #endif  // !defined(OS_ANDROID)
+  std::vector<scoped_refptr<IPC::MessageFilter>> platform_message_filters_;
 
   DISALLOW_COPY_AND_ASSIGN(CastRenderProcessObserver);
 };