OLD | NEW |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/trusted/plugin/ppapi/file_downloader.h" | 5 #include "native_client/src/trusted/plugin/ppapi/file_downloader.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "native_client/src/include/portability_io.h" | 10 #include "native_client/src/include/portability_io.h" |
11 #include "native_client/src/shared/platform/nacl_check.h" | 11 #include "native_client/src/shared/platform/nacl_check.h" |
| 12 #include "native_client/src/trusted/plugin/ppapi/plugin_ppapi.h" |
12 #include "native_client/src/trusted/plugin/utility.h" | 13 #include "native_client/src/trusted/plugin/utility.h" |
13 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
14 #include "ppapi/c/dev/ppb_file_io_dev.h" | 15 #include "ppapi/c/dev/ppb_file_io_dev.h" |
| 16 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
15 #include "ppapi/cpp/dev/file_ref_dev.h" | 17 #include "ppapi/cpp/dev/file_ref_dev.h" |
16 #include "ppapi/cpp/url_request_info.h" | 18 #include "ppapi/cpp/url_request_info.h" |
17 #include "ppapi/cpp/url_response_info.h" | 19 #include "ppapi/cpp/url_response_info.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 const char* const kChromeExtensionScheme = "chrome-extension"; | 23 const char* const kChromeExtensionScheme = "chrome-extension:"; |
22 const int32_t kExtensionRequestStatusOk = 0; | 24 const int32_t kExtensionRequestStatusOk = 0; |
23 | 25 |
24 // A helper function that tests to see if |url| is in the chrome-extension | 26 // A helper function that tests to see if |url| is in the chrome-extension |
25 // scheme. Note that this routine assumes that the scheme part of |url| is | 27 // scheme. Note that this routine assumes that the scheme part of |url| is |
26 // all lower-case UTF8. | 28 // all lower-case UTF8. |
27 bool IsChromeExtensionUrl(const std::string& url) { | 29 bool IsChromeExtensionUrl(const std::string& url) { |
28 // The scheme has to exist and be at the start of |url|. | 30 // The scheme has to exist and be at the start of |url|. |
29 return url.find(kChromeExtensionScheme) == 0; | 31 return url.find(kChromeExtensionScheme) == 0; |
30 } | 32 } |
31 } | 33 } |
32 | 34 |
33 namespace plugin { | 35 namespace plugin { |
34 | 36 |
35 void FileDownloader::Initialize(pp::Instance* instance) { | 37 void FileDownloader::Initialize(PluginPpapi* instance) { |
36 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", | 38 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", |
37 static_cast<void*>(this))); | 39 static_cast<void*>(this))); |
38 CHECK(instance != NULL); | 40 CHECK(instance != NULL); |
39 CHECK(instance_ == NULL); | 41 CHECK(instance_ == NULL); |
40 if (instance == NULL) | 42 if (instance == NULL) |
41 return; | 43 return; |
42 if (instance_ != NULL) | 44 if (instance_ != NULL) |
43 return; // Can only initialize once. | 45 return; // Can only initialize once. |
44 instance_ = instance; | 46 instance_ = instance; |
45 callback_factory_.Initialize(this); | 47 callback_factory_.Initialize(this); |
46 } | 48 } |
47 | 49 |
48 | 50 |
49 bool FileDownloader::Open(const nacl::string& url, | 51 bool FileDownloader::Open(const nacl::string& url, |
50 const pp::CompletionCallback& callback) { | 52 const pp::CompletionCallback& callback) { |
51 CHECK(instance_ != NULL); | 53 CHECK(instance_ != NULL); |
52 url_to_open_ = url; | 54 url_to_open_ = url; |
53 url_ = url; | 55 url_ = url; |
54 file_open_notify_callback_ = callback; | 56 file_open_notify_callback_ = callback; |
55 // Reset the url loader and file reader. | 57 // Reset the url loader and file reader. |
56 // Note that we have the only refernce to the underlying objects, so | 58 // Note that we have the only refernce to the underlying objects, so |
57 // this will implicitly close any pending IO and destroy them. | 59 // this will implicitly close any pending IO and destroy them. |
58 url_loader_ = pp::URLLoader(instance_); | 60 url_loader_ = pp::URLLoader(instance_); |
| 61 |
| 62 if (instance_->mime_type() != kNaClMIMEType && IsChromeExtensionUrl(url)) { |
| 63 // This NEXE is being used as a content type handler rather than directly |
| 64 // by an HTML document. In that case, the NEXE runs in the security context |
| 65 // of the content it is rendering and the NEXE itself appears to be a |
| 66 // cross-origin resource stored in a Chrome extension. We request universal |
| 67 // access during this load so that we can read the NEXE. |
| 68 const PPB_URLLoaderTrusted* url_loaded_trusted = |
| 69 static_cast<const PPB_URLLoaderTrusted*>( |
| 70 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADERTRUSTED_INTERFACE)); |
| 71 if (url_loaded_trusted) |
| 72 url_loaded_trusted->GrantUniversalAccess(url_loader_.pp_resource()); |
| 73 } |
| 74 |
59 file_reader_ = pp::FileIO_Dev(instance_); | 75 file_reader_ = pp::FileIO_Dev(instance_); |
60 file_io_trusted_interface_ = static_cast<const PPB_FileIOTrusted_Dev*>( | 76 file_io_trusted_interface_ = static_cast<const PPB_FileIOTrusted_Dev*>( |
61 pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_DEV_INTERFACE)); | 77 pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_DEV_INTERFACE)); |
62 if (file_io_trusted_interface_ == NULL) | 78 if (file_io_trusted_interface_ == NULL) |
63 return false; // Interface not supported by our browser | 79 return false; // Interface not supported by our browser |
64 | 80 |
65 // Prepare the url request. | 81 // Prepare the url request. |
66 pp::URLRequestInfo url_request(instance_); | 82 pp::URLRequestInfo url_request(instance_); |
67 url_request.SetURL(url_); | 83 url_request.SetURL(url_); |
68 url_request.SetStreamToFile(true); | 84 url_request.SetStreamToFile(true); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 } | 226 } |
211 | 227 |
212 | 228 |
213 void FileDownloader::FileOpenNotify(int32_t pp_error) { | 229 void FileDownloader::FileOpenNotify(int32_t pp_error) { |
214 PLUGIN_PRINTF(("FileDownloader::FileOpenNotify (pp_error=%"NACL_PRId32")\n", | 230 PLUGIN_PRINTF(("FileDownloader::FileOpenNotify (pp_error=%"NACL_PRId32")\n", |
215 pp_error)); | 231 pp_error)); |
216 file_open_notify_callback_.Run(pp_error); | 232 file_open_notify_callback_.Run(pp_error); |
217 } | 233 } |
218 | 234 |
219 } // namespace plugin | 235 } // namespace plugin |
OLD | NEW |