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