Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / updater / manifest_fetch_data.cc
blobf72f32efcbc94f5220166c2469d421757e418474
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.
5 #include "extensions/browser/updater/manifest_fetch_data.h"
7 #include <vector>
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 "base/strings/stringprintf.h"
14 #include "extensions/common/extension.h"
15 #include "net/base/escape.h"
17 namespace extensions {
19 namespace {
21 // Maximum length of an extension manifest update check url, since it is a GET
22 // request. We want to stay under 2K because of proxies, etc.
23 const int kExtensionsManifestMaxURLSize = 2000;
25 void AddEnabledStateToPing(std::string* ping_value,
26 const ManifestFetchData::PingData* ping_data) {
27 *ping_value += "&e=" + std::string(ping_data->is_enabled ? "1" : "0");
28 if (!ping_data->is_enabled) {
29 // Add a dr=<number> param for each bit set in disable reasons.
30 for (int enum_value = 1; enum_value < Extension::DISABLE_REASON_LAST;
31 enum_value <<= 1) {
32 if (ping_data->disable_reasons & enum_value)
33 *ping_value += "&dr=" + base::IntToString(enum_value);
38 } // namespace
40 ManifestFetchData::ManifestFetchData(const GURL& update_url,
41 int request_id,
42 const std::string& brand_code,
43 const std::string& base_query_params,
44 PingMode ping_mode)
45 : base_url_(update_url),
46 full_url_(update_url),
47 brand_code_(brand_code),
48 ping_mode_(ping_mode) {
49 std::string query =
50 full_url_.has_query() ? full_url_.query() + "&" : std::string();
51 query += base_query_params;
52 GURL::Replacements replacements;
53 replacements.SetQueryStr(query);
54 full_url_ = full_url_.ReplaceComponents(replacements);
56 request_ids_.insert(request_id);
59 ManifestFetchData::~ManifestFetchData() {
62 // The format for request parameters in update checks is:
64 // ?x=EXT1_INFO&x=EXT2_INFO
66 // where EXT1_INFO and EXT2_INFO are url-encoded strings of the form:
68 // id=EXTENSION_ID&v=VERSION&uc
70 // Provide ping data with the parameter ping=PING_DATA where PING_DATA
71 // looks like r=DAYS or a=DAYS for extensions in the Chrome extensions gallery.
72 // ('r' refers to 'roll call' ie installation, and 'a' refers to 'active').
73 // These values will each be present at most once every 24 hours, and indicate
74 // the number of days since the last time it was present in an update check.
76 // So for two extensions like:
77 // Extension 1- id:aaaa version:1.1
78 // Extension 2- id:bbbb version:2.0
80 // the full update url would be:
81 // http://somehost/path?x=id%3Daaaa%26v%3D1.1%26uc&x=id%3Dbbbb%26v%3D2.0%26uc
83 // (Note that '=' is %3D and '&' is %26 when urlencoded.)
84 bool ManifestFetchData::AddExtension(const std::string& id,
85 const std::string& version,
86 const PingData* ping_data,
87 const std::string& update_url_data,
88 const std::string& install_source,
89 bool force_update) {
90 if (extension_ids_.find(id) != extension_ids_.end()) {
91 NOTREACHED() << "Duplicate extension id " << id;
92 return false;
95 if (force_update)
96 forced_updates_.insert(id);
98 // If we want to force an update, we send 0.0.0.0 as the installed version
99 // number.
100 const std::string installed_version = force_update ? "0.0.0.0" : version;
102 // Compute the string we'd append onto the full_url_, and see if it fits.
103 std::vector<std::string> parts;
104 parts.push_back("id=" + id);
105 parts.push_back("v=" + installed_version);
106 if (!install_source.empty())
107 parts.push_back("installsource=" + install_source);
108 parts.push_back("uc");
110 if (!update_url_data.empty()) {
111 // Make sure the update_url_data string is escaped before using it so that
112 // there is no chance of overriding the id or v other parameter value
113 // we place into the x= value.
114 parts.push_back("ap=" + net::EscapeQueryParamValue(update_url_data, true));
117 // Append brand code, rollcall and active ping parameters.
118 if (ping_mode_ != NO_PING) {
119 if (!brand_code_.empty())
120 parts.push_back(base::StringPrintf("brand=%s", brand_code_.c_str()));
122 std::string ping_value;
123 pings_[id] = PingData(0, 0, false, 0);
124 if (ping_data) {
125 if (ping_data->rollcall_days == kNeverPinged ||
126 ping_data->rollcall_days > 0) {
127 ping_value += "r=" + base::IntToString(ping_data->rollcall_days);
128 if (ping_mode_ == PING_WITH_ENABLED_STATE)
129 AddEnabledStateToPing(&ping_value, ping_data);
130 pings_[id].rollcall_days = ping_data->rollcall_days;
131 pings_[id].is_enabled = ping_data->is_enabled;
133 if (ping_data->active_days == kNeverPinged ||
134 ping_data->active_days > 0) {
135 if (!ping_value.empty())
136 ping_value += "&";
137 ping_value += "a=" + base::IntToString(ping_data->active_days);
138 pings_[id].active_days = ping_data->active_days;
141 if (!ping_value.empty())
142 parts.push_back("ping=" + net::EscapeQueryParamValue(ping_value, true));
145 std::string extra = full_url_.has_query() ? "&" : "?";
146 extra +=
147 "x=" + net::EscapeQueryParamValue(base::JoinString(parts, "&"), true);
149 // Check against our max url size, exempting the first extension added.
150 int new_size = full_url_.possibly_invalid_spec().size() + extra.size();
151 if (!extension_ids_.empty() && new_size > kExtensionsManifestMaxURLSize) {
152 UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 1);
153 return false;
155 UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 0);
157 // We have room so go ahead and add the extension.
158 extension_ids_.insert(id);
159 full_url_ = GURL(full_url_.possibly_invalid_spec() + extra);
160 return true;
163 bool ManifestFetchData::Includes(const std::string& extension_id) const {
164 return extension_ids_.find(extension_id) != extension_ids_.end();
167 bool ManifestFetchData::DidPing(const std::string& extension_id,
168 PingType type) const {
169 std::map<std::string, PingData>::const_iterator i = pings_.find(extension_id);
170 if (i == pings_.end())
171 return false;
172 int value = 0;
173 if (type == ROLLCALL)
174 value = i->second.rollcall_days;
175 else if (type == ACTIVE)
176 value = i->second.active_days;
177 else
178 NOTREACHED();
179 return value == kNeverPinged || value > 0;
182 void ManifestFetchData::Merge(const ManifestFetchData& other) {
183 DCHECK(full_url() == other.full_url());
184 request_ids_.insert(other.request_ids_.begin(), other.request_ids_.end());
187 bool ManifestFetchData::DidForceUpdate(const std::string& extension_id) const {
188 return forced_updates_.find(extension_id) != forced_updates_.end();
191 } // namespace extensions