Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.cc

Issue 1887253002: Rate limit programmatic update checks for extensions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.h" 5 #include "chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.h"
6 6
7 #include <memory>
7 #include <string> 8 #include <string>
8 #include <utility> 9 #include <utility>
9 10
10 #include "base/location.h" 11 #include "base/location.h"
11 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h" 14 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "build/build_config.h" 16 #include "build/build_config.h"
16 #include "chrome/browser/extensions/extension_service.h" 17 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_tab_util.h" 18 #include "chrome/browser/extensions/extension_tab_util.h"
18 #include "chrome/browser/extensions/updater/extension_updater.h" 19 #include "chrome/browser/extensions/updater/extension_updater.h"
19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser_finder.h" 21 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/browser_navigator.h" 22 #include "chrome/browser/ui/browser_navigator.h"
22 #include "chrome/browser/ui/browser_navigator_params.h" 23 #include "chrome/browser/ui/browser_navigator_params.h"
23 #include "chrome/browser/ui/browser_window.h" 24 #include "chrome/browser/ui/browser_window.h"
24 #include "components/update_client/update_query_params.h" 25 #include "components/update_client/update_query_params.h"
25 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
27 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h" 28 #include "extensions/browser/extension_system.h"
27 #include "extensions/browser/notification_types.h" 29 #include "extensions/browser/notification_types.h"
28 #include "extensions/browser/warning_service.h" 30 #include "extensions/browser/warning_service.h"
29 #include "extensions/browser/warning_set.h" 31 #include "extensions/browser/warning_set.h"
30 #include "extensions/common/api/runtime.h" 32 #include "extensions/common/api/runtime.h"
33 #include "extensions/common/constants.h"
34 #include "net/base/backoff_entry.h"
31 35
32 #if defined(OS_CHROMEOS) 36 #if defined(OS_CHROMEOS)
33 #include "chromeos/dbus/dbus_thread_manager.h" 37 #include "chromeos/dbus/dbus_thread_manager.h"
34 #include "chromeos/dbus/power_manager_client.h" 38 #include "chromeos/dbus/power_manager_client.h"
35 #include "components/user_manager/user_manager.h" 39 #include "components/user_manager/user_manager.h"
36 #endif 40 #endif
37 41
38 using extensions::Extension; 42 using extensions::Extension;
39 using extensions::ExtensionSystem; 43 using extensions::ExtensionSystem;
40 using extensions::ExtensionUpdater; 44 using extensions::ExtensionUpdater;
41 45
42 using extensions::api::runtime::PlatformInfo; 46 using extensions::api::runtime::PlatformInfo;
43 47
44 namespace { 48 namespace {
45 49
46 const char kUpdateThrottled[] = "throttled"; 50 const char kUpdateThrottled[] = "throttled";
47 const char kUpdateNotFound[] = "no_update"; 51 const char kUpdateNotFound[] = "no_update";
48 const char kUpdateFound[] = "update_available"; 52 const char kUpdateFound[] = "update_available";
49 53
50 // If an extension reloads itself within this many miliseconds of reloading 54 // If an extension reloads itself within this many miliseconds of reloading
51 // itself, the reload is considered suspiciously fast. 55 // itself, the reload is considered suspiciously fast.
52 const int kFastReloadTime = 10000; 56 const int kFastReloadTime = 10000;
53 57
54 // After this many suspiciously fast consecutive reloads, an extension will get 58 // After this many suspiciously fast consecutive reloads, an extension will get
55 // disabled. 59 // disabled.
56 const int kFastReloadCount = 5; 60 const int kFastReloadCount = 5;
57 61
62 // The policy we use for exponential backoff of update check requests.
63 const net::BackoffEntry::Policy kBackoffPolicy = {
Devlin 2016/04/15 17:57:37 I think this causes problems with static initializ
asargent_no_longer_on_chrome 2016/04/19 17:56:52 I think this is actually ok because net::BackoffEn
64 // num_errors_to_ignore
65 0,
66
67 // initial_delay_ms (note that we set 'always_use_initial_delay' to false
68 // below)
69 1000 * extensions::kDefaultUpdateFrequencySeconds,
70
71 // multiply_factor
72 1,
73
74 // jitter_factor
75 0.1,
76
77 // maximum_backoff_ms
Devlin 2016/04/15 17:57:37 might be worth commenting that -1 == no maximum
asargent_no_longer_on_chrome 2016/04/19 17:56:52 Done.
78 -1,
79
80 // entry_lifetime_ms (-1 means never discard)
81 -1,
82
83 // always_use_initial_delay
84 false,
85 };
86
87 base::TickClock* g_test_clock = nullptr;
88
58 } // namespace 89 } // namespace
59 90
91 struct ChromeRuntimeAPIDelegate::UpdateCheckInfo {
92 public:
93 UpdateCheckInfo() {
94 if (g_test_clock)
95 backoff.reset(new net::BackoffEntry(&kBackoffPolicy, g_test_clock));
96 else
97 backoff.reset(new net::BackoffEntry(&kBackoffPolicy));
98 }
99
100 std::unique_ptr<net::BackoffEntry> backoff;
101 std::vector<UpdateCheckCallback> callbacks;
102 };
103
60 ChromeRuntimeAPIDelegate::ChromeRuntimeAPIDelegate( 104 ChromeRuntimeAPIDelegate::ChromeRuntimeAPIDelegate(
61 content::BrowserContext* context) 105 content::BrowserContext* context)
62 : browser_context_(context), registered_for_updates_(false) { 106 : browser_context_(context),
107 registered_for_updates_(false),
108 extension_registry_observer_(this) {
63 registrar_.Add(this, 109 registrar_.Add(this,
64 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND, 110 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND,
65 content::NotificationService::AllSources()); 111 content::NotificationService::AllSources());
112 extension_registry_observer_.Add(
113 extensions::ExtensionRegistry::Get(browser_context_));
66 } 114 }
67 115
68 ChromeRuntimeAPIDelegate::~ChromeRuntimeAPIDelegate() { 116 ChromeRuntimeAPIDelegate::~ChromeRuntimeAPIDelegate() {
69 } 117 }
70 118
119 // static
120 void ChromeRuntimeAPIDelegate::set_tick_clock_for_tests(
121 base::TickClock* clock) {
122 g_test_clock = clock;
123 }
124
71 void ChromeRuntimeAPIDelegate::AddUpdateObserver( 125 void ChromeRuntimeAPIDelegate::AddUpdateObserver(
72 extensions::UpdateObserver* observer) { 126 extensions::UpdateObserver* observer) {
73 registered_for_updates_ = true; 127 registered_for_updates_ = true;
74 ExtensionSystem::Get(browser_context_) 128 ExtensionSystem::Get(browser_context_)
75 ->extension_service() 129 ->extension_service()
76 ->AddUpdateObserver(observer); 130 ->AddUpdateObserver(observer);
77 } 131 }
78 132
79 void ChromeRuntimeAPIDelegate::RemoveUpdateObserver( 133 void ChromeRuntimeAPIDelegate::RemoveUpdateObserver(
80 extensions::UpdateObserver* observer) { 134 extensions::UpdateObserver* observer) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 199
146 bool ChromeRuntimeAPIDelegate::CheckForUpdates( 200 bool ChromeRuntimeAPIDelegate::CheckForUpdates(
147 const std::string& extension_id, 201 const std::string& extension_id,
148 const UpdateCheckCallback& callback) { 202 const UpdateCheckCallback& callback) {
149 ExtensionSystem* system = ExtensionSystem::Get(browser_context_); 203 ExtensionSystem* system = ExtensionSystem::Get(browser_context_);
150 ExtensionService* service = system->extension_service(); 204 ExtensionService* service = system->extension_service();
151 ExtensionUpdater* updater = service->updater(); 205 ExtensionUpdater* updater = service->updater();
152 if (!updater) { 206 if (!updater) {
153 return false; 207 return false;
154 } 208 }
155 if (!updater->CheckExtensionSoon( 209
156 extension_id, 210 UpdateCheckInfo& info = update_check_info_[extension_id];
157 base::Bind(&ChromeRuntimeAPIDelegate::UpdateCheckComplete, 211
158 base::Unretained(this), 212 if (info.backoff->ShouldRejectRequest() || info.callbacks.size() >= 10) {
Devlin 2016/04/15 17:57:37 document what 10 is supposed to signify here.
asargent_no_longer_on_chrome 2016/04/19 17:56:52 Done.
159 extension_id))) {
160 base::ThreadTaskRunnerHandle::Get()->PostTask( 213 base::ThreadTaskRunnerHandle::Get()->PostTask(
161 FROM_HERE, 214 FROM_HERE,
162 base::Bind(callback, UpdateCheckResult(true, kUpdateThrottled, ""))); 215 base::Bind(callback, UpdateCheckResult(true, kUpdateThrottled, "")));
163 } else { 216 } else {
164 UpdateCallbackList& callbacks = pending_update_checks_[extension_id]; 217 info.callbacks.push_back(callback);
165 callbacks.push_back(callback); 218 updater->CheckExtensionSoon(
219 extension_id, base::Bind(&ChromeRuntimeAPIDelegate::UpdateCheckComplete,
220 base::Unretained(this), extension_id));
166 } 221 }
167 return true; 222 return true;
168 } 223 }
169 224
170 void ChromeRuntimeAPIDelegate::OpenURL(const GURL& uninstall_url) { 225 void ChromeRuntimeAPIDelegate::OpenURL(const GURL& uninstall_url) {
171 Profile* profile = Profile::FromBrowserContext(browser_context_); 226 Profile* profile = Profile::FromBrowserContext(browser_context_);
172 Browser* browser = chrome::FindLastActiveWithProfile(profile); 227 Browser* browser = chrome::FindLastActiveWithProfile(profile);
173 if (!browser) 228 if (!browser)
174 browser = new Browser(Browser::CreateParams(profile)); 229 browser = new Browser(Browser::CreateParams(profile));
175 230
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 DCHECK(type == extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND); 307 DCHECK(type == extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND);
253 typedef const std::pair<std::string, Version> UpdateDetails; 308 typedef const std::pair<std::string, Version> UpdateDetails;
254 const std::string& id = content::Details<UpdateDetails>(details)->first; 309 const std::string& id = content::Details<UpdateDetails>(details)->first;
255 const Version& version = content::Details<UpdateDetails>(details)->second; 310 const Version& version = content::Details<UpdateDetails>(details)->second;
256 if (version.IsValid()) { 311 if (version.IsValid()) {
257 CallUpdateCallbacks( 312 CallUpdateCallbacks(
258 id, UpdateCheckResult(true, kUpdateFound, version.GetString())); 313 id, UpdateCheckResult(true, kUpdateFound, version.GetString()));
259 } 314 }
260 } 315 }
261 316
317 void ChromeRuntimeAPIDelegate::OnExtensionInstalled(
318 content::BrowserContext* browser_context,
319 const Extension* extension,
320 bool is_update) {
321 if (!is_update)
322 return;
323 auto info = update_check_info_.find(extension->id());
324 if (info != update_check_info_.end()) {
325 info->second.backoff->Reset();
326 }
327 }
328
262 void ChromeRuntimeAPIDelegate::UpdateCheckComplete( 329 void ChromeRuntimeAPIDelegate::UpdateCheckComplete(
263 const std::string& extension_id) { 330 const std::string& extension_id) {
264 ExtensionSystem* system = ExtensionSystem::Get(browser_context_); 331 ExtensionSystem* system = ExtensionSystem::Get(browser_context_);
265 ExtensionService* service = system->extension_service(); 332 ExtensionService* service = system->extension_service();
266 const Extension* update = service->GetPendingExtensionUpdate(extension_id); 333 const Extension* update = service->GetPendingExtensionUpdate(extension_id);
334 UpdateCheckInfo& info = update_check_info_[extension_id];
335
336 // We always inform the BackoffEntry of a "failure" here, because we only
337 // want to consider an update check request a success from a throttling
338 // standpoint once the extension goes on to actually update to a new
339 // version. See OnExtensionInstalled for where we reset the BackoffEntry.
340 info.backoff->InformOfRequest(false);
341
267 if (update) { 342 if (update) {
268 CallUpdateCallbacks( 343 CallUpdateCallbacks(
269 extension_id, 344 extension_id,
270 UpdateCheckResult(true, kUpdateFound, update->VersionString())); 345 UpdateCheckResult(true, kUpdateFound, update->VersionString()));
271 } else { 346 } else {
272 CallUpdateCallbacks(extension_id, 347 CallUpdateCallbacks(extension_id,
273 UpdateCheckResult(true, kUpdateNotFound, "")); 348 UpdateCheckResult(true, kUpdateNotFound, ""));
274 } 349 }
275 } 350 }
276 351
277 void ChromeRuntimeAPIDelegate::CallUpdateCallbacks( 352 void ChromeRuntimeAPIDelegate::CallUpdateCallbacks(
278 const std::string& extension_id, 353 const std::string& extension_id,
279 const UpdateCheckResult& result) { 354 const UpdateCheckResult& result) {
280 UpdateCallbackList callbacks = pending_update_checks_[extension_id]; 355 auto it = update_check_info_.find(extension_id);
281 pending_update_checks_.erase(extension_id); 356 if (it == update_check_info_.end())
Devlin 2016/04/15 17:57:37 Can this happen?
asargent_no_longer_on_chrome 2016/04/19 17:56:52 Yep, CallUpdateCallbacks gets called from ChromeRu
282 for (UpdateCallbackList::const_iterator iter = callbacks.begin(); 357 return;
283 iter != callbacks.end(); 358 UpdateCheckInfo& info = it->second;
284 ++iter) { 359 std::vector<UpdateCheckCallback> callbacks = std::move(info.callbacks);
285 const UpdateCheckCallback& callback = *iter; 360 info.callbacks.clear();
361 for (const auto& callback : callbacks) {
286 callback.Run(result); 362 callback.Run(result);
287 } 363 }
288 } 364 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698