Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Side by Side Diff: ppapi/proxy/ppb_broker_proxy.cc

Issue 6833002: Implemented PPB_Broker_Proxy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/proxy/ppb_broker_proxy.h ('k') | webkit/plugins/ppapi/ppb_broker_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "ppapi/proxy/ppb_broker_proxy.h"
6
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/trusted/ppb_broker_trusted.h"
9 #include "ppapi/proxy/plugin_dispatcher.h"
10 #include "ppapi/proxy/plugin_resource.h"
11 #include "ppapi/proxy/ppapi_messages.h"
12
13 namespace pp {
14 namespace proxy {
15
16 class Broker : public PluginResource {
17 public:
18 explicit Broker(const HostResource& resource);
19
20 // Resource overrides.
21 virtual Broker* AsBroker() { return this; }
22
23 int32_t Connect(PP_Resource broker, PP_CompletionCallback connect_callback);
24 int32_t GetHandle(PP_Resource broker, int32_t* handle);
25
26 private:
27 DISALLOW_COPY_AND_ASSIGN(Broker);
28 };
29
30 Broker::Broker(const HostResource& resource)
31 : PluginResource(resource) {
32 }
33
34 int32_t Broker::Connect(PP_Resource broker,
35 PP_CompletionCallback connect_callback) {
36 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance());
37 if (!dispatcher)
38 return PP_ERROR_BADRESOURCE;
39 if (!connect_callback.func) {
40 // Synchronous calls are not supported.
41 return PP_ERROR_BADARGUMENT;
42 }
43
44 bool success = dispatcher->Send(new PpapiHostMsg_PPBBroker_Connect(
45 INTERFACE_ID_PPB_BROKER,
46 host_resource(),
47 dispatcher->callback_tracker().SendCallback(connect_callback)));
48 return success ? PP_ERROR_WOULDBLOCK : PP_ERROR_FAILED;
49 }
50
51 // Assumes the IPC does not populate the ipc_handle unless result is S_OK.
52 // TODO(ddorwin): Depending on how what is decided regarding ownership,
53 // it may make sense to cache the handle here.
brettw 2011/04/13 17:53:40 I think we should have the Broker object manage th
ddorwin 2011/04/14 18:08:38 Done. I also updated several comments about handl
54 int32_t Broker::GetHandle(PP_Resource broker, int32_t* handle) {
55 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance());
56 if (!dispatcher)
57 return PP_ERROR_BADRESOURCE;
58
59 IPC::PlatformFileForTransit ipc_handle = IPC::InvalidPlatformFileForTransit();
60 int32_t result = PP_ERROR_FAILED;
61 bool success = dispatcher->Send(new PpapiHostMsg_PPBBroker_GetHandle(
62 INTERFACE_ID_PPB_BROKER,
63 host_resource(),
64 &ipc_handle,
65 &result));
66 if (!success)
67 return PP_ERROR_FAILED;
68 if (result != PP_OK)
69 return result;
70
71 base::SyncSocket::Handle socket_handle =
72 IPC::PlatformFileForTransitToPlatformFile(ipc_handle);
73 *handle = socket_handle;
74 return result;
75 }
76
77 namespace {
78
79 PP_Resource CreateTrusted(PP_Instance instance) {
80 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
81 if (!dispatcher)
82 return 0;
83
84 HostResource result;
85 dispatcher->Send(new PpapiHostMsg_PPBBroker_Create(
86 INTERFACE_ID_PPB_BROKER, instance, &result));
87 if (result.is_null())
88 return 0;
89
90 linked_ptr<Broker> object(new Broker(result));
91 return PluginResourceTracker::GetInstance()->AddResource(object);
92 }
93
94 PP_Bool IsBrokerTrusted(PP_Resource resource) {
95 Broker* object = PluginResource::GetAs<Broker>(resource);
96 return BoolToPPBool(!!object);
97 }
98
99 int32_t Connect(PP_Resource resource,
100 PP_CompletionCallback connect_callback) {
101 Broker* object = PluginResource::GetAs<Broker>(resource);
102 if (!object)
103 return PP_ERROR_BADRESOURCE;
104 return object->Connect(resource, connect_callback);
105 }
106
107 int32_t GetHandle(PP_Resource resource, int32_t* handle) {
108 Broker* object = PluginResource::GetAs<Broker>(resource);
109 if (!object)
110 return PP_ERROR_BADRESOURCE;
111 return object->GetHandle(resource, handle);
112 }
113
114 const PPB_BrokerTrusted broker_interface = {
115 &CreateTrusted,
116 &IsBrokerTrusted,
117 &Connect,
118 &GetHandle,
119 };
120
121 InterfaceProxy* CreateBrokerProxy(Dispatcher* dispatcher,
122 const void* target_interface) {
123 return new PPB_Broker_Proxy(dispatcher, target_interface);
124 }
125
126 base::PlatformFile IntToPlatformFile(int32_t handle) {
127 #if defined(OS_WIN)
128 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle));
129 #elif defined(OS_POSIX)
130 return handle;
131 #else
132 #error Not implemented.
133 #endif
134 }
135
136 } // namespace
137
138 PPB_Broker_Proxy::PPB_Broker_Proxy(Dispatcher* dispatcher,
139 const void* target_interface)
140 : InterfaceProxy(dispatcher, target_interface) {
141 }
142
143 PPB_Broker_Proxy::~PPB_Broker_Proxy() {
144 }
145
146 // static
147 const InterfaceProxy::Info* PPB_Broker_Proxy::GetInfo() {
148 static const Info info = {
149 &broker_interface,
150 PPB_BROKER_TRUSTED_INTERFACE,
151 INTERFACE_ID_PPB_BROKER,
152 false,
brettw 2011/04/13 17:53:40 This should be true for is_trusted.
ddorwin 2011/04/14 18:08:38 Done.
153 &CreateBrokerProxy,
154 };
155 return &info;
156 }
157
158 bool PPB_Broker_Proxy::OnMessageReceived(const IPC::Message& msg) {
159 bool handled = true;
160 IPC_BEGIN_MESSAGE_MAP(PPB_Broker_Proxy, msg)
161 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBroker_Create, OnMsgCreate)
162 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBroker_Connect, OnMsgConnect)
163 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBroker_GetHandle, OnMsgGetHandle)
164 IPC_MESSAGE_UNHANDLED(handled = false)
165 IPC_END_MESSAGE_MAP()
166 return handled;
167 }
168
169 void PPB_Broker_Proxy::OnMsgCreate(PP_Instance instance,
170 HostResource* result_resource) {
171 result_resource->SetHostResource(
172 instance,
173 ppb_broker_target()->CreateTrusted(instance));
174 }
175
176 void PPB_Broker_Proxy::OnMsgConnect(const HostResource& broker,
177 uint32_t serialized_callback) {
178 PP_CompletionCallback callback = ReceiveCallback(serialized_callback);
179 int32_t result = ppb_broker_target()->Connect(broker.host_resource(),
180 callback);
181 if (result != PP_ERROR_WOULDBLOCK)
182 PP_RunCompletionCallback(&callback, result);
183 }
184
185 void PPB_Broker_Proxy::OnMsgGetHandle(
186 const HostResource& broker,
187 IPC::PlatformFileForTransit* foreign_socket_handle,
188 int32_t* result) {
189 DCHECK(foreign_socket_handle);
190 DCHECK(result);
191
192 int32_t socket_handle = 0;
193 *result = ppb_broker_target()->GetHandle(broker.host_resource(),
194 &socket_handle);
195 if (*result != PP_OK)
196 return;
197
198 // socket_handle doesn't belong to us: don't close it.
199 *foreign_socket_handle = dispatcher()->ShareHandleWithRemote(
200 IntToPlatformFile(socket_handle), false);
201 if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit())
202 *result = PP_ERROR_FAILED;
203 }
204
205 } // namespace proxy
206 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/proxy/ppb_broker_proxy.h ('k') | webkit/plugins/ppapi/ppb_broker_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698