Home | History | Annotate | Download | only in updater
      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_brand.h"
     14 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
     15 #include "chrome/browser/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(const std::string& id,
     66                                      const std::string& version,
     67                                      const PingData* ping_data,
     68                                      const std::string& update_url_data,
     69                                      const std::string& install_source) {
     70   if (extension_ids_.find(id) != extension_ids_.end()) {
     71     NOTREACHED() << "Duplicate extension id " << id;
     72     return false;
     73   }
     74 
     75   // Compute the string we'd append onto the full_url_, and see if it fits.
     76   std::vector<std::string> parts;
     77   parts.push_back("id=" + id);
     78   parts.push_back("v=" + version);
     79   if (!install_source.empty())
     80     parts.push_back("installsource=" + install_source);
     81   parts.push_back("uc");
     82 
     83   if (!update_url_data.empty()) {
     84     // Make sure the update_url_data string is escaped before using it so that
     85     // there is no chance of overriding the id or v other parameter value
     86     // we place into the x= value.
     87     parts.push_back("ap=" + net::EscapeQueryParamValue(update_url_data, true));
     88   }
     89 
     90   // Append brand code, rollcall and active ping parameters.
     91   if (base_url_.DomainIs("google.com")) {
     92 #if defined(GOOGLE_CHROME_BUILD)
     93     std::string brand;
     94     google_brand::GetBrand(&brand);
     95     if (!brand.empty() && !google_brand::IsOrganic(brand))
     96       parts.push_back("brand=" + brand);
     97 #endif
     98 
     99     std::string ping_value;
    100     pings_[id] = PingData(0, 0, false);
    101 
    102     if (ping_data) {
    103       if (ping_data->rollcall_days == kNeverPinged ||
    104           ping_data->rollcall_days > 0) {
    105         ping_value += "r=" + base::IntToString(ping_data->rollcall_days);
    106         if (ChromeMetricsServiceAccessor::IsMetricsReportingEnabled()) {
    107           ping_value += "&e=" + std::string(ping_data->is_enabled ? "1" : "0");
    108         }
    109         pings_[id].rollcall_days = ping_data->rollcall_days;
    110         pings_[id].is_enabled = ping_data->is_enabled;
    111       }
    112       if (ping_data->active_days == kNeverPinged ||
    113           ping_data->active_days > 0) {
    114         if (!ping_value.empty())
    115           ping_value += "&";
    116         ping_value += "a=" + base::IntToString(ping_data->active_days);
    117         pings_[id].active_days = ping_data->active_days;
    118       }
    119     }
    120     if (!ping_value.empty())
    121       parts.push_back("ping=" + net::EscapeQueryParamValue(ping_value, true));
    122   }
    123 
    124   std::string extra = full_url_.has_query() ? "&" : "?";
    125   extra += "x=" + net::EscapeQueryParamValue(JoinString(parts, '&'), true);
    126 
    127   // Check against our max url size, exempting the first extension added.
    128   int new_size = full_url_.possibly_invalid_spec().size() + extra.size();
    129   if (!extension_ids_.empty() && new_size > kExtensionsManifestMaxURLSize) {
    130     UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 1);
    131     return false;
    132   }
    133   UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 0);
    134 
    135   // We have room so go ahead and add the extension.
    136   extension_ids_.insert(id);
    137   full_url_ = GURL(full_url_.possibly_invalid_spec() + extra);
    138   return true;
    139 }
    140 
    141 bool ManifestFetchData::Includes(const std::string& extension_id) const {
    142   return extension_ids_.find(extension_id) != extension_ids_.end();
    143 }
    144 
    145 bool ManifestFetchData::DidPing(const std::string& extension_id,
    146                                 PingType type) const {
    147   std::map<std::string, PingData>::const_iterator i = pings_.find(extension_id);
    148   if (i == pings_.end())
    149     return false;
    150   int value = 0;
    151   if (type == ROLLCALL)
    152     value = i->second.rollcall_days;
    153   else if (type == ACTIVE)
    154     value = i->second.active_days;
    155   else
    156     NOTREACHED();
    157   return value == kNeverPinged || value > 0;
    158 }
    159 
    160 void ManifestFetchData::Merge(const ManifestFetchData& other) {
    161   DCHECK(full_url() == other.full_url());
    162   request_ids_.insert(other.request_ids_.begin(), other.request_ids_.end());
    163 }
    164 
    165 }  // namespace extensions
    166