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