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