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 "chrome/browser/component_updater/update_checker.h" | 5 #include "chrome/browser/component_updater/update_checker.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/threading/thread_checker.h" |
10 #include "chrome/browser/component_updater/component_updater_configurator.h" | 11 #include "chrome/browser/component_updater/component_updater_configurator.h" |
11 #include "chrome/browser/component_updater/component_updater_utils.h" | 12 #include "chrome/browser/component_updater/component_updater_utils.h" |
12 #include "chrome/browser/component_updater/crx_update_item.h" | 13 #include "chrome/browser/component_updater/crx_update_item.h" |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_fetcher.h" |
15 #include "net/url_request/url_fetcher_delegate.h" | 15 #include "net/url_request/url_fetcher_delegate.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 using content::BrowserThread; | |
19 | |
20 namespace component_updater { | 18 namespace component_updater { |
21 | 19 |
22 // Builds an update check request for |components|. |additional_attributes| is | 20 // Builds an update check request for |components|. |additional_attributes| is |
23 // serialized as part of the <request> element of the request to customize it | 21 // serialized as part of the <request> element of the request to customize it |
24 // with data that is not platform or component specific. For each |item|, a | 22 // with data that is not platform or component specific. For each |item|, a |
25 // corresponding <app> element is created and inserted as a child node of | 23 // corresponding <app> element is created and inserted as a child node of |
26 // the <request>. | 24 // the <request>. |
27 // | 25 // |
28 // An app element looks like this: | 26 // An app element looks like this: |
29 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" | 27 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 79 |
82 // Overrides for UrlFetcher. | 80 // Overrides for UrlFetcher. |
83 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 81 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
84 | 82 |
85 private: | 83 private: |
86 const Configurator& config_; | 84 const Configurator& config_; |
87 const UpdateCheckCallback update_check_callback_; | 85 const UpdateCheckCallback update_check_callback_; |
88 | 86 |
89 scoped_ptr<net::URLFetcher> url_fetcher_; | 87 scoped_ptr<net::URLFetcher> url_fetcher_; |
90 | 88 |
| 89 base::ThreadChecker thread_checker_; |
| 90 |
91 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); | 91 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); |
92 }; | 92 }; |
93 | 93 |
94 scoped_ptr<UpdateChecker> UpdateChecker::Create( | 94 scoped_ptr<UpdateChecker> UpdateChecker::Create( |
95 const Configurator& config, | 95 const Configurator& config, |
96 const UpdateCheckCallback& update_check_callback) { | 96 const UpdateCheckCallback& update_check_callback) { |
97 scoped_ptr<UpdateCheckerImpl> update_checker( | 97 scoped_ptr<UpdateCheckerImpl> update_checker( |
98 new UpdateCheckerImpl(config, update_check_callback)); | 98 new UpdateCheckerImpl(config, update_check_callback)); |
99 return update_checker.PassAs<UpdateChecker>(); | 99 return update_checker.PassAs<UpdateChecker>(); |
100 } | 100 } |
101 | 101 |
102 UpdateCheckerImpl::UpdateCheckerImpl( | 102 UpdateCheckerImpl::UpdateCheckerImpl( |
103 const Configurator& config, | 103 const Configurator& config, |
104 const UpdateCheckCallback& update_check_callback) | 104 const UpdateCheckCallback& update_check_callback) |
105 : config_(config), update_check_callback_(update_check_callback) { | 105 : config_(config), update_check_callback_(update_check_callback) { |
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
107 } | 106 } |
108 | 107 |
109 UpdateCheckerImpl::~UpdateCheckerImpl() { | 108 UpdateCheckerImpl::~UpdateCheckerImpl() { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 109 DCHECK(thread_checker_.CalledOnValidThread()); |
111 } | 110 } |
112 | 111 |
113 bool UpdateCheckerImpl::CheckForUpdates( | 112 bool UpdateCheckerImpl::CheckForUpdates( |
114 const std::vector<CrxUpdateItem*>& items_to_check, | 113 const std::vector<CrxUpdateItem*>& items_to_check, |
115 const std::string& additional_attributes) { | 114 const std::string& additional_attributes) { |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 DCHECK(thread_checker_.CalledOnValidThread()); |
117 | 116 |
118 if (url_fetcher_) | 117 if (url_fetcher_) |
119 return false; // Another fetch is in progress. | 118 return false; // Another fetch is in progress. |
120 | 119 |
121 url_fetcher_.reset(SendProtocolRequest( | 120 url_fetcher_.reset(SendProtocolRequest( |
122 config_.UpdateUrl(), | 121 config_.UpdateUrl(), |
123 BuildUpdateCheckRequest(config_, items_to_check, additional_attributes), | 122 BuildUpdateCheckRequest(config_, items_to_check, additional_attributes), |
124 this, | 123 this, |
125 config_.RequestContext())); | 124 config_.RequestContext())); |
126 | 125 |
127 return true; | 126 return true; |
128 } | 127 } |
129 | 128 |
130 void UpdateCheckerImpl::OnURLFetchComplete(const net::URLFetcher* source) { | 129 void UpdateCheckerImpl::OnURLFetchComplete(const net::URLFetcher* source) { |
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
132 DCHECK(url_fetcher_.get() == source); | 131 DCHECK(url_fetcher_.get() == source); |
133 | 132 |
134 int error = 0; | 133 int error = 0; |
135 std::string error_message; | 134 std::string error_message; |
136 UpdateResponse update_response; | 135 UpdateResponse update_response; |
137 | 136 |
138 if (FetchSuccess(*source)) { | 137 if (FetchSuccess(*source)) { |
139 std::string xml; | 138 std::string xml; |
140 source->GetResponseAsString(&xml); | 139 source->GetResponseAsString(&xml); |
141 if (!update_response.Parse(xml)) { | 140 if (!update_response.Parse(xml)) { |
142 error = -1; | 141 error = -1; |
143 error_message = update_response.errors(); | 142 error_message = update_response.errors(); |
144 VLOG(1) << "Update request failed: " << error_message; | 143 VLOG(1) << "Update request failed: " << error_message; |
145 } | 144 } |
146 } else { | 145 } else { |
147 error = GetFetchError(*source); | 146 error = GetFetchError(*source); |
148 error_message.assign("network error"); | 147 error_message.assign("network error"); |
149 VLOG(1) << "Update request failed: network error"; | 148 VLOG(1) << "Update request failed: network error"; |
150 } | 149 } |
151 | 150 |
152 url_fetcher_.reset(); | 151 url_fetcher_.reset(); |
153 update_check_callback_.Run(error, error_message, update_response.results()); | 152 update_check_callback_.Run(error, error_message, update_response.results()); |
154 } | 153 } |
155 | 154 |
156 } // namespace component_updater | 155 } // namespace component_updater |
OLD | NEW |