Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 The Native Client Authors. All rights reserved. | 1 // Copyright 2010 The Native Client Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can | 2 // Use of this source code is governed by a BSD-style license that can |
| 3 // be found in the LICENSE file. | 3 // be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "native_client/tests/ppapi_geturl/url_load_request.h" | 5 #include "native_client/tests/ppapi_geturl/url_load_request.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| 11 #include "native_client/src/include/portability.h" | 11 #include "native_client/src/include/portability.h" |
| 12 #include "native_client/src/include/nacl_macros.h" | 12 #include "native_client/src/include/nacl_macros.h" |
| 13 #include "native_client/src/shared/platform/nacl_check.h" | 13 #include "native_client/src/shared/platform/nacl_check.h" |
| 14 #include "native_client/tests/ppapi_geturl/module.h" | 14 #include "native_client/tests/ppapi_geturl/module.h" |
| 15 #include "ppapi/c/pp_bool.h" | |
| 15 #include "ppapi/c/pp_completion_callback.h" | 16 #include "ppapi/c/pp_completion_callback.h" |
| 16 #include "ppapi/c/pp_errors.h" | 17 #include "ppapi/c/pp_errors.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 21 // A local helper that does not contribut to loading/reading of urls, but | |
|
sehr (please use chromium)
2011/01/12 17:49:12
contribute
polina
2011/01/12 23:44:27
Done.
| |
| 22 // allows us to test proxying of Is<Interface> functions. | |
| 23 // TODO(polina): when we have unit tests, move this there. | |
| 24 void TestIsInterface(std::string test_interface, | |
| 25 PP_Resource resource, | |
| 26 const PPB_URLRequestInfo* ppb_url_request_info, | |
| 27 const PPB_URLResponseInfo* ppb_url_response_info, | |
| 28 const PPB_URLLoader* ppb_url_loader) { | |
| 29 printf("--- TestIsInterface: %s\n", test_interface.c_str()); | |
| 30 if (test_interface == PPB_URLREQUESTINFO_INTERFACE) { | |
| 31 CHECK(ppb_url_request_info->IsURLRequestInfo(resource) == PP_TRUE); | |
| 32 CHECK(ppb_url_response_info->IsURLResponseInfo(resource) == PP_FALSE); | |
| 33 CHECK(ppb_url_loader->IsURLLoader(resource) == PP_FALSE); | |
| 34 } else if (test_interface == PPB_URLRESPONSEINFO_INTERFACE) { | |
| 35 CHECK(ppb_url_request_info->IsURLRequestInfo(resource) == PP_FALSE); | |
| 36 CHECK(ppb_url_response_info->IsURLResponseInfo(resource) == PP_TRUE); | |
| 37 CHECK(ppb_url_loader->IsURLLoader(resource) == PP_FALSE); | |
| 38 } else if (test_interface == PPB_URLLOADER_INTERFACE) { | |
| 39 CHECK(ppb_url_request_info->IsURLRequestInfo(resource) == PP_FALSE); | |
| 40 CHECK(ppb_url_response_info->IsURLResponseInfo(resource) == PP_FALSE); | |
| 41 CHECK(ppb_url_loader->IsURLLoader(resource) == PP_TRUE); | |
| 42 } | |
| 43 } | |
| 44 | |
| 20 void OpenCallback(void* user_data, int32_t pp_error) { | 45 void OpenCallback(void* user_data, int32_t pp_error) { |
| 21 UrlLoadRequest* obj = reinterpret_cast<UrlLoadRequest*>(user_data); | 46 UrlLoadRequest* obj = reinterpret_cast<UrlLoadRequest*>(user_data); |
| 22 if (NULL != obj) | 47 if (NULL != obj) |
| 23 obj->OpenCallback(pp_error); | 48 obj->OpenCallback(pp_error); |
| 24 } | 49 } |
| 25 | 50 |
| 26 void FinishStreamingToFileCallback(void* user_data, int32_t pp_error) { | 51 void FinishStreamingToFileCallback(void* user_data, int32_t pp_error) { |
| 27 UrlLoadRequest* obj = reinterpret_cast<UrlLoadRequest*>(user_data); | 52 UrlLoadRequest* obj = reinterpret_cast<UrlLoadRequest*>(user_data); |
| 28 if (NULL != obj) | 53 if (NULL != obj) |
| 29 obj->FinishStreamingToFileCallback(pp_error); | 54 obj->FinishStreamingToFileCallback(pp_error); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 if (NULL == loader_interface_) { | 186 if (NULL == loader_interface_) { |
| 162 *error = "Failed to get browser interface '" PPB_URLLOADER_INTERFACE; | 187 *error = "Failed to get browser interface '" PPB_URLLOADER_INTERFACE; |
| 163 return false; | 188 return false; |
| 164 } | 189 } |
| 165 loader_ = loader_interface_->Create(instance_); | 190 loader_ = loader_interface_->Create(instance_); |
| 166 if (0 == loader_) { | 191 if (0 == loader_) { |
| 167 *error = "PPB_URLLoader::Create: failed"; | 192 *error = "PPB_URLLoader::Create: failed"; |
| 168 return false; | 193 return false; |
| 169 } | 194 } |
| 170 | 195 |
| 196 TestIsInterface(PPB_URLREQUESTINFO_INTERFACE, request_, | |
| 197 request_interface_, response_interface_, loader_interface_); | |
| 198 TestIsInterface(PPB_URLLOADER_INTERFACE, loader_, | |
| 199 request_interface_, response_interface_, loader_interface_); | |
| 200 | |
| 201 // TODO(sanga): enable this for untrusted code when FileIO proxy is supported. | |
| 171 #if !defined(__native_client__) | 202 #if !defined(__native_client__) |
| 172 fileio_interface_ = static_cast<const PPB_FileIO_Dev*>( | 203 fileio_interface_ = static_cast<const PPB_FileIO_Dev*>( |
| 173 module->GetBrowserInterface(PPB_FILEIO_DEV_INTERFACE)); | 204 module->GetBrowserInterface(PPB_FILEIO_DEV_INTERFACE)); |
| 174 if (NULL == fileio_interface_) { | 205 if (NULL == fileio_interface_) { |
| 175 *error = "Failed to get browser interface '" PPB_FILEIO_DEV_INTERFACE; | 206 *error = "Failed to get browser interface '" PPB_FILEIO_DEV_INTERFACE; |
| 176 return false; | 207 return false; |
| 177 } | 208 } |
| 178 fileio_ = fileio_interface_->Create(module->module_id()); | 209 fileio_ = fileio_interface_->Create(module->module_id()); |
| 179 if (0 == fileio_) { | 210 if (0 == fileio_) { |
| 180 *error = "PPB_FileIO_Dev::Create: failed"; | 211 *error = "PPB_FileIO_Dev::Create: failed"; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 ReportFailure("UrlLoadRequest::OpenCallback: ", pp_error); | 249 ReportFailure("UrlLoadRequest::OpenCallback: ", pp_error); |
| 219 return; | 250 return; |
| 220 } | 251 } |
| 221 | 252 |
| 222 // Validating response headers to confirm successful loading. | 253 // Validating response headers to confirm successful loading. |
| 223 response_ = loader_interface_->GetResponseInfo(loader_); | 254 response_ = loader_interface_->GetResponseInfo(loader_); |
| 224 if (0 == response_) { | 255 if (0 == response_) { |
| 225 ReportFailure("UrlLoadRequest::OpenCallback: null response"); | 256 ReportFailure("UrlLoadRequest::OpenCallback: null response"); |
| 226 return; | 257 return; |
| 227 } | 258 } |
| 259 TestIsInterface(PPB_URLRESPONSEINFO_INTERFACE, response_, | |
| 260 request_interface_, response_interface_, loader_interface_); | |
| 228 PP_Var url = response_interface_->GetProperty(response_, | 261 PP_Var url = response_interface_->GetProperty(response_, |
| 229 PP_URLRESPONSEPROPERTY_URL); | 262 PP_URLRESPONSEPROPERTY_URL); |
| 230 if (url.type != PP_VARTYPE_STRING) { | 263 if (url.type != PP_VARTYPE_STRING) { |
| 231 ReportFailure("URLLoadRequest::OpenCallback: bad url type"); | 264 ReportFailure("URLLoadRequest::OpenCallback: bad url type"); |
| 232 return; | 265 return; |
| 233 } | 266 } |
| 234 url_ = Module::VarToStr(url); // Update url to be fully qualified. | 267 url_ = Module::VarToStr(url); // Update url to be fully qualified. |
| 235 PP_Var status_code = | 268 PP_Var status_code = |
| 236 response_interface_->GetProperty(response_, | 269 response_interface_->GetProperty(response_, |
| 237 PP_URLRESPONSEPROPERTY_STATUSCODE); | 270 PP_URLRESPONSEPROPERTY_STATUSCODE); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 261 printf("--- UrlLoadRequest::FinishStreamingToFileCallback\n"); | 294 printf("--- UrlLoadRequest::FinishStreamingToFileCallback\n"); |
| 262 if (pp_error != PP_OK) { | 295 if (pp_error != PP_OK) { |
| 263 ReportFailure("UrlLoadRequest::FinishStreamingToFileCallback: ", pp_error); | 296 ReportFailure("UrlLoadRequest::FinishStreamingToFileCallback: ", pp_error); |
| 264 return; | 297 return; |
| 265 } | 298 } |
| 266 PP_Resource fileref = response_interface_->GetBodyAsFileRef(response_); | 299 PP_Resource fileref = response_interface_->GetBodyAsFileRef(response_); |
| 267 if (0 == fileref) { | 300 if (0 == fileref) { |
| 268 ReportFailure("UrlLoadRequest::FinishStreamingToFileCallback: null file"); | 301 ReportFailure("UrlLoadRequest::FinishStreamingToFileCallback: null file"); |
| 269 return; | 302 return; |
| 270 } | 303 } |
| 304 // TODO(sanga): enable this for untrusted code when FileIO proxy is supported. | |
| 305 #if !defined(__native_client__) | |
| 271 pp_error = fileio_interface_->Open( | 306 pp_error = fileio_interface_->Open( |
| 272 fileio_, | 307 fileio_, |
| 273 fileref, | 308 fileref, |
| 274 PP_FILEOPENFLAG_READ, | 309 PP_FILEOPENFLAG_READ, |
| 275 PP_MakeCompletionCallback(::OpenFileBodyCallback, this)); | 310 PP_MakeCompletionCallback(::OpenFileBodyCallback, this)); |
| 276 CHECK(pp_error != PP_OK); // Open() never succeeds synchronously. | 311 CHECK(pp_error != PP_OK); // Open() never succeeds synchronously. |
| 277 if (pp_error != PP_ERROR_WOULDBLOCK) { // Async failure. | 312 if (pp_error != PP_ERROR_WOULDBLOCK) { // Async failure. |
| 278 ReportFailure("PPB_FileIO::Open: ", pp_error); | 313 ReportFailure("PPB_FileIO::Open: ", pp_error); |
| 279 } | 314 } |
| 315 #else | |
| 316 ReportFailure("PPB_FileIO not supported"); | |
| 317 #endif | |
| 280 } | 318 } |
| 281 | 319 |
| 282 void UrlLoadRequest::ReadResponseBodyCallback(int32_t pp_error_or_bytes) { | 320 void UrlLoadRequest::ReadResponseBodyCallback(int32_t pp_error_or_bytes) { |
| 283 printf("--- UrlLoadRequest::ReadResponseBodyCallback\n"); | 321 printf("--- UrlLoadRequest::ReadResponseBodyCallback\n"); |
| 284 if (pp_error_or_bytes < PP_OK) { | 322 if (pp_error_or_bytes < PP_OK) { |
| 285 ReportFailure("UrlLoadRequest::ReadResponseBodyCallback: ", | 323 ReportFailure("UrlLoadRequest::ReadResponseBodyCallback: ", |
| 286 pp_error_or_bytes); | 324 pp_error_or_bytes); |
| 287 } else if (pp_error_or_bytes == PP_OK) { // Reached EOF. | 325 } else if (pp_error_or_bytes == PP_OK) { // Reached EOF. |
| 288 ReportSuccess(); | 326 ReportSuccess(); |
| 289 } else { // Partial read, so copy out the buffer and continue reading. | 327 } else { // Partial read, so copy out the buffer and continue reading. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 309 } | 347 } |
| 310 | 348 |
| 311 void UrlLoadRequest::OpenFileBodyCallback(int32_t pp_error) { | 349 void UrlLoadRequest::OpenFileBodyCallback(int32_t pp_error) { |
| 312 printf("--- UrlLoadRequest::OpenFileBodyCallback\n"); | 350 printf("--- UrlLoadRequest::OpenFileBodyCallback\n"); |
| 313 if (pp_error != PP_OK) { | 351 if (pp_error != PP_OK) { |
| 314 ReportFailure("UrlLoadRequest::OpenFileBodyCallback: ", pp_error); | 352 ReportFailure("UrlLoadRequest::OpenFileBodyCallback: ", pp_error); |
| 315 return; | 353 return; |
| 316 } | 354 } |
| 317 ReadFileBody(); | 355 ReadFileBody(); |
| 318 } | 356 } |
| OLD | NEW |