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 "chrome/browser/component_updater/update_checker.h"
7 #include "base/compiler_specific.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/component_updater/component_updater_utils.h"
10 #include "chrome/browser/component_updater/crx_update_item.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_fetcher_delegate.h"
16 using content::BrowserThread
;
18 namespace component_updater
{
20 // Builds an update check request for |components|. |additional_attributes| is
21 // 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
23 // corresponding <app> element is created and inserted as a child node of
26 // An app element looks like this:
27 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc"
28 // version="0.1.2.3" installsource="ondemand">
31 // <package fp="abcd" />
34 std::string
BuildUpdateCheckRequest(const std::vector
<CrxUpdateItem
*>& items
,
35 const std::string
& additional_attributes
) {
36 std::string app_elements
;
37 for (size_t i
= 0; i
!= items
.size(); ++i
) {
38 const CrxUpdateItem
* item
= items
[i
];
39 std::string
app("<app ");
40 base::StringAppendF(&app
,
41 "appid=\"%s\" version=\"%s\"",
43 item
->component
.version
.GetString().c_str());
45 base::StringAppendF(&app
, " installsource=\"ondemand\"");
46 base::StringAppendF(&app
, ">");
47 base::StringAppendF(&app
, "<updatecheck />");
48 if (!item
->component
.fingerprint
.empty()) {
49 base::StringAppendF(&app
,
51 "<package fp=\"%s\"/>"
53 item
->component
.fingerprint
.c_str());
55 base::StringAppendF(&app
, "</app>");
56 app_elements
.append(app
);
59 return BuildProtocolRequest(app_elements
, additional_attributes
);
62 class UpdateCheckerImpl
63 : public UpdateChecker
,
64 public net::URLFetcherDelegate
{
66 UpdateCheckerImpl(const GURL
& url
,
67 net::URLRequestContextGetter
* url_request_context_getter
,
68 const UpdateCheckCallback
& update_check_callback
);
69 virtual ~UpdateCheckerImpl();
71 // Overrides for UpdateChecker.
72 virtual bool CheckForUpdates(
73 const std::vector
<CrxUpdateItem
*>& items_to_check
,
74 const std::string
& additional_attributes
) OVERRIDE
;
76 // Overrides for UrlFetcher.
77 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
81 net::URLRequestContextGetter
* url_request_context_getter_
; // Not owned.
82 const UpdateCheckCallback update_check_callback_
;
84 scoped_ptr
<net::URLFetcher
> url_fetcher_
;
86 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl
);
89 scoped_ptr
<UpdateChecker
> UpdateChecker::Create(
91 net::URLRequestContextGetter
* url_request_context_getter
,
92 const UpdateCheckCallback
& update_check_callback
) {
93 scoped_ptr
<UpdateCheckerImpl
> update_checker(new UpdateCheckerImpl(
94 url
, url_request_context_getter
, update_check_callback
));
95 return update_checker
.PassAs
<UpdateChecker
>();
98 UpdateCheckerImpl::UpdateCheckerImpl(
100 net::URLRequestContextGetter
* url_request_context_getter
,
101 const UpdateCheckCallback
& update_check_callback
)
103 url_request_context_getter_(url_request_context_getter
),
104 update_check_callback_(update_check_callback
) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
108 UpdateCheckerImpl::~UpdateCheckerImpl() {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
112 bool UpdateCheckerImpl::CheckForUpdates(
113 const std::vector
<CrxUpdateItem
*>& items_to_check
,
114 const std::string
& additional_attributes
) {
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
118 return false; // Another fetch is in progress.
120 url_fetcher_
.reset(SendProtocolRequest(
122 BuildUpdateCheckRequest(items_to_check
, additional_attributes
),
124 url_request_context_getter_
));
129 void UpdateCheckerImpl::OnURLFetchComplete(const net::URLFetcher
* source
) {
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
131 DCHECK(url_fetcher_
.get() == source
);
134 std::string error_message
;
135 UpdateResponse update_response
;
137 if (FetchSuccess(*source
)) {
139 source
->GetResponseAsString(&xml
);
140 if (!update_response
.Parse(xml
)) {
142 error_message
= update_response
.errors();
145 error
= GetFetchError(*source
);
146 error_message
.assign("network error");
149 url_fetcher_
.reset();
150 update_check_callback_
.Run(error
, error_message
, update_response
.results());
153 } // namespace component_updater