blob: 3ccc537b5011f3630ead3454e29c1898dc543b81 [file] [log] [blame]
[email protected]93df81e2012-08-10 22:22:461// 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/renderer/pepper/pepper_in_process_router.h"
6
7#include "base/bind.h"
8#include "base/message_loop.h"
9#include "content/renderer/pepper/renderer_ppapi_host_impl.h"
10#include "ipc/ipc_message.h"
11#include "ipc/ipc_message_macros.h"
12#include "ipc/ipc_sender.h"
13#include "ppapi/proxy/ppapi_messages.h"
14#include "ppapi/shared_impl/ppapi_globals.h"
15#include "ppapi/shared_impl/resource_tracker.h"
16
17namespace content {
18
19class PepperInProcessRouter::DummyBrowserChannel : public IPC::Sender {
20 public:
21 DummyBrowserChannel() {}
22 ~DummyBrowserChannel() {}
23
24 // Sender implementation.
25 virtual bool Send(IPC::Message* msg) OVERRIDE {
26 // See the class comment in the header file for what this is about.
27 NOTREACHED();
28 delete msg;
29 return false;
30 }
31};
32
33class PepperInProcessRouter::PluginToHostRouter : public IPC::Sender {
34 public:
35 PluginToHostRouter(RendererPpapiHostImpl* host_impl);
36 virtual ~PluginToHostRouter() {}
37
38 // Sender implementation.
39 virtual bool Send(IPC::Message* msg) OVERRIDE;
40
41 private:
42 void DoSend(IPC::Message* msg);
43
44 RendererPpapiHostImpl* host_impl_;
45
46 base::WeakPtrFactory<PluginToHostRouter> weak_factory_;
47
48 DISALLOW_COPY_AND_ASSIGN(PluginToHostRouter);
49};
50
51PepperInProcessRouter::PluginToHostRouter::PluginToHostRouter(
52 RendererPpapiHostImpl* host_impl)
53 : host_impl_(host_impl),
54 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
55}
56
57bool PepperInProcessRouter::PluginToHostRouter::Send(
58 IPC::Message* msg) {
59 // Don't directly call into the message handler to avoid reentrancy. The IPC
60 // systen assumes everything is executed from the message loop, so emulate
61 // the same thing for in-process.
62 MessageLoop::current()->PostTask(FROM_HERE,
63 base::Bind(&PluginToHostRouter::DoSend, weak_factory_.GetWeakPtr(),
64 base::Owned(msg)));
65 return true;
66}
67
68void PepperInProcessRouter::PluginToHostRouter::DoSend(
69 IPC::Message* msg) {
70 host_impl_->GetPpapiHost()->OnMessageReceived(*msg);
71}
72
73// HostToPluginRouter ---------------------------------------------------------
74
75class PepperInProcessRouter::HostToPluginRouter : public IPC::Sender {
76 public:
77 HostToPluginRouter();
78 virtual ~HostToPluginRouter() {}
79
80 // Sender implementation.
81 virtual bool Send(IPC::Message* msg) OVERRIDE;
82
83 private:
84 void DispatchMsg(IPC::Message* msg);
85
86 void OnMsgResourceReply(
87 const ppapi::proxy::ResourceMessageReplyParams& reply_params,
88 const IPC::Message& nested_msg);
89
90 base::WeakPtrFactory<HostToPluginRouter> weak_factory_;
91
92 DISALLOW_COPY_AND_ASSIGN(HostToPluginRouter);
93};
94
95PepperInProcessRouter::HostToPluginRouter::HostToPluginRouter()
96 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
97}
98
99bool PepperInProcessRouter::HostToPluginRouter::Send(
100 IPC::Message* msg) {
101 // As in the PluginToHostRouter, dispatch from the message loop.
102 MessageLoop::current()->PostTask(FROM_HERE,
103 base::Bind(&HostToPluginRouter::DispatchMsg,
104 weak_factory_.GetWeakPtr(),
105 base::Owned(msg)));
106 return true;
107}
108
109void PepperInProcessRouter::HostToPluginRouter::DispatchMsg(
110 IPC::Message* msg) {
111 // Emulate the proxy by dispatching the relevant message here.
112 IPC_BEGIN_MESSAGE_MAP(HostToPluginRouter, *msg)
113 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply, OnMsgResourceReply)
114 IPC_END_MESSAGE_MAP()
115}
116
117void PepperInProcessRouter::HostToPluginRouter::OnMsgResourceReply(
118 const ppapi::proxy::ResourceMessageReplyParams& reply_params,
119 const IPC::Message& nested_msg) {
120 ppapi::Resource* resource =
121 ppapi::PpapiGlobals::Get()->GetResourceTracker()->GetResource(
122 reply_params.pp_resource());
123 if (!resource) {
124 // The resource could have been destroyed while the async processing was
125 // pending. Just drop the message.
126 return;
127 }
128 resource->OnReplyReceived(reply_params, nested_msg);
129}
130
131PepperInProcessRouter::PepperInProcessRouter(
132 RendererPpapiHostImpl* host_impl)
133 : host_to_plugin_router_(new HostToPluginRouter),
134 plugin_to_host_router_(new PluginToHostRouter(host_impl)) {
135}
136
137PepperInProcessRouter::~PepperInProcessRouter() {
138}
139
140IPC::Sender* PepperInProcessRouter::GetPluginToRendererSender() {
141 return plugin_to_host_router_.get();
142}
143
144IPC::Sender* PepperInProcessRouter::GetRendererToPluginSender() {
145 return host_to_plugin_router_.get();
146}
147
148ppapi::proxy::Connection PepperInProcessRouter::GetPluginConnection() {
149 return ppapi::proxy::Connection(dummy_browser_channel_.get(),
150 plugin_to_host_router_.get());
151}
152
153} // namespace content