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/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
18 #include "base/threading/worker_pool.h" | 18 #include "base/threading/worker_pool.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "chrome/browser/extensions/extension_info_map.h" | 20 #include "chrome/browser/extensions/extension_info_map.h" |
21 #include "chrome/browser/extensions/image_loader.h" | 21 #include "chrome/browser/extensions/image_loader.h" |
22 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
23 #include "chrome/common/extensions/background_info.h" | 23 #include "chrome/common/extensions/background_info.h" |
24 #include "chrome/common/extensions/csp_handler.h" | 24 #include "chrome/common/extensions/csp_handler.h" |
25 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
26 #include "chrome/common/extensions/extension_file_util.h" | 26 #include "chrome/common/extensions/extension_file_util.h" |
27 #include "chrome/common/extensions/incognito_handler.h" | 27 #include "chrome/common/extensions/incognito_handler.h" |
28 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" | 28 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" |
| 29 #include "chrome/common/extensions/manifest_handlers/shared_module_info.h" |
29 #include "chrome/common/extensions/manifest_url_handler.h" | 30 #include "chrome/common/extensions/manifest_url_handler.h" |
30 #include "chrome/common/extensions/web_accessible_resources_handler.h" | 31 #include "chrome/common/extensions/web_accessible_resources_handler.h" |
31 #include "chrome/common/url_constants.h" | 32 #include "chrome/common/url_constants.h" |
32 #include "content/public/browser/resource_request_info.h" | 33 #include "content/public/browser/resource_request_info.h" |
33 #include "extensions/common/constants.h" | 34 #include "extensions/common/constants.h" |
34 #include "extensions/common/extension_resource.h" | 35 #include "extensions/common/extension_resource.h" |
35 #include "googleurl/src/url_util.h" | 36 #include "googleurl/src/url_util.h" |
36 #include "grit/component_extension_resources_map.h" | 37 #include "grit/component_extension_resources_map.h" |
37 #include "net/base/mime_util.h" | 38 #include "net/base/mime_util.h" |
38 #include "net/base/net_errors.h" | 39 #include "net/base/net_errors.h" |
39 #include "net/http/http_response_headers.h" | 40 #include "net/http/http_response_headers.h" |
40 #include "net/http/http_response_info.h" | 41 #include "net/http/http_response_info.h" |
41 #include "net/url_request/url_request_error_job.h" | 42 #include "net/url_request/url_request_error_job.h" |
42 #include "net/url_request/url_request_file_job.h" | 43 #include "net/url_request/url_request_file_job.h" |
43 #include "net/url_request/url_request_simple_job.h" | 44 #include "net/url_request/url_request_simple_job.h" |
44 #include "ui/base/resource/resource_bundle.h" | 45 #include "ui/base/resource/resource_bundle.h" |
45 | 46 |
46 using content::ResourceRequestInfo; | 47 using content::ResourceRequestInfo; |
47 using extensions::Extension; | 48 using extensions::Extension; |
| 49 using extensions::SharedModuleInfo; |
48 | 50 |
49 namespace { | 51 namespace { |
50 | 52 |
51 net::HttpResponseHeaders* BuildHttpHeaders( | 53 net::HttpResponseHeaders* BuildHttpHeaders( |
52 const std::string& content_security_policy, bool send_cors_header) { | 54 const std::string& content_security_policy, bool send_cors_header) { |
53 std::string raw_headers; | 55 std::string raw_headers; |
54 raw_headers.append("HTTP/1.1 200 OK"); | 56 raw_headers.append("HTTP/1.1 200 OK"); |
55 if (!content_security_policy.empty()) { | 57 if (!content_security_policy.empty()) { |
56 raw_headers.append(1, '\0'); | 58 raw_headers.append(1, '\0'); |
57 raw_headers.append("Content-Security-Policy: "); | 59 raw_headers.append("Content-Security-Policy: "); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 base::FilePath* file_path) { | 200 base::FilePath* file_path) { |
199 *file_path = resource.GetFilePath(); | 201 *file_path = resource.GetFilePath(); |
200 } | 202 } |
201 | 203 |
202 class URLRequestExtensionJob : public net::URLRequestFileJob { | 204 class URLRequestExtensionJob : public net::URLRequestFileJob { |
203 public: | 205 public: |
204 URLRequestExtensionJob(net::URLRequest* request, | 206 URLRequestExtensionJob(net::URLRequest* request, |
205 net::NetworkDelegate* network_delegate, | 207 net::NetworkDelegate* network_delegate, |
206 const std::string& extension_id, | 208 const std::string& extension_id, |
207 const base::FilePath& directory_path, | 209 const base::FilePath& directory_path, |
| 210 const base::FilePath& relative_path, |
208 const std::string& content_security_policy, | 211 const std::string& content_security_policy, |
209 bool send_cors_header) | 212 bool send_cors_header) |
210 : net::URLRequestFileJob(request, network_delegate, base::FilePath()), | 213 : net::URLRequestFileJob(request, network_delegate, base::FilePath()), |
211 // TODO(tc): Move all of these files into resources.pak so we don't break | 214 // TODO(tc): Move all of these files into resources.pak so we don't break |
212 // when updating on Linux. | 215 // when updating on Linux. |
213 resource_(extension_id, directory_path, | 216 resource_(extension_id, directory_path, relative_path), |
214 extension_file_util::ExtensionURLToRelativeFilePath( | |
215 request->url())), | |
216 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 217 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
217 response_info_.headers = BuildHttpHeaders(content_security_policy, | 218 response_info_.headers = BuildHttpHeaders(content_security_policy, |
218 send_cors_header); | 219 send_cors_header); |
219 } | 220 } |
220 | 221 |
221 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | 222 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { |
222 *info = response_info_; | 223 *info = response_info_; |
223 } | 224 } |
224 | 225 |
225 virtual void Start() OVERRIDE { | 226 virtual void Start() OVERRIDE { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 const bool is_incognito_; | 382 const bool is_incognito_; |
382 ExtensionInfoMap* const extension_info_map_; | 383 ExtensionInfoMap* const extension_info_map_; |
383 DISALLOW_COPY_AND_ASSIGN(ExtensionProtocolHandler); | 384 DISALLOW_COPY_AND_ASSIGN(ExtensionProtocolHandler); |
384 }; | 385 }; |
385 | 386 |
386 // Creates URLRequestJobs for extension:// URLs. | 387 // Creates URLRequestJobs for extension:// URLs. |
387 net::URLRequestJob* | 388 net::URLRequestJob* |
388 ExtensionProtocolHandler::MaybeCreateJob( | 389 ExtensionProtocolHandler::MaybeCreateJob( |
389 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 390 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
390 // chrome-extension://extension-id/resource/path.js | 391 // chrome-extension://extension-id/resource/path.js |
391 const std::string& extension_id = request->url().host(); | 392 std::string extension_id = request->url().host(); |
392 const Extension* extension = | 393 const Extension* extension = |
393 extension_info_map_->extensions().GetByID(extension_id); | 394 extension_info_map_->extensions().GetByID(extension_id); |
394 | 395 |
395 // TODO(mpcomplete): better error code. | 396 // TODO(mpcomplete): better error code. |
396 if (!AllowExtensionResourceLoad( | 397 if (!AllowExtensionResourceLoad( |
397 request, is_incognito_, extension, extension_info_map_)) { | 398 request, is_incognito_, extension, extension_info_map_)) { |
398 return new net::URLRequestErrorJob( | 399 return new net::URLRequestErrorJob( |
399 request, network_delegate, net::ERR_ADDRESS_UNREACHABLE); | 400 request, network_delegate, net::ERR_ADDRESS_UNREACHABLE); |
400 } | 401 } |
401 | 402 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 return new URLRequestResourceBundleJob( | 456 return new URLRequestResourceBundleJob( |
456 request, | 457 request, |
457 network_delegate, | 458 network_delegate, |
458 relative_path, | 459 relative_path, |
459 resource_id, | 460 resource_id, |
460 content_security_policy, | 461 content_security_policy, |
461 send_cors_header); | 462 send_cors_header); |
462 } | 463 } |
463 } | 464 } |
464 | 465 |
| 466 relative_path = |
| 467 extension_file_util::ExtensionURLToRelativeFilePath(request->url()); |
| 468 |
| 469 if (SharedModuleInfo::IsImportedPath(path)) { |
| 470 std::string new_extension_id; |
| 471 std::string new_relative_path; |
| 472 SharedModuleInfo::ParseImportedPath(path, &new_extension_id, |
| 473 &new_relative_path); |
| 474 const Extension* new_extension = |
| 475 extension_info_map_->extensions().GetByID(new_extension_id); |
| 476 |
| 477 bool first_party_in_import = false; |
| 478 // NB: This first_party_for_cookies call is not for security, it is only |
| 479 // used so an exported extension can limit the visible surface to the |
| 480 // extension that imports it, more or less constituting its API. |
| 481 const std::string& first_party_path = |
| 482 request->first_party_for_cookies().path(); |
| 483 if (SharedModuleInfo::IsImportedPath(first_party_path)) { |
| 484 std::string first_party_id; |
| 485 std::string dummy; |
| 486 SharedModuleInfo::ParseImportedPath(first_party_path, &first_party_id, |
| 487 &dummy); |
| 488 if (first_party_id == new_extension_id) { |
| 489 first_party_in_import = true; |
| 490 } |
| 491 } |
| 492 |
| 493 if (SharedModuleInfo::ImportsExtensionById(extension, new_extension_id) && |
| 494 new_extension && |
| 495 (first_party_in_import || |
| 496 SharedModuleInfo::IsExportAllowed(new_extension, new_relative_path))) { |
| 497 directory_path = new_extension->path(); |
| 498 extension_id = new_extension_id; |
| 499 relative_path = base::FilePath(new_relative_path); |
| 500 } else { |
| 501 return NULL; |
| 502 } |
| 503 } |
| 504 |
465 return new URLRequestExtensionJob(request, | 505 return new URLRequestExtensionJob(request, |
466 network_delegate, | 506 network_delegate, |
467 extension_id, | 507 extension_id, |
468 directory_path, | 508 directory_path, |
| 509 relative_path, |
469 content_security_policy, | 510 content_security_policy, |
470 send_cors_header); | 511 send_cors_header); |
471 } | 512 } |
472 | 513 |
473 } // namespace | 514 } // namespace |
474 | 515 |
475 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( | 516 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( |
476 bool is_incognito, | 517 bool is_incognito, |
477 ExtensionInfoMap* extension_info_map) { | 518 ExtensionInfoMap* extension_info_map) { |
478 return new ExtensionProtocolHandler(is_incognito, extension_info_map); | 519 return new ExtensionProtocolHandler(is_incognito, extension_info_map); |
479 } | 520 } |
OLD | NEW |