| 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 "components/component_updater/component_updater_service.h" | 5 #include "components/component_updater/component_updater_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 DCHECK_EQ(it->first, id); | 232 DCHECK_EQ(it->first, id); |
| 233 if (OnDemandUpdateWithCooldown(id)) { | 233 if (OnDemandUpdateWithCooldown(id)) { |
| 234 ready_callbacks_.insert(std::make_pair(id, callback)); | 234 ready_callbacks_.insert(std::make_pair(id, callback)); |
| 235 return; | 235 return; |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 callback.Run(); // Unblock the request if the request can't be throttled. | 239 callback.Run(); // Unblock the request if the request can't be throttled. |
| 240 } | 240 } |
| 241 | 241 |
| 242 bool CrxUpdateService::OnDemandUpdate(const std::string& id) { | 242 void CrxUpdateService::OnDemandUpdate(const std::string& id, |
| 243 CompletionCallback callback) { |
| 243 DCHECK(thread_checker_.CalledOnValidThread()); | 244 DCHECK(thread_checker_.CalledOnValidThread()); |
| 244 | 245 |
| 245 if (!GetComponent(id)) | 246 if (!GetComponent(id)) { |
| 246 return false; | 247 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 248 FROM_HERE, |
| 249 base::Bind(callback, |
| 250 update_client::Error::ERROR_UPDATE_INVALID_ARGUMENT)); |
| 251 return; |
| 252 } |
| 247 | 253 |
| 248 return OnDemandUpdateInternal(id); | 254 OnDemandUpdateInternal(id, callback); |
| 249 } | 255 } |
| 250 | 256 |
| 251 bool CrxUpdateService::OnDemandUpdateWithCooldown(const std::string& id) { | 257 bool CrxUpdateService::OnDemandUpdateWithCooldown(const std::string& id) { |
| 252 DCHECK(thread_checker_.CalledOnValidThread()); | 258 DCHECK(thread_checker_.CalledOnValidThread()); |
| 253 | 259 |
| 254 DCHECK(GetComponent(id)); | 260 DCHECK(GetComponent(id)); |
| 255 | 261 |
| 256 // Check if the request is too soon. | 262 // Check if the request is too soon. |
| 257 const auto* component_state(GetComponentState(id)); | 263 const auto* component_state(GetComponentState(id)); |
| 258 if (component_state) { | 264 if (component_state) { |
| 259 base::TimeDelta delta = base::Time::Now() - component_state->last_check; | 265 base::TimeDelta delta = base::Time::Now() - component_state->last_check; |
| 260 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) | 266 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) |
| 261 return false; | 267 return false; |
| 262 } | 268 } |
| 263 | 269 |
| 264 return OnDemandUpdateInternal(id); | 270 OnDemandUpdateInternal(id, CompletionCallback()); |
| 271 return true; |
| 265 } | 272 } |
| 266 | 273 |
| 267 bool CrxUpdateService::OnDemandUpdateInternal(const std::string& id) { | 274 void CrxUpdateService::OnDemandUpdateInternal(const std::string& id, |
| 275 CompletionCallback callback) { |
| 268 DCHECK(thread_checker_.CalledOnValidThread()); | 276 DCHECK(thread_checker_.CalledOnValidThread()); |
| 269 | 277 |
| 270 UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_MANUAL, | 278 UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_MANUAL, |
| 271 UPDATE_TYPE_COUNT); | 279 UPDATE_TYPE_COUNT); |
| 272 | |
| 273 update_client_->Install( | 280 update_client_->Install( |
| 274 id, base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)), | 281 id, base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)), |
| 275 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this), | 282 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this), |
| 276 base::TimeTicks::Now())); | 283 callback, base::TimeTicks::Now())); |
| 277 | |
| 278 return true; | |
| 279 } | 284 } |
| 280 | 285 |
| 281 bool CrxUpdateService::CheckForUpdates() { | 286 bool CrxUpdateService::CheckForUpdates() { |
| 282 DCHECK(thread_checker_.CalledOnValidThread()); | 287 DCHECK(thread_checker_.CalledOnValidThread()); |
| 283 | 288 |
| 284 UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_AUTOMATIC, | 289 UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_AUTOMATIC, |
| 285 UPDATE_TYPE_COUNT); | 290 UPDATE_TYPE_COUNT); |
| 286 | 291 |
| 287 std::vector<std::string> secure_ids; // Requires HTTPS for update checks. | 292 std::vector<std::string> secure_ids; // Requires HTTPS for update checks. |
| 288 std::vector<std::string> unsecure_ids; // Can fallback to HTTP. | 293 std::vector<std::string> unsecure_ids; // Can fallback to HTTP. |
| 289 for (const auto id : components_order_) { | 294 for (const auto id : components_order_) { |
| 290 DCHECK(components_.find(id) != components_.end()); | 295 DCHECK(components_.find(id) != components_.end()); |
| 291 | 296 |
| 292 auto* component(GetComponent(id)); | 297 auto* component(GetComponent(id)); |
| 293 if (!component || component->requires_network_encryption) | 298 if (!component || component->requires_network_encryption) |
| 294 secure_ids.push_back(id); | 299 secure_ids.push_back(id); |
| 295 else | 300 else |
| 296 unsecure_ids.push_back(id); | 301 unsecure_ids.push_back(id); |
| 297 } | 302 } |
| 298 | 303 |
| 299 if (!unsecure_ids.empty()) { | 304 if (!unsecure_ids.empty()) { |
| 300 update_client_->Update( | 305 update_client_->Update( |
| 301 unsecure_ids, | 306 unsecure_ids, |
| 302 base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)), | 307 base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)), |
| 303 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this), | 308 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this), |
| 304 base::TimeTicks::Now())); | 309 CompletionCallback(), base::TimeTicks::Now())); |
| 305 } | 310 } |
| 306 | 311 |
| 307 if (!secure_ids.empty()) { | 312 if (!secure_ids.empty()) { |
| 308 update_client_->Update( | 313 update_client_->Update( |
| 309 secure_ids, | 314 secure_ids, |
| 310 base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)), | 315 base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)), |
| 311 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this), | 316 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this), |
| 312 base::TimeTicks::Now())); | 317 CompletionCallback(), base::TimeTicks::Now())); |
| 313 } | 318 } |
| 314 | 319 |
| 315 return true; | 320 return true; |
| 316 } | 321 } |
| 317 | 322 |
| 318 scoped_refptr<base::SequencedTaskRunner> | 323 scoped_refptr<base::SequencedTaskRunner> |
| 319 CrxUpdateService::GetSequencedTaskRunner() { | 324 CrxUpdateService::GetSequencedTaskRunner() { |
| 320 DCHECK(thread_checker_.CalledOnValidThread()); | 325 DCHECK(thread_checker_.CalledOnValidThread()); |
| 321 return blocking_task_runner_; | 326 return blocking_task_runner_; |
| 322 } | 327 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 347 DCHECK(components->empty()); | 352 DCHECK(components->empty()); |
| 348 | 353 |
| 349 for (const auto& id : ids) { | 354 for (const auto& id : ids) { |
| 350 const auto* registered_component(GetComponent(id)); | 355 const auto* registered_component(GetComponent(id)); |
| 351 if (registered_component) { | 356 if (registered_component) { |
| 352 components->push_back(*registered_component); | 357 components->push_back(*registered_component); |
| 353 } | 358 } |
| 354 } | 359 } |
| 355 } | 360 } |
| 356 | 361 |
| 357 void CrxUpdateService::OnUpdateComplete(const base::TimeTicks& start_time, | 362 void CrxUpdateService::OnUpdateComplete(CompletionCallback callback, |
| 363 const base::TimeTicks& start_time, |
| 358 int error) { | 364 int error) { |
| 359 DCHECK(thread_checker_.CalledOnValidThread()); | 365 DCHECK(thread_checker_.CalledOnValidThread()); |
| 360 VLOG(1) << "Update completed with error " << error; | 366 VLOG(1) << "Update completed with error " << error; |
| 361 | 367 |
| 362 UMA_HISTOGRAM_BOOLEAN("ComponentUpdater.UpdateCompleteResult", error != 0); | 368 UMA_HISTOGRAM_BOOLEAN("ComponentUpdater.UpdateCompleteResult", error != 0); |
| 363 UMA_HISTOGRAM_LONG_TIMES_100("ComponentUpdater.UpdateCompleteTime", | 369 UMA_HISTOGRAM_LONG_TIMES_100("ComponentUpdater.UpdateCompleteTime", |
| 364 base::TimeTicks::Now() - start_time); | 370 base::TimeTicks::Now() - start_time); |
| 365 | 371 |
| 366 for (const auto id : components_pending_unregistration_) { | 372 for (const auto id : components_pending_unregistration_) { |
| 367 if (!update_client_->IsUpdating(id)) { | 373 if (!update_client_->IsUpdating(id)) { |
| 368 const auto* component = GetComponent(id); | 374 const auto* component = GetComponent(id); |
| 369 if (component) | 375 if (component) |
| 370 DoUnregisterComponent(*component); | 376 DoUnregisterComponent(*component); |
| 371 } | 377 } |
| 372 } | 378 } |
| 379 |
| 380 if (!callback.is_null()) { |
| 381 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 382 base::Bind(callback, error)); |
| 383 } |
| 373 } | 384 } |
| 374 | 385 |
| 375 void CrxUpdateService::OnEvent(Events event, const std::string& id) { | 386 void CrxUpdateService::OnEvent(Events event, const std::string& id) { |
| 376 DCHECK(thread_checker_.CalledOnValidThread()); | 387 DCHECK(thread_checker_.CalledOnValidThread()); |
| 377 | 388 |
| 378 // Unblock all throttles for the component. | 389 // Unblock all throttles for the component. |
| 379 if (event == Observer::Events::COMPONENT_UPDATED || | 390 if (event == Observer::Events::COMPONENT_UPDATED || |
| 380 event == Observer::Events::COMPONENT_NOT_UPDATED) { | 391 event == Observer::Events::COMPONENT_NOT_UPDATED) { |
| 381 auto callbacks = ready_callbacks_.equal_range(id); | 392 auto callbacks = ready_callbacks_.equal_range(id); |
| 382 for (auto it = callbacks.first; it != callbacks.second; ++it) { | 393 for (auto it = callbacks.first; it != callbacks.second; ++it) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 411 // TODO(sorin): consider making this a singleton. | 422 // TODO(sorin): consider making this a singleton. |
| 412 std::unique_ptr<ComponentUpdateService> ComponentUpdateServiceFactory( | 423 std::unique_ptr<ComponentUpdateService> ComponentUpdateServiceFactory( |
| 413 const scoped_refptr<Configurator>& config) { | 424 const scoped_refptr<Configurator>& config) { |
| 414 DCHECK(config); | 425 DCHECK(config); |
| 415 auto update_client = update_client::UpdateClientFactory(config); | 426 auto update_client = update_client::UpdateClientFactory(config); |
| 416 return base::WrapUnique( | 427 return base::WrapUnique( |
| 417 new CrxUpdateService(config, std::move(update_client))); | 428 new CrxUpdateService(config, std::move(update_client))); |
| 418 } | 429 } |
| 419 | 430 |
| 420 } // namespace component_updater | 431 } // namespace component_updater |
| OLD | NEW |