Chromium Code Reviews| 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/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/metrics/field_trial.h" | |
| 17 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
| 18 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| 19 #include "base/sha1.h" | 20 #include "base/sha1.h" |
| 20 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/threading/sequenced_worker_pool.h" | 25 #include "base/threading/sequenced_worker_pool.h" |
| 25 #include "base/threading/thread_restrictions.h" | 26 #include "base/threading/thread_restrictions.h" |
| 26 #include "base/timer/elapsed_timer.h" | 27 #include "base/timer/elapsed_timer.h" |
| 27 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 28 #include "chrome/browser/extensions/extension_renderer_state.h" | 29 #include "chrome/browser/extensions/extension_renderer_state.h" |
| 29 #include "chrome/browser/extensions/image_loader.h" | 30 #include "chrome/browser/extensions/image_loader.h" |
| 30 #include "chrome/common/chrome_paths.h" | 31 #include "chrome/common/chrome_paths.h" |
| 31 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" | 32 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" |
| 32 #include "chrome/common/extensions/manifest_url_handler.h" | 33 #include "chrome/common/extensions/manifest_url_handler.h" |
| 33 #include "chrome/common/url_constants.h" | 34 #include "chrome/common/url_constants.h" |
| 34 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 35 #include "content/public/browser/resource_request_info.h" | 36 #include "content/public/browser/resource_request_info.h" |
| 37 #include "crypto/secure_hash.h" | |
| 38 #include "crypto/sha2.h" | |
| 36 #include "extensions/browser/info_map.h" | 39 #include "extensions/browser/info_map.h" |
| 37 #include "extensions/common/constants.h" | 40 #include "extensions/common/constants.h" |
| 38 #include "extensions/common/extension.h" | 41 #include "extensions/common/extension.h" |
| 39 #include "extensions/common/extension_resource.h" | 42 #include "extensions/common/extension_resource.h" |
| 40 #include "extensions/common/file_util.h" | 43 #include "extensions/common/file_util.h" |
| 41 #include "extensions/common/manifest_handlers/background_info.h" | 44 #include "extensions/common/manifest_handlers/background_info.h" |
| 42 #include "extensions/common/manifest_handlers/csp_info.h" | 45 #include "extensions/common/manifest_handlers/csp_info.h" |
| 43 #include "extensions/common/manifest_handlers/incognito_info.h" | 46 #include "extensions/common/manifest_handlers/incognito_info.h" |
| 44 #include "extensions/common/manifest_handlers/shared_module_info.h" | 47 #include "extensions/common/manifest_handlers/shared_module_info.h" |
| 45 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" | 48 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" |
| 46 #include "extensions/common/manifest_handlers/webview_info.h" | 49 #include "extensions/common/manifest_handlers/webview_info.h" |
| 47 #include "grit/component_extension_resources_map.h" | 50 #include "grit/component_extension_resources_map.h" |
| 51 #include "net/base/io_buffer.h" | |
| 48 #include "net/base/mime_util.h" | 52 #include "net/base/mime_util.h" |
| 49 #include "net/base/net_errors.h" | 53 #include "net/base/net_errors.h" |
| 50 #include "net/http/http_request_headers.h" | 54 #include "net/http/http_request_headers.h" |
| 51 #include "net/http/http_response_headers.h" | 55 #include "net/http/http_response_headers.h" |
| 52 #include "net/http/http_response_info.h" | 56 #include "net/http/http_response_info.h" |
| 53 #include "net/url_request/url_request_error_job.h" | 57 #include "net/url_request/url_request_error_job.h" |
| 54 #include "net/url_request/url_request_file_job.h" | 58 #include "net/url_request/url_request_file_job.h" |
| 55 #include "net/url_request/url_request_simple_job.h" | 59 #include "net/url_request/url_request_simple_job.h" |
| 56 #include "ui/base/resource/resource_bundle.h" | 60 #include "ui/base/resource/resource_bundle.h" |
| 57 #include "url/url_util.h" | 61 #include "url/url_util.h" |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 class URLRequestExtensionJob : public net::URLRequestFileJob { | 282 class URLRequestExtensionJob : public net::URLRequestFileJob { |
| 279 public: | 283 public: |
| 280 URLRequestExtensionJob(net::URLRequest* request, | 284 URLRequestExtensionJob(net::URLRequest* request, |
| 281 net::NetworkDelegate* network_delegate, | 285 net::NetworkDelegate* network_delegate, |
| 282 const std::string& extension_id, | 286 const std::string& extension_id, |
| 283 const base::FilePath& directory_path, | 287 const base::FilePath& directory_path, |
| 284 const base::FilePath& relative_path, | 288 const base::FilePath& relative_path, |
| 285 const std::string& content_security_policy, | 289 const std::string& content_security_policy, |
| 286 bool send_cors_header, | 290 bool send_cors_header, |
| 287 bool follow_symlinks_anywhere) | 291 bool follow_symlinks_anywhere) |
| 288 : net::URLRequestFileJob( | 292 : net::URLRequestFileJob( |
| 289 request, network_delegate, base::FilePath(), | 293 request, |
| 290 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 294 network_delegate, |
| 291 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 295 base::FilePath(), |
| 292 directory_path_(directory_path), | 296 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 293 // TODO(tc): Move all of these files into resources.pak so we don't break | 297 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 294 // when updating on Linux. | 298 seek_position_(0), |
| 295 resource_(extension_id, directory_path, relative_path), | 299 bytes_read_(0), |
| 296 content_security_policy_(content_security_policy), | 300 directory_path_(directory_path), |
| 297 send_cors_header_(send_cors_header), | 301 // TODO(tc): Move all of these files into resources.pak so we don't |
| 298 weak_factory_(this) { | 302 // break when updating on Linux. |
| 303 resource_(extension_id, directory_path, relative_path), | |
| 304 content_security_policy_(content_security_policy), | |
| 305 send_cors_header_(send_cors_header), | |
| 306 weak_factory_(this) { | |
| 299 if (follow_symlinks_anywhere) { | 307 if (follow_symlinks_anywhere) { |
| 300 resource_.set_follow_symlinks_anywhere(); | 308 resource_.set_follow_symlinks_anywhere(); |
| 301 } | 309 } |
| 310 const std::string& group = | |
| 311 base::FieldTrialList::FindFullName("ExtensionContentHashMeasurement"); | |
| 312 if (group == "Yes") { | |
| 313 base::ElapsedTimer timer; | |
| 314 hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | |
| 315 hashing_time_ += timer.Elapsed(); | |
|
rvargas (doing something else)
2014/04/08 16:53:45
hashing_time_ =
asargent_no_longer_on_chrome
2014/04/09 20:59:59
Done.
| |
| 316 } | |
| 302 } | 317 } |
| 303 | 318 |
| 304 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { | 319 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE { |
| 305 *info = response_info_; | 320 *info = response_info_; |
| 306 } | 321 } |
| 307 | 322 |
| 308 virtual void Start() OVERRIDE { | 323 virtual void Start() OVERRIDE { |
| 309 base::FilePath* read_file_path = new base::FilePath; | 324 base::FilePath* read_file_path = new base::FilePath; |
| 310 base::Time* last_modified_time = new base::Time(); | 325 base::Time* last_modified_time = new base::Time(); |
| 311 bool posted = BrowserThread::PostBlockingPoolTaskAndReply( | 326 bool posted = BrowserThread::PostBlockingPoolTaskAndReply( |
| 312 FROM_HERE, | 327 FROM_HERE, |
| 313 base::Bind(&ReadResourceFilePathAndLastModifiedTime, resource_, | 328 base::Bind(&ReadResourceFilePathAndLastModifiedTime, |
| 329 resource_, | |
| 314 directory_path_, | 330 directory_path_, |
| 315 base::Unretained(read_file_path), | 331 base::Unretained(read_file_path), |
| 316 base::Unretained(last_modified_time)), | 332 base::Unretained(last_modified_time)), |
| 317 base::Bind(&URLRequestExtensionJob::OnFilePathAndLastModifiedTimeRead, | 333 base::Bind(&URLRequestExtensionJob::OnFilePathAndLastModifiedTimeRead, |
| 318 weak_factory_.GetWeakPtr(), | 334 weak_factory_.GetWeakPtr(), |
| 319 base::Owned(read_file_path), | 335 base::Owned(read_file_path), |
| 320 base::Owned(last_modified_time))); | 336 base::Owned(last_modified_time))); |
| 321 DCHECK(posted); | 337 DCHECK(posted); |
| 322 } | 338 } |
| 323 | 339 |
| 340 virtual void OnSeekComplete(int64 result) OVERRIDE { | |
| 341 DCHECK_EQ(seek_position_, 0); | |
| 342 seek_position_ = result; | |
| 343 } | |
| 344 | |
| 345 virtual void OnReadComplete(net::IOBuffer* buffer, int result) OVERRIDE { | |
| 346 UMA_HISTOGRAM_COUNTS("ExtensionUrlRequest.OnReadCompleteResult", result); | |
|
rvargas (doing something else)
2014/04/08 16:53:45
What do you expect to get out of this histogram?
asargent_no_longer_on_chrome
2014/04/09 20:59:59
I'm interested to see the distribution of common r
| |
| 347 if (result > 0) { | |
| 348 bytes_read_ += result; | |
| 349 if (hash_.get()) { | |
| 350 base::ElapsedTimer timer; | |
| 351 hash_->Update(buffer->data(), result); | |
| 352 hashing_time_ += timer.Elapsed(); | |
| 353 } | |
| 354 } | |
| 355 } | |
| 356 | |
| 324 private: | 357 private: |
| 325 virtual ~URLRequestExtensionJob() {} | 358 virtual ~URLRequestExtensionJob() { |
| 359 if (hash_.get()) { | |
| 360 base::ElapsedTimer timer; | |
| 361 std::string hash_bytes(crypto::kSHA256Length, 0); | |
| 362 hash_->Finish(string_as_array(&hash_bytes), hash_bytes.size()); | |
| 363 hashing_time_ += timer.Elapsed(); | |
| 364 UMA_HISTOGRAM_TIMES("ExtensionUrlRequest.HashTimeMs", hashing_time_); | |
| 365 } | |
| 366 UMA_HISTOGRAM_COUNTS("ExtensionUrlRequest.TotalKbRead", | |
| 367 bytes_read_ / 1024); | |
| 368 UMA_HISTOGRAM_COUNTS("ExtensionUrlRequest.SeekPosition", seek_position_); | |
| 369 } | |
| 326 | 370 |
| 327 void OnFilePathAndLastModifiedTimeRead(base::FilePath* read_file_path, | 371 void OnFilePathAndLastModifiedTimeRead(base::FilePath* read_file_path, |
| 328 base::Time* last_modified_time) { | 372 base::Time* last_modified_time) { |
| 329 file_path_ = *read_file_path; | 373 file_path_ = *read_file_path; |
| 330 response_info_.headers = BuildHttpHeaders( | 374 response_info_.headers = BuildHttpHeaders( |
| 331 content_security_policy_, | 375 content_security_policy_, |
| 332 send_cors_header_, | 376 send_cors_header_, |
| 333 *last_modified_time); | 377 *last_modified_time); |
| 334 URLRequestFileJob::Start(); | 378 URLRequestFileJob::Start(); |
| 335 } | 379 } |
| 336 | 380 |
| 381 // A hash of the contents we've read from the file. | |
| 382 scoped_ptr<crypto::SecureHash> hash_; | |
| 383 | |
| 384 // The position we seeked to in the file. | |
| 385 int64 seek_position_; | |
| 386 | |
| 387 // The number of bytes of content we read from the file. | |
| 388 int bytes_read_; | |
| 389 | |
| 390 // Used to count the total time it takes to do hashing operations. | |
| 391 base::TimeDelta hashing_time_; | |
| 392 | |
| 337 net::HttpResponseInfo response_info_; | 393 net::HttpResponseInfo response_info_; |
| 338 base::FilePath directory_path_; | 394 base::FilePath directory_path_; |
| 339 extensions::ExtensionResource resource_; | 395 extensions::ExtensionResource resource_; |
| 340 std::string content_security_policy_; | 396 std::string content_security_policy_; |
| 341 bool send_cors_header_; | 397 bool send_cors_header_; |
| 342 base::WeakPtrFactory<URLRequestExtensionJob> weak_factory_; | 398 base::WeakPtrFactory<URLRequestExtensionJob> weak_factory_; |
| 343 }; | 399 }; |
| 344 | 400 |
| 345 bool ExtensionCanLoadInIncognito(const ResourceRequestInfo* info, | 401 bool ExtensionCanLoadInIncognito(const ResourceRequestInfo* info, |
| 346 const std::string& extension_id, | 402 const std::string& extension_id, |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 636 follow_symlinks_anywhere); | 692 follow_symlinks_anywhere); |
| 637 } | 693 } |
| 638 | 694 |
| 639 } // namespace | 695 } // namespace |
| 640 | 696 |
| 641 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( | 697 net::URLRequestJobFactory::ProtocolHandler* CreateExtensionProtocolHandler( |
| 642 Profile::ProfileType profile_type, | 698 Profile::ProfileType profile_type, |
| 643 extensions::InfoMap* extension_info_map) { | 699 extensions::InfoMap* extension_info_map) { |
| 644 return new ExtensionProtocolHandler(profile_type, extension_info_map); | 700 return new ExtensionProtocolHandler(profile_type, extension_info_map); |
| 645 } | 701 } |
| OLD | NEW |