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 "chrome/browser/extensions/updater/manifest_fetch_data.h"
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"
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;
26 namespace extensions {
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);
39 request_ids_.insert(request_id);
42 ManifestFetchData::~ManifestFetchData() {}
44 // The format for request parameters in update checks is:
46 // ?x=EXT1_INFO&x=EXT2_INFO
48 // where EXT1_INFO and EXT2_INFO are url-encoded strings of the form:
50 // id=EXTENSION_ID&v=VERSION&uc
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.
58 // So for two extensions like:
59 // Extension 1- id:aaaa version:1.1
60 // Extension 2- id:bbbb version:2.0
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
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 if (extension_ids_.find(id) != extension_ids_.end()) {
72 NOTREACHED() << "Duplicate extension id " << id;
76 // Compute the string we'd append onto the full_url_, and see if it fits.
77 std::vector<std::string> parts;
78 parts.push_back("id=" + id);
79 parts.push_back("v=" + version);
80 if (!install_source.empty())
81 parts.push_back("installsource=" + install_source);
82 parts.push_back("uc");
84 if (!update_url_data.empty()) {
85 // Make sure the update_url_data string is escaped before using it so that
86 // there is no chance of overriding the id or v other parameter value
87 // we place into the x= value.
88 parts.push_back("ap=" + net::EscapeQueryParamValue(update_url_data, true));
91 // Append brand code, rollcall and active ping parameters.
92 if (base_url_.DomainIs("google.com")) {
93 #if defined(GOOGLE_CHROME_BUILD)
95 google_brand::GetBrand(&brand);
96 if (!brand.empty() && !google_brand::IsOrganic(brand))
97 parts.push_back("brand=" + brand);
100 std::string ping_value;
101 pings_[id] = PingData(0, 0, false);
104 if (ping_data->rollcall_days == kNeverPinged ||
105 ping_data->rollcall_days > 0) {
106 ping_value += "r=" + base::IntToString(ping_data->rollcall_days);
107 if (ChromeMetricsServiceAccessor::IsMetricsReportingEnabled()) {
108 ping_value += "&e=" + std::string(ping_data->is_enabled ? "1" : "0");
110 pings_[id].rollcall_days = ping_data->rollcall_days;
111 pings_[id].is_enabled = ping_data->is_enabled;
113 if (ping_data->active_days == kNeverPinged ||
114 ping_data->active_days > 0) {
115 if (!ping_value.empty())
117 ping_value += "a=" + base::IntToString(ping_data->active_days);
118 pings_[id].active_days = ping_data->active_days;
121 if (!ping_value.empty())
122 parts.push_back("ping=" + net::EscapeQueryParamValue(ping_value, true));
125 std::string extra = full_url_.has_query() ? "&" : "?";
126 extra += "x=" + net::EscapeQueryParamValue(JoinString(parts, '&'), true);
128 // Check against our max url size, exempting the first extension added.
129 int new_size = full_url_.possibly_invalid_spec().size() + extra.size();
130 if (!extension_ids_.empty() && new_size > kExtensionsManifestMaxURLSize) {
131 UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 1);
134 UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 0);
136 // We have room so go ahead and add the extension.
137 extension_ids_.insert(id);
138 full_url_ = GURL(full_url_.possibly_invalid_spec() + extra);
142 bool ManifestFetchData::Includes(const std::string& extension_id) const {
143 return extension_ids_.find(extension_id) != extension_ids_.end();
146 bool ManifestFetchData::DidPing(const std::string& extension_id,
147 PingType type) const {
148 std::map<std::string, PingData>::const_iterator i = pings_.find(extension_id);
149 if (i == pings_.end())
152 if (type == ROLLCALL)
153 value = i->second.rollcall_days;
154 else if (type == ACTIVE)
155 value = i->second.active_days;
158 return value == kNeverPinged || value > 0;
161 void ManifestFetchData::Merge(const ManifestFetchData& other) {
162 DCHECK(full_url() == other.full_url());
163 request_ids_.insert(other.request_ids_.begin(), other.request_ids_.end());
166 } // namespace extensions