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