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/updater/extension_downloader.h" | 5 #include "chrome/browser/extensions/updater/extension_downloader.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_handle.h" | 14 #include "base/memory/scoped_handle.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
17 #include "base/platform_file.h" | 17 #include "base/platform_file.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" |
20 #include "base/time/time.h" | 22 #include "base/time/time.h" |
21 #include "base/version.h" | 23 #include "base/version.h" |
22 #include "chrome/browser/chrome_notification_types.h" | 24 #include "chrome/browser/chrome_notification_types.h" |
23 #include "chrome/browser/extensions/updater/extension_cache.h" | 25 #include "chrome/browser/extensions/updater/extension_cache.h" |
24 #include "chrome/browser/extensions/updater/request_queue_impl.h" | 26 #include "chrome/browser/extensions/updater/request_queue_impl.h" |
25 #include "chrome/browser/extensions/updater/safe_manifest_parser.h" | 27 #include "chrome/browser/extensions/updater/safe_manifest_parser.h" |
26 #include "chrome/browser/metrics/metrics_service.h" | 28 #include "chrome/browser/metrics/metrics_service.h" |
27 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/chrome_version_info.h" | 30 #include "chrome/common/chrome_version_info.h" |
29 #include "chrome/common/extensions/extension_constants.h" | 31 #include "chrome/common/extensions/extension_constants.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 -1, | 69 -1, |
68 | 70 |
69 // Time to keep an entry from being discarded even when it | 71 // Time to keep an entry from being discarded even when it |
70 // has no significant state, -1 to never discard. | 72 // has no significant state, -1 to never discard. |
71 -1, | 73 -1, |
72 | 74 |
73 // Don't use initial delay unless the last request was an error. | 75 // Don't use initial delay unless the last request was an error. |
74 false, | 76 false, |
75 }; | 77 }; |
76 | 78 |
| 79 const char kAuthUserQueryKey[] = "authuser"; |
| 80 |
| 81 const int kMaxAuthUserValue = 10; |
| 82 |
77 const char kNotFromWebstoreInstallSource[] = "notfromwebstore"; | 83 const char kNotFromWebstoreInstallSource[] = "notfromwebstore"; |
78 const char kDefaultInstallSource[] = ""; | 84 const char kDefaultInstallSource[] = ""; |
79 | 85 |
80 #define RETRY_HISTOGRAM(name, retry_count, url) \ | 86 #define RETRY_HISTOGRAM(name, retry_count, url) \ |
81 if ((url).DomainIs("google.com")) { \ | 87 if ((url).DomainIs("google.com")) { \ |
82 UMA_HISTOGRAM_CUSTOM_COUNTS( \ | 88 UMA_HISTOGRAM_CUSTOM_COUNTS( \ |
83 "Extensions." name "RetryCountGoogleUrl", retry_count, 1, \ | 89 "Extensions." name "RetryCountGoogleUrl", retry_count, 1, \ |
84 kMaxRetries, kMaxRetries+1); \ | 90 kMaxRetries, kMaxRetries+1); \ |
85 } else { \ | 91 } else { \ |
86 UMA_HISTOGRAM_CUSTOM_COUNTS( \ | 92 UMA_HISTOGRAM_CUSTOM_COUNTS( \ |
87 "Extensions." name "RetryCountOtherUrl", retry_count, 1, \ | 93 "Extensions." name "RetryCountOtherUrl", retry_count, 1, \ |
88 kMaxRetries, kMaxRetries+1); \ | 94 kMaxRetries, kMaxRetries+1); \ |
89 } | 95 } |
90 | 96 |
91 bool ShouldRetryRequest(const net::URLRequestStatus& status, | 97 bool ShouldRetryRequest(const net::URLRequestStatus& status, |
92 int response_code) { | 98 int response_code) { |
93 // Retry if the response code is a server error, or the request failed because | 99 // Retry if the response code is a server error, or the request failed because |
94 // of network errors as opposed to file errors. | 100 // of network errors as opposed to file errors. |
95 return (response_code >= 500 && status.is_success()) || | 101 return ((response_code >= 500 && status.is_success()) || |
96 status.status() == net::URLRequestStatus::FAILED; | 102 status.status() == net::URLRequestStatus::FAILED); |
| 103 } |
| 104 |
| 105 bool ShouldRetryRequestWithCookies(const net::URLRequestStatus& status, |
| 106 int response_code, |
| 107 bool included_cookies) { |
| 108 if (included_cookies) |
| 109 return false; |
| 110 |
| 111 if (status.status() == net::URLRequestStatus::CANCELED) |
| 112 return true; |
| 113 |
| 114 // Retry if a 401 or 403 is received. |
| 115 return (status.status() == net::URLRequestStatus::SUCCESS && |
| 116 (response_code == 401 || response_code == 403)); |
| 117 } |
| 118 |
| 119 bool ShouldRetryRequestWithNextUser(const net::URLRequestStatus& status, |
| 120 int response_code, |
| 121 bool included_cookies) { |
| 122 // Retry if a 403 is received in response to a request including cookies. |
| 123 // Note that receiving a 401 in response to a request which included cookies |
| 124 // should indicate that the |authuser| index was out of bounds for the profile |
| 125 // and therefore Chrome should NOT retry with another index. |
| 126 return (status.status() == net::URLRequestStatus::SUCCESS && |
| 127 response_code == 403 && included_cookies); |
| 128 } |
| 129 |
| 130 // This parses and updates a URL query such that the value of the |authuser| |
| 131 // query parameter is incremented by 1. If parameter was not present in the URL, |
| 132 // it will be added with a value of 1. All other query keys and values are |
| 133 // preserved as-is. Returns |false| if the user index exceeds a hard-coded |
| 134 // maximum. |
| 135 bool IncrementAuthUserIndex(GURL* url) { |
| 136 int user_index = 0; |
| 137 std::string old_query = url->query(); |
| 138 std::vector<std::string> new_query_parts; |
| 139 url::Component query(0, old_query.length()); |
| 140 url::Component key, value; |
| 141 while (url::ExtractQueryKeyValue(old_query.c_str(), &query, &key, &value)) { |
| 142 std::string key_string = old_query.substr(key.begin, key.len); |
| 143 std::string value_string = old_query.substr(value.begin, value.len); |
| 144 if (key_string == kAuthUserQueryKey) { |
| 145 base::StringToInt(value_string, &user_index); |
| 146 } else { |
| 147 new_query_parts.push_back(base::StringPrintf( |
| 148 "%s=%s", key_string.c_str(), value_string.c_str())); |
| 149 } |
| 150 } |
| 151 if (user_index >= kMaxAuthUserValue) |
| 152 return false; |
| 153 new_query_parts.push_back( |
| 154 base::StringPrintf("%s=%d", kAuthUserQueryKey, user_index + 1)); |
| 155 std::string new_query_string = JoinString(new_query_parts, '&'); |
| 156 url::Component new_query(0, new_query_string.size()); |
| 157 url::Replacements<char> replacements; |
| 158 replacements.SetQuery(new_query_string.c_str(), new_query); |
| 159 *url = url->ReplaceComponents(replacements); |
| 160 return true; |
97 } | 161 } |
98 | 162 |
99 } // namespace | 163 } // namespace |
100 | 164 |
101 UpdateDetails::UpdateDetails(const std::string& id, const Version& version) | 165 UpdateDetails::UpdateDetails(const std::string& id, const Version& version) |
102 : id(id), version(version) {} | 166 : id(id), version(version) {} |
103 | 167 |
104 UpdateDetails::~UpdateDetails() {} | 168 UpdateDetails::~UpdateDetails() {} |
105 | 169 |
106 ExtensionDownloader::ExtensionFetch::ExtensionFetch() | 170 ExtensionDownloader::ExtensionFetch::ExtensionFetch() |
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 extensions_queue_.reset_active_request(); | 764 extensions_queue_.reset_active_request(); |
701 if (extension_cache_) { | 765 if (extension_cache_) { |
702 const std::string& version = fetch_data->version; | 766 const std::string& version = fetch_data->version; |
703 extension_cache_->PutExtension(id, crx_path, version, | 767 extension_cache_->PutExtension(id, crx_path, version, |
704 base::Bind(&ExtensionDownloader::NotifyDelegateDownloadFinished, | 768 base::Bind(&ExtensionDownloader::NotifyDelegateDownloadFinished, |
705 weak_ptr_factory_.GetWeakPtr(), | 769 weak_ptr_factory_.GetWeakPtr(), |
706 base::Passed(&fetch_data))); | 770 base::Passed(&fetch_data))); |
707 } else { | 771 } else { |
708 NotifyDelegateDownloadFinished(fetch_data.Pass(), crx_path, true); | 772 NotifyDelegateDownloadFinished(fetch_data.Pass(), crx_path, true); |
709 } | 773 } |
710 } else if (status.status() == net::URLRequestStatus::SUCCESS && | 774 } else if (ShouldRetryRequestWithCookies( |
711 (response_code == 401 || response_code == 403) && | 775 status, |
712 !extensions_queue_.active_request()->is_protected) { | 776 response_code, |
713 // On 401 or 403, requeue this fetch with cookies enabled. | 777 extensions_queue_.active_request()->is_protected)) { |
| 778 // Requeue the fetch with |is_protected| set, enabling cookies. |
714 extensions_queue_.active_request()->is_protected = true; | 779 extensions_queue_.active_request()->is_protected = true; |
715 extensions_queue_.RetryRequest(backoff_delay); | 780 extensions_queue_.RetryRequest(backoff_delay); |
| 781 } else if (ShouldRetryRequestWithNextUser( |
| 782 status, |
| 783 response_code, |
| 784 extensions_queue_.active_request()->is_protected) && |
| 785 IncrementAuthUserIndex(&extensions_queue_.active_request()->url)) { |
| 786 extensions_queue_.RetryRequest(backoff_delay); |
716 } else { | 787 } else { |
717 const std::set<int>& request_ids = | 788 const std::set<int>& request_ids = |
718 extensions_queue_.active_request()->request_ids; | 789 extensions_queue_.active_request()->request_ids; |
719 const ExtensionDownloaderDelegate::PingResult& ping = ping_results_[id]; | 790 const ExtensionDownloaderDelegate::PingResult& ping = ping_results_[id]; |
720 | |
721 VLOG(1) << "Failed to fetch extension '" << url.possibly_invalid_spec() | 791 VLOG(1) << "Failed to fetch extension '" << url.possibly_invalid_spec() |
722 << "' response code:" << response_code; | 792 << "' response code:" << response_code; |
723 if (ShouldRetryRequest(status, response_code) && | 793 if (ShouldRetryRequest(status, response_code) && |
724 extensions_queue_.active_request_failure_count() < kMaxRetries) { | 794 extensions_queue_.active_request_failure_count() < kMaxRetries) { |
725 extensions_queue_.RetryRequest(backoff_delay); | 795 extensions_queue_.RetryRequest(backoff_delay); |
726 } else { | 796 } else { |
727 RETRY_HISTOGRAM("CrxFetchFailure", | 797 RETRY_HISTOGRAM("CrxFetchFailure", |
728 extensions_queue_.active_request_failure_count(), url); | 798 extensions_queue_.active_request_failure_count(), url); |
729 // status.error() is 0 (net::OK) or negative. (See net/base/net_errors.h) | 799 // status.error() is 0 (net::OK) or negative. (See net/base/net_errors.h) |
730 UMA_HISTOGRAM_SPARSE_SLOWLY("Extensions.CrxFetchError", -status.error()); | 800 UMA_HISTOGRAM_SPARSE_SLOWLY("Extensions.CrxFetchError", -status.error()); |
(...skipping 25 matching lines...) Expand all Loading... |
756 void ExtensionDownloader::NotifyUpdateFound(const std::string& id, | 826 void ExtensionDownloader::NotifyUpdateFound(const std::string& id, |
757 const std::string& version) { | 827 const std::string& version) { |
758 UpdateDetails updateInfo(id, Version(version)); | 828 UpdateDetails updateInfo(id, Version(version)); |
759 content::NotificationService::current()->Notify( | 829 content::NotificationService::current()->Notify( |
760 chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND, | 830 chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND, |
761 content::NotificationService::AllBrowserContextsAndSources(), | 831 content::NotificationService::AllBrowserContextsAndSources(), |
762 content::Details<UpdateDetails>(&updateInfo)); | 832 content::Details<UpdateDetails>(&updateInfo)); |
763 } | 833 } |
764 | 834 |
765 } // namespace extensions | 835 } // namespace extensions |
OLD | NEW |