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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "chrome/common/extensions/extension_file_util.h" | 24 #include "chrome/common/extensions/extension_file_util.h" |
25 #include "chrome/common/extensions/extension_resource.h" | 25 #include "chrome/common/extensions/extension_resource.h" |
26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
27 #include "content/public/browser/resource_request_info.h" | 27 #include "content/public/browser/resource_request_info.h" |
28 #include "googleurl/src/url_util.h" | 28 #include "googleurl/src/url_util.h" |
29 #include "grit/component_extension_resources_map.h" | 29 #include "grit/component_extension_resources_map.h" |
30 #include "net/base/mime_util.h" | 30 #include "net/base/mime_util.h" |
31 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
32 #include "net/http/http_response_headers.h" | 32 #include "net/http/http_response_headers.h" |
33 #include "net/http/http_response_info.h" | 33 #include "net/http/http_response_info.h" |
34 #include "net/url_request/url_request.h" | |
35 #include "net/url_request/url_request_context.h" | |
erikwright (departed)
2012/08/21 02:05:01
why add this?
shalev
2012/08/22 20:34:10
Done.
| |
34 #include "net/url_request/url_request_error_job.h" | 36 #include "net/url_request/url_request_error_job.h" |
35 #include "net/url_request/url_request_file_job.h" | 37 #include "net/url_request/url_request_file_job.h" |
36 #include "net/url_request/url_request_simple_job.h" | 38 #include "net/url_request/url_request_simple_job.h" |
37 #include "ui/base/layout.h" | 39 #include "ui/base/layout.h" |
38 #include "ui/base/resource/resource_bundle.h" | 40 #include "ui/base/resource/resource_bundle.h" |
39 | 41 |
40 using content::ResourceRequestInfo; | 42 using content::ResourceRequestInfo; |
41 using extensions::Extension; | 43 using extensions::Extension; |
42 | 44 |
43 namespace { | 45 namespace { |
(...skipping 17 matching lines...) Expand all Loading... | |
61 } | 63 } |
62 | 64 |
63 void ReadMimeTypeFromFile(const FilePath& filename, | 65 void ReadMimeTypeFromFile(const FilePath& filename, |
64 std::string* mime_type, | 66 std::string* mime_type, |
65 bool* result) { | 67 bool* result) { |
66 *result = net::GetMimeTypeFromFile(filename, mime_type); | 68 *result = net::GetMimeTypeFromFile(filename, mime_type); |
67 } | 69 } |
68 | 70 |
69 class URLRequestResourceBundleJob : public net::URLRequestSimpleJob { | 71 class URLRequestResourceBundleJob : public net::URLRequestSimpleJob { |
70 public: | 72 public: |
71 URLRequestResourceBundleJob( | 73 URLRequestResourceBundleJob(net::URLRequest* request, |
72 net::URLRequest* request, const FilePath& filename, int resource_id, | 74 net::NetworkDelegate* network_delegate, |
73 const std::string& content_security_policy, bool send_cors_header) | 75 const FilePath& filename, |
74 : net::URLRequestSimpleJob(request), | 76 int resource_id, |
77 const std::string& content_security_policy, | |
78 bool send_cors_header) | |
79 : net::URLRequestSimpleJob(request, network_delegate), | |
75 filename_(filename), | 80 filename_(filename), |
76 resource_id_(resource_id), | 81 resource_id_(resource_id), |
77 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 82 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
78 response_info_.headers = BuildHttpHeaders(content_security_policy, | 83 response_info_.headers = BuildHttpHeaders(content_security_policy, |
79 send_cors_header); | 84 send_cors_header); |
80 } | 85 } |
81 | 86 |
82 // Overridden from URLRequestSimpleJob: | 87 // Overridden from URLRequestSimpleJob: |
83 virtual int GetData(std::string* mime_type, | 88 virtual int GetData(std::string* mime_type, |
84 std::string* charset, | 89 std::string* charset, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 int resource_id_; | 143 int resource_id_; |
139 | 144 |
140 net::HttpResponseInfo response_info_; | 145 net::HttpResponseInfo response_info_; |
141 | 146 |
142 mutable base::WeakPtrFactory<URLRequestResourceBundleJob> weak_factory_; | 147 mutable base::WeakPtrFactory<URLRequestResourceBundleJob> weak_factory_; |
143 }; | 148 }; |
144 | 149 |
145 class GeneratedBackgroundPageJob : public net::URLRequestSimpleJob { | 150 class GeneratedBackgroundPageJob : public net::URLRequestSimpleJob { |
146 public: | 151 public: |
147 GeneratedBackgroundPageJob(net::URLRequest* request, | 152 GeneratedBackgroundPageJob(net::URLRequest* request, |
153 net::NetworkDelegate* network_delegate, | |
148 const scoped_refptr<const Extension> extension, | 154 const scoped_refptr<const Extension> extension, |
149 const std::string& content_security_policy) | 155 const std::string& content_security_policy) |
150 : net::URLRequestSimpleJob(request), | 156 : net::URLRequestSimpleJob(request, network_delegate), |
151 extension_(extension) { | 157 extension_(extension) { |
152 const bool send_cors_headers = false; | 158 const bool send_cors_headers = false; |
153 response_info_.headers = BuildHttpHeaders(content_security_policy, | 159 response_info_.headers = BuildHttpHeaders(content_security_policy, |
154 send_cors_headers); | 160 send_cors_headers); |
155 } | 161 } |
156 | 162 |
157 // Overridden from URLRequestSimpleJob: | 163 // Overridden from URLRequestSimpleJob: |
158 virtual int GetData(std::string* mime_type, | 164 virtual int GetData(std::string* mime_type, |
159 std::string* charset, | 165 std::string* charset, |
160 std::string* data, | 166 std::string* data, |
(...skipping 23 matching lines...) Expand all Loading... | |
184 }; | 190 }; |
185 | 191 |
186 void ReadResourceFilePath(const ExtensionResource& resource, | 192 void ReadResourceFilePath(const ExtensionResource& resource, |
187 FilePath* file_path) { | 193 FilePath* file_path) { |
188 *file_path = resource.GetFilePath(); | 194 *file_path = resource.GetFilePath(); |
189 } | 195 } |
190 | 196 |
191 class URLRequestExtensionJob : public net::URLRequestFileJob { | 197 class URLRequestExtensionJob : public net::URLRequestFileJob { |
192 public: | 198 public: |
193 URLRequestExtensionJob(net::URLRequest* request, | 199 URLRequestExtensionJob(net::URLRequest* request, |
200 net::NetworkDelegate* network_delegate, | |
194 const std::string& extension_id, | 201 const std::string& extension_id, |
195 const FilePath& directory_path, | 202 const FilePath& directory_path, |
196 const std::string& content_security_policy, | 203 const std::string& content_security_policy, |
197 bool send_cors_header) | 204 bool send_cors_header) |
198 : net::URLRequestFileJob(request, FilePath()), | 205 : net::URLRequestFileJob(request, network_delegate, FilePath()), |
199 // TODO(tc): Move all of these files into resources.pak so we don't break | 206 // TODO(tc): Move all of these files into resources.pak so we don't break |
200 // when updating on Linux. | 207 // when updating on Linux. |
201 resource_(extension_id, directory_path, | 208 resource_(extension_id, directory_path, |
202 extension_file_util::ExtensionURLToRelativeFilePath( | 209 extension_file_util::ExtensionURLToRelativeFilePath( |
203 request->url())), | 210 request->url())), |
204 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 211 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
205 response_info_.headers = BuildHttpHeaders(content_security_policy, | 212 response_info_.headers = BuildHttpHeaders(content_security_policy, |
206 send_cors_header); | 213 send_cors_header); |
207 } | 214 } |
208 | 215 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 : public net::URLRequestJobFactory::ProtocolHandler { | 303 : public net::URLRequestJobFactory::ProtocolHandler { |
297 public: | 304 public: |
298 ExtensionProtocolHandler(bool is_incognito, | 305 ExtensionProtocolHandler(bool is_incognito, |
299 ExtensionInfoMap* extension_info_map) | 306 ExtensionInfoMap* extension_info_map) |
300 : is_incognito_(is_incognito), | 307 : is_incognito_(is_incognito), |
301 extension_info_map_(extension_info_map) {} | 308 extension_info_map_(extension_info_map) {} |
302 | 309 |
303 virtual ~ExtensionProtocolHandler() {} | 310 virtual ~ExtensionProtocolHandler() {} |
304 | 311 |
305 virtual net::URLRequestJob* MaybeCreateJob( | 312 virtual net::URLRequestJob* MaybeCreateJob( |
306 net::URLRequest* request) const OVERRIDE; | 313 net::URLRequest* request, |
314 net::NetworkDelegate* network_delegate) const OVERRIDE; | |
307 | 315 |
308 private: | 316 private: |
309 const bool is_incognito_; | 317 const bool is_incognito_; |
310 ExtensionInfoMap* const extension_info_map_; | 318 ExtensionInfoMap* const extension_info_map_; |
311 DISALLOW_COPY_AND_ASSIGN(ExtensionProtocolHandler); | 319 DISALLOW_COPY_AND_ASSIGN(ExtensionProtocolHandler); |
312 }; | 320 }; |
313 | 321 |
314 // Creates URLRequestJobs for extension:// URLs. | 322 // Creates URLRequestJobs for extension:// URLs. |
315 net::URLRequestJob* | 323 net::URLRequestJob* |
316 ExtensionProtocolHandler::MaybeCreateJob(net::URLRequest* request) const { | 324 ExtensionProtocolHandler::MaybeCreateJob( |
325 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | |
317 // TODO(mpcomplete): better error code. | 326 // TODO(mpcomplete): better error code. |
318 if (!AllowExtensionResourceLoad( | 327 if (!AllowExtensionResourceLoad( |
319 request, is_incognito_, extension_info_map_)) { | 328 request, is_incognito_, extension_info_map_)) { |
320 return new net::URLRequestErrorJob(request, net::ERR_ADDRESS_UNREACHABLE); | 329 return new net::URLRequestErrorJob( |
330 request, network_delegate, net::ERR_ADDRESS_UNREACHABLE); | |
321 } | 331 } |
322 | 332 |
323 // chrome-extension://extension-id/resource/path.js | 333 // chrome-extension://extension-id/resource/path.js |
324 const std::string& extension_id = request->url().host(); | 334 const std::string& extension_id = request->url().host(); |
325 const Extension* extension = | 335 const Extension* extension = |
326 extension_info_map_->extensions().GetByID(extension_id); | 336 extension_info_map_->extensions().GetByID(extension_id); |
327 FilePath directory_path; | 337 FilePath directory_path; |
328 if (extension) | 338 if (extension) |
329 directory_path = extension->path(); | 339 directory_path = extension->path(); |
330 if (directory_path.value().empty()) { | 340 if (directory_path.value().empty()) { |
(...skipping 16 matching lines...) Expand all Loading... | |
347 if ((extension->manifest_version() >= 2 || | 357 if ((extension->manifest_version() >= 2 || |
348 extension->HasWebAccessibleResources()) && | 358 extension->HasWebAccessibleResources()) && |
349 extension->IsResourceWebAccessible(resource_path)) | 359 extension->IsResourceWebAccessible(resource_path)) |
350 send_cors_header = true; | 360 send_cors_header = true; |
351 } | 361 } |
352 | 362 |
353 std::string path = request->url().path(); | 363 std::string path = request->url().path(); |
354 if (path.size() > 1 && | 364 if (path.size() > 1 && |
355 path.substr(1) == extension_filenames::kGeneratedBackgroundPageFilename) { | 365 path.substr(1) == extension_filenames::kGeneratedBackgroundPageFilename) { |
356 return new GeneratedBackgroundPageJob( | 366 return new GeneratedBackgroundPageJob( |
357 request, extension, content_security_policy); | 367 request, network_delegate, extension, content_security_policy); |
358 } | 368 } |
359 | 369 |
360 FilePath resources_path; | 370 FilePath resources_path; |
361 FilePath relative_path; | 371 FilePath relative_path; |
362 // Try to load extension resources from chrome resource file if | 372 // Try to load extension resources from chrome resource file if |
363 // directory_path is a descendant of resources_path. resources_path | 373 // directory_path is a descendant of resources_path. resources_path |
364 // corresponds to src/chrome/browser/resources in source tree. | 374 // corresponds to src/chrome/browser/resources in source tree. |
365 if (PathService::Get(chrome::DIR_RESOURCES, &resources_path) && | 375 if (PathService::Get(chrome::DIR_RESOURCES, &resources_path) && |
366 // Since component extension resources are included in | 376 // Since component extension resources are included in |
367 // component_extension_resources.pak file in resources_path, calculate | 377 // component_extension_resources.pak file in resources_path, calculate |
368 // extension relative path against resources_path. | 378 // extension relative path against resources_path. |
369 resources_path.AppendRelativePath(directory_path, &relative_path)) { | 379 resources_path.AppendRelativePath(directory_path, &relative_path)) { |
370 relative_path = relative_path.Append( | 380 relative_path = relative_path.Append( |
371 extension_file_util::ExtensionURLToRelativeFilePath(request->url())); | 381 extension_file_util::ExtensionURLToRelativeFilePath(request->url())); |
372 relative_path = relative_path.NormalizePathSeparators(); | 382 relative_path = relative_path.NormalizePathSeparators(); |
373 | 383 |
374 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to | 384 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to |
375 // covert to FilePaths all the time. This will be more useful as we add | 385 // covert to FilePaths all the time. This will be more useful as we add |
376 // more resources. | 386 // more resources. |
377 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | 387 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { |
378 FilePath bm_resource_path = | 388 FilePath bm_resource_path = |
379 FilePath().AppendASCII(kComponentExtensionResources[i].name); | 389 FilePath().AppendASCII(kComponentExtensionResources[i].name); |
380 bm_resource_path = bm_resource_path.NormalizePathSeparators(); | 390 bm_resource_path = bm_resource_path.NormalizePathSeparators(); |
381 if (relative_path == bm_resource_path) { | 391 if (relative_path == bm_resource_path) { |
382 return new URLRequestResourceBundleJob(request, relative_path, | 392 return new URLRequestResourceBundleJob( |
383 kComponentExtensionResources[i].value, content_security_policy, | 393 request, |
394 network_delegate, | |
395 relative_path, | |
396 kComponentExtensionResources[i].value, | |
397 content_security_policy, | |
384 send_cors_header); | 398 send_cors_header); |
385 } | 399 } |
386 } | 400 } |
387 } | 401 } |
388 | 402 |
389 return new URLRequestExtensionJob(request, extension_id, directory_path, | 403 return new URLRequestExtensionJob(request, |
390 content_security_policy, send_cors_header); | 404 network_delegate, |
405 extension_id, | |
406 directory_path, | |
407 content_security_policy, | |
408 send_cors_header); | |
391 } | 409 } |
392 | 410 |
393 } // namespace | 411 } // namespace |
394 | 412 |
395 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( | 413 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( |
396 bool is_incognito, | 414 bool is_incognito, |
397 ExtensionInfoMap* extension_info_map) { | 415 ExtensionInfoMap* extension_info_map) { |
398 return new ExtensionProtocolHandler(is_incognito, extension_info_map); | 416 return new ExtensionProtocolHandler(is_incognito, extension_info_map); |
399 } | 417 } |
OLD | NEW |