OLD | NEW |
| (Empty) |
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 "chrome/browser/extensions/updater/manifest_fetch_data.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "chrome/browser/google/google_brand.h" | |
14 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | |
15 #include "components/omaha_query_params/omaha_query_params.h" | |
16 #include "net/base/escape.h" | |
17 | |
18 namespace { | |
19 | |
20 // Maximum length of an extension manifest update check url, since it is a GET | |
21 // request. We want to stay under 2K because of proxies, etc. | |
22 const int kExtensionsManifestMaxURLSize = 2000; | |
23 | |
24 } // namespace | |
25 | |
26 namespace extensions { | |
27 | |
28 ManifestFetchData::ManifestFetchData(const GURL& update_url, int request_id) | |
29 : base_url_(update_url), | |
30 full_url_(update_url) { | |
31 std::string query = full_url_.has_query() ? | |
32 full_url_.query() + "&" : std::string(); | |
33 query += omaha_query_params::OmahaQueryParams::Get( | |
34 omaha_query_params::OmahaQueryParams::CRX); | |
35 GURL::Replacements replacements; | |
36 replacements.SetQueryStr(query); | |
37 full_url_ = full_url_.ReplaceComponents(replacements); | |
38 | |
39 request_ids_.insert(request_id); | |
40 } | |
41 | |
42 ManifestFetchData::~ManifestFetchData() {} | |
43 | |
44 // The format for request parameters in update checks is: | |
45 // | |
46 // ?x=EXT1_INFO&x=EXT2_INFO | |
47 // | |
48 // where EXT1_INFO and EXT2_INFO are url-encoded strings of the form: | |
49 // | |
50 // id=EXTENSION_ID&v=VERSION&uc | |
51 // | |
52 // Additionally, we may include the parameter ping=PING_DATA where PING_DATA | |
53 // looks like r=DAYS or a=DAYS for extensions in the Chrome extensions gallery. | |
54 // ('r' refers to 'roll call' ie installation, and 'a' refers to 'active'). | |
55 // These values will each be present at most once every 24 hours, and indicate | |
56 // the number of days since the last time it was present in an update check. | |
57 // | |
58 // So for two extensions like: | |
59 // Extension 1- id:aaaa version:1.1 | |
60 // Extension 2- id:bbbb version:2.0 | |
61 // | |
62 // the full update url would be: | |
63 // http://somehost/path?x=id%3Daaaa%26v%3D1.1%26uc&x=id%3Dbbbb%26v%3D2.0%26uc | |
64 // | |
65 // (Note that '=' is %3D and '&' is %26 when urlencoded.) | |
66 bool ManifestFetchData::AddExtension(const std::string& id, | |
67 const std::string& version, | |
68 const PingData* ping_data, | |
69 const std::string& update_url_data, | |
70 const std::string& install_source, | |
71 bool force_update) { | |
72 if (extension_ids_.find(id) != extension_ids_.end()) { | |
73 NOTREACHED() << "Duplicate extension id " << id; | |
74 return false; | |
75 } | |
76 | |
77 if (force_update) | |
78 forced_updates_.insert(id); | |
79 | |
80 // If we want to force an update, we send 0.0.0.0 as the installed version | |
81 // number. | |
82 const std::string installed_version = force_update ? "0.0.0.0" : version; | |
83 | |
84 // Compute the string we'd append onto the full_url_, and see if it fits. | |
85 std::vector<std::string> parts; | |
86 parts.push_back("id=" + id); | |
87 parts.push_back("v=" + installed_version); | |
88 if (!install_source.empty()) | |
89 parts.push_back("installsource=" + install_source); | |
90 parts.push_back("uc"); | |
91 | |
92 if (!update_url_data.empty()) { | |
93 // Make sure the update_url_data string is escaped before using it so that | |
94 // there is no chance of overriding the id or v other parameter value | |
95 // we place into the x= value. | |
96 parts.push_back("ap=" + net::EscapeQueryParamValue(update_url_data, true)); | |
97 } | |
98 | |
99 // Append brand code, rollcall and active ping parameters. | |
100 if (base_url_.DomainIs("google.com")) { | |
101 #if defined(GOOGLE_CHROME_BUILD) | |
102 std::string brand; | |
103 google_brand::GetBrand(&brand); | |
104 if (!brand.empty() && !google_brand::IsOrganic(brand)) | |
105 parts.push_back("brand=" + brand); | |
106 #endif | |
107 | |
108 std::string ping_value; | |
109 pings_[id] = PingData(0, 0, false); | |
110 | |
111 if (ping_data) { | |
112 if (ping_data->rollcall_days == kNeverPinged || | |
113 ping_data->rollcall_days > 0) { | |
114 ping_value += "r=" + base::IntToString(ping_data->rollcall_days); | |
115 if (ChromeMetricsServiceAccessor::IsMetricsReportingEnabled()) { | |
116 ping_value += "&e=" + std::string(ping_data->is_enabled ? "1" : "0"); | |
117 } | |
118 pings_[id].rollcall_days = ping_data->rollcall_days; | |
119 pings_[id].is_enabled = ping_data->is_enabled; | |
120 } | |
121 if (ping_data->active_days == kNeverPinged || | |
122 ping_data->active_days > 0) { | |
123 if (!ping_value.empty()) | |
124 ping_value += "&"; | |
125 ping_value += "a=" + base::IntToString(ping_data->active_days); | |
126 pings_[id].active_days = ping_data->active_days; | |
127 } | |
128 } | |
129 if (!ping_value.empty()) | |
130 parts.push_back("ping=" + net::EscapeQueryParamValue(ping_value, true)); | |
131 } | |
132 | |
133 std::string extra = full_url_.has_query() ? "&" : "?"; | |
134 extra += "x=" + net::EscapeQueryParamValue(JoinString(parts, '&'), true); | |
135 | |
136 // Check against our max url size, exempting the first extension added. | |
137 int new_size = full_url_.possibly_invalid_spec().size() + extra.size(); | |
138 if (!extension_ids_.empty() && new_size > kExtensionsManifestMaxURLSize) { | |
139 UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 1); | |
140 return false; | |
141 } | |
142 UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 0); | |
143 | |
144 // We have room so go ahead and add the extension. | |
145 extension_ids_.insert(id); | |
146 full_url_ = GURL(full_url_.possibly_invalid_spec() + extra); | |
147 return true; | |
148 } | |
149 | |
150 bool ManifestFetchData::Includes(const std::string& extension_id) const { | |
151 return extension_ids_.find(extension_id) != extension_ids_.end(); | |
152 } | |
153 | |
154 bool ManifestFetchData::DidPing(const std::string& extension_id, | |
155 PingType type) const { | |
156 std::map<std::string, PingData>::const_iterator i = pings_.find(extension_id); | |
157 if (i == pings_.end()) | |
158 return false; | |
159 int value = 0; | |
160 if (type == ROLLCALL) | |
161 value = i->second.rollcall_days; | |
162 else if (type == ACTIVE) | |
163 value = i->second.active_days; | |
164 else | |
165 NOTREACHED(); | |
166 return value == kNeverPinged || value > 0; | |
167 } | |
168 | |
169 void ManifestFetchData::Merge(const ManifestFetchData& other) { | |
170 DCHECK(full_url() == other.full_url()); | |
171 request_ids_.insert(other.request_ids_.begin(), other.request_ids_.end()); | |
172 } | |
173 | |
174 bool ManifestFetchData::DidForceUpdate(const std::string& extension_id) const { | |
175 return forced_updates_.find(extension_id) != forced_updates_.end(); | |
176 } | |
177 | |
178 } // namespace extensions | |
OLD | NEW |