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 "components/component_updater/configurator_impl.h"
6
7 #include <stddef.h>
8
9 #include <algorithm>
10
11 #include "base/callback.h"
12 #include "base/feature_list.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/version.h"
17 #include "build/build_config.h"
18 #include "components/component_updater/component_updater_switches.h"
19 #include "components/component_updater/component_updater_url_constants.h"
20 #include "components/update_client/command_line_config_policy.h"
21 #include "components/update_client/protocol_handler.h"
22 #include "components/update_client/utils.h"
23 #include "components/version_info/version_info.h"
24
25 #if defined(OS_WIN)
26 #include "base/win/win_util.h"
27 #endif // OS_WIN
28
29 namespace component_updater {
30
31 namespace {
32
33 // Default time constants.
34 const int kDelayOneMinute = 60;
35 const int kDelayOneHour = kDelayOneMinute * 60;
36
37 } // namespace
38
ConfiguratorImpl(const update_client::CommandLineConfigPolicy & config_policy,bool require_encryption)39 ConfiguratorImpl::ConfiguratorImpl(
40 const update_client::CommandLineConfigPolicy& config_policy,
41 bool require_encryption)
42 : background_downloads_enabled_(config_policy.BackgroundDownloadsEnabled()),
43 deltas_enabled_(config_policy.DeltaUpdatesEnabled()),
44 fast_update_(config_policy.FastUpdate()),
45 pings_enabled_(config_policy.PingsEnabled()),
46 require_encryption_(require_encryption),
47 url_source_override_(config_policy.UrlSourceOverride()),
48 initial_delay_(config_policy.InitialDelay()) {
49 if (config_policy.TestRequest()) {
50 extra_info_["testrequest"] = "1";
51 extra_info_["testsource"] = "dev";
52 }
53 }
54
55 ConfiguratorImpl::~ConfiguratorImpl() = default;
56
InitialDelay() const57 int ConfiguratorImpl::InitialDelay() const {
58 if (initial_delay_)
59 return initial_delay_;
60 return fast_update_ ? 10 : kDelayOneMinute;
61 }
62
NextCheckDelay() const63 int ConfiguratorImpl::NextCheckDelay() const {
64 return 5 * kDelayOneHour;
65 }
66
OnDemandDelay() const67 int ConfiguratorImpl::OnDemandDelay() const {
68 return fast_update_ ? 2 : (30 * kDelayOneMinute);
69 }
70
UpdateDelay() const71 int ConfiguratorImpl::UpdateDelay() const {
72 return fast_update_ ? 10 : (15 * kDelayOneMinute);
73 }
74
UpdateUrl() const75 std::vector<GURL> ConfiguratorImpl::UpdateUrl() const {
76 if (url_source_override_.is_valid())
77 return {GURL(url_source_override_)};
78
79 std::vector<GURL> urls{GURL(kUpdaterJSONDefaultUrl),
80 GURL(kUpdaterJSONFallbackUrl)};
81 if (require_encryption_)
82 update_client::RemoveUnsecureUrls(&urls);
83
84 return urls;
85 }
86
PingUrl() const87 std::vector<GURL> ConfiguratorImpl::PingUrl() const {
88 return pings_enabled_ ? UpdateUrl() : std::vector<GURL>();
89 }
90
GetBrowserVersion() const91 const base::Version& ConfiguratorImpl::GetBrowserVersion() const {
92 return version_info::GetVersion();
93 }
94
GetOSLongName() const95 std::string ConfiguratorImpl::GetOSLongName() const {
96 return version_info::GetOSType();
97 }
98
ExtraRequestParams() const99 base::flat_map<std::string, std::string> ConfiguratorImpl::ExtraRequestParams()
100 const {
101 return extra_info_;
102 }
103
GetDownloadPreference() const104 std::string ConfiguratorImpl::GetDownloadPreference() const {
105 return std::string();
106 }
107
EnabledDeltas() const108 bool ConfiguratorImpl::EnabledDeltas() const {
109 return deltas_enabled_;
110 }
111
EnabledComponentUpdates() const112 bool ConfiguratorImpl::EnabledComponentUpdates() const {
113 return true;
114 }
115
EnabledBackgroundDownloader() const116 bool ConfiguratorImpl::EnabledBackgroundDownloader() const {
117 return background_downloads_enabled_;
118 }
119
EnabledCupSigning() const120 bool ConfiguratorImpl::EnabledCupSigning() const {
121 return true;
122 }
123
124 // The default implementation for most embedders returns an empty string.
125 // Desktop embedders, such as the Windows component updater can provide a
126 // meaningful implementation for this function.
GetAppGuid() const127 std::string ConfiguratorImpl::GetAppGuid() const {
128 return {};
129 }
130
131 std::unique_ptr<update_client::ProtocolHandlerFactory>
GetProtocolHandlerFactory() const132 ConfiguratorImpl::GetProtocolHandlerFactory() const {
133 return std::make_unique<update_client::ProtocolHandlerFactoryJSON>();
134 }
135
136 } // namespace component_updater
137