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