OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_protocols.h" | 5 #include "chrome/browser/extensions/extension_protocols.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 #include "net/base/net_errors.h" | 49 #include "net/base/net_errors.h" |
50 #include "net/http/http_request_headers.h" | 50 #include "net/http/http_request_headers.h" |
51 #include "net/http/http_response_headers.h" | 51 #include "net/http/http_response_headers.h" |
52 #include "net/http/http_response_info.h" | 52 #include "net/http/http_response_info.h" |
53 #include "net/url_request/url_request_error_job.h" | 53 #include "net/url_request/url_request_error_job.h" |
54 #include "net/url_request/url_request_file_job.h" | 54 #include "net/url_request/url_request_file_job.h" |
55 #include "net/url_request/url_request_simple_job.h" | 55 #include "net/url_request/url_request_simple_job.h" |
56 #include "ui/base/resource/resource_bundle.h" | 56 #include "ui/base/resource/resource_bundle.h" |
57 #include "url/url_util.h" | 57 #include "url/url_util.h" |
58 | 58 |
| 59 using content::BrowserThread; |
59 using content::ResourceRequestInfo; | 60 using content::ResourceRequestInfo; |
60 using extensions::Extension; | 61 using extensions::Extension; |
61 using extensions::SharedModuleInfo; | 62 using extensions::SharedModuleInfo; |
62 | 63 |
63 namespace { | 64 namespace { |
64 | 65 |
65 net::HttpResponseHeaders* BuildHttpHeaders( | 66 net::HttpResponseHeaders* BuildHttpHeaders( |
66 const std::string& content_security_policy, bool send_cors_header, | 67 const std::string& content_security_policy, bool send_cors_header, |
67 const base::Time& last_modified_time) { | 68 const base::Time& last_modified_time) { |
68 std::string raw_headers; | 69 std::string raw_headers; |
(...skipping 24 matching lines...) Expand all Loading... |
93 // Also force revalidation. | 94 // Also force revalidation. |
94 raw_headers.append(1, '\0'); | 95 raw_headers.append(1, '\0'); |
95 raw_headers.append("cache-control: no-cache"); | 96 raw_headers.append("cache-control: no-cache"); |
96 } | 97 } |
97 } | 98 } |
98 | 99 |
99 raw_headers.append(2, '\0'); | 100 raw_headers.append(2, '\0'); |
100 return new net::HttpResponseHeaders(raw_headers); | 101 return new net::HttpResponseHeaders(raw_headers); |
101 } | 102 } |
102 | 103 |
103 void ReadMimeTypeFromFile(const base::FilePath& filename, | |
104 std::string* mime_type, | |
105 bool* result) { | |
106 *result = net::GetMimeTypeFromFile(filename, mime_type); | |
107 } | |
108 | |
109 class URLRequestResourceBundleJob : public net::URLRequestSimpleJob { | 104 class URLRequestResourceBundleJob : public net::URLRequestSimpleJob { |
110 public: | 105 public: |
111 URLRequestResourceBundleJob(net::URLRequest* request, | 106 URLRequestResourceBundleJob(net::URLRequest* request, |
112 net::NetworkDelegate* network_delegate, | 107 net::NetworkDelegate* network_delegate, |
113 const base::FilePath& filename, | 108 const base::FilePath& filename, |
114 int resource_id, | 109 int resource_id, |
115 const std::string& content_security_policy, | 110 const std::string& content_security_policy, |
116 bool send_cors_header) | 111 bool send_cors_header) |
117 : net::URLRequestSimpleJob(request, network_delegate), | 112 : net::URLRequestSimpleJob(request, network_delegate), |
118 filename_(filename), | 113 filename_(filename), |
(...skipping 12 matching lines...) Expand all Loading... |
131 const net::CompletionCallback& callback) const OVERRIDE { | 126 const net::CompletionCallback& callback) const OVERRIDE { |
132 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 127 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
133 *data = rb.GetRawDataResource(resource_id_).as_string(); | 128 *data = rb.GetRawDataResource(resource_id_).as_string(); |
134 | 129 |
135 // Add the Content-Length header now that we know the resource length. | 130 // Add the Content-Length header now that we know the resource length. |
136 response_info_.headers->AddHeader(base::StringPrintf( | 131 response_info_.headers->AddHeader(base::StringPrintf( |
137 "%s: %s", net::HttpRequestHeaders::kContentLength, | 132 "%s: %s", net::HttpRequestHeaders::kContentLength, |
138 base::UintToString(data->size()).c_str())); | 133 base::UintToString(data->size()).c_str())); |
139 | 134 |
140 std::string* read_mime_type = new std::string; | 135 std::string* read_mime_type = new std::string; |
141 bool* read_result = new bool; | 136 bool posted = base::PostTaskAndReplyWithResult( |
142 bool posted = content::BrowserThread::PostBlockingPoolTaskAndReply( | 137 BrowserThread::GetBlockingPool(), |
143 FROM_HERE, | 138 FROM_HERE, |
144 base::Bind(&ReadMimeTypeFromFile, filename_, | 139 base::Bind(&net::GetMimeTypeFromFile, filename_, |
145 base::Unretained(read_mime_type), | 140 base::Unretained(read_mime_type)), |
146 base::Unretained(read_result)), | |
147 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead, | 141 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead, |
148 weak_factory_.GetWeakPtr(), | 142 weak_factory_.GetWeakPtr(), |
149 mime_type, charset, data, | 143 mime_type, charset, data, |
150 base::Owned(read_mime_type), | 144 base::Owned(read_mime_type), |
151 base::Owned(read_result), | |
152 callback)); | 145 callback)); |
153 DCHECK(posted); | 146 DCHECK(posted); |
154 | 147 |
155 return net::ERR_IO_PENDING; | 148 return net::ERR_IO_PENDING; |
156 } | 149 } |
157 | 150 |
158 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | 151 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { |
159 *info = response_info_; | 152 *info = response_info_; |
160 } | 153 } |
161 | 154 |
162 private: | 155 private: |
163 virtual ~URLRequestResourceBundleJob() { } | 156 virtual ~URLRequestResourceBundleJob() { } |
164 | 157 |
165 void OnMimeTypeRead(std::string* out_mime_type, | 158 void OnMimeTypeRead(std::string* out_mime_type, |
166 std::string* charset, | 159 std::string* charset, |
167 std::string* data, | 160 std::string* data, |
168 std::string* read_mime_type, | 161 std::string* read_mime_type, |
169 bool* read_result, | 162 const net::CompletionCallback& callback, |
170 const net::CompletionCallback& callback) { | 163 bool read_result) { |
171 *out_mime_type = *read_mime_type; | 164 *out_mime_type = *read_mime_type; |
172 if (StartsWithASCII(*read_mime_type, "text/", false)) { | 165 if (StartsWithASCII(*read_mime_type, "text/", false)) { |
173 // All of our HTML files should be UTF-8 and for other resource types | 166 // All of our HTML files should be UTF-8 and for other resource types |
174 // (like images), charset doesn't matter. | 167 // (like images), charset doesn't matter. |
175 DCHECK(IsStringUTF8(*data)); | 168 DCHECK(IsStringUTF8(*data)); |
176 *charset = "utf-8"; | 169 *charset = "utf-8"; |
177 } | 170 } |
178 int result = *read_result? net::OK: net::ERR_INVALID_URL; | 171 int result = read_result ? net::OK : net::ERR_INVALID_URL; |
179 callback.Run(result); | 172 callback.Run(result); |
180 } | 173 } |
181 | 174 |
182 // We need the filename of the resource to determine the mime type. | 175 // We need the filename of the resource to determine the mime type. |
183 base::FilePath filename_; | 176 base::FilePath filename_; |
184 | 177 |
185 // The resource bundle id to load. | 178 // The resource bundle id to load. |
186 int resource_id_; | 179 int resource_id_; |
187 | 180 |
188 net::HttpResponseInfo response_info_; | 181 net::HttpResponseInfo response_info_; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 public: | 280 public: |
288 URLRequestExtensionJob(net::URLRequest* request, | 281 URLRequestExtensionJob(net::URLRequest* request, |
289 net::NetworkDelegate* network_delegate, | 282 net::NetworkDelegate* network_delegate, |
290 const std::string& extension_id, | 283 const std::string& extension_id, |
291 const base::FilePath& directory_path, | 284 const base::FilePath& directory_path, |
292 const base::FilePath& relative_path, | 285 const base::FilePath& relative_path, |
293 const std::string& content_security_policy, | 286 const std::string& content_security_policy, |
294 bool send_cors_header) | 287 bool send_cors_header) |
295 : net::URLRequestFileJob( | 288 : net::URLRequestFileJob( |
296 request, network_delegate, base::FilePath(), | 289 request, network_delegate, base::FilePath(), |
297 content::BrowserThread::GetBlockingPool()-> | 290 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
298 GetTaskRunnerWithShutdownBehavior( | |
299 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 291 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
300 directory_path_(directory_path), | 292 directory_path_(directory_path), |
301 // TODO(tc): Move all of these files into resources.pak so we don't break | 293 // TODO(tc): Move all of these files into resources.pak so we don't break |
302 // when updating on Linux. | 294 // when updating on Linux. |
303 resource_(extension_id, directory_path, relative_path), | 295 resource_(extension_id, directory_path, relative_path), |
304 content_security_policy_(content_security_policy), | 296 content_security_policy_(content_security_policy), |
305 send_cors_header_(send_cors_header), | 297 send_cors_header_(send_cors_header), |
306 weak_factory_(this) { | 298 weak_factory_(this) { |
307 } | 299 } |
308 | 300 |
309 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | 301 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { |
310 *info = response_info_; | 302 *info = response_info_; |
311 } | 303 } |
312 | 304 |
313 virtual void Start() OVERRIDE { | 305 virtual void Start() OVERRIDE { |
314 base::FilePath* read_file_path = new base::FilePath; | 306 base::FilePath* read_file_path = new base::FilePath; |
315 base::Time* last_modified_time = new base::Time(); | 307 base::Time* last_modified_time = new base::Time(); |
316 bool posted = content::BrowserThread::PostBlockingPoolTaskAndReply( | 308 bool posted = BrowserThread::PostBlockingPoolTaskAndReply( |
317 FROM_HERE, | 309 FROM_HERE, |
318 base::Bind(&ReadResourceFilePathAndLastModifiedTime, resource_, | 310 base::Bind(&ReadResourceFilePathAndLastModifiedTime, resource_, |
319 directory_path_, | 311 directory_path_, |
320 base::Unretained(read_file_path), | 312 base::Unretained(read_file_path), |
321 base::Unretained(last_modified_time)), | 313 base::Unretained(last_modified_time)), |
322 base::Bind(&URLRequestExtensionJob::OnFilePathAndLastModifiedTimeRead, | 314 base::Bind(&URLRequestExtensionJob::OnFilePathAndLastModifiedTimeRead, |
323 weak_factory_.GetWeakPtr(), | 315 weak_factory_.GetWeakPtr(), |
324 base::Owned(read_file_path), | 316 base::Owned(read_file_path), |
325 base::Owned(last_modified_time))); | 317 base::Owned(last_modified_time))); |
326 DCHECK(posted); | 318 DCHECK(posted); |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 send_cors_header); | 621 send_cors_header); |
630 } | 622 } |
631 | 623 |
632 } // namespace | 624 } // namespace |
633 | 625 |
634 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( | 626 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( |
635 bool is_incognito, | 627 bool is_incognito, |
636 extensions::InfoMap* extension_info_map) { | 628 extensions::InfoMap* extension_info_map) { |
637 return new ExtensionProtocolHandler(is_incognito, extension_info_map); | 629 return new ExtensionProtocolHandler(is_incognito, extension_info_map); |
638 } | 630 } |
OLD | NEW |