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