OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/ppb_url_loader_proxy.h" | 5 #include "ppapi/proxy/ppb_url_loader_proxy.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "ppapi/c/pp_completion_callback.h" | 11 #include "ppapi/c/pp_completion_callback.h" |
12 #include "ppapi/c/pp_errors.h" | 12 #include "ppapi/c/pp_errors.h" |
13 #include "ppapi/c/pp_resource.h" | 13 #include "ppapi/c/pp_resource.h" |
14 #include "ppapi/c/ppb_url_loader.h" | 14 #include "ppapi/c/ppb_url_loader.h" |
15 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 15 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
16 #include "ppapi/proxy/host_dispatcher.h" | 16 #include "ppapi/proxy/host_dispatcher.h" |
17 #include "ppapi/proxy/plugin_dispatcher.h" | 17 #include "ppapi/proxy/plugin_dispatcher.h" |
18 #include "ppapi/proxy/plugin_resource.h" | 18 #include "ppapi/proxy/plugin_resource.h" |
| 19 #include "ppapi/proxy/plugin_resource_tracker.h" |
19 #include "ppapi/proxy/ppapi_messages.h" | 20 #include "ppapi/proxy/ppapi_messages.h" |
20 #include "ppapi/proxy/ppb_url_response_info_proxy.h" | 21 #include "ppapi/proxy/ppb_url_response_info_proxy.h" |
21 | 22 |
22 #if defined(OS_LINUX) | 23 #if defined(OS_LINUX) |
23 #include <sys/shm.h> | 24 #include <sys/shm.h> |
24 #endif | 25 #endif |
25 | 26 |
26 namespace pp { | 27 namespace pp { |
27 namespace proxy { | 28 namespace proxy { |
28 | 29 |
29 class URLLoader : public PluginResource { | 30 class URLLoader : public PluginResource { |
30 public: | 31 public: |
31 URLLoader(PP_Instance instance); | 32 URLLoader(const HostResource& resource); |
32 virtual ~URLLoader(); | 33 virtual ~URLLoader(); |
33 | 34 |
34 // Resource overrides. | 35 // Resource overrides. |
35 virtual URLLoader* AsURLLoader() { return this; } | 36 virtual URLLoader* AsURLLoader() { return this; } |
36 | 37 |
| 38 PP_Resource GetResponseInfo(); |
| 39 |
37 // Initialized to -1. Will be set to nonnegative values by the UpdateProgress | 40 // Initialized to -1. Will be set to nonnegative values by the UpdateProgress |
38 // message when the values are known. | 41 // message when the values are known. |
39 int64_t bytes_sent_; | 42 int64_t bytes_sent_; |
40 int64_t total_bytes_to_be_sent_; | 43 int64_t total_bytes_to_be_sent_; |
41 int64_t bytes_received_; | 44 int64_t bytes_received_; |
42 int64_t total_bytes_to_be_received_; | 45 int64_t total_bytes_to_be_received_; |
43 | 46 |
44 // When an asynchronous read is pending, this will contain the callback and | 47 // When an asynchronous read is pending, this will contain the callback and |
45 // the buffer to put the data. | 48 // the buffer to put the data. |
46 PP_CompletionCallback current_read_callback_; | 49 PP_CompletionCallback current_read_callback_; |
47 char* current_read_buffer_; | 50 char* current_read_buffer_; |
48 | 51 |
| 52 // Cached copy of the response info. When nonzero, we're holding a reference |
| 53 // to this resource. |
| 54 PP_Resource response_info_; |
| 55 |
49 private: | 56 private: |
50 DISALLOW_COPY_AND_ASSIGN(URLLoader); | 57 DISALLOW_COPY_AND_ASSIGN(URLLoader); |
51 }; | 58 }; |
52 | 59 |
53 URLLoader::URLLoader(PP_Instance instance) | 60 URLLoader::URLLoader(const HostResource& resource) |
54 : PluginResource(instance), | 61 : PluginResource(resource), |
55 bytes_sent_(-1), | 62 bytes_sent_(-1), |
56 total_bytes_to_be_sent_(-1), | 63 total_bytes_to_be_sent_(-1), |
57 bytes_received_(-1), | 64 bytes_received_(-1), |
58 total_bytes_to_be_received_(-1), | 65 total_bytes_to_be_received_(-1), |
59 current_read_callback_(PP_MakeCompletionCallback(NULL, NULL)), | 66 current_read_callback_(PP_MakeCompletionCallback(NULL, NULL)), |
60 current_read_buffer_(NULL) { | 67 current_read_buffer_(NULL), |
| 68 response_info_(0) { |
61 } | 69 } |
62 | 70 |
63 URLLoader::~URLLoader() { | 71 URLLoader::~URLLoader() { |
| 72 if (response_info_) |
| 73 PluginResourceTracker::GetInstance()->ReleaseResource(response_info_); |
| 74 } |
| 75 |
| 76 PP_Resource URLLoader::GetResponseInfo() { |
| 77 if (!response_info_) { |
| 78 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance()); |
| 79 if (!dispatcher) |
| 80 return 0; |
| 81 |
| 82 HostResource response_id; |
| 83 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_GetResponseInfo( |
| 84 INTERFACE_ID_PPB_URL_LOADER, host_resource(), &response_id)); |
| 85 if (response_id.is_null()) |
| 86 return 0; |
| 87 |
| 88 response_info_ = PPB_URLResponseInfo_Proxy::CreateResponseForResource( |
| 89 response_id); |
| 90 } |
| 91 |
| 92 // The caller expects to get a ref, and we want to keep holding ours. |
| 93 PluginResourceTracker::GetInstance()->AddRefResource(response_info_); |
| 94 return response_info_; |
64 } | 95 } |
65 | 96 |
66 namespace { | 97 namespace { |
67 | 98 |
68 // Converts the given loader ID to the dispatcher associated with it, or NULL | 99 // Converts the given loader ID to the dispatcher associated with it and the |
69 // if it couldn't be found. | 100 // loader object. Returns true if the object was found. |
70 PluginDispatcher* DispatcherFromURLLoader(PP_Resource loader_id) { | 101 bool RoutingDataFromURLLoader(PP_Resource loader_id, |
71 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); | 102 URLLoader** loader_object, |
72 if (!object) | 103 PluginDispatcher** dispatcher) { |
73 return NULL; | 104 *loader_object = PluginResource::GetAs<URLLoader>(loader_id); |
74 return PluginDispatcher::GetForInstance(object->instance()); | 105 if (!*loader_object) |
| 106 return false; |
| 107 *dispatcher = PluginDispatcher::GetForInstance((*loader_object)->instance()); |
| 108 return !!*dispatcher; |
75 } | 109 } |
76 | 110 |
77 // Plugin PPB_URLLoader implmentation ------------------------------------------ | 111 // Plugin PPB_URLLoader implmentation ------------------------------------------ |
78 | 112 |
79 PP_Resource Create(PP_Instance instance_id) { | 113 PP_Resource Create(PP_Instance instance_id) { |
80 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); | 114 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
81 if (!dispatcher) | 115 if (!dispatcher) |
82 return 0; | 116 return 0; |
83 | 117 |
84 PP_Resource result = 0; | 118 HostResource result; |
85 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Create( | 119 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Create( |
86 INTERFACE_ID_PPB_URL_LOADER, instance_id, &result)); | 120 INTERFACE_ID_PPB_URL_LOADER, instance_id, &result)); |
87 if (result) | 121 if (result.is_null()) |
88 PPB_URLLoader_Proxy::TrackPluginResource(instance_id, result); | 122 return 0; |
89 return result; | 123 return PPB_URLLoader_Proxy::TrackPluginResource(result); |
90 } | 124 } |
91 | 125 |
92 PP_Bool IsURLLoader(PP_Resource resource) { | 126 PP_Bool IsURLLoader(PP_Resource resource) { |
93 URLLoader* object = PluginResource::GetAs<URLLoader>(resource); | 127 URLLoader* object = PluginResource::GetAs<URLLoader>(resource); |
94 return BoolToPPBool(!!object); | 128 return BoolToPPBool(!!object); |
95 } | 129 } |
96 | 130 |
97 int32_t Open(PP_Resource loader_id, | 131 int32_t Open(PP_Resource loader_id, |
98 PP_Resource request_id, | 132 PP_Resource request_id, |
99 PP_CompletionCallback callback) { | 133 PP_CompletionCallback callback) { |
100 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); | 134 URLLoader* loader_object; |
101 if (!dispatcher) | 135 PluginDispatcher* dispatcher; |
| 136 if (!RoutingDataFromURLLoader(loader_id, &loader_object, &dispatcher)) |
| 137 return PP_ERROR_BADRESOURCE; |
| 138 PluginResource* request_object = |
| 139 PluginResourceTracker::GetInstance()->GetResourceObject(request_id); |
| 140 if (!request_object) |
102 return PP_ERROR_BADRESOURCE; | 141 return PP_ERROR_BADRESOURCE; |
103 | 142 |
104 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Open( | 143 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Open( |
105 INTERFACE_ID_PPB_URL_LOADER, loader_id, request_id, | 144 INTERFACE_ID_PPB_URL_LOADER, loader_object->host_resource(), |
| 145 request_object->host_resource(), |
106 dispatcher->callback_tracker().SendCallback(callback))); | 146 dispatcher->callback_tracker().SendCallback(callback))); |
107 return PP_ERROR_WOULDBLOCK; | 147 return PP_ERROR_WOULDBLOCK; |
108 } | 148 } |
109 | 149 |
110 int32_t FollowRedirect(PP_Resource loader_id, | 150 int32_t FollowRedirect(PP_Resource loader_id, |
111 PP_CompletionCallback callback) { | 151 PP_CompletionCallback callback) { |
112 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); | 152 URLLoader* loader_object; |
113 if (!dispatcher) | 153 PluginDispatcher* dispatcher; |
| 154 if (!RoutingDataFromURLLoader(loader_id, &loader_object, &dispatcher)) |
114 return PP_ERROR_BADRESOURCE; | 155 return PP_ERROR_BADRESOURCE; |
115 | 156 |
116 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FollowRedirect( | 157 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FollowRedirect( |
117 INTERFACE_ID_PPB_URL_LOADER, loader_id, | 158 INTERFACE_ID_PPB_URL_LOADER, loader_object->host_resource(), |
118 dispatcher->callback_tracker().SendCallback(callback))); | 159 dispatcher->callback_tracker().SendCallback(callback))); |
119 return PP_ERROR_WOULDBLOCK; | 160 return PP_ERROR_WOULDBLOCK; |
120 } | 161 } |
121 | 162 |
122 PP_Bool GetUploadProgress(PP_Resource loader_id, | 163 PP_Bool GetUploadProgress(PP_Resource loader_id, |
123 int64_t* bytes_sent, | 164 int64_t* bytes_sent, |
124 int64_t* total_bytes_to_be_sent) { | 165 int64_t* total_bytes_to_be_sent) { |
125 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); | 166 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); |
126 if (!object || object->bytes_sent_ == -1) { | 167 if (!object || object->bytes_sent_ == -1) { |
127 *bytes_sent = 0; | 168 *bytes_sent = 0; |
(...skipping 16 matching lines...) Expand all Loading... |
144 } | 185 } |
145 *bytes_received = object->bytes_received_; | 186 *bytes_received = object->bytes_received_; |
146 *total_bytes_to_be_received = object->total_bytes_to_be_received_; | 187 *total_bytes_to_be_received = object->total_bytes_to_be_received_; |
147 return PP_TRUE; | 188 return PP_TRUE; |
148 } | 189 } |
149 | 190 |
150 PP_Resource GetResponseInfo(PP_Resource loader_id) { | 191 PP_Resource GetResponseInfo(PP_Resource loader_id) { |
151 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); | 192 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); |
152 if (!object) | 193 if (!object) |
153 return 0; | 194 return 0; |
154 | 195 return object->GetResponseInfo(); |
155 // If we find that plugins are frequently requesting the response info, we | |
156 // can improve performance by caching the PP_Resource in the URLLoader | |
157 // object. This way we only have to do IPC for the first request. However, | |
158 // it seems that most plugins will only call this once so there's no use | |
159 // optimizing this case. | |
160 | |
161 PP_Resource result; | |
162 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | |
163 object->instance()); | |
164 if (!dispatcher) | |
165 return PP_ERROR_BADRESOURCE; | |
166 | |
167 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_GetResponseInfo( | |
168 INTERFACE_ID_PPB_URL_LOADER, loader_id, &result)); | |
169 if (PluginResourceTracker::GetInstance()-> | |
170 PreparePreviouslyTrackedResource(result)) | |
171 return result; | |
172 | |
173 // Tell the response info to create a tracking object and add it to the | |
174 // resource tracker. | |
175 PPB_URLResponseInfo_Proxy::TrackPluginResource(object->instance(), result); | |
176 return result; | |
177 } | 196 } |
178 | 197 |
179 int32_t ReadResponseBody(PP_Resource loader_id, | 198 int32_t ReadResponseBody(PP_Resource loader_id, |
180 char* buffer, | 199 char* buffer, |
181 int32_t bytes_to_read, | 200 int32_t bytes_to_read, |
182 PP_CompletionCallback callback) { | 201 PP_CompletionCallback callback) { |
183 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); | 202 URLLoader* loader_object; |
184 if (!object) | 203 PluginDispatcher* dispatcher; |
185 return PP_ERROR_BADRESOURCE; | 204 if (!RoutingDataFromURLLoader(loader_id, &loader_object, &dispatcher)) |
186 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | |
187 object->instance()); | |
188 if (!dispatcher) | |
189 return PP_ERROR_BADRESOURCE; | 205 return PP_ERROR_BADRESOURCE; |
190 | 206 |
191 if (!buffer) | 207 if (!buffer) |
192 return PP_ERROR_BADARGUMENT; // Must specify an output buffer. | 208 return PP_ERROR_BADARGUMENT; // Must specify an output buffer. |
193 if (object->current_read_callback_.func) | 209 if (loader_object->current_read_callback_.func) |
194 return PP_ERROR_INPROGRESS; // Can only have one request pending. | 210 return PP_ERROR_INPROGRESS; // Can only have one request pending. |
195 | 211 |
196 // Currently we don't support sync calls to read. We'll need to revisit | 212 // Currently we don't support sync calls to read. We'll need to revisit |
197 // how this works when we allow blocking calls (from background threads). | 213 // how this works when we allow blocking calls (from background threads). |
198 if (!callback.func) | 214 if (!callback.func) |
199 return PP_ERROR_BADARGUMENT; | 215 return PP_ERROR_BADARGUMENT; |
200 | 216 |
201 object->current_read_callback_ = callback; | 217 loader_object->current_read_callback_ = callback; |
202 object->current_read_buffer_ = buffer; | 218 loader_object->current_read_buffer_ = buffer; |
203 | 219 |
204 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_ReadResponseBody( | 220 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_ReadResponseBody( |
205 INTERFACE_ID_PPB_URL_LOADER, loader_id, bytes_to_read)); | 221 INTERFACE_ID_PPB_URL_LOADER, |
| 222 loader_object->host_resource(), bytes_to_read)); |
206 return PP_ERROR_WOULDBLOCK; | 223 return PP_ERROR_WOULDBLOCK; |
207 } | 224 } |
208 | 225 |
209 int32_t FinishStreamingToFile(PP_Resource loader_id, | 226 int32_t FinishStreamingToFile(PP_Resource loader_id, |
210 PP_CompletionCallback callback) { | 227 PP_CompletionCallback callback) { |
211 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); | 228 URLLoader* loader_object; |
212 if (!dispatcher) | 229 PluginDispatcher* dispatcher; |
| 230 if (!RoutingDataFromURLLoader(loader_id, &loader_object, &dispatcher)) |
213 return PP_ERROR_BADRESOURCE; | 231 return PP_ERROR_BADRESOURCE; |
214 | 232 |
215 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FinishStreamingToFile( | 233 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FinishStreamingToFile( |
216 INTERFACE_ID_PPB_URL_LOADER, loader_id, | 234 INTERFACE_ID_PPB_URL_LOADER, loader_object->host_resource(), |
217 dispatcher->callback_tracker().SendCallback(callback))); | 235 dispatcher->callback_tracker().SendCallback(callback))); |
218 return PP_ERROR_WOULDBLOCK; | 236 return PP_ERROR_WOULDBLOCK; |
219 } | 237 } |
220 | 238 |
221 void Close(PP_Resource loader_id) { | 239 void Close(PP_Resource loader_id) { |
222 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); | 240 URLLoader* loader_object; |
223 if (!dispatcher) | 241 PluginDispatcher* dispatcher; |
| 242 if (!RoutingDataFromURLLoader(loader_id, &loader_object, &dispatcher)) |
224 return; | 243 return; |
225 | 244 |
226 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Close( | 245 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Close( |
227 INTERFACE_ID_PPB_URL_LOADER, loader_id)); | 246 INTERFACE_ID_PPB_URL_LOADER, loader_object->host_resource())); |
228 } | 247 } |
229 | 248 |
230 const PPB_URLLoader ppb_urlloader = { | 249 const PPB_URLLoader ppb_urlloader = { |
231 &Create, | 250 &Create, |
232 &IsURLLoader, | 251 &IsURLLoader, |
233 &Open, | 252 &Open, |
234 &FollowRedirect, | 253 &FollowRedirect, |
235 &GetUploadProgress, | 254 &GetUploadProgress, |
236 &GetDownloadProgress, | 255 &GetDownloadProgress, |
237 &GetResponseInfo, | 256 &GetResponseInfo, |
238 &ReadResponseBody, | 257 &ReadResponseBody, |
239 &FinishStreamingToFile, | 258 &FinishStreamingToFile, |
240 &Close | 259 &Close |
241 }; | 260 }; |
242 | 261 |
243 // Plugin URLLoaderTrusted implementation -------------------------------------- | 262 // Plugin URLLoaderTrusted implementation -------------------------------------- |
244 | 263 |
245 void GrantUniversalAccess(PP_Resource loader) { | 264 void GrantUniversalAccess(PP_Resource loader_id) { |
246 PluginDispatcher::Get()->Send( | 265 URLLoader* loader_object; |
| 266 PluginDispatcher* dispatcher; |
| 267 if (!RoutingDataFromURLLoader(loader_id, &loader_object, &dispatcher)) |
| 268 return; |
| 269 |
| 270 dispatcher->Send( |
247 new PpapiHostMsg_PPBURLLoaderTrusted_GrantUniversalAccess( | 271 new PpapiHostMsg_PPBURLLoaderTrusted_GrantUniversalAccess( |
248 INTERFACE_ID_PPB_URL_LOADER_TRUSTED, loader)); | 272 INTERFACE_ID_PPB_URL_LOADER_TRUSTED, loader_object->host_resource())); |
249 } | 273 } |
250 | 274 |
251 const PPB_URLLoaderTrusted ppb_urlloader_trusted = { | 275 const PPB_URLLoaderTrusted ppb_urlloader_trusted = { |
252 &GrantUniversalAccess, | 276 &GrantUniversalAccess, |
253 NULL, // RegisterStatusCallback is used internally by the proxy only. | 277 NULL, // RegisterStatusCallback is used internally by the proxy only. |
254 }; | 278 }; |
255 | 279 |
256 } // namespace | 280 } // namespace |
257 | 281 |
258 // PPB_URLLoader_Proxy --------------------------------------------------------- | 282 // PPB_URLLoader_Proxy --------------------------------------------------------- |
259 | 283 |
260 struct PPB_URLLoader_Proxy::ReadCallbackInfo { | 284 struct PPB_URLLoader_Proxy::ReadCallbackInfo { |
261 PP_Resource pp_resource; | 285 HostResource resource; |
262 std::string read_buffer; | 286 std::string read_buffer; |
263 }; | 287 }; |
264 | 288 |
265 PPB_URLLoader_Proxy::PPB_URLLoader_Proxy(Dispatcher* dispatcher, | 289 PPB_URLLoader_Proxy::PPB_URLLoader_Proxy(Dispatcher* dispatcher, |
266 const void* target_interface) | 290 const void* target_interface) |
267 : InterfaceProxy(dispatcher, target_interface), | 291 : InterfaceProxy(dispatcher, target_interface), |
268 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 292 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
269 } | 293 } |
270 | 294 |
271 PPB_URLLoader_Proxy::~PPB_URLLoader_Proxy() { | 295 PPB_URLLoader_Proxy::~PPB_URLLoader_Proxy() { |
272 } | 296 } |
273 | 297 |
274 // static | 298 // static |
275 void PPB_URLLoader_Proxy::TrackPluginResource(PP_Instance instance, | 299 PP_Resource PPB_URLLoader_Proxy::TrackPluginResource( |
276 PP_Resource url_loader_resource) { | 300 const HostResource& url_loader_resource) { |
277 linked_ptr<URLLoader> object(new URLLoader(instance)); | 301 linked_ptr<URLLoader> object(new URLLoader(url_loader_resource)); |
278 PluginResourceTracker::GetInstance()->AddResource(url_loader_resource, | 302 return PluginResourceTracker::GetInstance()->AddResource(object); |
279 object); | |
280 } | 303 } |
281 | 304 |
282 const void* PPB_URLLoader_Proxy::GetSourceInterface() const { | 305 const void* PPB_URLLoader_Proxy::GetSourceInterface() const { |
283 return &ppb_urlloader; | 306 return &ppb_urlloader; |
284 } | 307 } |
285 | 308 |
286 InterfaceID PPB_URLLoader_Proxy::GetInterfaceId() const { | 309 InterfaceID PPB_URLLoader_Proxy::GetInterfaceId() const { |
287 return INTERFACE_ID_PPB_URL_LOADER; | 310 return INTERFACE_ID_PPB_URL_LOADER; |
288 } | 311 } |
289 | 312 |
(...skipping 19 matching lines...) Expand all Loading... |
309 OnMsgUpdateProgress) | 332 OnMsgUpdateProgress) |
310 IPC_MESSAGE_HANDLER(PpapiMsg_PPBURLLoader_ReadResponseBody_Ack, | 333 IPC_MESSAGE_HANDLER(PpapiMsg_PPBURLLoader_ReadResponseBody_Ack, |
311 OnMsgReadResponseBodyAck) | 334 OnMsgReadResponseBodyAck) |
312 IPC_MESSAGE_UNHANDLED(handled = false) | 335 IPC_MESSAGE_UNHANDLED(handled = false) |
313 IPC_END_MESSAGE_MAP() | 336 IPC_END_MESSAGE_MAP() |
314 // TODO(brettw) handle bad messages! | 337 // TODO(brettw) handle bad messages! |
315 return handled; | 338 return handled; |
316 } | 339 } |
317 | 340 |
318 void PPB_URLLoader_Proxy::OnMsgCreate(PP_Instance instance, | 341 void PPB_URLLoader_Proxy::OnMsgCreate(PP_Instance instance, |
319 PP_Resource* result) { | 342 HostResource* result) { |
320 *result = ppb_url_loader_target()->Create(instance); | 343 result->SetHostResource(instance, ppb_url_loader_target()->Create(instance)); |
321 } | 344 } |
322 | 345 |
323 void PPB_URLLoader_Proxy::OnMsgOpen(PP_Resource loader, | 346 void PPB_URLLoader_Proxy::OnMsgOpen(const HostResource& loader, |
324 PP_Resource request_info, | 347 const HostResource& request_info, |
325 uint32_t serialized_callback) { | 348 uint32_t serialized_callback) { |
326 PP_CompletionCallback callback = ReceiveCallback(serialized_callback); | 349 PP_CompletionCallback callback = ReceiveCallback(serialized_callback); |
327 int32_t result = ppb_url_loader_target()->Open( | 350 int32_t result = ppb_url_loader_target()->Open( |
328 loader, request_info, callback); | 351 loader.host_resource(), request_info.host_resource(), callback); |
329 if (result != PP_ERROR_WOULDBLOCK) | 352 if (result != PP_ERROR_WOULDBLOCK) |
330 PP_RunCompletionCallback(&callback, result); | 353 PP_RunCompletionCallback(&callback, result); |
331 } | 354 } |
332 | 355 |
333 void PPB_URLLoader_Proxy::OnMsgFollowRedirect( | 356 void PPB_URLLoader_Proxy::OnMsgFollowRedirect( |
334 PP_Resource loader, | 357 const HostResource& loader, |
335 uint32_t serialized_callback) { | 358 uint32_t serialized_callback) { |
336 PP_CompletionCallback callback = ReceiveCallback(serialized_callback); | 359 PP_CompletionCallback callback = ReceiveCallback(serialized_callback); |
337 int32_t result = ppb_url_loader_target()->FollowRedirect( | 360 int32_t result = ppb_url_loader_target()->FollowRedirect( |
338 loader, callback); | 361 loader.host_resource(), callback); |
339 if (result != PP_ERROR_WOULDBLOCK) | 362 if (result != PP_ERROR_WOULDBLOCK) |
340 PP_RunCompletionCallback(&callback, result); | 363 PP_RunCompletionCallback(&callback, result); |
341 } | 364 } |
342 | 365 |
343 void PPB_URLLoader_Proxy::OnMsgGetResponseInfo(PP_Resource loader, | 366 void PPB_URLLoader_Proxy::OnMsgGetResponseInfo(const HostResource& loader, |
344 PP_Resource* result) { | 367 HostResource* result) { |
345 *result = ppb_url_loader_target()->GetResponseInfo(loader); | 368 result->SetHostResource(loader.instance(), |
| 369 ppb_url_loader_target()->GetResponseInfo(loader.host_resource())); |
346 } | 370 } |
347 | 371 |
348 void PPB_URLLoader_Proxy::OnMsgReadResponseBody( | 372 void PPB_URLLoader_Proxy::OnMsgReadResponseBody( |
349 PP_Resource loader, | 373 const HostResource& loader, |
350 int32_t bytes_to_read) { | 374 int32_t bytes_to_read) { |
351 // The plugin could be sending us malicious messages, don't accept negative | 375 // The plugin could be sending us malicious messages, don't accept negative |
352 // sizes. | 376 // sizes. |
353 if (bytes_to_read < 0) { | 377 if (bytes_to_read < 0) { |
354 // TODO(brettw) kill plugin. | 378 // TODO(brettw) kill plugin. |
355 bytes_to_read = 0; | 379 bytes_to_read = 0; |
356 } | 380 } |
357 | 381 |
358 // This heap object will get deleted by the callback handler. | 382 // This heap object will get deleted by the callback handler. |
359 // TODO(brettw) this will be leaked if the plugin closes the resource! | 383 // TODO(brettw) this will be leaked if the plugin closes the resource! |
360 // (Also including the plugin unloading and having the resource implicitly | 384 // (Also including the plugin unloading and having the resource implicitly |
361 // destroyed. Depending on the cleanup ordering, we may not need the weak | 385 // destroyed. Depending on the cleanup ordering, we may not need the weak |
362 // pointer here.) | 386 // pointer here.) |
363 ReadCallbackInfo* info = new ReadCallbackInfo; | 387 ReadCallbackInfo* info = new ReadCallbackInfo; |
364 info->pp_resource = loader; | 388 info->resource = loader; |
| 389 // TODO(brettw) have a way to check for out-of-memory. |
365 info->read_buffer.resize(bytes_to_read); | 390 info->read_buffer.resize(bytes_to_read); |
366 | 391 |
367 CompletionCallback callback = callback_factory_.NewCallback( | 392 CompletionCallback callback = callback_factory_.NewCallback( |
368 &PPB_URLLoader_Proxy::OnReadCallback, info); | 393 &PPB_URLLoader_Proxy::OnReadCallback, info); |
369 | 394 |
370 int32_t result = ppb_url_loader_target()->ReadResponseBody( | 395 int32_t result = ppb_url_loader_target()->ReadResponseBody( |
371 loader, const_cast<char*>(info->read_buffer.c_str()), bytes_to_read, | 396 loader.host_resource(), const_cast<char*>(info->read_buffer.c_str()), |
372 callback.pp_completion_callback()); | 397 bytes_to_read, callback.pp_completion_callback()); |
373 if (result != PP_ERROR_WOULDBLOCK) { | 398 if (result != PP_ERROR_WOULDBLOCK) { |
374 // Send error (or perhaps success for synchronous reads) back to plugin. | 399 // Send error (or perhaps success for synchronous reads) back to plugin. |
375 // The callback function is already set up to do this and also delete the | 400 // The callback function is already set up to do this and also delete the |
376 // callback info. | 401 // callback info. |
377 callback.Run(result); | 402 callback.Run(result); |
378 } | 403 } |
379 } | 404 } |
380 | 405 |
381 void PPB_URLLoader_Proxy::OnMsgFinishStreamingToFile( | 406 void PPB_URLLoader_Proxy::OnMsgFinishStreamingToFile( |
382 PP_Resource loader, | 407 const HostResource& loader, |
383 uint32_t serialized_callback) { | 408 uint32_t serialized_callback) { |
384 PP_CompletionCallback callback = ReceiveCallback(serialized_callback); | 409 PP_CompletionCallback callback = ReceiveCallback(serialized_callback); |
385 int32_t result = ppb_url_loader_target()->FinishStreamingToFile( | 410 int32_t result = ppb_url_loader_target()->FinishStreamingToFile( |
386 loader, callback); | 411 loader.host_resource(), callback); |
387 if (result != PP_ERROR_WOULDBLOCK) | 412 if (result != PP_ERROR_WOULDBLOCK) |
388 PP_RunCompletionCallback(&callback, result); | 413 PP_RunCompletionCallback(&callback, result); |
389 } | 414 } |
390 | 415 |
391 void PPB_URLLoader_Proxy::OnMsgClose(PP_Resource loader) { | 416 void PPB_URLLoader_Proxy::OnMsgClose(const HostResource& loader) { |
392 ppb_url_loader_target()->Close(loader); | 417 ppb_url_loader_target()->Close(loader.host_resource()); |
393 } | 418 } |
394 | 419 |
| 420 // Called in the Plugin. |
395 void PPB_URLLoader_Proxy::OnMsgUpdateProgress( | 421 void PPB_URLLoader_Proxy::OnMsgUpdateProgress( |
396 PP_Resource resource, | 422 const PPBURLLoader_UpdateProgress_Params& params) { |
397 int64_t bytes_sent, | 423 PP_Resource plugin_resource = |
398 int64_t total_bytes_to_be_sent, | 424 PluginResourceTracker::GetInstance()->PluginResourceForHostResource( |
399 int64_t bytes_received, | 425 params.resource); |
400 int64_t total_bytes_to_be_received) { | 426 if (!plugin_resource) |
401 URLLoader* object = PluginResource::GetAs<URLLoader>(resource); | |
402 if (!object) { | |
403 NOTREACHED(); | |
404 return; | 427 return; |
405 } | 428 URLLoader* object = PluginResource::GetAs<URLLoader>(plugin_resource); |
| 429 if (!object) |
| 430 return; |
406 | 431 |
407 object->bytes_sent_ = bytes_sent; | 432 object->bytes_sent_ = params.bytes_sent; |
408 object->total_bytes_to_be_sent_ = total_bytes_to_be_sent; | 433 object->total_bytes_to_be_sent_ = params.total_bytes_to_be_sent; |
409 object->bytes_received_ = bytes_received; | 434 object->bytes_received_ = params.bytes_received; |
410 object->total_bytes_to_be_received_ = total_bytes_to_be_received; | 435 object->total_bytes_to_be_received_ = params.total_bytes_to_be_received; |
411 } | 436 } |
412 | 437 |
413 void PPB_URLLoader_Proxy::OnMsgReadResponseBodyAck(PP_Resource pp_resource, | 438 // Called in the Plugin. |
414 int32 result, | 439 void PPB_URLLoader_Proxy::OnMsgReadResponseBodyAck( |
415 const std::string& data) { | 440 const HostResource& host_resource, |
416 URLLoader* object = PluginResource::GetAs<URLLoader>(pp_resource); | 441 int32 result, |
417 if (!object) { | 442 const std::string& data) { |
418 NOTREACHED(); | 443 PP_Resource plugin_resource = |
| 444 PluginResourceTracker::GetInstance()->PluginResourceForHostResource( |
| 445 host_resource); |
| 446 if (!plugin_resource) |
419 return; | 447 return; |
420 } | 448 URLLoader* object = PluginResource::GetAs<URLLoader>(plugin_resource); |
| 449 if (!object) |
| 450 return; |
421 | 451 |
422 if (!object->current_read_callback_.func || !object->current_read_buffer_) { | 452 if (!object->current_read_callback_.func || !object->current_read_buffer_) { |
423 NOTREACHED(); | 453 NOTREACHED(); |
424 return; | 454 return; |
425 } | 455 } |
426 | 456 |
427 // In the error case, the string will be empty, so we can always just copy | 457 // In the error case, the string will be empty, so we can always just copy |
428 // out of it before issuing the callback. | 458 // out of it before issuing the callback. |
429 memcpy(object->current_read_buffer_, data.c_str(), data.length()); | 459 memcpy(object->current_read_buffer_, data.c_str(), data.length()); |
430 | 460 |
431 // The plugin should be able to make a new request from their callback, so | 461 // The plugin should be able to make a new request from their callback, so |
432 // we have to clear our copy first. | 462 // we have to clear our copy first. |
433 PP_CompletionCallback temp_callback = object->current_read_callback_; | 463 PP_CompletionCallback temp_callback = object->current_read_callback_; |
434 object->current_read_callback_ = PP_BlockUntilComplete(); | 464 object->current_read_callback_ = PP_BlockUntilComplete(); |
435 object->current_read_buffer_ = NULL; | 465 object->current_read_buffer_ = NULL; |
436 PP_RunCompletionCallback(&temp_callback, result); | 466 PP_RunCompletionCallback(&temp_callback, result); |
437 } | 467 } |
438 | 468 |
439 void PPB_URLLoader_Proxy::OnReadCallback(int32_t result, | 469 void PPB_URLLoader_Proxy::OnReadCallback(int32_t result, |
440 ReadCallbackInfo* info) { | 470 ReadCallbackInfo* info) { |
441 int32_t bytes_read = 0; | 471 int32_t bytes_read = 0; |
442 if (result > 0) | 472 if (result > 0) |
443 bytes_read = result; // Positive results indicate bytes read. | 473 bytes_read = result; // Positive results indicate bytes read. |
444 info->read_buffer.resize(bytes_read); | 474 info->read_buffer.resize(bytes_read); |
445 | 475 |
446 dispatcher()->Send(new PpapiMsg_PPBURLLoader_ReadResponseBody_Ack( | 476 dispatcher()->Send(new PpapiMsg_PPBURLLoader_ReadResponseBody_Ack( |
447 INTERFACE_ID_PPB_URL_LOADER, info->pp_resource, | 477 INTERFACE_ID_PPB_URL_LOADER, info->resource, result, info->read_buffer)); |
448 result, info->read_buffer)); | |
449 | 478 |
450 delete info; | 479 delete info; |
451 } | 480 } |
452 | 481 |
453 // PPB_URLLoaderTrusted_Proxy -------------------------------------------------- | 482 // PPB_URLLoaderTrusted_Proxy -------------------------------------------------- |
454 | 483 |
455 PPB_URLLoaderTrusted_Proxy::PPB_URLLoaderTrusted_Proxy( | 484 PPB_URLLoaderTrusted_Proxy::PPB_URLLoaderTrusted_Proxy( |
456 Dispatcher* dispatcher, | 485 Dispatcher* dispatcher, |
457 const void* target_interface) | 486 const void* target_interface) |
458 : InterfaceProxy(dispatcher, target_interface) { | 487 : InterfaceProxy(dispatcher, target_interface) { |
(...skipping 14 matching lines...) Expand all Loading... |
473 bool handled = true; | 502 bool handled = true; |
474 IPC_BEGIN_MESSAGE_MAP(PPB_URLLoaderTrusted_Proxy, msg) | 503 IPC_BEGIN_MESSAGE_MAP(PPB_URLLoaderTrusted_Proxy, msg) |
475 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLLoaderTrusted_GrantUniversalAccess, | 504 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBURLLoaderTrusted_GrantUniversalAccess, |
476 OnMsgGrantUniversalAccess) | 505 OnMsgGrantUniversalAccess) |
477 IPC_MESSAGE_UNHANDLED(handled = false) | 506 IPC_MESSAGE_UNHANDLED(handled = false) |
478 IPC_END_MESSAGE_MAP(); | 507 IPC_END_MESSAGE_MAP(); |
479 // TODO(brettw) handle bad messages! | 508 // TODO(brettw) handle bad messages! |
480 return handled; | 509 return handled; |
481 } | 510 } |
482 | 511 |
483 void PPB_URLLoaderTrusted_Proxy::OnMsgGrantUniversalAccess(PP_Resource loader) { | 512 void PPB_URLLoaderTrusted_Proxy::OnMsgGrantUniversalAccess( |
484 ppb_url_loader_trusted_target()->GrantUniversalAccess(loader); | 513 const HostResource& loader) { |
| 514 ppb_url_loader_trusted_target()->GrantUniversalAccess(loader.host_resource()); |
485 } | 515 } |
486 | 516 |
487 } // namespace proxy | 517 } // namespace proxy |
488 } // namespace pp | 518 } // namespace pp |
OLD | NEW |