Home | History | Annotate | Download | only in browser_plugin
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/browser/browser_plugin/browser_plugin_message_filter.h"
      6 
      7 #include "base/supports_user_data.h"
      8 #include "content/browser/browser_plugin/browser_plugin_guest.h"
      9 #include "content/browser/gpu/gpu_process_host.h"
     10 #include "content/browser/web_contents/web_contents_impl.h"
     11 #include "content/common/browser_plugin/browser_plugin_constants.h"
     12 #include "content/common/browser_plugin/browser_plugin_messages.h"
     13 #include "content/common/gpu/gpu_messages.h"
     14 #include "content/common/view_messages.h"
     15 #include "content/public/browser/browser_context.h"
     16 #include "content/public/browser/browser_plugin_guest_manager.h"
     17 #include "content/public/browser/browser_thread.h"
     18 #include "content/public/browser/render_view_host.h"
     19 
     20 namespace content {
     21 
     22 BrowserPluginMessageFilter::BrowserPluginMessageFilter(int render_process_id)
     23     : BrowserMessageFilter(BrowserPluginMsgStart),
     24       render_process_id_(render_process_id) {
     25 }
     26 
     27 BrowserPluginMessageFilter::~BrowserPluginMessageFilter() {
     28   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     29 }
     30 
     31 bool BrowserPluginMessageFilter::OnMessageReceived(
     32     const IPC::Message& message) {
     33   // Any message requested by a BrowserPluginGuest should be routed through
     34   // a BrowserPluginGuestManager.
     35   if (BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(message)) {
     36     ForwardMessageToGuest(message);
     37     // We always swallow messages destined for BrowserPluginGuestManager because
     38     // we're on the UI thread and fallback code is expected to be run on the IO
     39     // thread.
     40     return true;
     41   }
     42   bool handled = true;
     43   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     44   IPC_BEGIN_MESSAGE_MAP(BrowserPluginMessageFilter, message)
     45     IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_BuffersSwappedACK,
     46                         OnSwapBuffersACK)
     47     IPC_MESSAGE_UNHANDLED(handled = false)
     48   IPC_END_MESSAGE_MAP()
     49   return handled;
     50 }
     51 
     52 void BrowserPluginMessageFilter::OnDestruct() const {
     53   BrowserThread::DeleteOnIOThread::Destruct(this);
     54 }
     55 
     56 void BrowserPluginMessageFilter::OverrideThreadForMessage(
     57     const IPC::Message& message, BrowserThread::ID* thread) {
     58   if (BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(message))
     59     *thread = BrowserThread::UI;
     60 }
     61 
     62 static void BrowserPluginGuestMessageCallback(const IPC::Message& message,
     63                                               WebContents* guest_web_contents) {
     64   if (!guest_web_contents)
     65     return;
     66   static_cast<WebContentsImpl*>(guest_web_contents)->GetBrowserPluginGuest()->
     67       OnMessageReceivedFromEmbedder(message);
     68 }
     69 
     70 void BrowserPluginMessageFilter::ForwardMessageToGuest(
     71     const IPC::Message& message) {
     72   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     73   RenderProcessHostImpl* host = static_cast<RenderProcessHostImpl*>(
     74       RenderProcessHost::FromID(render_process_id_));
     75   if (!host)
     76     return;
     77 
     78   int instance_id = 0;
     79   // All allowed messages must have instance_id as their first parameter.
     80   PickleIterator iter(message);
     81   bool success = iter.ReadInt(&instance_id);
     82   DCHECK(success);
     83   host->GetBrowserContext()->GetGuestManager()->
     84       MaybeGetGuestByInstanceIDOrKill(
     85           instance_id,
     86           render_process_id_,
     87           base::Bind(&BrowserPluginGuestMessageCallback,
     88                      message));
     89 }
     90 
     91 void BrowserPluginMessageFilter::OnSwapBuffersACK(
     92     const FrameHostMsg_BuffersSwappedACK_Params& params) {
     93   GpuProcessHost* gpu_host = GpuProcessHost::FromID(params.gpu_host_id);
     94   if (!gpu_host)
     95     return;
     96   AcceleratedSurfaceMsg_BufferPresented_Params ack_params;
     97   ack_params.mailbox = params.mailbox;
     98   ack_params.sync_point = params.sync_point;
     99   gpu_host->Send(new AcceleratedSurfaceMsg_BufferPresented(params.gpu_route_id,
    100                                                            ack_params));
    101 }
    102 
    103 } // namespace content
    104