- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / component_updater_configurator.cc
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/win/windows_version.h"
15 #include "build/build_config.h"
16 #include "chrome/browser/component_updater/component_patcher.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/omaha_query_params/omaha_query_params.h"
19 #include "net/url_request/url_request_context_getter.h"
20
21 #if defined(OS_WIN)
22 #include "chrome/browser/component_updater/component_patcher_win.h"
23 #endif
24
25 namespace {
26
27 // Default time constants.
28 const int kDelayOneMinute = 60;
29 const int kDelayOneHour = kDelayOneMinute * 60;
30
31 // Debug values you can pass to --component-updater=value1,value2.
32 // Speed up component checking.
33 const char kSwitchFastUpdate[] = "fast-update";
34 // Force out-of-process-xml parsing.
35 const char kSwitchOutOfProcess[] = "out-of-process";
36 // Add "testrequest=1" parameter to the update check query.
37 const char kSwitchRequestParam[] = "test-request";
38 // Disables pings. Pings are the requests sent to the update server that report
39 // the success or the failure of component install or update attempts.
40 extern const char kSwitchDisablePings[] = "disable-pings";
41
42 // Sets the URL for updates.
43 const char kSwitchUrlSource[] = "url-source";
44
45 // The default url from which an update manifest can be fetched. Can be
46 // overridden with --component-updater=url-source=someurl.
47 const char kDefaultUrlSource[] =
48     "http://clients2.google.com/service/update2/crx";
49
50 // The url to send the pings to.
51 const char kPingUrl[] = "http://tools.google.com/service/update2";
52
53 #if defined(OS_WIN)
54 // Disables differential updates.
55 const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
56 #endif  // defined(OS_WIN)
57
58 // Returns true if and only if |test| is contained in |vec|.
59 bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) {
60   if (vec.empty())
61     return 0;
62   return (std::find(vec.begin(), vec.end(), test) != vec.end());
63 }
64
65 // If there is an element of |vec| of the form |test|=.*, returns the right-
66 // hand side of that assignment. Otherwise, returns an empty string.
67 // The right-hand side may contain additional '=' characters, allowing for
68 // further nesting of switch arguments.
69 std::string GetSwitchArgument(const std::vector<std::string>& vec,
70                               const char* test) {
71   if (vec.empty())
72     return std::string();
73   for (std::vector<std::string>::const_iterator it = vec.begin();
74       it != vec.end();
75       ++it) {
76     const std::size_t found = it->find("=");
77     if (found != std::string::npos) {
78       if (it->substr(0, found) == test) {
79         return it->substr(found + 1);
80       }
81     }
82   }
83   return std::string();
84 }
85
86 }  // namespace
87
88 class ChromeConfigurator : public ComponentUpdateService::Configurator {
89  public:
90   ChromeConfigurator(const CommandLine* cmdline,
91                      net::URLRequestContextGetter* url_request_getter);
92
93   virtual ~ChromeConfigurator() {}
94
95   virtual int InitialDelay() OVERRIDE;
96   virtual int NextCheckDelay() OVERRIDE;
97   virtual int StepDelay() OVERRIDE;
98   virtual int StepDelayMedium() OVERRIDE;
99   virtual int MinimumReCheckWait() OVERRIDE;
100   virtual int OnDemandDelay() OVERRIDE;
101   virtual GURL UpdateUrl() OVERRIDE;
102   virtual GURL PingUrl() OVERRIDE;
103   virtual const char* ExtraRequestParams() OVERRIDE;
104   virtual size_t UrlSizeLimit() OVERRIDE;
105   virtual net::URLRequestContextGetter* RequestContext() OVERRIDE;
106   virtual bool InProcess() OVERRIDE;
107   virtual ComponentPatcher* CreateComponentPatcher() OVERRIDE;
108   virtual bool DeltasEnabled() const OVERRIDE;
109
110  private:
111   net::URLRequestContextGetter* url_request_getter_;
112   std::string extra_info_;
113   std::string url_source_;
114   bool fast_update_;
115   bool out_of_process_;
116   bool pings_enabled_;
117   bool deltas_enabled_;
118 };
119
120 ChromeConfigurator::ChromeConfigurator(const CommandLine* cmdline,
121     net::URLRequestContextGetter* url_request_getter)
122       : url_request_getter_(url_request_getter),
123         extra_info_(chrome::OmahaQueryParams::Get(
124             chrome::OmahaQueryParams::CHROME)),
125         fast_update_(false),
126         out_of_process_(false),
127         pings_enabled_(false),
128         deltas_enabled_(false) {
129   // Parse comma-delimited debug flags.
130   std::vector<std::string> switch_values;
131   Tokenize(cmdline->GetSwitchValueASCII(switches::kComponentUpdater),
132       ",", &switch_values);
133   fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate);
134   out_of_process_ = HasSwitchValue(switch_values, kSwitchOutOfProcess);
135   pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings);
136 #if defined(OS_WIN)
137   deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates);
138 #else
139   deltas_enabled_ = false;
140 #endif
141
142   url_source_ = GetSwitchArgument(switch_values, kSwitchUrlSource);
143   if (url_source_.empty()) {
144     url_source_ = kDefaultUrlSource;
145   }
146
147   // Make the extra request params, they are necessary so omaha does
148   // not deliver components that are going to be rejected at install time.
149 #if defined(OS_WIN)
150   if (base::win::OSInfo::GetInstance()->wow64_status() ==
151       base::win::OSInfo::WOW64_ENABLED)
152     extra_info_ += "&wow64=1";
153 #endif
154   if (HasSwitchValue(switch_values, kSwitchRequestParam))
155     extra_info_ += "&testrequest=1";
156 }
157
158 int ChromeConfigurator::InitialDelay() {
159   return fast_update_ ? 1 : (6 * kDelayOneMinute);
160 }
161
162 int ChromeConfigurator::NextCheckDelay() {
163   return fast_update_ ? 3 : (2 * kDelayOneHour);
164 }
165
166 int ChromeConfigurator::StepDelayMedium() {
167   return fast_update_ ? 3 : (15 * kDelayOneMinute);
168 }
169
170 int ChromeConfigurator::StepDelay() {
171   return fast_update_ ? 1 : 1;
172 }
173
174 int ChromeConfigurator::MinimumReCheckWait() {
175   return fast_update_ ? 30 : (6 * kDelayOneHour);
176 }
177
178 int ChromeConfigurator::OnDemandDelay() {
179   return fast_update_ ? 2 : (30 * kDelayOneMinute);
180 }
181
182 GURL ChromeConfigurator::UpdateUrl() {
183   return GURL(url_source_);
184 }
185
186 GURL ChromeConfigurator::PingUrl() {
187   return pings_enabled_ ? GURL(kPingUrl) : GURL();
188 }
189
190 const char* ChromeConfigurator::ExtraRequestParams() {
191   return extra_info_.c_str();
192 }
193
194 size_t ChromeConfigurator::UrlSizeLimit() {
195   return 1024ul;
196 }
197
198 net::URLRequestContextGetter* ChromeConfigurator::RequestContext() {
199   return url_request_getter_;
200 }
201
202 bool ChromeConfigurator::InProcess() {
203   return !out_of_process_;
204 }
205
206 ComponentPatcher* ChromeConfigurator::CreateComponentPatcher() {
207 #if defined(OS_WIN)
208   return new ComponentPatcherWin();
209 #else
210   return new ComponentPatcherCrossPlatform();
211 #endif
212 }
213
214 bool ChromeConfigurator::DeltasEnabled() const {
215   return deltas_enabled_;
216 }
217
218 ComponentUpdateService::Configurator* MakeChromeComponentUpdaterConfigurator(
219     const CommandLine* cmdline, net::URLRequestContextGetter* context_getter) {
220   return new ChromeConfigurator(cmdline, context_getter);
221 }