| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/stringprintf.h" |
| 15 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 17 #include "chrome/browser/extensions/extension_info_map.h" | 18 #include "chrome/browser/extensions/extension_info_map.h" |
| 18 #include "chrome/browser/net/chrome_url_request_context.h" | 19 #include "chrome/browser/net/chrome_url_request_context.h" |
| 19 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/extensions/extension_file_util.h" | 22 #include "chrome/common/extensions/extension_file_util.h" |
| 22 #include "chrome/common/extensions/extension_resource.h" | 23 #include "chrome/common/extensions/extension_resource.h" |
| 23 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 24 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 25 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 95 |
| 95 // We need the filename of the resource to determine the mime type. | 96 // We need the filename of the resource to determine the mime type. |
| 96 FilePath filename_; | 97 FilePath filename_; |
| 97 | 98 |
| 98 // The resource bundle id to load. | 99 // The resource bundle id to load. |
| 99 int resource_id_; | 100 int resource_id_; |
| 100 | 101 |
| 101 net::HttpResponseInfo response_info_; | 102 net::HttpResponseInfo response_info_; |
| 102 }; | 103 }; |
| 103 | 104 |
| 105 class GeneratedBackgroundPageJob : public net::URLRequestSimpleJob { |
| 106 public: |
| 107 GeneratedBackgroundPageJob(net::URLRequest* request, |
| 108 const scoped_refptr<const Extension> extension, |
| 109 const std::string& content_security_policy) |
| 110 : net::URLRequestSimpleJob(request), |
| 111 extension_(extension) { |
| 112 response_info_.headers = BuildHttpHeaders(content_security_policy); |
| 113 } |
| 114 |
| 115 // Overridden from URLRequestSimpleJob: |
| 116 virtual bool GetData(std::string* mime_type, |
| 117 std::string* charset, |
| 118 std::string* data) const OVERRIDE { |
| 119 *mime_type = "text/html"; |
| 120 *charset = "utf-8"; |
| 121 |
| 122 *data = "<!DOCTYPE html>\n<body>\n"; |
| 123 for (size_t i = 0; i < extension_->background_scripts().size(); ++i) { |
| 124 *data += "<script src=\""; |
| 125 *data += extension_->background_scripts()[i]; |
| 126 *data += "\"></script>\n"; |
| 127 } |
| 128 |
| 129 return true; |
| 130 } |
| 131 |
| 132 virtual void GetResponseInfo(net::HttpResponseInfo* info) { |
| 133 *info = response_info_; |
| 134 } |
| 135 |
| 136 private: |
| 137 scoped_refptr<const Extension> extension_; |
| 138 net::HttpResponseInfo response_info_; |
| 139 }; |
| 140 |
| 104 class URLRequestExtensionJob : public net::URLRequestFileJob { | 141 class URLRequestExtensionJob : public net::URLRequestFileJob { |
| 105 public: | 142 public: |
| 106 URLRequestExtensionJob(net::URLRequest* request, | 143 URLRequestExtensionJob(net::URLRequest* request, |
| 107 const FilePath& filename, | 144 const FilePath& filename, |
| 108 const std::string& content_security_policy) | 145 const std::string& content_security_policy) |
| 109 : net::URLRequestFileJob(request, filename) { | 146 : net::URLRequestFileJob(request, filename) { |
| 110 response_info_.headers = BuildHttpHeaders(content_security_policy); | 147 response_info_.headers = BuildHttpHeaders(content_security_policy); |
| 111 } | 148 } |
| 112 | 149 |
| 113 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | 150 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 if (directory_path.value().empty()) { | 251 if (directory_path.value().empty()) { |
| 215 LOG(WARNING) << "Failed to GetPathForExtension: " << extension_id; | 252 LOG(WARNING) << "Failed to GetPathForExtension: " << extension_id; |
| 216 return NULL; | 253 return NULL; |
| 217 } | 254 } |
| 218 } | 255 } |
| 219 | 256 |
| 220 std::string content_security_policy; | 257 std::string content_security_policy; |
| 221 if (extension) | 258 if (extension) |
| 222 content_security_policy = extension->content_security_policy(); | 259 content_security_policy = extension->content_security_policy(); |
| 223 | 260 |
| 261 std::string path = request->url().path(); |
| 262 if (path.size() > 1 && |
| 263 path.substr(1) == extension_filenames::kGeneratedBackgroundPageFilename) { |
| 264 return new GeneratedBackgroundPageJob( |
| 265 request, extension, content_security_policy); |
| 266 } |
| 267 |
| 224 FilePath resources_path; | 268 FilePath resources_path; |
| 225 if (PathService::Get(chrome::DIR_RESOURCES, &resources_path) && | 269 if (PathService::Get(chrome::DIR_RESOURCES, &resources_path) && |
| 226 directory_path.DirName() == resources_path) { | 270 directory_path.DirName() == resources_path) { |
| 227 FilePath relative_path = directory_path.BaseName().Append( | 271 FilePath relative_path = directory_path.BaseName().Append( |
| 228 extension_file_util::ExtensionURLToRelativeFilePath(request->url())); | 272 extension_file_util::ExtensionURLToRelativeFilePath(request->url())); |
| 229 #if defined(OS_WIN) | 273 #if defined(OS_WIN) |
| 230 relative_path = relative_path.NormalizeWindowsPathSeparators(); | 274 relative_path = relative_path.NormalizeWindowsPathSeparators(); |
| 231 #endif | 275 #endif |
| 232 | 276 |
| 233 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to | 277 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to |
| (...skipping 28 matching lines...) Expand all Loading... |
| 262 content_security_policy); | 306 content_security_policy); |
| 263 } | 307 } |
| 264 | 308 |
| 265 } // namespace | 309 } // namespace |
| 266 | 310 |
| 267 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( | 311 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( |
| 268 bool is_incognito, | 312 bool is_incognito, |
| 269 ExtensionInfoMap* extension_info_map) { | 313 ExtensionInfoMap* extension_info_map) { |
| 270 return new ExtensionProtocolHandler(is_incognito, extension_info_map); | 314 return new ExtensionProtocolHandler(is_incognito, extension_info_map); |
| 271 } | 315 } |
| OLD | NEW |