OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/update_client/update_checker.h" | 5 #include "components/update_client/update_checker.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
22 #include "components/update_client/component.h" | 22 #include "components/update_client/component.h" |
23 #include "components/update_client/configurator.h" | 23 #include "components/update_client/configurator.h" |
24 #include "components/update_client/persisted_data.h" | 24 #include "components/update_client/persisted_data.h" |
| 25 #include "components/update_client/protocol_builder.h" |
25 #include "components/update_client/protocol_parser.h" | 26 #include "components/update_client/protocol_parser.h" |
26 #include "components/update_client/request_sender.h" | 27 #include "components/update_client/request_sender.h" |
27 #include "components/update_client/update_client.h" | 28 #include "components/update_client/update_client.h" |
28 #include "components/update_client/updater_state.h" | 29 #include "components/update_client/updater_state.h" |
29 #include "components/update_client/utils.h" | 30 #include "components/update_client/utils.h" |
30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
31 | 32 |
32 namespace update_client { | 33 namespace update_client { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 // Returns a sanitized version of the brand or an empty string otherwise. | |
37 std::string SanitizeBrand(const std::string& brand) { | |
38 return IsValidBrand(brand) ? brand : std::string(""); | |
39 } | |
40 | |
41 // Filters invalid attributes from |installer_attributes|. | |
42 update_client::InstallerAttributes SanitizeInstallerAttributes( | |
43 const update_client::InstallerAttributes& installer_attributes) { | |
44 update_client::InstallerAttributes sanitized_attrs; | |
45 for (const auto& attr : installer_attributes) { | |
46 if (IsValidInstallerAttribute(attr)) | |
47 sanitized_attrs.insert(attr); | |
48 } | |
49 return sanitized_attrs; | |
50 } | |
51 | |
52 // Returns true if at least one item requires network encryption. | 37 // Returns true if at least one item requires network encryption. |
53 bool IsEncryptionRequired(const IdToComponentPtrMap& components) { | 38 bool IsEncryptionRequired(const IdToComponentPtrMap& components) { |
54 for (const auto& item : components) { | 39 for (const auto& item : components) { |
55 const auto& component = item.second; | 40 const auto& component = item.second; |
56 if (component->crx_component().requires_network_encryption) | 41 if (component->crx_component().requires_network_encryption) |
57 return true; | 42 return true; |
58 } | 43 } |
59 return false; | 44 return false; |
60 } | 45 } |
61 | 46 |
62 // Builds an update check request for |components|. |additional_attributes| is | |
63 // serialized as part of the <request> element of the request to customize it | |
64 // with data that is not platform or component specific. For each |item|, a | |
65 // corresponding <app> element is created and inserted as a child node of | |
66 // the <request>. | |
67 // | |
68 // An app element looks like this: | |
69 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" | |
70 // version="0.1.2.3" installsource="ondemand"> | |
71 // <updatecheck/> | |
72 // <packages> | |
73 // <package fp="abcd"/> | |
74 // </packages> | |
75 // </app> | |
76 std::string BuildUpdateCheckRequest( | |
77 const Configurator& config, | |
78 const std::vector<std::string>& ids_checked, | |
79 const IdToComponentPtrMap& components, | |
80 PersistedData* metadata, | |
81 const std::string& additional_attributes, | |
82 bool enabled_component_updates, | |
83 const std::unique_ptr<UpdaterState::Attributes>& updater_state_attributes) { | |
84 const std::string brand(SanitizeBrand(config.GetBrand())); | |
85 std::string app_elements; | |
86 for (const auto& id : ids_checked) { | |
87 DCHECK_EQ(1u, components.count(id)); | |
88 const Component& component = *components.at(id); | |
89 | |
90 const update_client::InstallerAttributes installer_attributes( | |
91 SanitizeInstallerAttributes( | |
92 component.crx_component().installer_attributes)); | |
93 std::string app("<app "); | |
94 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", | |
95 component.id().c_str(), | |
96 component.crx_component().version.GetString().c_str()); | |
97 if (!brand.empty()) | |
98 base::StringAppendF(&app, " brand=\"%s\"", brand.c_str()); | |
99 if (component.on_demand()) | |
100 base::StringAppendF(&app, " installsource=\"ondemand\""); | |
101 for (const auto& attr : installer_attributes) { | |
102 base::StringAppendF(&app, " %s=\"%s\"", attr.first.c_str(), | |
103 attr.second.c_str()); | |
104 } | |
105 const std::string cohort = metadata->GetCohort(component.id()); | |
106 const std::string cohort_name = metadata->GetCohortName(component.id()); | |
107 const std::string cohort_hint = metadata->GetCohortHint(component.id()); | |
108 if (!cohort.empty()) | |
109 base::StringAppendF(&app, " cohort=\"%s\"", cohort.c_str()); | |
110 if (!cohort_name.empty()) | |
111 base::StringAppendF(&app, " cohortname=\"%s\"", cohort_name.c_str()); | |
112 if (!cohort_hint.empty()) | |
113 base::StringAppendF(&app, " cohorthint=\"%s\"", cohort_hint.c_str()); | |
114 base::StringAppendF(&app, ">"); | |
115 | |
116 base::StringAppendF(&app, "<updatecheck"); | |
117 if (component.crx_component() | |
118 .supports_group_policy_enable_component_updates && | |
119 !enabled_component_updates) { | |
120 base::StringAppendF(&app, " updatedisabled=\"true\""); | |
121 } | |
122 base::StringAppendF(&app, "/>"); | |
123 | |
124 base::StringAppendF(&app, "<ping rd=\"%d\" ping_freshness=\"%s\"/>", | |
125 metadata->GetDateLastRollCall(component.id()), | |
126 metadata->GetPingFreshness(component.id()).c_str()); | |
127 if (!component.crx_component().fingerprint.empty()) { | |
128 base::StringAppendF(&app, | |
129 "<packages>" | |
130 "<package fp=\"%s\"/>" | |
131 "</packages>", | |
132 component.crx_component().fingerprint.c_str()); | |
133 } | |
134 base::StringAppendF(&app, "</app>"); | |
135 app_elements.append(app); | |
136 VLOG(1) << "Appending to update request: " << app; | |
137 } | |
138 | |
139 // Include the updater state in the update check request. | |
140 return BuildProtocolRequest( | |
141 config.GetProdId(), config.GetBrowserVersion().GetString(), | |
142 config.GetChannel(), config.GetLang(), config.GetOSLongName(), | |
143 config.GetDownloadPreference(), app_elements, additional_attributes, | |
144 updater_state_attributes); | |
145 } | |
146 | 47 |
147 class UpdateCheckerImpl : public UpdateChecker { | 48 class UpdateCheckerImpl : public UpdateChecker { |
148 public: | 49 public: |
149 UpdateCheckerImpl(const scoped_refptr<Configurator>& config, | 50 UpdateCheckerImpl(const scoped_refptr<Configurator>& config, |
150 PersistedData* metadata); | 51 PersistedData* metadata); |
151 ~UpdateCheckerImpl() override; | 52 ~UpdateCheckerImpl() override; |
152 | 53 |
153 // Overrides for UpdateChecker. | 54 // Overrides for UpdateChecker. |
154 bool CheckForUpdates( | 55 bool CheckForUpdates( |
155 const std::vector<std::string>& ids_checked, | 56 const std::vector<std::string>& ids_checked, |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 | 215 |
315 } // namespace | 216 } // namespace |
316 | 217 |
317 std::unique_ptr<UpdateChecker> UpdateChecker::Create( | 218 std::unique_ptr<UpdateChecker> UpdateChecker::Create( |
318 const scoped_refptr<Configurator>& config, | 219 const scoped_refptr<Configurator>& config, |
319 PersistedData* persistent) { | 220 PersistedData* persistent) { |
320 return base::MakeUnique<UpdateCheckerImpl>(config, persistent); | 221 return base::MakeUnique<UpdateCheckerImpl>(config, persistent); |
321 } | 222 } |
322 | 223 |
323 } // namespace update_client | 224 } // namespace update_client |
OLD | NEW |