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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 #if defined(OS_LINUX) | 22 #if defined(OS_LINUX) |
23 #include <sys/shm.h> | 23 #include <sys/shm.h> |
24 #endif | 24 #endif |
25 | 25 |
26 namespace pp { | 26 namespace pp { |
27 namespace proxy { | 27 namespace proxy { |
28 | 28 |
29 class URLLoader : public PluginResource { | 29 class URLLoader : public PluginResource { |
30 public: | 30 public: |
31 URLLoader(); | 31 URLLoader(PP_Instance instance); |
32 virtual ~URLLoader(); | 32 virtual ~URLLoader(); |
33 | 33 |
34 // Resource overrides. | 34 // Resource overrides. |
35 virtual URLLoader* AsURLLoader() { return this; } | 35 virtual URLLoader* AsURLLoader() { return this; } |
36 | 36 |
37 // Initialized to -1. Will be set to nonnegative values by the UpdateProgress | 37 // Initialized to -1. Will be set to nonnegative values by the UpdateProgress |
38 // message when the values are known. | 38 // message when the values are known. |
39 int64_t bytes_sent_; | 39 int64_t bytes_sent_; |
40 int64_t total_bytes_to_be_sent_; | 40 int64_t total_bytes_to_be_sent_; |
41 int64_t bytes_received_; | 41 int64_t bytes_received_; |
42 int64_t total_bytes_to_be_received_; | 42 int64_t total_bytes_to_be_received_; |
43 | 43 |
44 // When an asynchronous read is pending, this will contain the callback and | 44 // When an asynchronous read is pending, this will contain the callback and |
45 // the buffer to put the data. | 45 // the buffer to put the data. |
46 PP_CompletionCallback current_read_callback_; | 46 PP_CompletionCallback current_read_callback_; |
47 char* current_read_buffer_; | 47 char* current_read_buffer_; |
48 | 48 |
49 private: | 49 private: |
50 DISALLOW_COPY_AND_ASSIGN(URLLoader); | 50 DISALLOW_COPY_AND_ASSIGN(URLLoader); |
51 }; | 51 }; |
52 | 52 |
53 URLLoader::URLLoader() | 53 URLLoader::URLLoader(PP_Instance instance) |
54 : bytes_sent_(-1), | 54 : PluginResource(instance), |
| 55 bytes_sent_(-1), |
55 total_bytes_to_be_sent_(-1), | 56 total_bytes_to_be_sent_(-1), |
56 bytes_received_(-1), | 57 bytes_received_(-1), |
57 total_bytes_to_be_received_(-1), | 58 total_bytes_to_be_received_(-1), |
58 current_read_callback_(PP_MakeCompletionCallback(NULL, NULL)), | 59 current_read_callback_(PP_MakeCompletionCallback(NULL, NULL)), |
59 current_read_buffer_(NULL) { | 60 current_read_buffer_(NULL) { |
60 } | 61 } |
61 | 62 |
62 URLLoader::~URLLoader() { | 63 URLLoader::~URLLoader() { |
63 } | 64 } |
64 | 65 |
65 namespace { | 66 namespace { |
66 | 67 |
| 68 // Converts the given loader ID to the dispatcher associated with it, or NULL |
| 69 // if it couldn't be found. |
| 70 PluginDispatcher* DispatcherFromURLLoader(PP_Resource loader_id) { |
| 71 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); |
| 72 if (!object) |
| 73 return NULL; |
| 74 return PluginDispatcher::GetForInstance(object->instance()); |
| 75 } |
| 76 |
67 // Plugin PPB_URLLoader implmentation ------------------------------------------ | 77 // Plugin PPB_URLLoader implmentation ------------------------------------------ |
68 | 78 |
69 PP_Resource Create(PP_Instance instance_id) { | 79 PP_Resource Create(PP_Instance instance_id) { |
70 PluginDispatcher* dispatcher = PluginDispatcher::Get(); | 80 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 81 if (!dispatcher) |
| 82 return 0; |
| 83 |
71 PP_Resource result = 0; | 84 PP_Resource result = 0; |
72 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Create( | 85 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Create( |
73 INTERFACE_ID_PPB_URL_LOADER, instance_id, &result)); | 86 INTERFACE_ID_PPB_URL_LOADER, instance_id, &result)); |
74 if (result) | 87 if (result) |
75 PPB_URLLoader_Proxy::TrackPluginResource(result); | 88 PPB_URLLoader_Proxy::TrackPluginResource(instance_id, result); |
76 return result; | 89 return result; |
77 } | 90 } |
78 | 91 |
79 PP_Bool IsURLLoader(PP_Resource resource) { | 92 PP_Bool IsURLLoader(PP_Resource resource) { |
80 URLLoader* object = PluginResource::GetAs<URLLoader>(resource); | 93 URLLoader* object = PluginResource::GetAs<URLLoader>(resource); |
81 return BoolToPPBool(!!object); | 94 return BoolToPPBool(!!object); |
82 } | 95 } |
83 | 96 |
84 int32_t Open(PP_Resource loader_id, | 97 int32_t Open(PP_Resource loader_id, |
85 PP_Resource request_id, | 98 PP_Resource request_id, |
86 PP_CompletionCallback callback) { | 99 PP_CompletionCallback callback) { |
87 Dispatcher* dispatcher = PluginDispatcher::Get(); | 100 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); |
| 101 if (!dispatcher) |
| 102 return PP_ERROR_BADRESOURCE; |
| 103 |
88 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Open( | 104 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Open( |
89 INTERFACE_ID_PPB_URL_LOADER, loader_id, request_id, | 105 INTERFACE_ID_PPB_URL_LOADER, loader_id, request_id, |
90 dispatcher->callback_tracker().SendCallback(callback))); | 106 dispatcher->callback_tracker().SendCallback(callback))); |
91 return PP_ERROR_WOULDBLOCK; | 107 return PP_ERROR_WOULDBLOCK; |
92 } | 108 } |
93 | 109 |
94 int32_t FollowRedirect(PP_Resource loader_id, | 110 int32_t FollowRedirect(PP_Resource loader_id, |
95 PP_CompletionCallback callback) { | 111 PP_CompletionCallback callback) { |
96 Dispatcher* dispatcher = PluginDispatcher::Get(); | 112 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); |
| 113 if (!dispatcher) |
| 114 return PP_ERROR_BADRESOURCE; |
| 115 |
97 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FollowRedirect( | 116 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FollowRedirect( |
98 INTERFACE_ID_PPB_URL_LOADER, loader_id, | 117 INTERFACE_ID_PPB_URL_LOADER, loader_id, |
99 dispatcher->callback_tracker().SendCallback(callback))); | 118 dispatcher->callback_tracker().SendCallback(callback))); |
100 return PP_ERROR_WOULDBLOCK; | 119 return PP_ERROR_WOULDBLOCK; |
101 } | 120 } |
102 | 121 |
103 PP_Bool GetUploadProgress(PP_Resource loader_id, | 122 PP_Bool GetUploadProgress(PP_Resource loader_id, |
104 int64_t* bytes_sent, | 123 int64_t* bytes_sent, |
105 int64_t* total_bytes_to_be_sent) { | 124 int64_t* total_bytes_to_be_sent) { |
106 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); | 125 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); |
(...skipping 15 matching lines...) Expand all Loading... |
122 *bytes_received = 0; | 141 *bytes_received = 0; |
123 *total_bytes_to_be_received = 0; | 142 *total_bytes_to_be_received = 0; |
124 return PP_FALSE; | 143 return PP_FALSE; |
125 } | 144 } |
126 *bytes_received = object->bytes_received_; | 145 *bytes_received = object->bytes_received_; |
127 *total_bytes_to_be_received = object->total_bytes_to_be_received_; | 146 *total_bytes_to_be_received = object->total_bytes_to_be_received_; |
128 return PP_TRUE; | 147 return PP_TRUE; |
129 } | 148 } |
130 | 149 |
131 PP_Resource GetResponseInfo(PP_Resource loader_id) { | 150 PP_Resource GetResponseInfo(PP_Resource loader_id) { |
| 151 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); |
| 152 if (!object) |
| 153 return 0; |
| 154 |
132 // If we find that plugins are frequently requesting the response info, we | 155 // If we find that plugins are frequently requesting the response info, we |
133 // can improve performance by caching the PP_Resource in the URLLoader | 156 // can improve performance by caching the PP_Resource in the URLLoader |
134 // object. This way we only have to do IPC for the first request. However, | 157 // object. This way we only have to do IPC for the first request. However, |
135 // it seems that most plugins will only call this once so there's no use | 158 // it seems that most plugins will only call this once so there's no use |
136 // optimizing this case. | 159 // optimizing this case. |
137 | 160 |
138 PP_Resource result; | 161 PP_Resource result; |
139 PluginDispatcher* dispatcher = PluginDispatcher::Get(); | 162 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
| 163 object->instance()); |
| 164 if (!dispatcher) |
| 165 return PP_ERROR_BADRESOURCE; |
| 166 |
140 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_GetResponseInfo( | 167 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_GetResponseInfo( |
141 INTERFACE_ID_PPB_URL_LOADER, loader_id, &result)); | 168 INTERFACE_ID_PPB_URL_LOADER, loader_id, &result)); |
142 if (dispatcher->plugin_resource_tracker()->PreparePreviouslyTrackedResource( | 169 if (PluginResourceTracker::GetInstance()-> |
143 result)) | 170 PreparePreviouslyTrackedResource(result)) |
144 return result; | 171 return result; |
145 | 172 |
146 // Tell the response info to create a tracking object and add it to the | 173 // Tell the response info to create a tracking object and add it to the |
147 // resource tracker. | 174 // resource tracker. |
148 PPB_URLResponseInfo_Proxy::TrackPluginResource(result); | 175 PPB_URLResponseInfo_Proxy::TrackPluginResource(object->instance(), result); |
149 return result; | 176 return result; |
150 } | 177 } |
151 | 178 |
152 int32_t ReadResponseBody(PP_Resource loader_id, | 179 int32_t ReadResponseBody(PP_Resource loader_id, |
153 char* buffer, | 180 char* buffer, |
154 int32_t bytes_to_read, | 181 int32_t bytes_to_read, |
155 PP_CompletionCallback callback) { | 182 PP_CompletionCallback callback) { |
156 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); | 183 URLLoader* object = PluginResource::GetAs<URLLoader>(loader_id); |
157 if (!object) | 184 if (!object) |
158 return PP_ERROR_BADRESOURCE; | 185 return PP_ERROR_BADRESOURCE; |
| 186 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
| 187 object->instance()); |
| 188 if (!dispatcher) |
| 189 return PP_ERROR_BADRESOURCE; |
159 | 190 |
160 if (!buffer) | 191 if (!buffer) |
161 return PP_ERROR_BADARGUMENT; // Must specify an output buffer. | 192 return PP_ERROR_BADARGUMENT; // Must specify an output buffer. |
162 if (object->current_read_callback_.func) | 193 if (object->current_read_callback_.func) |
163 return PP_ERROR_INPROGRESS; // Can only have one request pending. | 194 return PP_ERROR_INPROGRESS; // Can only have one request pending. |
164 | 195 |
165 // Currently we don't support sync calls to read. We'll need to revisit | 196 // Currently we don't support sync calls to read. We'll need to revisit |
166 // how this works when we allow blocking calls (from background threads). | 197 // how this works when we allow blocking calls (from background threads). |
167 if (!callback.func) | 198 if (!callback.func) |
168 return PP_ERROR_BADARGUMENT; | 199 return PP_ERROR_BADARGUMENT; |
169 | 200 |
170 object->current_read_callback_ = callback; | 201 object->current_read_callback_ = callback; |
171 object->current_read_buffer_ = buffer; | 202 object->current_read_buffer_ = buffer; |
172 | 203 |
173 PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBURLLoader_ReadResponseBody( | 204 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_ReadResponseBody( |
174 INTERFACE_ID_PPB_URL_LOADER, loader_id, bytes_to_read)); | 205 INTERFACE_ID_PPB_URL_LOADER, loader_id, bytes_to_read)); |
175 return PP_ERROR_WOULDBLOCK; | 206 return PP_ERROR_WOULDBLOCK; |
176 } | 207 } |
177 | 208 |
178 int32_t FinishStreamingToFile(PP_Resource loader_id, | 209 int32_t FinishStreamingToFile(PP_Resource loader_id, |
179 PP_CompletionCallback callback) { | 210 PP_CompletionCallback callback) { |
180 Dispatcher* dispatcher = PluginDispatcher::Get(); | 211 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); |
| 212 if (!dispatcher) |
| 213 return PP_ERROR_BADRESOURCE; |
| 214 |
181 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FinishStreamingToFile( | 215 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_FinishStreamingToFile( |
182 INTERFACE_ID_PPB_URL_LOADER, loader_id, | 216 INTERFACE_ID_PPB_URL_LOADER, loader_id, |
183 dispatcher->callback_tracker().SendCallback(callback))); | 217 dispatcher->callback_tracker().SendCallback(callback))); |
184 return PP_ERROR_WOULDBLOCK; | 218 return PP_ERROR_WOULDBLOCK; |
185 } | 219 } |
186 | 220 |
187 void Close(PP_Resource loader_id) { | 221 void Close(PP_Resource loader_id) { |
188 PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBURLLoader_Close( | 222 PluginDispatcher* dispatcher = DispatcherFromURLLoader(loader_id); |
| 223 if (!dispatcher) |
| 224 return; |
| 225 |
| 226 dispatcher->Send(new PpapiHostMsg_PPBURLLoader_Close( |
189 INTERFACE_ID_PPB_URL_LOADER, loader_id)); | 227 INTERFACE_ID_PPB_URL_LOADER, loader_id)); |
190 } | 228 } |
191 | 229 |
192 const PPB_URLLoader ppb_urlloader = { | 230 const PPB_URLLoader ppb_urlloader = { |
193 &Create, | 231 &Create, |
194 &IsURLLoader, | 232 &IsURLLoader, |
195 &Open, | 233 &Open, |
196 &FollowRedirect, | 234 &FollowRedirect, |
197 &GetUploadProgress, | 235 &GetUploadProgress, |
198 &GetDownloadProgress, | 236 &GetDownloadProgress, |
(...skipping 28 matching lines...) Expand all Loading... |
227 PPB_URLLoader_Proxy::PPB_URLLoader_Proxy(Dispatcher* dispatcher, | 265 PPB_URLLoader_Proxy::PPB_URLLoader_Proxy(Dispatcher* dispatcher, |
228 const void* target_interface) | 266 const void* target_interface) |
229 : InterfaceProxy(dispatcher, target_interface), | 267 : InterfaceProxy(dispatcher, target_interface), |
230 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 268 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
231 } | 269 } |
232 | 270 |
233 PPB_URLLoader_Proxy::~PPB_URLLoader_Proxy() { | 271 PPB_URLLoader_Proxy::~PPB_URLLoader_Proxy() { |
234 } | 272 } |
235 | 273 |
236 // static | 274 // static |
237 void PPB_URLLoader_Proxy::TrackPluginResource(PP_Resource url_loader_resource) { | 275 void PPB_URLLoader_Proxy::TrackPluginResource(PP_Instance instance, |
238 linked_ptr<URLLoader> object(new URLLoader); | 276 PP_Resource url_loader_resource) { |
239 PluginDispatcher::Get()->plugin_resource_tracker()->AddResource( | 277 linked_ptr<URLLoader> object(new URLLoader(instance)); |
240 url_loader_resource, object); | 278 PluginResourceTracker::GetInstance()->AddResource(url_loader_resource, |
| 279 object); |
241 } | 280 } |
242 | 281 |
243 const void* PPB_URLLoader_Proxy::GetSourceInterface() const { | 282 const void* PPB_URLLoader_Proxy::GetSourceInterface() const { |
244 return &ppb_urlloader; | 283 return &ppb_urlloader; |
245 } | 284 } |
246 | 285 |
247 InterfaceID PPB_URLLoader_Proxy::GetInterfaceId() const { | 286 InterfaceID PPB_URLLoader_Proxy::GetInterfaceId() const { |
248 return INTERFACE_ID_PPB_URL_LOADER; | 287 return INTERFACE_ID_PPB_URL_LOADER; |
249 } | 288 } |
250 | 289 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 // TODO(brettw) handle bad messages! | 479 // TODO(brettw) handle bad messages! |
441 return handled; | 480 return handled; |
442 } | 481 } |
443 | 482 |
444 void PPB_URLLoaderTrusted_Proxy::OnMsgGrantUniversalAccess(PP_Resource loader) { | 483 void PPB_URLLoaderTrusted_Proxy::OnMsgGrantUniversalAccess(PP_Resource loader) { |
445 ppb_url_loader_trusted_target()->GrantUniversalAccess(loader); | 484 ppb_url_loader_trusted_target()->GrantUniversalAccess(loader); |
446 } | 485 } |
447 | 486 |
448 } // namespace proxy | 487 } // namespace proxy |
449 } // namespace pp | 488 } // namespace pp |
OLD | NEW |