OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/at_exit.h" | 10 #include "base/at_exit.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 95 |
96 // Helper template class that allows our main class to have separate | 96 // Helper template class that allows our main class to have separate |
97 // OnURLFetchComplete() callbacks for diffent types of url requests | 97 // OnURLFetchComplete() callbacks for diffent types of url requests |
98 // they are differentiated by the |Ctx| type. | 98 // they are differentiated by the |Ctx| type. |
99 template <typename Del, typename Ctx> | 99 template <typename Del, typename Ctx> |
100 class DelegateWithContext : public content::URLFetcherDelegate { | 100 class DelegateWithContext : public content::URLFetcherDelegate { |
101 public: | 101 public: |
102 DelegateWithContext(Del* delegate, Ctx* context) | 102 DelegateWithContext(Del* delegate, Ctx* context) |
103 : delegate_(delegate), context_(context) {} | 103 : delegate_(delegate), context_(context) {} |
104 | 104 |
105 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { | 105 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE { |
106 delegate_->OnURLFetchComplete(source, context_); | 106 delegate_->OnURLFetchComplete(source, context_); |
107 delete this; | 107 delete this; |
108 } | 108 } |
109 | 109 |
110 private: | 110 private: |
111 ~DelegateWithContext() {} | 111 ~DelegateWithContext() {} |
112 | 112 |
113 Del* delegate_; | 113 Del* delegate_; |
114 Ctx* context_; | 114 Ctx* context_; |
115 }; | 115 }; |
116 // This function creates the right DelegateWithContext using template inference. | 116 // This function creates the right DelegateWithContext using template inference. |
117 template <typename Del, typename Ctx> | 117 template <typename Del, typename Ctx> |
118 content::URLFetcherDelegate* MakeContextDelegate(Del* delegate, Ctx* context) { | 118 content::URLFetcherDelegate* MakeContextDelegate(Del* delegate, Ctx* context) { |
119 return new DelegateWithContext<Del, Ctx>(delegate, context); | 119 return new DelegateWithContext<Del, Ctx>(delegate, context); |
120 } | 120 } |
121 | 121 |
122 // Helper to start a url request using |fetcher| with the common flags. | 122 // Helper to start a url request using |fetcher| with the common flags. |
123 void StartFetch(URLFetcher* fetcher, | 123 void StartFetch(content::URLFetcher* fetcher, |
124 net::URLRequestContextGetter* context_getter, | 124 net::URLRequestContextGetter* context_getter, |
125 bool save_to_file) { | 125 bool save_to_file) { |
126 fetcher->set_request_context(context_getter); | 126 fetcher->SetRequestContext(context_getter); |
127 fetcher->set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES | | 127 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
128 net::LOAD_DO_NOT_SAVE_COOKIES | | 128 net::LOAD_DO_NOT_SAVE_COOKIES | |
129 net::LOAD_DISABLE_CACHE); | 129 net::LOAD_DISABLE_CACHE); |
130 // TODO(cpu): Define our retry and backoff policy. | 130 // TODO(cpu): Define our retry and backoff policy. |
131 fetcher->set_automatically_retry_on_5xx(false); | 131 fetcher->SetAutomaticallyRetryOn5xx(false); |
132 if (save_to_file) { | 132 if (save_to_file) { |
133 fetcher->SaveResponseToTemporaryFile( | 133 fetcher->SaveResponseToTemporaryFile( |
134 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 134 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
135 } | 135 } |
136 fetcher->Start(); | 136 fetcher->Start(); |
137 } | 137 } |
138 | 138 |
139 // Returs true if the url request of |fetcher| was succesful. | 139 // Returs true if the url request of |fetcher| was succesful. |
140 bool FetchSuccess(const URLFetcher& fetcher) { | 140 bool FetchSuccess(const content::URLFetcher& fetcher) { |
141 return (fetcher.status().status() == net::URLRequestStatus::SUCCESS) && | 141 return (fetcher.GetStatus().status() == net::URLRequestStatus::SUCCESS) && |
142 (fetcher.response_code() == 200); | 142 (fetcher.GetResponseCode() == 200); |
143 } | 143 } |
144 | 144 |
145 // This is the one and only per-item state structure. Designed to be hosted | 145 // This is the one and only per-item state structure. Designed to be hosted |
146 // in a std::vector or a std::list. The two main members are |component| | 146 // in a std::vector or a std::list. The two main members are |component| |
147 // which is supplied by the the component updater client and |status| which | 147 // which is supplied by the the component updater client and |status| which |
148 // is modified as the item is processed by the update pipeline. The expected | 148 // is modified as the item is processed by the update pipeline. The expected |
149 // transition graph is: | 149 // transition graph is: |
150 // error error error | 150 // error error error |
151 // +--kNoUpdate<------<-------+------<------+------<------+ | 151 // +--kNoUpdate<------<-------+------<------+------<------+ |
152 // | | | | | 152 // | | | | |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 }; | 265 }; |
266 | 266 |
267 // Context for a crx download url request. See DelegateWithContext above. | 267 // Context for a crx download url request. See DelegateWithContext above. |
268 struct CRXContext { | 268 struct CRXContext { |
269 ComponentInstaller* installer; | 269 ComponentInstaller* installer; |
270 std::vector<uint8> pk_hash; | 270 std::vector<uint8> pk_hash; |
271 std::string id; | 271 std::string id; |
272 CRXContext() : installer(NULL) {} | 272 CRXContext() : installer(NULL) {} |
273 }; | 273 }; |
274 | 274 |
275 void OnURLFetchComplete(const URLFetcher* source, UpdateContext* context); | 275 void OnURLFetchComplete(const content::URLFetcher* source, |
| 276 UpdateContext* context); |
276 | 277 |
277 void OnURLFetchComplete(const URLFetcher* source, CRXContext* context); | 278 void OnURLFetchComplete(const content::URLFetcher* source, |
| 279 CRXContext* context); |
278 | 280 |
279 private: | 281 private: |
280 // See ManifestParserBridge. | 282 // See ManifestParserBridge. |
281 void OnParseUpdateManifestSucceeded( | 283 void OnParseUpdateManifestSucceeded( |
282 const UpdateManifest::Results& results); | 284 const UpdateManifest::Results& results); |
283 | 285 |
284 // See ManifestParserBridge. | 286 // See ManifestParserBridge. |
285 void OnParseUpdateManifestFailed( | 287 void OnParseUpdateManifestFailed( |
286 const std::string& error_message); | 288 const std::string& error_message); |
287 | 289 |
(...skipping 10 matching lines...) Expand all Loading... |
298 void DoneInstalling(const std::string& component_id, | 300 void DoneInstalling(const std::string& component_id, |
299 ComponentUnpacker::Error error); | 301 ComponentUnpacker::Error error); |
300 | 302 |
301 size_t ChangeItemStatus(CrxUpdateItem::Status from, | 303 size_t ChangeItemStatus(CrxUpdateItem::Status from, |
302 CrxUpdateItem::Status to); | 304 CrxUpdateItem::Status to); |
303 | 305 |
304 CrxUpdateItem* FindUpdateItemById(const std::string& id); | 306 CrxUpdateItem* FindUpdateItemById(const std::string& id); |
305 | 307 |
306 scoped_ptr<Config> config_; | 308 scoped_ptr<Config> config_; |
307 | 309 |
308 scoped_ptr<URLFetcher> url_fetcher_; | 310 scoped_ptr<content::URLFetcher> url_fetcher_; |
309 | 311 |
310 typedef std::vector<CrxUpdateItem*> UpdateItems; | 312 typedef std::vector<CrxUpdateItem*> UpdateItems; |
311 UpdateItems work_items_; | 313 UpdateItems work_items_; |
312 | 314 |
313 base::OneShotTimer<CrxUpdateService> timer_; | 315 base::OneShotTimer<CrxUpdateService> timer_; |
314 | 316 |
315 Version chrome_version_; | 317 Version chrome_version_; |
316 | 318 |
317 bool running_; | 319 bool running_; |
318 | 320 |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 const std::string full_query = MakeFinalQuery(config_->UpdateUrl().spec(), | 552 const std::string full_query = MakeFinalQuery(config_->UpdateUrl().spec(), |
551 query, | 553 query, |
552 config_->ExtraRequestParams()); | 554 config_->ExtraRequestParams()); |
553 url_fetcher_.reset(URLFetcher::Create(0, GURL(full_query), URLFetcher::GET, | 555 url_fetcher_.reset(URLFetcher::Create(0, GURL(full_query), URLFetcher::GET, |
554 MakeContextDelegate(this, new UpdateContext()))); | 556 MakeContextDelegate(this, new UpdateContext()))); |
555 StartFetch(url_fetcher_.get(), config_->RequestContext(), false); | 557 StartFetch(url_fetcher_.get(), config_->RequestContext(), false); |
556 } | 558 } |
557 | 559 |
558 // Caled when we got a response from the update server. It consists of an xml | 560 // Caled when we got a response from the update server. It consists of an xml |
559 // document following the omaha update scheme. | 561 // document following the omaha update scheme. |
560 void CrxUpdateService::OnURLFetchComplete(const URLFetcher* source, | 562 void CrxUpdateService::OnURLFetchComplete(const content::URLFetcher* source, |
561 UpdateContext* context) { | 563 UpdateContext* context) { |
562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
563 if (FetchSuccess(*source)) { | 565 if (FetchSuccess(*source)) { |
564 std::string xml; | 566 std::string xml; |
565 source->GetResponseAsString(&xml); | 567 source->GetResponseAsString(&xml); |
566 url_fetcher_.reset(); | 568 url_fetcher_.reset(); |
567 ParseManifest(xml); | 569 ParseManifest(xml); |
568 } else { | 570 } else { |
569 url_fetcher_.reset(); | 571 url_fetcher_.reset(); |
570 CrxUpdateService::OnParseUpdateManifestFailed("network error"); | 572 CrxUpdateService::OnParseUpdateManifestFailed("network error"); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking, | 657 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking, |
656 CrxUpdateItem::kNoUpdate); | 658 CrxUpdateItem::kNoUpdate); |
657 config_->OnEvent(Configurator::kManifestError, static_cast<int>(count)); | 659 config_->OnEvent(Configurator::kManifestError, static_cast<int>(count)); |
658 DCHECK_GT(count, 0ul); | 660 DCHECK_GT(count, 0ul); |
659 ScheduleNextRun(false); | 661 ScheduleNextRun(false); |
660 } | 662 } |
661 | 663 |
662 // Called when the CRX package has been downloaded to a temporary location. | 664 // Called when the CRX package has been downloaded to a temporary location. |
663 // Here we fire the notifications and schedule the component-specific installer | 665 // Here we fire the notifications and schedule the component-specific installer |
664 // to be called in the file thread. | 666 // to be called in the file thread. |
665 void CrxUpdateService::OnURLFetchComplete(const URLFetcher* source, | 667 void CrxUpdateService::OnURLFetchComplete(const content::URLFetcher* source, |
666 CRXContext* context) { | 668 CRXContext* context) { |
667 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 669 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
668 base::PlatformFileError error_code; | 670 base::PlatformFileError error_code; |
669 | 671 |
670 if (source->FileErrorOccurred(&error_code) || !FetchSuccess(*source)) { | 672 if (source->FileErrorOccurred(&error_code) || !FetchSuccess(*source)) { |
671 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading, | 673 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading, |
672 CrxUpdateItem::kNoUpdate); | 674 CrxUpdateItem::kNoUpdate); |
673 DCHECK_EQ(count, 1ul); | 675 DCHECK_EQ(count, 1ul); |
674 config_->OnEvent(Configurator::kNetworkError, CrxIdtoUMAId(context->id)); | 676 config_->OnEvent(Configurator::kNetworkError, CrxIdtoUMAId(context->id)); |
675 url_fetcher_.reset(); | 677 url_fetcher_.reset(); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 ScheduleNextRun(false); | 746 ScheduleNextRun(false); |
745 } | 747 } |
746 | 748 |
747 // The component update factory. Using the component updater as a singleton | 749 // The component update factory. Using the component updater as a singleton |
748 // is the job of the browser process. | 750 // is the job of the browser process. |
749 ComponentUpdateService* ComponentUpdateServiceFactory( | 751 ComponentUpdateService* ComponentUpdateServiceFactory( |
750 ComponentUpdateService::Configurator* config) { | 752 ComponentUpdateService::Configurator* config) { |
751 DCHECK(config); | 753 DCHECK(config); |
752 return new CrxUpdateService(config); | 754 return new CrxUpdateService(config); |
753 } | 755 } |
OLD | NEW |