OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/component_updater/component_updater_service.h" | 5 #include "chrome/browser/component_updater/component_updater_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 CrxUpdateItem::~CrxUpdateItem() { | 81 CrxUpdateItem::~CrxUpdateItem() { |
82 } | 82 } |
83 | 83 |
84 CrxComponent::CrxComponent() | 84 CrxComponent::CrxComponent() |
85 : installer(NULL), allow_background_download(true) { | 85 : installer(NULL), allow_background_download(true) { |
86 } | 86 } |
87 | 87 |
88 CrxComponent::~CrxComponent() { | 88 CrxComponent::~CrxComponent() { |
89 } | 89 } |
90 | 90 |
91 CrxComponentInfo::CrxComponentInfo() { | |
92 } | |
93 | |
94 CrxComponentInfo::~CrxComponentInfo() { | |
95 } | |
96 | |
97 /////////////////////////////////////////////////////////////////////////////// | 91 /////////////////////////////////////////////////////////////////////////////// |
98 // In charge of blocking url requests until the |crx_id| component has been | 92 // In charge of blocking url requests until the |crx_id| component has been |
99 // updated. This class is touched solely from the IO thread. The UI thread | 93 // updated. This class is touched solely from the IO thread. The UI thread |
100 // can post tasks to it via weak pointers. By default the request is blocked | 94 // can post tasks to it via weak pointers. By default the request is blocked |
101 // unless the CrxUpdateService calls Unblock(). | 95 // unless the CrxUpdateService calls Unblock(). |
102 // The lifetime is controlled by Chrome's resource loader so the component | 96 // The lifetime is controlled by Chrome's resource loader so the component |
103 // updater cannot touch objects from this class except via weak pointers. | 97 // updater cannot touch objects from this class except via weak pointers. |
104 class CUResourceThrottle : public content::ResourceThrottle, | 98 class CUResourceThrottle : public content::ResourceThrottle, |
105 public base::SupportsWeakPtr<CUResourceThrottle> { | 99 public base::SupportsWeakPtr<CUResourceThrottle> { |
106 public: | 100 public: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 explicit CrxUpdateService(ComponentUpdateService::Configurator* config); | 148 explicit CrxUpdateService(ComponentUpdateService::Configurator* config); |
155 virtual ~CrxUpdateService(); | 149 virtual ~CrxUpdateService(); |
156 | 150 |
157 // Overrides for ComponentUpdateService. | 151 // Overrides for ComponentUpdateService. |
158 virtual void AddObserver(Observer* observer) OVERRIDE; | 152 virtual void AddObserver(Observer* observer) OVERRIDE; |
159 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 153 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
160 virtual Status Start() OVERRIDE; | 154 virtual Status Start() OVERRIDE; |
161 virtual Status Stop() OVERRIDE; | 155 virtual Status Stop() OVERRIDE; |
162 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE; | 156 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE; |
163 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE; | 157 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE; |
164 virtual void GetComponents( | 158 virtual std::vector<std::string> GetComponentIDs() const OVERRIDE; |
165 std::vector<CrxComponentInfo>* components) OVERRIDE; | 159 virtual CrxUpdateItem* GetComponentDetails( |
| 160 const std::string& component_id) const OVERRIDE; |
| 161 |
166 virtual content::ResourceThrottle* GetOnDemandResourceThrottle( | 162 virtual content::ResourceThrottle* GetOnDemandResourceThrottle( |
167 net::URLRequest* request, | 163 net::URLRequest* request, |
168 const std::string& crx_id) OVERRIDE; | 164 const std::string& crx_id) OVERRIDE; |
169 | 165 |
170 // Context for a crx download url request. | 166 // Context for a crx download url request. |
171 struct CRXContext { | 167 struct CRXContext { |
172 ComponentInstaller* installer; | 168 ComponentInstaller* installer; |
173 std::vector<uint8> pk_hash; | 169 std::vector<uint8> pk_hash; |
174 std::string id; | 170 std::string id; |
175 std::string fingerprint; | 171 std::string fingerprint; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 int extended_error); | 224 int extended_error); |
229 | 225 |
230 void DoneInstalling(const std::string& component_id, | 226 void DoneInstalling(const std::string& component_id, |
231 ComponentUnpacker::Error error, | 227 ComponentUnpacker::Error error, |
232 int extended_error); | 228 int extended_error); |
233 | 229 |
234 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to); | 230 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to); |
235 | 231 |
236 size_t ChangeItemStatus(CrxUpdateItem::Status from, CrxUpdateItem::Status to); | 232 size_t ChangeItemStatus(CrxUpdateItem::Status from, CrxUpdateItem::Status to); |
237 | 233 |
238 CrxUpdateItem* FindUpdateItemById(const std::string& id); | 234 CrxUpdateItem* FindUpdateItemById(const std::string& id) const; |
239 | 235 |
240 void NotifyObservers(Observer::Events event, const std::string& id); | 236 void NotifyObservers(Observer::Events event, const std::string& id); |
241 | 237 |
242 bool HasOnDemandItems() const; | 238 bool HasOnDemandItems() const; |
243 | 239 |
244 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt, | 240 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt, |
245 const std::string& crx_id); | 241 const std::string& crx_id); |
246 | 242 |
| 243 Status GetServiceStatus(const CrxUpdateItem::Status status); |
| 244 |
247 scoped_ptr<ComponentUpdateService::Configurator> config_; | 245 scoped_ptr<ComponentUpdateService::Configurator> config_; |
248 | 246 |
249 scoped_ptr<UpdateChecker> update_checker_; | 247 scoped_ptr<UpdateChecker> update_checker_; |
250 | 248 |
251 scoped_ptr<PingManager> ping_manager_; | 249 scoped_ptr<PingManager> ping_manager_; |
252 | 250 |
253 scoped_refptr<ComponentUnpacker> unpacker_; | 251 scoped_refptr<ComponentUnpacker> unpacker_; |
254 | 252 |
255 scoped_ptr<CrxDownloader> crx_downloader_; | 253 scoped_ptr<CrxDownloader> crx_downloader_; |
256 | 254 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 } | 387 } |
390 | 388 |
391 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds"; | 389 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds"; |
392 timer_.Start(FROM_HERE, | 390 timer_.Start(FROM_HERE, |
393 base::TimeDelta::FromSeconds(delay_seconds), | 391 base::TimeDelta::FromSeconds(delay_seconds), |
394 this, | 392 this, |
395 &CrxUpdateService::ProcessPendingItems); | 393 &CrxUpdateService::ProcessPendingItems); |
396 } | 394 } |
397 | 395 |
398 // Given a extension-like component id, find the associated component. | 396 // Given a extension-like component id, find the associated component. |
399 CrxUpdateItem* CrxUpdateService::FindUpdateItemById(const std::string& id) { | 397 CrxUpdateItem* CrxUpdateService::FindUpdateItemById( |
| 398 const std::string& id) const { |
400 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
401 CrxUpdateItem::FindById finder(id); | 400 CrxUpdateItem::FindById finder(id); |
402 UpdateItems::iterator it = | 401 UpdateItems::const_iterator it = |
403 std::find_if(work_items_.begin(), work_items_.end(), finder); | 402 std::find_if(work_items_.begin(), work_items_.end(), finder); |
404 return it != work_items_.end() ? *it : NULL; | 403 return it != work_items_.end() ? *it : NULL; |
405 } | 404 } |
406 | 405 |
407 // Changes a component's status, clearing on_demand and firing notifications as | 406 // Changes a component's status, clearing on_demand and firing notifications as |
408 // necessary. By convention, this is the only function that can change a | 407 // necessary. By convention, this is the only function that can change a |
409 // CrxUpdateItem's |status|. | 408 // CrxUpdateItem's |status|. |
410 // TODO(waffles): Do we want to add DCHECKS for valid state transitions here? | 409 // TODO(waffles): Do we want to add DCHECKS for valid state transitions here? |
411 void CrxUpdateService::ChangeItemState(CrxUpdateItem* item, | 410 void CrxUpdateService::ChangeItemState(CrxUpdateItem* item, |
412 CrxUpdateItem::Status to) { | 411 CrxUpdateItem::Status to) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal( | 506 ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal( |
508 CrxUpdateItem* uit) { | 507 CrxUpdateItem* uit) { |
509 if (!uit) | 508 if (!uit) |
510 return kError; | 509 return kError; |
511 | 510 |
512 // Check if the request is too soon. | 511 // Check if the request is too soon. |
513 base::TimeDelta delta = base::Time::Now() - uit->last_check; | 512 base::TimeDelta delta = base::Time::Now() - uit->last_check; |
514 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) | 513 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) |
515 return kError; | 514 return kError; |
516 | 515 |
517 switch (uit->status) { | 516 Status service_status = GetServiceStatus(uit->status); |
518 // If the item is already in the process of being updated, there is | 517 // If the item is already in the process of being updated, there is |
519 // no point in this call, so return kInProgress. | 518 // no point in this call, so return kInProgress. |
520 case CrxUpdateItem::kChecking: | 519 if (service_status == kInProgress) |
521 case CrxUpdateItem::kCanUpdate: | 520 return service_status; |
522 case CrxUpdateItem::kDownloadingDiff: | 521 |
523 case CrxUpdateItem::kDownloading: | 522 // Otherwise the item was already checked a while back (or it is new), |
524 case CrxUpdateItem::kUpdatingDiff: | 523 // set its status to kNew to give it a slightly higher priority. |
525 case CrxUpdateItem::kUpdating: | 524 ChangeItemState(uit, CrxUpdateItem::kNew); |
526 return kInProgress; | 525 uit->on_demand = true; |
527 // Otherwise the item was already checked a while back (or it is new), | |
528 // set its status to kNew to give it a slightly higher priority. | |
529 case CrxUpdateItem::kNew: | |
530 case CrxUpdateItem::kUpdated: | |
531 case CrxUpdateItem::kUpToDate: | |
532 case CrxUpdateItem::kNoUpdate: | |
533 ChangeItemState(uit, CrxUpdateItem::kNew); | |
534 uit->on_demand = true; | |
535 break; | |
536 case CrxUpdateItem::kLastStatus: | |
537 NOTREACHED() << uit->status; | |
538 } | |
539 | 526 |
540 // In case the current delay is long, set the timer to a shorter value | 527 // In case the current delay is long, set the timer to a shorter value |
541 // to get the ball rolling. | 528 // to get the ball rolling. |
542 if (timer_.IsRunning()) { | 529 if (timer_.IsRunning()) { |
543 timer_.Stop(); | 530 timer_.Stop(); |
544 timer_.Start(FROM_HERE, | 531 timer_.Start(FROM_HERE, |
545 base::TimeDelta::FromSeconds(config_->StepDelay()), | 532 base::TimeDelta::FromSeconds(config_->StepDelay()), |
546 this, | 533 this, |
547 &CrxUpdateService::ProcessPendingItems); | 534 &CrxUpdateService::ProcessPendingItems); |
548 } | 535 } |
549 | 536 |
550 return kOk; | 537 return kOk; |
551 } | 538 } |
552 | 539 |
553 void CrxUpdateService::GetComponents( | 540 std::vector<std::string> CrxUpdateService::GetComponentIDs() const { |
554 std::vector<CrxComponentInfo>* components) { | |
555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 542 std::vector<std::string> component_ids; |
556 for (UpdateItems::const_iterator it = work_items_.begin(); | 543 for (UpdateItems::const_iterator it = work_items_.begin(); |
557 it != work_items_.end(); | 544 it != work_items_.end(); |
558 ++it) { | 545 ++it) { |
559 const CrxUpdateItem* item = *it; | 546 const CrxUpdateItem* item = *it; |
560 CrxComponentInfo info; | 547 component_ids.push_back(item->id); |
561 info.id = GetCrxComponentID(item->component); | |
562 info.version = item->component.version.GetString(); | |
563 info.name = item->component.name; | |
564 components->push_back(info); | |
565 } | 548 } |
| 549 return component_ids; |
| 550 } |
| 551 |
| 552 CrxUpdateItem* CrxUpdateService::GetComponentDetails( |
| 553 const std::string& component_id) const { |
| 554 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 555 return FindUpdateItemById(component_id); |
566 } | 556 } |
567 | 557 |
568 // This is the main loop of the component updater. It updates one component | 558 // This is the main loop of the component updater. It updates one component |
569 // at a time if updates are available. Otherwise, it does an update check or | 559 // at a time if updates are available. Otherwise, it does an update check or |
570 // takes a long sleep until the loop runs again. | 560 // takes a long sleep until the loop runs again. |
571 void CrxUpdateService::ProcessPendingItems() { | 561 void CrxUpdateService::ProcessPendingItems() { |
572 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
573 | 563 |
574 CrxUpdateItem* ready_upgrade = FindReadyComponent(); | 564 CrxUpdateItem* ready_upgrade = FindReadyComponent(); |
575 if (ready_upgrade) { | 565 if (ready_upgrade) { |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1003 // Check if we can on-demand update, else unblock the request anyway. | 993 // Check if we can on-demand update, else unblock the request anyway. |
1004 CrxUpdateItem* item = FindUpdateItemById(crx_id); | 994 CrxUpdateItem* item = FindUpdateItemById(crx_id); |
1005 Status status = OnDemandUpdateInternal(item); | 995 Status status = OnDemandUpdateInternal(item); |
1006 if (status == kOk || status == kInProgress) { | 996 if (status == kOk || status == kInProgress) { |
1007 item->throttles.push_back(rt); | 997 item->throttles.push_back(rt); |
1008 return; | 998 return; |
1009 } | 999 } |
1010 UnblockResourceThrottle(rt); | 1000 UnblockResourceThrottle(rt); |
1011 } | 1001 } |
1012 | 1002 |
| 1003 ComponentUpdateService::Status CrxUpdateService::GetServiceStatus( |
| 1004 CrxUpdateItem::Status status) { |
| 1005 switch (status) { |
| 1006 case CrxUpdateItem::kChecking: |
| 1007 case CrxUpdateItem::kCanUpdate: |
| 1008 case CrxUpdateItem::kDownloadingDiff: |
| 1009 case CrxUpdateItem::kDownloading: |
| 1010 case CrxUpdateItem::kUpdatingDiff: |
| 1011 case CrxUpdateItem::kUpdating: |
| 1012 return kInProgress; |
| 1013 case CrxUpdateItem::kNew: |
| 1014 case CrxUpdateItem::kUpdated: |
| 1015 case CrxUpdateItem::kUpToDate: |
| 1016 case CrxUpdateItem::kNoUpdate: |
| 1017 return kOk; |
| 1018 break; |
| 1019 case CrxUpdateItem::kLastStatus: |
| 1020 NOTREACHED() << status; |
| 1021 } |
| 1022 return kError; |
| 1023 } |
| 1024 |
1013 /////////////////////////////////////////////////////////////////////////////// | 1025 /////////////////////////////////////////////////////////////////////////////// |
1014 | 1026 |
1015 CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request) | 1027 CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request) |
1016 : state_(NEW) { | 1028 : state_(NEW) { |
1017 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1029 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
1018 } | 1030 } |
1019 | 1031 |
1020 CUResourceThrottle::~CUResourceThrottle() { | 1032 CUResourceThrottle::~CUResourceThrottle() { |
1021 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 1033 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
1022 } | 1034 } |
(...skipping 24 matching lines...) Expand all Loading... |
1047 | 1059 |
1048 // The component update factory. Using the component updater as a singleton | 1060 // The component update factory. Using the component updater as a singleton |
1049 // is the job of the browser process. | 1061 // is the job of the browser process. |
1050 ComponentUpdateService* ComponentUpdateServiceFactory( | 1062 ComponentUpdateService* ComponentUpdateServiceFactory( |
1051 ComponentUpdateService::Configurator* config) { | 1063 ComponentUpdateService::Configurator* config) { |
1052 DCHECK(config); | 1064 DCHECK(config); |
1053 return new CrxUpdateService(config); | 1065 return new CrxUpdateService(config); |
1054 } | 1066 } |
1055 | 1067 |
1056 } // namespace component_updater | 1068 } // namespace component_updater |
OLD | NEW |