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

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

Issue 1897293005: Rate limit programmatic update checks for extensions (reland) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: have Get() just return the net::BackoffEntry::Policy directly 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>
10 #include <vector>
9 11
12 #include "base/lazy_instance.h"
10 #include "base/location.h" 13 #include "base/location.h"
11 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h" 16 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h" 17 #include "base/time/time.h"
15 #include "build/build_config.h" 18 #include "build/build_config.h"
16 #include "chrome/browser/extensions/extension_service.h" 19 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_tab_util.h" 20 #include "chrome/browser/extensions/extension_tab_util.h"
18 #include "chrome/browser/extensions/updater/extension_updater.h" 21 #include "chrome/browser/extensions/updater/extension_updater.h"
19 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser_finder.h" 23 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/browser_navigator.h" 24 #include "chrome/browser/ui/browser_navigator.h"
22 #include "chrome/browser/ui/browser_navigator_params.h" 25 #include "chrome/browser/ui/browser_navigator_params.h"
23 #include "chrome/browser/ui/browser_window.h" 26 #include "chrome/browser/ui/browser_window.h"
24 #include "components/update_client/update_query_params.h" 27 #include "components/update_client/update_query_params.h"
25 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
29 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h" 30 #include "extensions/browser/extension_system.h"
27 #include "extensions/browser/notification_types.h" 31 #include "extensions/browser/notification_types.h"
28 #include "extensions/browser/warning_service.h" 32 #include "extensions/browser/warning_service.h"
29 #include "extensions/browser/warning_set.h" 33 #include "extensions/browser/warning_set.h"
30 #include "extensions/common/api/runtime.h" 34 #include "extensions/common/api/runtime.h"
35 #include "extensions/common/constants.h"
36 #include "net/base/backoff_entry.h"
31 37
32 #if defined(OS_CHROMEOS) 38 #if defined(OS_CHROMEOS)
33 #include "chromeos/dbus/dbus_thread_manager.h" 39 #include "chromeos/dbus/dbus_thread_manager.h"
34 #include "chromeos/dbus/power_manager_client.h" 40 #include "chromeos/dbus/power_manager_client.h"
35 #include "components/user_manager/user_manager.h" 41 #include "components/user_manager/user_manager.h"
36 #endif 42 #endif
37 43
38 using extensions::Extension; 44 using extensions::Extension;
39 using extensions::ExtensionSystem; 45 using extensions::ExtensionSystem;
40 using extensions::ExtensionUpdater; 46 using extensions::ExtensionUpdater;
41 47
42 using extensions::api::runtime::PlatformInfo; 48 using extensions::api::runtime::PlatformInfo;
43 49
44 namespace { 50 namespace {
45 51
46 const char kUpdateThrottled[] = "throttled"; 52 const char kUpdateThrottled[] = "throttled";
47 const char kUpdateNotFound[] = "no_update"; 53 const char kUpdateNotFound[] = "no_update";
48 const char kUpdateFound[] = "update_available"; 54 const char kUpdateFound[] = "update_available";
49 55
50 // If an extension reloads itself within this many miliseconds of reloading 56 // If an extension reloads itself within this many miliseconds of reloading
51 // itself, the reload is considered suspiciously fast. 57 // itself, the reload is considered suspiciously fast.
52 const int kFastReloadTime = 10000; 58 const int kFastReloadTime = 10000;
53 59
54 // After this many suspiciously fast consecutive reloads, an extension will get 60 // After this many suspiciously fast consecutive reloads, an extension will get
55 // disabled. 61 // disabled.
56 const int kFastReloadCount = 5; 62 const int kFastReloadCount = 5;
57 63
64 // A holder class for the policy we use for exponential backoff of update check
65 // requests.
66 class BackoffPolicy {
67 public:
68 BackoffPolicy();
69 ~BackoffPolicy();
70
71 // Returns the actual policy to use.
72 static const net::BackoffEntry::Policy* Get();
73
74 private:
75 net::BackoffEntry::Policy policy_;
76 };
77
78 // We use a LazyInstance since one of the the policy values references an
79 // extern symbol, which would cause a static initializer to be generated if we
80 // just declared the policy struct as a static variable.
81 base::LazyInstance<BackoffPolicy> g_backoff_policy = LAZY_INSTANCE_INITIALIZER;
82
83 BackoffPolicy::BackoffPolicy() {
84 policy_ = {
85 // num_errors_to_ignore
86 0,
87
88 // initial_delay_ms (note that we set 'always_use_initial_delay' to false
89 // below)
90 1000 * extensions::kDefaultUpdateFrequencySeconds,
91
92 // multiply_factor
93 1,
94
95 // jitter_factor
96 0.1,
97
98 // maximum_backoff_ms (-1 means no maximum)
99 -1,
100
101 // entry_lifetime_ms (-1 means never discard)
102 -1,
103
104 // always_use_initial_delay
105 false,
106 };
107 }
108
109 BackoffPolicy::~BackoffPolicy() {}
110
111 // static
112 const net::BackoffEntry::Policy* BackoffPolicy::Get() {
113 return &g_backoff_policy.Get().policy_;
114 }
115
116 base::TickClock* g_test_clock = nullptr;
117
58 } // namespace 118 } // namespace
59 119
120 struct ChromeRuntimeAPIDelegate::UpdateCheckInfo {
121 public:
122 UpdateCheckInfo() {
123 if (g_test_clock)
124 backoff.reset(
125 new net::BackoffEntry(BackoffPolicy::Get(), g_test_clock));
126 else
127 backoff.reset(new net::BackoffEntry(BackoffPolicy::Get()));
128 }
129
130 std::unique_ptr<net::BackoffEntry> backoff;
131 std::vector<UpdateCheckCallback> callbacks;
132 };
133
60 ChromeRuntimeAPIDelegate::ChromeRuntimeAPIDelegate( 134 ChromeRuntimeAPIDelegate::ChromeRuntimeAPIDelegate(
61 content::BrowserContext* context) 135 content::BrowserContext* context)
62 : browser_context_(context), registered_for_updates_(false) { 136 : browser_context_(context),
137 registered_for_updates_(false),
138 extension_registry_observer_(this) {
63 registrar_.Add(this, 139 registrar_.Add(this,
64 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND, 140 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND,
65 content::NotificationService::AllSources()); 141 content::NotificationService::AllSources());
142 extension_registry_observer_.Add(
143 extensions::ExtensionRegistry::Get(browser_context_));
66 } 144 }
67 145
68 ChromeRuntimeAPIDelegate::~ChromeRuntimeAPIDelegate() { 146 ChromeRuntimeAPIDelegate::~ChromeRuntimeAPIDelegate() {
69 } 147 }
70 148
149 // static
150 void ChromeRuntimeAPIDelegate::set_tick_clock_for_tests(
151 base::TickClock* clock) {
152 g_test_clock = clock;
153 }
154
71 void ChromeRuntimeAPIDelegate::AddUpdateObserver( 155 void ChromeRuntimeAPIDelegate::AddUpdateObserver(
72 extensions::UpdateObserver* observer) { 156 extensions::UpdateObserver* observer) {
73 registered_for_updates_ = true; 157 registered_for_updates_ = true;
74 ExtensionSystem::Get(browser_context_) 158 ExtensionSystem::Get(browser_context_)
75 ->extension_service() 159 ->extension_service()
76 ->AddUpdateObserver(observer); 160 ->AddUpdateObserver(observer);
77 } 161 }
78 162
79 void ChromeRuntimeAPIDelegate::RemoveUpdateObserver( 163 void ChromeRuntimeAPIDelegate::RemoveUpdateObserver(
80 extensions::UpdateObserver* observer) { 164 extensions::UpdateObserver* observer) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 229
146 bool ChromeRuntimeAPIDelegate::CheckForUpdates( 230 bool ChromeRuntimeAPIDelegate::CheckForUpdates(
147 const std::string& extension_id, 231 const std::string& extension_id,
148 const UpdateCheckCallback& callback) { 232 const UpdateCheckCallback& callback) {
149 ExtensionSystem* system = ExtensionSystem::Get(browser_context_); 233 ExtensionSystem* system = ExtensionSystem::Get(browser_context_);
150 ExtensionService* service = system->extension_service(); 234 ExtensionService* service = system->extension_service();
151 ExtensionUpdater* updater = service->updater(); 235 ExtensionUpdater* updater = service->updater();
152 if (!updater) { 236 if (!updater) {
153 return false; 237 return false;
154 } 238 }
155 if (!updater->CheckExtensionSoon( 239
156 extension_id, 240 UpdateCheckInfo& info = update_check_info_[extension_id];
157 base::Bind(&ChromeRuntimeAPIDelegate::UpdateCheckComplete, 241
158 base::Unretained(this), 242 // If not enough time has elapsed, or we have 10 or more outstanding calls,
159 extension_id))) { 243 // return a status of throttled.
244 if (info.backoff->ShouldRejectRequest() || info.callbacks.size() >= 10) {
160 base::ThreadTaskRunnerHandle::Get()->PostTask( 245 base::ThreadTaskRunnerHandle::Get()->PostTask(
161 FROM_HERE, 246 FROM_HERE,
162 base::Bind(callback, UpdateCheckResult(true, kUpdateThrottled, ""))); 247 base::Bind(callback, UpdateCheckResult(true, kUpdateThrottled, "")));
163 } else { 248 } else {
164 UpdateCallbackList& callbacks = pending_update_checks_[extension_id]; 249 info.callbacks.push_back(callback);
165 callbacks.push_back(callback); 250 updater->CheckExtensionSoon(
251 extension_id, base::Bind(&ChromeRuntimeAPIDelegate::UpdateCheckComplete,
252 base::Unretained(this), extension_id));
166 } 253 }
167 return true; 254 return true;
168 } 255 }
169 256
170 void ChromeRuntimeAPIDelegate::OpenURL(const GURL& uninstall_url) { 257 void ChromeRuntimeAPIDelegate::OpenURL(const GURL& uninstall_url) {
171 Profile* profile = Profile::FromBrowserContext(browser_context_); 258 Profile* profile = Profile::FromBrowserContext(browser_context_);
172 Browser* browser = chrome::FindLastActiveWithProfile(profile); 259 Browser* browser = chrome::FindLastActiveWithProfile(profile);
173 if (!browser) 260 if (!browser)
174 browser = new Browser(Browser::CreateParams(profile)); 261 browser = new Browser(Browser::CreateParams(profile));
175 262
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 DCHECK(type == extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND); 339 DCHECK(type == extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND);
253 typedef const std::pair<std::string, Version> UpdateDetails; 340 typedef const std::pair<std::string, Version> UpdateDetails;
254 const std::string& id = content::Details<UpdateDetails>(details)->first; 341 const std::string& id = content::Details<UpdateDetails>(details)->first;
255 const Version& version = content::Details<UpdateDetails>(details)->second; 342 const Version& version = content::Details<UpdateDetails>(details)->second;
256 if (version.IsValid()) { 343 if (version.IsValid()) {
257 CallUpdateCallbacks( 344 CallUpdateCallbacks(
258 id, UpdateCheckResult(true, kUpdateFound, version.GetString())); 345 id, UpdateCheckResult(true, kUpdateFound, version.GetString()));
259 } 346 }
260 } 347 }
261 348
349 void ChromeRuntimeAPIDelegate::OnExtensionInstalled(
350 content::BrowserContext* browser_context,
351 const Extension* extension,
352 bool is_update) {
353 if (!is_update)
354 return;
355 auto info = update_check_info_.find(extension->id());
356 if (info != update_check_info_.end()) {
357 info->second.backoff->Reset();
358 }
359 }
360
262 void ChromeRuntimeAPIDelegate::UpdateCheckComplete( 361 void ChromeRuntimeAPIDelegate::UpdateCheckComplete(
263 const std::string& extension_id) { 362 const std::string& extension_id) {
264 ExtensionSystem* system = ExtensionSystem::Get(browser_context_); 363 ExtensionSystem* system = ExtensionSystem::Get(browser_context_);
265 ExtensionService* service = system->extension_service(); 364 ExtensionService* service = system->extension_service();
266 const Extension* update = service->GetPendingExtensionUpdate(extension_id); 365 const Extension* update = service->GetPendingExtensionUpdate(extension_id);
366 UpdateCheckInfo& info = update_check_info_[extension_id];
367
368 // We always inform the BackoffEntry of a "failure" here, because we only
369 // want to consider an update check request a success from a throttling
370 // standpoint once the extension goes on to actually update to a new
371 // version. See OnExtensionInstalled for where we reset the BackoffEntry.
372 info.backoff->InformOfRequest(false);
373
267 if (update) { 374 if (update) {
268 CallUpdateCallbacks( 375 CallUpdateCallbacks(
269 extension_id, 376 extension_id,
270 UpdateCheckResult(true, kUpdateFound, update->VersionString())); 377 UpdateCheckResult(true, kUpdateFound, update->VersionString()));
271 } else { 378 } else {
272 CallUpdateCallbacks(extension_id, 379 CallUpdateCallbacks(extension_id,
273 UpdateCheckResult(true, kUpdateNotFound, "")); 380 UpdateCheckResult(true, kUpdateNotFound, ""));
274 } 381 }
275 } 382 }
276 383
277 void ChromeRuntimeAPIDelegate::CallUpdateCallbacks( 384 void ChromeRuntimeAPIDelegate::CallUpdateCallbacks(
278 const std::string& extension_id, 385 const std::string& extension_id,
279 const UpdateCheckResult& result) { 386 const UpdateCheckResult& result) {
280 UpdateCallbackList callbacks = pending_update_checks_[extension_id]; 387 auto it = update_check_info_.find(extension_id);
281 pending_update_checks_.erase(extension_id); 388 if (it == update_check_info_.end())
282 for (UpdateCallbackList::const_iterator iter = callbacks.begin(); 389 return;
283 iter != callbacks.end(); 390 std::vector<UpdateCheckCallback> callbacks;
284 ++iter) { 391 it->second.callbacks.swap(callbacks);
285 const UpdateCheckCallback& callback = *iter; 392 for (const auto& callback : callbacks) {
286 callback.Run(result); 393 callback.Run(result);
287 } 394 }
288 } 395 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698