| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/extension_protocols.h" | 5 #include "extensions/browser/extension_protocols.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> |
| 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/base64.h" | 11 #include "base/base64.h" |
| 10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 11 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 12 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 13 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
| 14 #include "base/logging.h" | 16 #include "base/logging.h" |
| 15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/metrics/field_trial.h" |
| 17 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
| 18 #include "base/path_service.h" | 21 #include "base/path_service.h" |
| 19 #include "base/sha1.h" | 22 #include "base/sha1.h" |
| 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/threading/sequenced_worker_pool.h" | 27 #include "base/threading/sequenced_worker_pool.h" |
| 23 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
| 24 #include "base/timer/elapsed_timer.h" | 29 #include "base/timer/elapsed_timer.h" |
| 25 #include "build/build_config.h" | 30 #include "build/build_config.h" |
| 26 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/resource_request_info.h" | 32 #include "content/public/browser/resource_request_info.h" |
| 33 #include "crypto/secure_hash.h" |
| 34 #include "crypto/sha2.h" |
| 28 #include "extensions/browser/extensions_browser_client.h" | 35 #include "extensions/browser/extensions_browser_client.h" |
| 29 #include "extensions/browser/info_map.h" | 36 #include "extensions/browser/info_map.h" |
| 30 #include "extensions/common/constants.h" | 37 #include "extensions/common/constants.h" |
| 31 #include "extensions/common/extension.h" | 38 #include "extensions/common/extension.h" |
| 32 #include "extensions/common/extension_resource.h" | 39 #include "extensions/common/extension_resource.h" |
| 33 #include "extensions/common/file_util.h" | 40 #include "extensions/common/file_util.h" |
| 34 #include "extensions/common/manifest_handlers/background_info.h" | 41 #include "extensions/common/manifest_handlers/background_info.h" |
| 35 #include "extensions/common/manifest_handlers/csp_info.h" | 42 #include "extensions/common/manifest_handlers/csp_info.h" |
| 36 #include "extensions/common/manifest_handlers/icons_handler.h" | 43 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 37 #include "extensions/common/manifest_handlers/incognito_info.h" | 44 #include "extensions/common/manifest_handlers/incognito_info.h" |
| 38 #include "extensions/common/manifest_handlers/shared_module_info.h" | 45 #include "extensions/common/manifest_handlers/shared_module_info.h" |
| 39 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" | 46 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" |
| 47 #include "net/base/io_buffer.h" |
| 40 #include "net/base/net_errors.h" | 48 #include "net/base/net_errors.h" |
| 41 #include "net/http/http_request_headers.h" | 49 #include "net/http/http_request_headers.h" |
| 42 #include "net/http/http_response_headers.h" | 50 #include "net/http/http_response_headers.h" |
| 43 #include "net/http/http_response_info.h" | 51 #include "net/http/http_response_info.h" |
| 44 #include "net/url_request/url_request_error_job.h" | 52 #include "net/url_request/url_request_error_job.h" |
| 45 #include "net/url_request/url_request_file_job.h" | 53 #include "net/url_request/url_request_file_job.h" |
| 46 #include "net/url_request/url_request_simple_job.h" | 54 #include "net/url_request/url_request_simple_job.h" |
| 47 #include "url/url_util.h" | 55 #include "url/url_util.h" |
| 48 | 56 |
| 49 using content::BrowserThread; | 57 using content::BrowserThread; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 class URLRequestExtensionJob : public net::URLRequestFileJob { | 158 class URLRequestExtensionJob : public net::URLRequestFileJob { |
| 151 public: | 159 public: |
| 152 URLRequestExtensionJob(net::URLRequest* request, | 160 URLRequestExtensionJob(net::URLRequest* request, |
| 153 net::NetworkDelegate* network_delegate, | 161 net::NetworkDelegate* network_delegate, |
| 154 const std::string& extension_id, | 162 const std::string& extension_id, |
| 155 const base::FilePath& directory_path, | 163 const base::FilePath& directory_path, |
| 156 const base::FilePath& relative_path, | 164 const base::FilePath& relative_path, |
| 157 const std::string& content_security_policy, | 165 const std::string& content_security_policy, |
| 158 bool send_cors_header, | 166 bool send_cors_header, |
| 159 bool follow_symlinks_anywhere) | 167 bool follow_symlinks_anywhere) |
| 160 : net::URLRequestFileJob( | 168 : net::URLRequestFileJob( |
| 161 request, network_delegate, base::FilePath(), | 169 request, |
| 162 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 170 network_delegate, |
| 163 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 171 base::FilePath(), |
| 164 directory_path_(directory_path), | 172 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 165 // TODO(tc): Move all of these files into resources.pak so we don't break | 173 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 166 // when updating on Linux. | 174 seek_position_(0), |
| 167 resource_(extension_id, directory_path, relative_path), | 175 bytes_read_(0), |
| 168 content_security_policy_(content_security_policy), | 176 directory_path_(directory_path), |
| 169 send_cors_header_(send_cors_header), | 177 // TODO(tc): Move all of these files into resources.pak so we don't |
| 170 weak_factory_(this) { | 178 // break when updating on Linux. |
| 179 resource_(extension_id, directory_path, relative_path), |
| 180 content_security_policy_(content_security_policy), |
| 181 send_cors_header_(send_cors_header), |
| 182 weak_factory_(this) { |
| 171 if (follow_symlinks_anywhere) { | 183 if (follow_symlinks_anywhere) { |
| 172 resource_.set_follow_symlinks_anywhere(); | 184 resource_.set_follow_symlinks_anywhere(); |
| 173 } | 185 } |
| 186 const std::string& group = |
| 187 base::FieldTrialList::FindFullName("ExtensionContentHashMeasurement"); |
| 188 if (group == "Yes") { |
| 189 base::ElapsedTimer timer; |
| 190 hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 191 hashing_time_ = timer.Elapsed(); |
| 192 } |
| 174 } | 193 } |
| 175 | 194 |
| 176 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | 195 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { |
| 177 *info = response_info_; | 196 *info = response_info_; |
| 178 } | 197 } |
| 179 | 198 |
| 180 virtual void Start() OVERRIDE { | 199 virtual void Start() OVERRIDE { |
| 181 base::FilePath* read_file_path = new base::FilePath; | 200 base::FilePath* read_file_path = new base::FilePath; |
| 182 base::Time* last_modified_time = new base::Time(); | 201 base::Time* last_modified_time = new base::Time(); |
| 183 bool posted = BrowserThread::PostBlockingPoolTaskAndReply( | 202 bool posted = BrowserThread::PostBlockingPoolTaskAndReply( |
| 184 FROM_HERE, | 203 FROM_HERE, |
| 185 base::Bind(&ReadResourceFilePathAndLastModifiedTime, resource_, | 204 base::Bind(&ReadResourceFilePathAndLastModifiedTime, |
| 205 resource_, |
| 186 directory_path_, | 206 directory_path_, |
| 187 base::Unretained(read_file_path), | 207 base::Unretained(read_file_path), |
| 188 base::Unretained(last_modified_time)), | 208 base::Unretained(last_modified_time)), |
| 189 base::Bind(&URLRequestExtensionJob::OnFilePathAndLastModifiedTimeRead, | 209 base::Bind(&URLRequestExtensionJob::OnFilePathAndLastModifiedTimeRead, |
| 190 weak_factory_.GetWeakPtr(), | 210 weak_factory_.GetWeakPtr(), |
| 191 base::Owned(read_file_path), | 211 base::Owned(read_file_path), |
| 192 base::Owned(last_modified_time))); | 212 base::Owned(last_modified_time))); |
| 193 DCHECK(posted); | 213 DCHECK(posted); |
| 194 } | 214 } |
| 195 | 215 |
| 216 virtual void OnSeekComplete(int64 result) OVERRIDE { |
| 217 DCHECK_EQ(seek_position_, 0); |
| 218 seek_position_ = result; |
| 219 } |
| 220 |
| 221 virtual void OnReadComplete(net::IOBuffer* buffer, int result) OVERRIDE { |
| 222 UMA_HISTOGRAM_COUNTS("ExtensionUrlRequest.OnReadCompleteResult", result); |
| 223 if (result > 0) { |
| 224 bytes_read_ += result; |
| 225 if (hash_.get()) { |
| 226 base::ElapsedTimer timer; |
| 227 hash_->Update(buffer->data(), result); |
| 228 hashing_time_ += timer.Elapsed(); |
| 229 } |
| 230 } |
| 231 } |
| 232 |
| 196 private: | 233 private: |
| 197 virtual ~URLRequestExtensionJob() {} | 234 virtual ~URLRequestExtensionJob() { |
| 235 if (hash_.get()) { |
| 236 base::ElapsedTimer timer; |
| 237 std::string hash_bytes(crypto::kSHA256Length, 0); |
| 238 hash_->Finish(string_as_array(&hash_bytes), hash_bytes.size()); |
| 239 hashing_time_ += timer.Elapsed(); |
| 240 UMA_HISTOGRAM_TIMES("ExtensionUrlRequest.HashTimeMs", hashing_time_); |
| 241 } |
| 242 UMA_HISTOGRAM_COUNTS("ExtensionUrlRequest.TotalKbRead", bytes_read_ / 1024); |
| 243 UMA_HISTOGRAM_COUNTS("ExtensionUrlRequest.SeekPosition", seek_position_); |
| 244 } |
| 198 | 245 |
| 199 void OnFilePathAndLastModifiedTimeRead(base::FilePath* read_file_path, | 246 void OnFilePathAndLastModifiedTimeRead(base::FilePath* read_file_path, |
| 200 base::Time* last_modified_time) { | 247 base::Time* last_modified_time) { |
| 201 file_path_ = *read_file_path; | 248 file_path_ = *read_file_path; |
| 202 response_info_.headers = BuildHttpHeaders( | 249 response_info_.headers = BuildHttpHeaders( |
| 203 content_security_policy_, | 250 content_security_policy_, |
| 204 send_cors_header_, | 251 send_cors_header_, |
| 205 *last_modified_time); | 252 *last_modified_time); |
| 206 URLRequestFileJob::Start(); | 253 URLRequestFileJob::Start(); |
| 207 } | 254 } |
| 208 | 255 |
| 256 // A hash of the contents we've read from the file. |
| 257 scoped_ptr<crypto::SecureHash> hash_; |
| 258 |
| 259 // The position we seeked to in the file. |
| 260 int64 seek_position_; |
| 261 |
| 262 // The number of bytes of content we read from the file. |
| 263 int bytes_read_; |
| 264 |
| 265 // Used to count the total time it takes to do hashing operations. |
| 266 base::TimeDelta hashing_time_; |
| 267 |
| 209 net::HttpResponseInfo response_info_; | 268 net::HttpResponseInfo response_info_; |
| 210 base::FilePath directory_path_; | 269 base::FilePath directory_path_; |
| 211 extensions::ExtensionResource resource_; | 270 extensions::ExtensionResource resource_; |
| 212 std::string content_security_policy_; | 271 std::string content_security_policy_; |
| 213 bool send_cors_header_; | 272 bool send_cors_header_; |
| 214 base::WeakPtrFactory<URLRequestExtensionJob> weak_factory_; | 273 base::WeakPtrFactory<URLRequestExtensionJob> weak_factory_; |
| 215 }; | 274 }; |
| 216 | 275 |
| 217 bool ExtensionCanLoadInIncognito(const ResourceRequestInfo* info, | 276 bool ExtensionCanLoadInIncognito(const ResourceRequestInfo* info, |
| 218 const std::string& extension_id, | 277 const std::string& extension_id, |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 return new net::HttpResponseHeaders(raw_headers); | 538 return new net::HttpResponseHeaders(raw_headers); |
| 480 } | 539 } |
| 481 | 540 |
| 482 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( | 541 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( |
| 483 bool is_incognito, | 542 bool is_incognito, |
| 484 extensions::InfoMap* extension_info_map) { | 543 extensions::InfoMap* extension_info_map) { |
| 485 return new ExtensionProtocolHandler(is_incognito, extension_info_map); | 544 return new ExtensionProtocolHandler(is_incognito, extension_info_map); |
| 486 } | 545 } |
| 487 | 546 |
| 488 } // namespace extensions | 547 } // namespace extensions |
| OLD | NEW |