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

Side by Side Diff: webkit/plugins/ppapi/ppb_url_loader_impl.cc

Issue 5828003: Move the Pepper implementation from webkit/glue/plugins/pepper_* to... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 years 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
OLDNEW
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 "webkit/glue/plugins/pepper_url_loader.h" 5 #include "webkit/plugins/ppapi/ppb_url_loader_impl.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "ppapi/c/pp_completion_callback.h" 8 #include "ppapi/c/pp_completion_callback.h"
9 #include "ppapi/c/pp_errors.h" 9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/c/ppb_url_loader.h" 10 #include "ppapi/c/ppb_url_loader.h"
11 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" 11 #include "ppapi/c/trusted/ppb_url_loader_trusted.h"
12 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" 12 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
13 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" 13 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h"
14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" 14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
15 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h" 15 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
16 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h" 16 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h"
17 #include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h" 17 #include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h"
18 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" 18 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h"
19 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h" 19 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h"
20 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" 20 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h"
21 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" 21 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h"
22 #include "webkit/appcache/web_application_cache_host_impl.h" 22 #include "webkit/appcache/web_application_cache_host_impl.h"
23 #include "webkit/glue/plugins/pepper_common.h" 23 #include "webkit/plugins/ppapi/common.h"
24 #include "webkit/glue/plugins/pepper_plugin_instance.h" 24 #include "webkit/plugins/ppapi/plugin_instance.h"
25 #include "webkit/glue/plugins/pepper_url_request_info.h" 25 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h"
26 #include "webkit/glue/plugins/pepper_url_response_info.h" 26 #include "webkit/plugins/ppapi/ppb_url_response_info_impl.h"
27 27
28 using appcache::WebApplicationCacheHostImpl; 28 using appcache::WebApplicationCacheHostImpl;
29 using WebKit::WebFrame; 29 using WebKit::WebFrame;
30 using WebKit::WebString; 30 using WebKit::WebString;
31 using WebKit::WebURL; 31 using WebKit::WebURL;
32 using WebKit::WebURLError; 32 using WebKit::WebURLError;
33 using WebKit::WebURLLoader; 33 using WebKit::WebURLLoader;
34 using WebKit::WebURLRequest; 34 using WebKit::WebURLRequest;
35 using WebKit::WebURLResponse; 35 using WebKit::WebURLResponse;
36 36
37 #ifdef _MSC_VER 37 #ifdef _MSC_VER
38 // Do not warn about use of std::copy with raw pointers. 38 // Do not warn about use of std::copy with raw pointers.
39 #pragma warning(disable : 4996) 39 #pragma warning(disable : 4996)
40 #endif 40 #endif
41 41
42 namespace pepper { 42 namespace webkit {
43 namespace plugins {
44 namespace ppapi {
43 45
44 namespace { 46 namespace {
45 47
46 PP_Resource Create(PP_Instance instance_id) { 48 PP_Resource Create(PP_Instance instance_id) {
47 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); 49 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
48 if (!instance) 50 if (!instance)
49 return 0; 51 return 0;
50 52
51 URLLoader* loader = new URLLoader(instance, false); 53 PPB_URLLoader_Impl* loader = new PPB_URLLoader_Impl(instance, false);
52 return loader->GetReference(); 54 return loader->GetReference();
53 } 55 }
54 56
55 PP_Bool IsURLLoader(PP_Resource resource) { 57 PP_Bool IsURLLoader(PP_Resource resource) {
56 return BoolToPPBool(!!Resource::GetAs<URLLoader>(resource)); 58 return BoolToPPBool(!!Resource::GetAs<PPB_URLLoader_Impl>(resource));
57 } 59 }
58 60
59 int32_t Open(PP_Resource loader_id, 61 int32_t Open(PP_Resource loader_id,
60 PP_Resource request_id, 62 PP_Resource request_id,
61 PP_CompletionCallback callback) { 63 PP_CompletionCallback callback) {
62 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 64 scoped_refptr<PPB_URLLoader_Impl> loader(
65 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
63 if (!loader) 66 if (!loader)
64 return PP_ERROR_BADRESOURCE; 67 return PP_ERROR_BADRESOURCE;
65 68
66 scoped_refptr<URLRequestInfo> request( 69 scoped_refptr<PPB_URLRequestInfo_Impl> request(
67 Resource::GetAs<URLRequestInfo>(request_id)); 70 Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id));
68 if (!request) 71 if (!request)
69 return PP_ERROR_BADRESOURCE; 72 return PP_ERROR_BADRESOURCE;
70 73
71 return loader->Open(request, callback); 74 return loader->Open(request, callback);
72 } 75 }
73 76
74 int32_t FollowRedirect(PP_Resource loader_id, 77 int32_t FollowRedirect(PP_Resource loader_id,
75 PP_CompletionCallback callback) { 78 PP_CompletionCallback callback) {
76 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 79 scoped_refptr<PPB_URLLoader_Impl> loader(
80 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
77 if (!loader) 81 if (!loader)
78 return PP_ERROR_BADRESOURCE; 82 return PP_ERROR_BADRESOURCE;
79 83
80 return loader->FollowRedirect(callback); 84 return loader->FollowRedirect(callback);
81 } 85 }
82 86
83 PP_Bool GetUploadProgress(PP_Resource loader_id, 87 PP_Bool GetUploadProgress(PP_Resource loader_id,
84 int64_t* bytes_sent, 88 int64_t* bytes_sent,
85 int64_t* total_bytes_to_be_sent) { 89 int64_t* total_bytes_to_be_sent) {
86 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 90 scoped_refptr<PPB_URLLoader_Impl> loader(
91 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
87 if (!loader) 92 if (!loader)
88 return PP_FALSE; 93 return PP_FALSE;
89 94
90 return BoolToPPBool(loader->GetUploadProgress(bytes_sent, 95 return BoolToPPBool(loader->GetUploadProgress(bytes_sent,
91 total_bytes_to_be_sent)); 96 total_bytes_to_be_sent));
92 } 97 }
93 98
94 PP_Bool GetDownloadProgress(PP_Resource loader_id, 99 PP_Bool GetDownloadProgress(PP_Resource loader_id,
95 int64_t* bytes_received, 100 int64_t* bytes_received,
96 int64_t* total_bytes_to_be_received) { 101 int64_t* total_bytes_to_be_received) {
97 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 102 scoped_refptr<PPB_URLLoader_Impl> loader(
103 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
98 if (!loader) 104 if (!loader)
99 return PP_FALSE; 105 return PP_FALSE;
100 106
101 return BoolToPPBool(loader->GetDownloadProgress(bytes_received, 107 return BoolToPPBool(loader->GetDownloadProgress(bytes_received,
102 total_bytes_to_be_received)); 108 total_bytes_to_be_received));
103 } 109 }
104 110
105 PP_Resource GetResponseInfo(PP_Resource loader_id) { 111 PP_Resource GetResponseInfo(PP_Resource loader_id) {
106 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 112 scoped_refptr<PPB_URLLoader_Impl> loader(
113 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
107 if (!loader) 114 if (!loader)
108 return 0; 115 return 0;
109 116
110 URLResponseInfo* response_info = loader->response_info(); 117 PPB_URLResponseInfo_Impl* response_info = loader->response_info();
111 if (!response_info) 118 if (!response_info)
112 return 0; 119 return 0;
113 120
114 return response_info->GetReference(); 121 return response_info->GetReference();
115 } 122 }
116 123
117 int32_t ReadResponseBody(PP_Resource loader_id, 124 int32_t ReadResponseBody(PP_Resource loader_id,
118 char* buffer, 125 char* buffer,
119 int32_t bytes_to_read, 126 int32_t bytes_to_read,
120 PP_CompletionCallback callback) { 127 PP_CompletionCallback callback) {
121 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 128 scoped_refptr<PPB_URLLoader_Impl> loader(
129 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
122 if (!loader) 130 if (!loader)
123 return PP_ERROR_BADRESOURCE; 131 return PP_ERROR_BADRESOURCE;
124 132
125 return loader->ReadResponseBody(buffer, bytes_to_read, callback); 133 return loader->ReadResponseBody(buffer, bytes_to_read, callback);
126 } 134 }
127 135
128 int32_t FinishStreamingToFile(PP_Resource loader_id, 136 int32_t FinishStreamingToFile(PP_Resource loader_id,
129 PP_CompletionCallback callback) { 137 PP_CompletionCallback callback) {
130 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 138 scoped_refptr<PPB_URLLoader_Impl> loader(
139 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
131 if (!loader) 140 if (!loader)
132 return PP_ERROR_BADRESOURCE; 141 return PP_ERROR_BADRESOURCE;
133 142
134 return loader->FinishStreamingToFile(callback); 143 return loader->FinishStreamingToFile(callback);
135 } 144 }
136 145
137 void Close(PP_Resource loader_id) { 146 void Close(PP_Resource loader_id) {
138 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 147 scoped_refptr<PPB_URLLoader_Impl> loader(
148 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
139 if (!loader) 149 if (!loader)
140 return; 150 return;
141 151
142 loader->Close(); 152 loader->Close();
143 } 153 }
144 154
145 const PPB_URLLoader ppb_urlloader = { 155 const PPB_URLLoader ppb_urlloader = {
146 &Create, 156 &Create,
147 &IsURLLoader, 157 &IsURLLoader,
148 &Open, 158 &Open,
149 &FollowRedirect, 159 &FollowRedirect,
150 &GetUploadProgress, 160 &GetUploadProgress,
151 &GetDownloadProgress, 161 &GetDownloadProgress,
152 &GetResponseInfo, 162 &GetResponseInfo,
153 &ReadResponseBody, 163 &ReadResponseBody,
154 &FinishStreamingToFile, 164 &FinishStreamingToFile,
155 &Close 165 &Close
156 }; 166 };
157 167
158 void GrantUniversalAccess(PP_Resource loader_id) { 168 void GrantUniversalAccess(PP_Resource loader_id) {
159 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 169 scoped_refptr<PPB_URLLoader_Impl> loader(
170 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
160 if (!loader) 171 if (!loader)
161 return; 172 return;
162 173
163 loader->GrantUniversalAccess(); 174 loader->GrantUniversalAccess();
164 } 175 }
165 176
166 void SetStatusCallback(PP_Resource loader_id, 177 void SetStatusCallback(PP_Resource loader_id,
167 PP_URLLoaderTrusted_StatusCallback cb) { 178 PP_URLLoaderTrusted_StatusCallback cb) {
168 scoped_refptr<URLLoader> loader(Resource::GetAs<URLLoader>(loader_id)); 179 scoped_refptr<PPB_URLLoader_Impl> loader(
180 Resource::GetAs<PPB_URLLoader_Impl>(loader_id));
169 if (!loader) 181 if (!loader)
170 return; 182 return;
171 loader->SetStatusCallback(cb); 183 loader->SetStatusCallback(cb);
172 } 184 }
173 185
174 const PPB_URLLoaderTrusted ppb_urlloadertrusted = { 186 const PPB_URLLoaderTrusted ppb_urlloadertrusted = {
175 &GrantUniversalAccess, 187 &GrantUniversalAccess,
176 &SetStatusCallback 188 &SetStatusCallback
177 }; 189 };
178 190
179 WebKit::WebFrame* GetFrame(PluginInstance* instance) { 191 WebKit::WebFrame* GetFrame(PluginInstance* instance) {
180 return instance->container()->element().document().frame(); 192 return instance->container()->element().document().frame();
181 } 193 }
182 194
183 } // namespace 195 } // namespace
184 196
185 URLLoader::URLLoader(PluginInstance* instance, bool main_document_loader) 197 PPB_URLLoader_Impl::PPB_URLLoader_Impl(PluginInstance* instance,
198 bool main_document_loader)
186 : Resource(instance->module()), 199 : Resource(instance->module()),
187 instance_(instance), 200 instance_(instance),
188 main_document_loader_(main_document_loader), 201 main_document_loader_(main_document_loader),
189 pending_callback_(), 202 pending_callback_(),
190 bytes_sent_(0), 203 bytes_sent_(0),
191 total_bytes_to_be_sent_(-1), 204 total_bytes_to_be_sent_(-1),
192 bytes_received_(0), 205 bytes_received_(0),
193 total_bytes_to_be_received_(-1), 206 total_bytes_to_be_received_(-1),
194 user_buffer_(NULL), 207 user_buffer_(NULL),
195 user_buffer_size_(0), 208 user_buffer_size_(0),
196 done_status_(PP_ERROR_WOULDBLOCK), 209 done_status_(PP_ERROR_WOULDBLOCK),
197 has_universal_access_(false), 210 has_universal_access_(false),
198 status_callback_(NULL) { 211 status_callback_(NULL) {
199 instance->AddObserver(this); 212 instance->AddObserver(this);
200 } 213 }
201 214
202 URLLoader::~URLLoader() { 215 PPB_URLLoader_Impl::~PPB_URLLoader_Impl() {
203 if (instance_) 216 if (instance_)
204 instance_->RemoveObserver(this); 217 instance_->RemoveObserver(this);
205 } 218 }
206 219
207 // static 220 // static
208 const PPB_URLLoader* URLLoader::GetInterface() { 221 const PPB_URLLoader* PPB_URLLoader_Impl::GetInterface() {
209 return &ppb_urlloader; 222 return &ppb_urlloader;
210 } 223 }
211 224
212 // static 225 // static
213 const PPB_URLLoaderTrusted* URLLoader::GetTrustedInterface() { 226 const PPB_URLLoaderTrusted* PPB_URLLoader_Impl::GetTrustedInterface() {
214 return &ppb_urlloadertrusted; 227 return &ppb_urlloadertrusted;
215 } 228 }
216 229
217 URLLoader* URLLoader::AsURLLoader() { 230 PPB_URLLoader_Impl* PPB_URLLoader_Impl::AsURLLoader() {
218 return this; 231 return this;
219 } 232 }
220 233
221 int32_t URLLoader::Open(URLRequestInfo* request, 234 int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request,
222 PP_CompletionCallback callback) { 235 PP_CompletionCallback callback) {
223 if (loader_.get()) 236 if (loader_.get())
224 return PP_ERROR_INPROGRESS; 237 return PP_ERROR_INPROGRESS;
225 238
226 // We only support non-blocking calls. 239 // We only support non-blocking calls.
227 if (!callback.func) 240 if (!callback.func)
228 return PP_ERROR_BADARGUMENT; 241 return PP_ERROR_BADARGUMENT;
229 242
230 WebFrame* frame = GetFrame(instance_); 243 WebFrame* frame = GetFrame(instance_);
231 if (!frame) 244 if (!frame)
232 return PP_ERROR_FAILED; 245 return PP_ERROR_FAILED;
(...skipping 10 matching lines...) Expand all
243 WebApplicationCacheHostImpl::FromFrame(frame)) { 256 WebApplicationCacheHostImpl::FromFrame(frame)) {
244 appcache_host->willStartSubResourceRequest(web_request); 257 appcache_host->willStartSubResourceRequest(web_request);
245 } 258 }
246 259
247 loader_.reset(WebKit::webKitClient()->createURLLoader()); 260 loader_.reset(WebKit::webKitClient()->createURLLoader());
248 if (!loader_.get()) 261 if (!loader_.get())
249 return PP_ERROR_FAILED; 262 return PP_ERROR_FAILED;
250 263
251 loader_->loadAsynchronously(web_request, this); 264 loader_->loadAsynchronously(web_request, this);
252 265
253 request_info_ = scoped_refptr<URLRequestInfo>(request); 266 request_info_ = scoped_refptr<PPB_URLRequestInfo_Impl>(request);
254 pending_callback_ = callback; 267 pending_callback_ = callback;
255 268
256 // Notify completion when we receive a redirect or response headers. 269 // Notify completion when we receive a redirect or response headers.
257 return PP_ERROR_WOULDBLOCK; 270 return PP_ERROR_WOULDBLOCK;
258 } 271 }
259 272
260 int32_t URLLoader::FollowRedirect(PP_CompletionCallback callback) { 273 int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) {
261 if (pending_callback_.func) 274 if (pending_callback_.func)
262 return PP_ERROR_INPROGRESS; 275 return PP_ERROR_INPROGRESS;
263 276
264 // We only support non-blocking calls. 277 // We only support non-blocking calls.
265 if (!callback.func) 278 if (!callback.func)
266 return PP_ERROR_BADARGUMENT; 279 return PP_ERROR_BADARGUMENT;
267 280
268 WebURL redirect_url = GURL(response_info_->redirect_url()); 281 WebURL redirect_url = GURL(response_info_->redirect_url());
269 282
270 int32_t rv = CanRequest(GetFrame(instance_), redirect_url); 283 int32_t rv = CanRequest(GetFrame(instance_), redirect_url);
271 if (rv != PP_OK) 284 if (rv != PP_OK)
272 return rv; 285 return rv;
273 286
274 pending_callback_ = callback; 287 pending_callback_ = callback;
275 loader_->setDefersLoading(false); // Allow the redirect to continue. 288 loader_->setDefersLoading(false); // Allow the redirect to continue.
276 return PP_ERROR_WOULDBLOCK; 289 return PP_ERROR_WOULDBLOCK;
277 } 290 }
278 291
279 bool URLLoader::GetUploadProgress(int64_t* bytes_sent, 292 bool PPB_URLLoader_Impl::GetUploadProgress(int64_t* bytes_sent,
280 int64_t* total_bytes_to_be_sent) { 293 int64_t* total_bytes_to_be_sent) {
281 if (!RecordUploadProgress()) { 294 if (!RecordUploadProgress()) {
282 *bytes_sent = 0; 295 *bytes_sent = 0;
283 *total_bytes_to_be_sent = 0; 296 *total_bytes_to_be_sent = 0;
284 return false; 297 return false;
285 } 298 }
286 *bytes_sent = bytes_sent_; 299 *bytes_sent = bytes_sent_;
287 *total_bytes_to_be_sent = total_bytes_to_be_sent_; 300 *total_bytes_to_be_sent = total_bytes_to_be_sent_;
288 return true; 301 return true;
289 } 302 }
290 303
291 bool URLLoader::GetDownloadProgress(int64_t* bytes_received, 304 bool PPB_URLLoader_Impl::GetDownloadProgress(
292 int64_t* total_bytes_to_be_received) { 305 int64_t* bytes_received,
306 int64_t* total_bytes_to_be_received) {
293 if (!RecordDownloadProgress()) { 307 if (!RecordDownloadProgress()) {
294 *bytes_received = 0; 308 *bytes_received = 0;
295 *total_bytes_to_be_received = 0; 309 *total_bytes_to_be_received = 0;
296 return false; 310 return false;
297 } 311 }
298 *bytes_received = bytes_received_; 312 *bytes_received = bytes_received_;
299 *total_bytes_to_be_received = total_bytes_to_be_received_; 313 *total_bytes_to_be_received = total_bytes_to_be_received_;
300 return true; 314 return true;
301 } 315 }
302 316
303 int32_t URLLoader::ReadResponseBody(char* buffer, int32_t bytes_to_read, 317 int32_t PPB_URLLoader_Impl::ReadResponseBody(char* buffer,
304 PP_CompletionCallback callback) { 318 int32_t bytes_to_read,
319 PP_CompletionCallback callback) {
305 if (!response_info_ || response_info_->body()) 320 if (!response_info_ || response_info_->body())
306 return PP_ERROR_FAILED; 321 return PP_ERROR_FAILED;
307 if (bytes_to_read <= 0 || !buffer) 322 if (bytes_to_read <= 0 || !buffer)
308 return PP_ERROR_BADARGUMENT; 323 return PP_ERROR_BADARGUMENT;
309 if (pending_callback_.func) 324 if (pending_callback_.func)
310 return PP_ERROR_INPROGRESS; 325 return PP_ERROR_INPROGRESS;
311 326
312 // We only support non-blocking calls. 327 // We only support non-blocking calls.
313 if (!callback.func) 328 if (!callback.func)
314 return PP_ERROR_BADARGUMENT; 329 return PP_ERROR_BADARGUMENT;
315 330
316 user_buffer_ = buffer; 331 user_buffer_ = buffer;
317 user_buffer_size_ = bytes_to_read; 332 user_buffer_size_ = bytes_to_read;
318 333
319 if (!buffer_.empty()) 334 if (!buffer_.empty())
320 return FillUserBuffer(); 335 return FillUserBuffer();
321 336
322 // We may have already reached EOF. 337 // We may have already reached EOF.
323 if (done_status_ != PP_ERROR_WOULDBLOCK) { 338 if (done_status_ != PP_ERROR_WOULDBLOCK) {
324 user_buffer_ = NULL; 339 user_buffer_ = NULL;
325 user_buffer_size_ = 0; 340 user_buffer_size_ = 0;
326 return done_status_; 341 return done_status_;
327 } 342 }
328 343
329 pending_callback_ = callback; 344 pending_callback_ = callback;
330 return PP_ERROR_WOULDBLOCK; 345 return PP_ERROR_WOULDBLOCK;
331 } 346 }
332 347
333 int32_t URLLoader::FinishStreamingToFile(PP_CompletionCallback callback) { 348 int32_t PPB_URLLoader_Impl::FinishStreamingToFile(
349 PP_CompletionCallback callback) {
334 if (!response_info_ || !response_info_->body()) 350 if (!response_info_ || !response_info_->body())
335 return PP_ERROR_FAILED; 351 return PP_ERROR_FAILED;
336 if (pending_callback_.func) 352 if (pending_callback_.func)
337 return PP_ERROR_INPROGRESS; 353 return PP_ERROR_INPROGRESS;
338 354
339 // We may have already reached EOF. 355 // We may have already reached EOF.
340 if (done_status_ != PP_ERROR_WOULDBLOCK) 356 if (done_status_ != PP_ERROR_WOULDBLOCK)
341 return done_status_; 357 return done_status_;
342 358
343 // Wait for didFinishLoading / didFail. 359 // Wait for didFinishLoading / didFail.
344 pending_callback_ = callback; 360 pending_callback_ = callback;
345 return PP_ERROR_WOULDBLOCK; 361 return PP_ERROR_WOULDBLOCK;
346 } 362 }
347 363
348 void URLLoader::Close() { 364 void PPB_URLLoader_Impl::Close() {
349 if (loader_.get()) { 365 if (loader_.get()) {
350 loader_->cancel(); 366 loader_->cancel();
351 } else if (main_document_loader_) { 367 } else if (main_document_loader_) {
352 WebFrame* frame = instance_->container()->element().document().frame(); 368 WebFrame* frame = instance_->container()->element().document().frame();
353 frame->stopLoading(); 369 frame->stopLoading();
354 } 370 }
355 } 371 }
356 372
357 void URLLoader::GrantUniversalAccess() { 373 void PPB_URLLoader_Impl::GrantUniversalAccess() {
358 has_universal_access_ = true; 374 has_universal_access_ = true;
359 } 375 }
360 376
361 void URLLoader::SetStatusCallback(PP_URLLoaderTrusted_StatusCallback cb) { 377 void PPB_URLLoader_Impl::SetStatusCallback(
378 PP_URLLoaderTrusted_StatusCallback cb) {
362 status_callback_ = cb; 379 status_callback_ = cb;
363 } 380 }
364 381
365 void URLLoader::willSendRequest(WebURLLoader* loader, 382 void PPB_URLLoader_Impl::willSendRequest(
366 WebURLRequest& new_request, 383 WebURLLoader* loader,
367 const WebURLResponse& redirect_response) { 384 WebURLRequest& new_request,
385 const WebURLResponse& redirect_response) {
368 if (!request_info_->follow_redirects()) { 386 if (!request_info_->follow_redirects()) {
369 SaveResponse(redirect_response); 387 SaveResponse(redirect_response);
370 loader_->setDefersLoading(true); 388 loader_->setDefersLoading(true);
371 RunCallback(PP_OK); 389 RunCallback(PP_OK);
372 } else { 390 } else {
373 int32_t rv = CanRequest(GetFrame(instance_), new_request.url()); 391 int32_t rv = CanRequest(GetFrame(instance_), new_request.url());
374 if (rv != PP_OK) { 392 if (rv != PP_OK) {
375 loader_->setDefersLoading(true); 393 loader_->setDefersLoading(true);
376 RunCallback(rv); 394 RunCallback(rv);
377 } 395 }
378 } 396 }
379 } 397 }
380 398
381 void URLLoader::didSendData(WebURLLoader* loader, 399 void PPB_URLLoader_Impl::didSendData(
382 unsigned long long bytes_sent, 400 WebURLLoader* loader,
383 unsigned long long total_bytes_to_be_sent) { 401 unsigned long long bytes_sent,
402 unsigned long long total_bytes_to_be_sent) {
384 // TODO(darin): Bounds check input? 403 // TODO(darin): Bounds check input?
385 bytes_sent_ = static_cast<int64_t>(bytes_sent); 404 bytes_sent_ = static_cast<int64_t>(bytes_sent);
386 total_bytes_to_be_sent_ = static_cast<int64_t>(total_bytes_to_be_sent); 405 total_bytes_to_be_sent_ = static_cast<int64_t>(total_bytes_to_be_sent);
387 UpdateStatus(); 406 UpdateStatus();
388 } 407 }
389 408
390 void URLLoader::didReceiveResponse(WebURLLoader* loader, 409 void PPB_URLLoader_Impl::didReceiveResponse(WebURLLoader* loader,
391 const WebURLResponse& response) { 410 const WebURLResponse& response) {
392 SaveResponse(response); 411 SaveResponse(response);
393 412
394 // Sets -1 if the content length is unknown. 413 // Sets -1 if the content length is unknown.
395 total_bytes_to_be_received_ = response.expectedContentLength(); 414 total_bytes_to_be_received_ = response.expectedContentLength();
396 UpdateStatus(); 415 UpdateStatus();
397 416
398 RunCallback(PP_OK); 417 RunCallback(PP_OK);
399 } 418 }
400 419
401 void URLLoader::didDownloadData(WebURLLoader* loader, 420 void PPB_URLLoader_Impl::didDownloadData(WebURLLoader* loader,
402 int data_length) { 421 int data_length) {
403 bytes_received_ += data_length; 422 bytes_received_ += data_length;
404 UpdateStatus(); 423 UpdateStatus();
405 } 424 }
406 425
407 void URLLoader::didReceiveData(WebURLLoader* loader, 426 void PPB_URLLoader_Impl::didReceiveData(WebURLLoader* loader,
408 const char* data, 427 const char* data,
409 int data_length) { 428 int data_length) {
410 bytes_received_ += data_length; 429 bytes_received_ += data_length;
411 430
412 buffer_.insert(buffer_.end(), data, data + data_length); 431 buffer_.insert(buffer_.end(), data, data + data_length);
413 if (user_buffer_) { 432 if (user_buffer_) {
414 RunCallback(FillUserBuffer()); 433 RunCallback(FillUserBuffer());
415 } else { 434 } else {
416 DCHECK(!pending_callback_.func); 435 DCHECK(!pending_callback_.func);
417 } 436 }
418 } 437 }
419 438
420 void URLLoader::didFinishLoading(WebURLLoader* loader, double finish_time) { 439 void PPB_URLLoader_Impl::didFinishLoading(WebURLLoader* loader,
440 double finish_time) {
421 done_status_ = PP_OK; 441 done_status_ = PP_OK;
422 RunCallback(done_status_); 442 RunCallback(done_status_);
423 } 443 }
424 444
425 void URLLoader::didFail(WebURLLoader* loader, const WebURLError& error) { 445 void PPB_URLLoader_Impl::didFail(WebURLLoader* loader,
446 const WebURLError& error) {
426 // TODO(darin): Provide more detailed error information. 447 // TODO(darin): Provide more detailed error information.
427 done_status_ = PP_ERROR_FAILED; 448 done_status_ = PP_ERROR_FAILED;
428 RunCallback(done_status_); 449 RunCallback(done_status_);
429 } 450 }
430 451
431 void URLLoader::InstanceDestroyed(PluginInstance* instance) { 452 void PPB_URLLoader_Impl::InstanceDestroyed(PluginInstance* instance) {
432 // When the instance is destroyed, we force delete any associated loads. 453 // When the instance is destroyed, we force delete any associated loads.
433 DCHECK(instance == instance_); 454 DCHECK(instance == instance_);
434 instance_ = NULL; 455 instance_ = NULL;
435 456
436 // Normally the only ref to this class will be from the plugin which 457 // Normally the only ref to this class will be from the plugin which
437 // ForceDeletePluginResourceRefs will free. We don't want our object to be 458 // ForceDeletePluginResourceRefs will free. We don't want our object to be
438 // deleted out from under us until the function completes. 459 // deleted out from under us until the function completes.
439 scoped_refptr<URLLoader> death_grip(this); 460 scoped_refptr<PPB_URLLoader_Impl> death_grip(this);
440 461
441 // Force delete any plugin refs to us. If the instance is being deleted, we 462 // Force delete any plugin refs to us. If the instance is being deleted, we
442 // don't want to allow the requests to continue to use bandwidth and send us 463 // don't want to allow the requests to continue to use bandwidth and send us
443 // callbacks (for which we might have no plugin). 464 // callbacks (for which we might have no plugin).
444 ResourceTracker *tracker = ResourceTracker::Get(); 465 ResourceTracker *tracker = ResourceTracker::Get();
445 PP_Resource loader_resource = GetReferenceNoAddRef(); 466 PP_Resource loader_resource = GetReferenceNoAddRef();
446 if (loader_resource) 467 if (loader_resource)
447 tracker->ForceDeletePluginResourceRefs(loader_resource); 468 tracker->ForceDeletePluginResourceRefs(loader_resource);
448 469
449 // Also force free the response from the plugin, both the plugin's ref(s) 470 // Also force free the response from the plugin, both the plugin's ref(s)
450 // and ours. 471 // and ours.
451 if (response_info_.get()) { 472 if (response_info_.get()) {
452 PP_Resource response_info_resource = response_info_->GetReferenceNoAddRef(); 473 PP_Resource response_info_resource = response_info_->GetReferenceNoAddRef();
453 if (response_info_resource) 474 if (response_info_resource)
454 tracker->ForceDeletePluginResourceRefs(response_info_resource); 475 tracker->ForceDeletePluginResourceRefs(response_info_resource);
455 response_info_ = NULL; 476 response_info_ = NULL;
456 } 477 }
457 478
458 // Free the WebKit request. 479 // Free the WebKit request.
459 loader_.reset(); 480 loader_.reset();
460 481
461 // Often, |this| will be deleted at the end of this function when death_grip 482 // Often, |this| will be deleted at the end of this function when death_grip
462 // goes out of scope. 483 // goes out of scope.
463 } 484 }
464 485
465 void URLLoader::RunCallback(int32_t result) { 486 void PPB_URLLoader_Impl::RunCallback(int32_t result) {
466 if (!pending_callback_.func) 487 if (!pending_callback_.func)
467 return; 488 return;
468 489
469 PP_CompletionCallback callback = {0}; 490 PP_CompletionCallback callback = {0};
470 std::swap(callback, pending_callback_); 491 std::swap(callback, pending_callback_);
471 PP_RunCompletionCallback(&callback, result); 492 PP_RunCompletionCallback(&callback, result);
472 } 493 }
473 494
474 size_t URLLoader::FillUserBuffer() { 495 size_t PPB_URLLoader_Impl::FillUserBuffer() {
475 DCHECK(user_buffer_); 496 DCHECK(user_buffer_);
476 DCHECK(user_buffer_size_); 497 DCHECK(user_buffer_size_);
477 498
478 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); 499 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_);
479 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); 500 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_);
480 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); 501 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy);
481 502
482 // Reset for next time. 503 // Reset for next time.
483 user_buffer_ = NULL; 504 user_buffer_ = NULL;
484 user_buffer_size_ = 0; 505 user_buffer_size_ = 0;
485 return bytes_to_copy; 506 return bytes_to_copy;
486 } 507 }
487 508
488 void URLLoader::SaveResponse(const WebKit::WebURLResponse& response) { 509 void PPB_URLLoader_Impl::SaveResponse(const WebKit::WebURLResponse& response) {
489 scoped_refptr<URLResponseInfo> response_info(new URLResponseInfo(module())); 510 scoped_refptr<PPB_URLResponseInfo_Impl> response_info(
511 new PPB_URLResponseInfo_Impl(module()));
490 if (response_info->Initialize(response)) 512 if (response_info->Initialize(response))
491 response_info_ = response_info; 513 response_info_ = response_info;
492 } 514 }
493 515
494 // Checks that the client can request the URL. Returns a PPAPI error code. 516 // Checks that the client can request the URL. Returns a PPAPI error code.
495 int32_t URLLoader::CanRequest(const WebKit::WebFrame* frame, 517 int32_t PPB_URLLoader_Impl::CanRequest(const WebKit::WebFrame* frame,
496 const WebKit::WebURL& url) { 518 const WebKit::WebURL& url) {
497 if (!has_universal_access_ && 519 if (!has_universal_access_ &&
498 !frame->securityOrigin().canRequest(url)) 520 !frame->securityOrigin().canRequest(url))
499 return PP_ERROR_NOACCESS; 521 return PP_ERROR_NOACCESS;
500 522
501 return PP_OK; 523 return PP_OK;
502 } 524 }
503 525
504 void URLLoader::UpdateStatus() { 526 void PPB_URLLoader_Impl::UpdateStatus() {
505 if (status_callback_ && 527 if (status_callback_ &&
506 (RecordDownloadProgress() || RecordUploadProgress())) { 528 (RecordDownloadProgress() || RecordUploadProgress())) {
507 PP_Resource pp_resource = GetReferenceNoAddRef(); 529 PP_Resource pp_resource = GetReferenceNoAddRef();
508 if (pp_resource) { 530 if (pp_resource) {
509 // The PP_Resource on the plugin will be NULL if the plugin has no 531 // The PP_Resource on the plugin will be NULL if the plugin has no
510 // reference to this object. That's fine, because then we don't need to 532 // reference to this object. That's fine, because then we don't need to
511 // call UpdateStatus. 533 // call UpdateStatus.
512 // 534 //
513 // Here we go through some effort to only send the exact information that 535 // Here we go through some effort to only send the exact information that
514 // the requestor wanted in the request flags. It would be just as 536 // the requestor wanted in the request flags. It would be just as
515 // efficient to send all of it, but we don't want people to rely on 537 // efficient to send all of it, but we don't want people to rely on
516 // getting download progress when they happen to set the upload progress 538 // getting download progress when they happen to set the upload progress
517 // flag. 539 // flag.
518 status_callback_( 540 status_callback_(
519 instance_->pp_instance(), pp_resource, 541 instance_->pp_instance(), pp_resource,
520 RecordUploadProgress() ? bytes_sent_ : -1, 542 RecordUploadProgress() ? bytes_sent_ : -1,
521 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, 543 RecordUploadProgress() ? total_bytes_to_be_sent_ : -1,
522 RecordDownloadProgress() ? bytes_received_ : -1, 544 RecordDownloadProgress() ? bytes_received_ : -1,
523 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1); 545 RecordDownloadProgress() ? total_bytes_to_be_received_ : -1);
524 } 546 }
525 } 547 }
526 } 548 }
527 549
528 bool URLLoader::RecordDownloadProgress() const { 550 bool PPB_URLLoader_Impl::RecordDownloadProgress() const {
529 return request_info_ && request_info_->record_download_progress(); 551 return request_info_ && request_info_->record_download_progress();
530 } 552 }
531 553
532 bool URLLoader::RecordUploadProgress() const { 554 bool PPB_URLLoader_Impl::RecordUploadProgress() const {
533 return request_info_ && request_info_->record_upload_progress(); 555 return request_info_ && request_info_->record_upload_progress();
534 } 556 }
535 557
536 } // namespace pepper 558 } // namespace ppapi
559 } // namespace plugins
560 } // namespace webkit
561
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698