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