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