Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / component_updater / component_updater_configurator.cc
blob5afda5a1a8201059fcb86ac3058a0e6b7ba8f1f7
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.
5 #include "chrome/browser/component_updater/component_updater_configurator.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
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"
20 #if defined(OS_WIN)
21 #include "chrome/browser/component_updater/component_patcher_win.h"
22 #endif
24 namespace component_updater {
26 namespace {
28 // Default time constants.
29 const int kDelayOneMinute = 60;
30 const int kDelayOneHour = kDelayOneMinute * 60;
32 // Debug values you can pass to --component-updater=value1,value2.
33 // Speed up component checking.
34 const char kSwitchFastUpdate[] = "fast-update";
36 // Add "testrequest=1" attribute to the update check request.
37 const char kSwitchRequestParam[] = "test-request";
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";
43 // Sets the URL for updates.
44 const char kSwitchUrlSource[] = "url-source";
46 #define COMPONENT_UPDATER_SERVICE_ENDPOINT \
47 "//clients2.google.com/service/update2"
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;
53 // The url to send the pings to.
54 const char kPingUrl[] = "http:" COMPONENT_UPDATER_SERVICE_ENDPOINT;
56 #if defined(OS_WIN)
57 // Disables differential updates.
58 const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
59 // Disables background downloads.
60 const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads";
61 #endif // defined(OS_WIN)
63 // Returns true if and only if |test| is contained in |vec|.
64 bool HasSwitchValue(const std::vector<std::string>& vec, const char* test) {
65 if (vec.empty())
66 return 0;
67 return (std::find(vec.begin(), vec.end(), test) != vec.end());
70 // If there is an element of |vec| of the form |test|=.*, returns the right-
71 // hand side of that assignment. Otherwise, returns an empty string.
72 // The right-hand side may contain additional '=' characters, allowing for
73 // further nesting of switch arguments.
74 std::string GetSwitchArgument(const std::vector<std::string>& vec,
75 const char* test) {
76 if (vec.empty())
77 return std::string();
78 for (std::vector<std::string>::const_iterator it = vec.begin();
79 it != vec.end();
80 ++it) {
81 const std::size_t found = it->find("=");
82 if (found != std::string::npos) {
83 if (it->substr(0, found) == test) {
84 return it->substr(found + 1);
88 return std::string();
91 } // namespace
93 class ChromeConfigurator : public ComponentUpdateService::Configurator {
94 public:
95 ChromeConfigurator(const CommandLine* cmdline,
96 net::URLRequestContextGetter* url_request_getter);
98 virtual ~ChromeConfigurator() {}
100 virtual int InitialDelay() OVERRIDE;
101 virtual int NextCheckDelay() OVERRIDE;
102 virtual int StepDelay() OVERRIDE;
103 virtual int StepDelayMedium() OVERRIDE;
104 virtual int MinimumReCheckWait() OVERRIDE;
105 virtual int OnDemandDelay() OVERRIDE;
106 virtual GURL UpdateUrl() OVERRIDE;
107 virtual GURL PingUrl() OVERRIDE;
108 virtual std::string ExtraRequestParams() OVERRIDE;
109 virtual size_t UrlSizeLimit() OVERRIDE;
110 virtual net::URLRequestContextGetter* RequestContext() OVERRIDE;
111 virtual bool InProcess() OVERRIDE;
112 virtual ComponentPatcher* CreateComponentPatcher() OVERRIDE;
113 virtual bool DeltasEnabled() const OVERRIDE;
114 virtual bool UseBackgroundDownloader() const OVERRIDE;
116 private:
117 net::URLRequestContextGetter* url_request_getter_;
118 std::string extra_info_;
119 std::string url_source_;
120 bool fast_update_;
121 bool pings_enabled_;
122 bool deltas_enabled_;
123 bool background_downloads_enabled_;
126 ChromeConfigurator::ChromeConfigurator(const CommandLine* cmdline,
127 net::URLRequestContextGetter* url_request_getter)
128 : url_request_getter_(url_request_getter),
129 fast_update_(false),
130 pings_enabled_(false),
131 deltas_enabled_(false),
132 background_downloads_enabled_(false) {
133 // Parse comma-delimited debug flags.
134 std::vector<std::string> switch_values;
135 Tokenize(cmdline->GetSwitchValueASCII(switches::kComponentUpdater),
136 ",", &switch_values);
137 fast_update_ = HasSwitchValue(switch_values, kSwitchFastUpdate);
138 pings_enabled_ = !HasSwitchValue(switch_values, kSwitchDisablePings);
139 #if defined(OS_WIN)
140 deltas_enabled_ = !HasSwitchValue(switch_values, kSwitchDisableDeltaUpdates);
141 background_downloads_enabled_ =
142 !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads);
143 #else
144 deltas_enabled_ = false;
145 background_downloads_enabled_ = false;
146 #endif
148 url_source_ = GetSwitchArgument(switch_values, kSwitchUrlSource);
149 if (url_source_.empty()) {
150 url_source_ = kDefaultUrlSource;
153 if (HasSwitchValue(switch_values, kSwitchRequestParam))
154 extra_info_ += "testrequest=\"1\"";
157 int ChromeConfigurator::InitialDelay() {
158 return fast_update_ ? 1 : (6 * kDelayOneMinute);
161 int ChromeConfigurator::NextCheckDelay() {
162 return fast_update_ ? 3 : (2 * kDelayOneHour);
165 int ChromeConfigurator::StepDelayMedium() {
166 return fast_update_ ? 3 : (15 * kDelayOneMinute);
169 int ChromeConfigurator::StepDelay() {
170 return fast_update_ ? 1 : 1;
173 int ChromeConfigurator::MinimumReCheckWait() {
174 return fast_update_ ? 30 : (6 * kDelayOneHour);
177 int ChromeConfigurator::OnDemandDelay() {
178 return fast_update_ ? 2 : (30 * kDelayOneMinute);
181 GURL ChromeConfigurator::UpdateUrl() {
182 return GURL(url_source_);
185 GURL ChromeConfigurator::PingUrl() {
186 return pings_enabled_ ? GURL(kPingUrl) : GURL();
189 std::string ChromeConfigurator::ExtraRequestParams() {
190 return extra_info_;
193 size_t ChromeConfigurator::UrlSizeLimit() {
194 return 1024ul;
197 net::URLRequestContextGetter* ChromeConfigurator::RequestContext() {
198 return url_request_getter_;
201 bool ChromeConfigurator::InProcess() {
202 return false;
205 ComponentPatcher* ChromeConfigurator::CreateComponentPatcher() {
206 #if defined(OS_WIN)
207 return new ComponentPatcherWin();
208 #else
209 return new ComponentPatcherCrossPlatform();
210 #endif
213 bool ChromeConfigurator::DeltasEnabled() const {
214 return deltas_enabled_;
217 bool ChromeConfigurator::UseBackgroundDownloader() const {
218 return background_downloads_enabled_;
221 ComponentUpdateService::Configurator* MakeChromeComponentUpdaterConfigurator(
222 const CommandLine* cmdline, net::URLRequestContextGetter* context_getter) {
223 return new ChromeConfigurator(cmdline, context_getter);
226 } // namespace component_updater