Hotlist Slow: Remove extra thread pool created by VolumeMountWatcherWin.
[chromium-blink-merge.git] / components / update_client / update_checker.cc
blob9a38a5a4238a362a3939d2e5d0a34510e1d499ef
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
3 // found in the LICENSE file.
5 #include "components/update_client/update_checker.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/compiler_specific.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/threading/thread_checker.h"
19 #include "components/update_client/configurator.h"
20 #include "components/update_client/crx_update_item.h"
21 #include "components/update_client/request_sender.h"
22 #include "components/update_client/utils.h"
23 #include "net/url_request/url_fetcher.h"
24 #include "url/gurl.h"
26 namespace update_client {
28 namespace {
30 // Builds an update check request for |components|. |additional_attributes| is
31 // serialized as part of the <request> element of the request to customize it
32 // with data that is not platform or component specific. For each |item|, a
33 // corresponding <app> element is created and inserted as a child node of
34 // the <request>.
36 // An app element looks like this:
37 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc"
38 // version="0.1.2.3" installsource="ondemand">
39 // <updatecheck />
40 // <packages>
41 // <package fp="abcd" />
42 // </packages>
43 // </app>
44 std::string BuildUpdateCheckRequest(const Configurator& config,
45 const std::vector<CrxUpdateItem*>& items,
46 const std::string& additional_attributes) {
47 std::string app_elements;
48 for (size_t i = 0; i != items.size(); ++i) {
49 const CrxUpdateItem* item = items[i];
50 std::string app("<app ");
51 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", item->id.c_str(),
52 item->component.version.GetString().c_str());
53 if (item->on_demand)
54 base::StringAppendF(&app, " installsource=\"ondemand\"");
55 base::StringAppendF(&app, ">");
56 base::StringAppendF(&app, "<updatecheck />");
57 if (!item->component.fingerprint.empty()) {
58 base::StringAppendF(&app,
59 "<packages>"
60 "<package fp=\"%s\"/>"
61 "</packages>",
62 item->component.fingerprint.c_str());
64 base::StringAppendF(&app, "</app>");
65 app_elements.append(app);
66 VLOG(1) << "Appending to update request: " << app;
69 return BuildProtocolRequest(config.GetBrowserVersion().GetString(),
70 config.GetChannel(), config.GetLang(),
71 config.GetOSLongName(), app_elements,
72 additional_attributes);
75 class UpdateCheckerImpl : public UpdateChecker {
76 public:
77 explicit UpdateCheckerImpl(const Configurator& config);
78 ~UpdateCheckerImpl() override;
80 // Overrides for UpdateChecker.
81 bool CheckForUpdates(
82 const std::vector<CrxUpdateItem*>& items_to_check,
83 const std::string& additional_attributes,
84 const UpdateCheckCallback& update_check_callback) override;
86 private:
87 void OnRequestSenderComplete(const net::URLFetcher* source);
89 const Configurator& config_;
90 UpdateCheckCallback update_check_callback_;
91 scoped_ptr<RequestSender> request_sender_;
93 base::ThreadChecker thread_checker_;
95 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl);
98 UpdateCheckerImpl::UpdateCheckerImpl(const Configurator& config)
99 : config_(config) {
102 UpdateCheckerImpl::~UpdateCheckerImpl() {
103 DCHECK(thread_checker_.CalledOnValidThread());
106 bool UpdateCheckerImpl::CheckForUpdates(
107 const std::vector<CrxUpdateItem*>& items_to_check,
108 const std::string& additional_attributes,
109 const UpdateCheckCallback& update_check_callback) {
110 DCHECK(thread_checker_.CalledOnValidThread());
112 if (request_sender_.get()) {
113 NOTREACHED();
114 return false; // Another update check is in progress.
117 update_check_callback_ = update_check_callback;
119 request_sender_.reset(new RequestSender(config_));
120 request_sender_->Send(
121 BuildUpdateCheckRequest(config_, items_to_check, additional_attributes),
122 config_.UpdateUrl(),
123 base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete,
124 base::Unretained(this)));
125 return true;
128 void UpdateCheckerImpl::OnRequestSenderComplete(const net::URLFetcher* source) {
129 DCHECK(thread_checker_.CalledOnValidThread());
131 const GURL original_url(source->GetOriginalURL());
132 VLOG(1) << "Update check request went to: " << original_url.spec();
134 int error = 0;
135 std::string error_message;
136 UpdateResponse update_response;
138 if (FetchSuccess(*source)) {
139 std::string xml;
140 source->GetResponseAsString(&xml);
141 if (!update_response.Parse(xml)) {
142 error = -1;
143 error_message = update_response.errors();
144 VLOG(1) << "Update request failed: " << error_message;
146 } else {
147 error = GetFetchError(*source);
148 error_message.assign("network error");
149 VLOG(1) << "Update request failed: network error";
152 request_sender_.reset();
153 update_check_callback_.Run(original_url, error, error_message,
154 update_response.results());
157 } // namespace
159 scoped_ptr<UpdateChecker> UpdateChecker::Create(const Configurator& config) {
160 return scoped_ptr<UpdateChecker>(new UpdateCheckerImpl(config));
163 } // namespace update_client