Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / component_updater / component_updater_configurator.cc
blob269dea124262d606dabbe60bf425244d2f11aff0
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 namespace component_updater {
22 namespace {
24 // Default time constants.
25 const int kDelayOneMinute = 60;
26 const int kDelayOneHour = kDelayOneMinute * 60;
28 // Debug values you can pass to --component-updater=value1,value2.
29 // Speed up component checking.
30 const char kSwitchFastUpdate[] = "fast-update";
32 // Add "testrequest=1" attribute to the update check request.
33 const char kSwitchRequestParam[] = "test-request";
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";
39 // Sets the URL for updates.
40 const char kSwitchUrlSource[] = "url-source";
42 #define COMPONENT_UPDATER_SERVICE_ENDPOINT \
43 "//clients2.google.com/service/update2"
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;
49 // The url to send the pings to.
50 const char kPingUrl[] = "https:" COMPONENT_UPDATER_SERVICE_ENDPOINT;
52 // Disables differential updates.
53 const char kSwitchDisableDeltaUpdates[] = "disable-delta-updates";
55 #if defined(OS_WIN)
56 // Disables background downloads.
57 const char kSwitchDisableBackgroundDownloads[] = "disable-background-downloads";
58 #endif // defined(OS_WIN)
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());
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);
85 return std::string();
88 } // namespace
90 class ChromeConfigurator : public ComponentUpdateService::Configurator {
91 public:
92 ChromeConfigurator(const CommandLine* cmdline,
93 net::URLRequestContextGetter* url_request_getter);
95 virtual ~ChromeConfigurator() {}
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;
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_;
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);
139 #if defined(OS_WIN)
140 background_downloads_enabled_ =
141 !HasSwitchValue(switch_values, kSwitchDisableBackgroundDownloads);
142 #else
143 background_downloads_enabled_ = false;
144 #endif
146 url_source_ = GetSwitchArgument(switch_values, kSwitchUrlSource);
147 if (url_source_.empty()) {
148 url_source_ = kDefaultUrlSource;
151 if (HasSwitchValue(switch_values, kSwitchRequestParam))
152 extra_info_ += "testrequest=\"1\"";
155 int ChromeConfigurator::InitialDelay() {
156 return fast_update_ ? 1 : (6 * kDelayOneMinute);
159 int ChromeConfigurator::NextCheckDelay() {
160 return fast_update_ ? 3 : (6 * kDelayOneHour);
163 int ChromeConfigurator::StepDelayMedium() {
164 return fast_update_ ? 3 : (15 * kDelayOneMinute);
167 int ChromeConfigurator::StepDelay() {
168 return fast_update_ ? 1 : 1;
171 int ChromeConfigurator::MinimumReCheckWait() {
172 return fast_update_ ? 30 : (6 * kDelayOneHour);
175 int ChromeConfigurator::OnDemandDelay() {
176 return fast_update_ ? 2 : (30 * kDelayOneMinute);
179 GURL ChromeConfigurator::UpdateUrl() {
180 return GURL(url_source_);
183 GURL ChromeConfigurator::PingUrl() {
184 return pings_enabled_ ? GURL(kPingUrl) : GURL();
187 std::string ChromeConfigurator::ExtraRequestParams() {
188 return extra_info_;
191 size_t ChromeConfigurator::UrlSizeLimit() {
192 return 1024ul;
195 net::URLRequestContextGetter* ChromeConfigurator::RequestContext() {
196 return url_request_getter_;
199 bool ChromeConfigurator::InProcess() {
200 return false;
203 bool ChromeConfigurator::DeltasEnabled() const {
204 return deltas_enabled_;
207 bool ChromeConfigurator::UseBackgroundDownloader() const {
208 return background_downloads_enabled_;
211 ComponentUpdateService::Configurator* MakeChromeComponentUpdaterConfigurator(
212 const CommandLine* cmdline,
213 net::URLRequestContextGetter* context_getter) {
214 return new ChromeConfigurator(cmdline, context_getter);
217 } // namespace component_updater