OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "chrome/browser/component_updater/component_updater_configurator.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/command_line.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "base/version.h" | |
15 #include "build/build_config.h" | |
16 #include "chrome/browser/component_updater/component_patcher_operation_out_of_pr
ocess.h" | |
17 #include "chrome/browser/omaha_query_params/chrome_omaha_query_params_delegate.h
" | |
18 #include "chrome/common/chrome_version_info.h" | |
19 #include "components/component_updater/component_updater_switches.h" | |
20 #include "content/public/browser/browser_thread.h" | |
21 #include "net/url_request/url_request_context_getter.h" | |
22 #include "url/gurl.h" | |
23 | |
24 namespace component_updater { | |
25 | |
26 namespace { | |
27 | |
28 // Default time constants. | |
29 const int kDelayOneMinute = 60; | |
30 const int kDelayOneHour = kDelayOneMinute * 60; | |
31 | |
32 // Debug values you can pass to --component-updater=value1,value2. | |
33 // Speed up component checking. | |
34 const char kSwitchFastUpdate[] = "fast-update"; | |
35 | |
36 // Add "testrequest=1" attribute to the update check request. | |
37 const char kSwitchRequestParam[] = "test-request"; | |
38 | |
39 // Disables pings. Pings are the requests sent to the update server that report | |
40 // the success or the failure of component install or update attempts. | |
41 extern const char kSwitchDisablePings[] = "disable-pings"; | |
42 | |
43 // Sets the URL for updates. | |
44 const char kSwitchUrlSource[] = "url-source"; | |
45 | |
46 #define COMPONENT_UPDATER_SERVICE_ENDPOINT \ | |
47 "//clients2.google.com/service/update2" | |
48 | |
49 // The default url for the v3 protocol service endpoint. Can be | |
50 // overridden with --component-updater=url-source=someurl. | |
51 const char kDefaultUrlSource[] = "https:" COMPONENT_UPDATER_SERVICE_ENDPOINT; | |
52 | |
53 // The url to send the pings to. | |
54 const char kPingUrl[] = "https:" COMPONENT_UPDATER_SERVICE_ENDPOINT; | |
55 | |
56 // Disables differential updates. | |
57 const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates"; | |
58 | |
59 #if defined(OS_WIN) | |
60 // Disables background downloads. | |
61 const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads"; | |
62 #endif // defined(OS_WIN) | |
63 | |
64 // Returns true if and only if |test| is contained in |vec|. | |
65 bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) { | |
66 if (vec.empty()) | |
67 return 0; | |
68 return (std::find(vec.begin(), vec.end(), test) != vec.end()); | |
69 } | |
70 | |
71 // If there is an element of |vec| of the form |test|=.*, returns the right- | |
72 // hand side of that assignment. Otherwise, returns an empty string. | |
73 // The right-hand side may contain additional '=' characters, allowing for | |
74 // further nesting of switch arguments. | |
75 std::string GetSwitchArgument(const std::vector<std::string>& vec, | |
76 const char* test) { | |
77 if (vec.empty()) | |
78 return std::string(); | |
79 for (std::vector<std::string>::const_iterator it = vec.begin(); | |
80 it != vec.end(); | |
81 ++it) { | |
82 const std::size_t found = it->find("="); | |
83 if (found != std::string::npos) { | |
84 if (it->substr(0, found) == test) { | |
85 return it->substr(found + 1); | |
86 } | |
87 } | |
88 } | |
89 return std::string(); | |
90 } | |
91 | |
92 } // namespace | |
93 | |
94 class ChromeConfigurator : public Configurator { | |
95 public: | |
96 ChromeConfigurator(const CommandLine* cmdline, | |
97 net::URLRequestContextGetter* url_request_getter); | |
98 | |
99 virtual ~ChromeConfigurator() {} | |
100 | |
101 virtual int InitialDelay() const OVERRIDE; | |
102 virtual int NextCheckDelay() OVERRIDE; | |
103 virtual int StepDelay() const OVERRIDE; | |
104 virtual int StepDelayMedium() OVERRIDE; | |
105 virtual int MinimumReCheckWait() const OVERRIDE; | |
106 virtual int OnDemandDelay() const OVERRIDE; | |
107 virtual GURL UpdateUrl() const OVERRIDE; | |
108 virtual GURL PingUrl() const OVERRIDE; | |
109 virtual base::Version GetBrowserVersion() const OVERRIDE; | |
110 virtual std::string GetChannel() const OVERRIDE; | |
111 virtual std::string GetLang() const OVERRIDE; | |
112 virtual std::string GetOSLongName() const OVERRIDE; | |
113 virtual std::string ExtraRequestParams() const OVERRIDE; | |
114 virtual size_t UrlSizeLimit() const OVERRIDE; | |
115 virtual net::URLRequestContextGetter* RequestContext() const OVERRIDE; | |
116 virtual scoped_refptr<OutOfProcessPatcher> CreateOutOfProcessPatcher() | |
117 const OVERRIDE; | |
118 virtual bool DeltasEnabled() const OVERRIDE; | |
119 virtual bool UseBackgroundDownloader() const OVERRIDE; | |
120 virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() | |
121 const OVERRIDE; | |
122 virtual scoped_refptr<base::SingleThreadTaskRunner> | |
123 GetSingleThreadTaskRunner() const OVERRIDE; | |
124 | |
125 private: | |
126 net::URLRequestContextGetter* url_request_getter_; | |
127 std::string extra_info_; | |
128 std::string url_source_; | |
129 bool fast_update_; | |
130 bool pings_enabled_; | |
131 bool deltas_enabled_; | |
132 bool background_downloads_enabled_; | |
133 }; | |
134 | |
135 ChromeConfigurator::ChromeConfigurator( | |
136 const CommandLine* cmdline, | |
137 net::URLRequestContextGetter* url_request_getter) | |
138 : url_request_getter_(url_request_getter), | |
139 fast_update_(false), | |
140 pings_enabled_(false), | |
141 deltas_enabled_(false), | |
142 background_downloads_enabled_(false) { | |
143 // Parse comma-delimited debug flags. | |
144 std::vector<std::string> switch_values; | |
145 Tokenize(cmdline->GetSwitchValueASCII(switches::kComponentUpdater), | |
146 ",", | |
147 &switch_values); | |
148 fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate); | |
149 pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings); | |
150 deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates); | |
151 | |
152 #if defined(OS_WIN) | |
153 background_downloads_enabled_ = | |
154 !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads); | |
155 #else | |
156 background_downloads_enabled_ = false; | |
157 #endif | |
158 | |
159 url_source_ = GetSwitchArgument(switch_values, kSwitchUrlSource); | |
160 if (url_source_.empty()) { | |
161 url_source_ = kDefaultUrlSource; | |
162 } | |
163 | |
164 if (HasSwitchValue(switch_values, kSwitchRequestParam)) | |
165 extra_info_ += "testrequest=\"1\""; | |
166 } | |
167 | |
168 int ChromeConfigurator::InitialDelay() const { | |
169 return fast_update_ ? 1 : (6 * kDelayOneMinute); | |
170 } | |
171 | |
172 int ChromeConfigurator::NextCheckDelay() { | |
173 return fast_update_ ? 3 : (6 * kDelayOneHour); | |
174 } | |
175 | |
176 int ChromeConfigurator::StepDelayMedium() { | |
177 return fast_update_ ? 3 : (15 * kDelayOneMinute); | |
178 } | |
179 | |
180 int ChromeConfigurator::StepDelay() const { | |
181 return fast_update_ ? 1 : 1; | |
182 } | |
183 | |
184 int ChromeConfigurator::MinimumReCheckWait() const { | |
185 return fast_update_ ? 30 : (6 * kDelayOneHour); | |
186 } | |
187 | |
188 int ChromeConfigurator::OnDemandDelay() const { | |
189 return fast_update_ ? 2 : (30 * kDelayOneMinute); | |
190 } | |
191 | |
192 GURL ChromeConfigurator::UpdateUrl() const { | |
193 return GURL(url_source_); | |
194 } | |
195 | |
196 GURL ChromeConfigurator::PingUrl() const { | |
197 return pings_enabled_ ? GURL(kPingUrl) : GURL(); | |
198 } | |
199 | |
200 base::Version ChromeConfigurator::GetBrowserVersion() const { | |
201 return base::Version(chrome::VersionInfo().Version()); | |
202 } | |
203 | |
204 std::string ChromeConfigurator::GetChannel() const { | |
205 return ChromeOmahaQueryParamsDelegate::GetChannelString(); | |
206 } | |
207 | |
208 std::string ChromeConfigurator::GetLang() const { | |
209 return ChromeOmahaQueryParamsDelegate::GetLang(); | |
210 } | |
211 | |
212 std::string ChromeConfigurator::GetOSLongName() const { | |
213 return chrome::VersionInfo().OSType(); | |
214 } | |
215 | |
216 std::string ChromeConfigurator::ExtraRequestParams() const { | |
217 return extra_info_; | |
218 } | |
219 | |
220 size_t ChromeConfigurator::UrlSizeLimit() const { | |
221 return 1024ul; | |
222 } | |
223 | |
224 net::URLRequestContextGetter* ChromeConfigurator::RequestContext() const { | |
225 return url_request_getter_; | |
226 } | |
227 | |
228 scoped_refptr<OutOfProcessPatcher> | |
229 ChromeConfigurator::CreateOutOfProcessPatcher() const { | |
230 return make_scoped_refptr(new ChromeOutOfProcessPatcher); | |
231 } | |
232 | |
233 bool ChromeConfigurator::DeltasEnabled() const { | |
234 return deltas_enabled_; | |
235 } | |
236 | |
237 bool ChromeConfigurator::UseBackgroundDownloader() const { | |
238 return background_downloads_enabled_; | |
239 } | |
240 | |
241 scoped_refptr<base::SequencedTaskRunner> | |
242 ChromeConfigurator::GetSequencedTaskRunner() const { | |
243 return content::BrowserThread::GetBlockingPool() | |
244 ->GetSequencedTaskRunnerWithShutdownBehavior( | |
245 content::BrowserThread::GetBlockingPool()->GetSequenceToken(), | |
246 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
247 } | |
248 | |
249 scoped_refptr<base::SingleThreadTaskRunner> | |
250 ChromeConfigurator::GetSingleThreadTaskRunner() const { | |
251 return content::BrowserThread::GetMessageLoopProxyForThread( | |
252 content::BrowserThread::FILE); | |
253 } | |
254 | |
255 Configurator* MakeChromeComponentUpdaterConfigurator( | |
256 const base::CommandLine* cmdline, | |
257 net::URLRequestContextGetter* context_getter) { | |
258 return new ChromeConfigurator(cmdline, context_getter); | |
259 } | |
260 | |
261 } // namespace component_updater | |
OLD | NEW |