Chromium Code Reviews| 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/update_checker.h" | 5 #include "components/component_updater/update_checker.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 7 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/location.h" | |
| 8 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 10 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 11 #include "components/component_updater/component_updater_configurator.h" | 16 #include "components/component_updater/component_updater_configurator.h" |
| 12 #include "components/component_updater/component_updater_utils.h" | 17 #include "components/component_updater/component_updater_utils.h" |
| 13 #include "components/component_updater/crx_update_item.h" | 18 #include "components/component_updater/crx_update_item.h" |
| 19 #include "components/component_updater/request_sender.h" | |
| 14 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" | |
| 16 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 17 | 22 |
| 18 namespace component_updater { | 23 namespace component_updater { |
| 19 | 24 |
| 25 namespace { | |
| 26 | |
| 20 // Builds an update check request for |components|. |additional_attributes| is | 27 // Builds an update check request for |components|. |additional_attributes| is |
| 21 // serialized as part of the <request> element of the request to customize it | 28 // serialized as part of the <request> element of the request to customize it |
| 22 // with data that is not platform or component specific. For each |item|, a | 29 // with data that is not platform or component specific. For each |item|, a |
| 23 // corresponding <app> element is created and inserted as a child node of | 30 // corresponding <app> element is created and inserted as a child node of |
| 24 // the <request>. | 31 // the <request>. |
| 25 // | 32 // |
| 26 // An app element looks like this: | 33 // An app element looks like this: |
| 27 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" | 34 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" |
| 28 // version="0.1.2.3" installsource="ondemand"> | 35 // version="0.1.2.3" installsource="ondemand"> |
| 29 // <updatecheck /> | 36 // <updatecheck /> |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 59 } | 66 } |
| 60 | 67 |
| 61 return BuildProtocolRequest(config.GetBrowserVersion().GetString(), | 68 return BuildProtocolRequest(config.GetBrowserVersion().GetString(), |
| 62 config.GetChannel(), | 69 config.GetChannel(), |
| 63 config.GetLang(), | 70 config.GetLang(), |
| 64 config.GetOSLongName(), | 71 config.GetOSLongName(), |
| 65 app_elements, | 72 app_elements, |
| 66 additional_attributes); | 73 additional_attributes); |
| 67 } | 74 } |
| 68 | 75 |
| 69 class UpdateCheckerImpl : public UpdateChecker, public net::URLFetcherDelegate { | 76 class UpdateCheckerImpl : public UpdateChecker { |
| 70 public: | 77 public: |
| 71 UpdateCheckerImpl(const Configurator& config, | 78 explicit UpdateCheckerImpl(const Configurator& config); |
| 72 const UpdateCheckCallback& update_check_callback); | |
| 73 virtual ~UpdateCheckerImpl(); | 79 virtual ~UpdateCheckerImpl(); |
| 74 | 80 |
| 75 // Overrides for UpdateChecker. | 81 // Overrides for UpdateChecker. |
| 76 virtual bool CheckForUpdates( | 82 virtual bool CheckForUpdates( |
| 77 const std::vector<CrxUpdateItem*>& items_to_check, | 83 const std::vector<CrxUpdateItem*>& items_to_check, |
| 78 const std::string& additional_attributes) OVERRIDE; | 84 const std::string& additional_attributes, |
| 79 | 85 const UpdateCheckCallback& update_check_callback) OVERRIDE; |
| 80 // Overrides for UrlFetcher. | |
| 81 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 82 | 86 |
| 83 private: | 87 private: |
| 88 void OnRequestSenderComplete(const net::URLFetcher* source); | |
| 89 | |
| 84 const Configurator& config_; | 90 const Configurator& config_; |
| 85 const UpdateCheckCallback update_check_callback_; | 91 UpdateCheckCallback update_check_callback_; |
| 86 | 92 scoped_ptr<RequestSender> request_sender_; |
| 87 scoped_ptr<net::URLFetcher> url_fetcher_; | |
| 88 | 93 |
| 89 base::ThreadChecker thread_checker_; | 94 base::ThreadChecker thread_checker_; |
| 90 | 95 |
| 91 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); | 96 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); |
| 92 }; | 97 }; |
| 93 | 98 |
| 94 scoped_ptr<UpdateChecker> UpdateChecker::Create( | 99 UpdateCheckerImpl::UpdateCheckerImpl(const Configurator& config) |
| 95 const Configurator& config, | 100 : config_(config) { |
| 96 const UpdateCheckCallback& update_check_callback) { | |
| 97 scoped_ptr<UpdateCheckerImpl> update_checker( | |
| 98 new UpdateCheckerImpl(config, update_check_callback)); | |
| 99 return update_checker.PassAs<UpdateChecker>(); | |
| 100 } | |
| 101 | |
| 102 UpdateCheckerImpl::UpdateCheckerImpl( | |
| 103 const Configurator& config, | |
| 104 const UpdateCheckCallback& update_check_callback) | |
| 105 : config_(config), update_check_callback_(update_check_callback) { | |
| 106 } | 101 } |
| 107 | 102 |
| 108 UpdateCheckerImpl::~UpdateCheckerImpl() { | 103 UpdateCheckerImpl::~UpdateCheckerImpl() { |
| 109 DCHECK(thread_checker_.CalledOnValidThread()); | 104 DCHECK(thread_checker_.CalledOnValidThread()); |
| 110 } | 105 } |
| 111 | 106 |
| 112 bool UpdateCheckerImpl::CheckForUpdates( | 107 bool UpdateCheckerImpl::CheckForUpdates( |
| 113 const std::vector<CrxUpdateItem*>& items_to_check, | 108 const std::vector<CrxUpdateItem*>& items_to_check, |
| 114 const std::string& additional_attributes) { | 109 const std::string& additional_attributes, |
| 110 const UpdateCheckCallback& update_check_callback) { | |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 | 112 |
| 117 if (url_fetcher_) | 113 if (request_sender_.get()) { |
| 118 return false; // Another fetch is in progress. | 114 NOTREACHED(); |
|
waffles
2014/09/12 22:38:26
Should we really have NOTREACHED here? DVLOG might
Sorin Jianu
2014/09/12 23:24:00
as discussed, the code stays, we should not be ove
| |
| 115 return false; // Another update check is in progress. | |
| 116 } | |
| 119 | 117 |
| 120 url_fetcher_.reset(SendProtocolRequest( | 118 update_check_callback_ = update_check_callback; |
| 121 config_.UpdateUrl(), | 119 |
| 120 request_sender_.reset(new RequestSender(config_, config_.UpdateUrl())); | |
| 121 request_sender_->Send( | |
| 122 BuildUpdateCheckRequest(config_, items_to_check, additional_attributes), | 122 BuildUpdateCheckRequest(config_, items_to_check, additional_attributes), |
| 123 this, | 123 base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, |
| 124 config_.RequestContext())); | 124 base::Unretained(this))); |
| 125 | |
| 126 return true; | 125 return true; |
| 127 } | 126 } |
| 128 | 127 |
| 129 void UpdateCheckerImpl::OnURLFetchComplete(const net::URLFetcher* source) { | 128 void UpdateCheckerImpl::OnRequestSenderComplete(const net::URLFetcher* source) { |
| 130 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 131 DCHECK(url_fetcher_.get() == source); | 130 |
| 131 const GURL original_url(source->GetOriginalURL()); | |
| 132 VLOG(1) << "Update check request went to: " << original_url.spec(); | |
| 132 | 133 |
| 133 int error = 0; | 134 int error = 0; |
| 134 std::string error_message; | 135 std::string error_message; |
| 135 UpdateResponse update_response; | 136 UpdateResponse update_response; |
| 136 | 137 |
| 137 if (FetchSuccess(*source)) { | 138 if (FetchSuccess(*source)) { |
| 138 std::string xml; | 139 std::string xml; |
| 139 source->GetResponseAsString(&xml); | 140 source->GetResponseAsString(&xml); |
| 140 if (!update_response.Parse(xml)) { | 141 if (!update_response.Parse(xml)) { |
| 141 error = -1; | 142 error = -1; |
| 142 error_message = update_response.errors(); | 143 error_message = update_response.errors(); |
| 143 VLOG(1) << "Update request failed: " << error_message; | 144 VLOG(1) << "Update request failed: " << error_message; |
| 144 } | 145 } |
| 145 } else { | 146 } else { |
| 146 error = GetFetchError(*source); | 147 error = GetFetchError(*source); |
| 147 error_message.assign("network error"); | 148 error_message.assign("network error"); |
| 148 VLOG(1) << "Update request failed: network error"; | 149 VLOG(1) << "Update request failed: network error"; |
| 149 } | 150 } |
| 150 | 151 |
| 151 url_fetcher_.reset(); | 152 request_sender_.reset(); |
| 152 update_check_callback_.Run(error, error_message, update_response.results()); | 153 update_check_callback_.Run( |
| 154 original_url, error, error_message, update_response.results()); | |
| 155 } | |
| 156 | |
| 157 } // namespace | |
| 158 | |
| 159 scoped_ptr<UpdateChecker> UpdateChecker::Create(const Configurator& config) { | |
| 160 scoped_ptr<UpdateCheckerImpl> update_checker(new UpdateCheckerImpl(config)); | |
| 161 return update_checker.PassAs<UpdateChecker>(); | |
| 153 } | 162 } |
| 154 | 163 |
| 155 } // namespace component_updater | 164 } // namespace component_updater |
| OLD | NEW |