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