8ed4fe041fba2b78d0e878857fd2982bbc864d1b
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / updater / manifest_fetch_data.cc
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   if (extension_ids_.find(id) != extension_ids_.end()) {
72     NOTREACHED() << "Duplicate extension id " << id;
73     return false;
74   }
75
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");
83
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));
89   }
90
91   // Append brand code, rollcall and active ping parameters.
92   if (base_url_.DomainIs("google.com")) {
93 #if defined(GOOGLE_CHROME_BUILD)
94     std::string brand;
95     google_brand::GetBrand(&brand);
96     if (!brand.empty() && !google_brand::IsOrganic(brand))
97       parts.push_back("brand=" + brand);
98 #endif
99
100     std::string ping_value;
101     pings_[id] = PingData(0, 0, false);
102
103     if (ping_data) {
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");
109         }
110         pings_[id].rollcall_days = ping_data->rollcall_days;
111         pings_[id].is_enabled = ping_data->is_enabled;
112       }
113       if (ping_data->active_days == kNeverPinged ||
114           ping_data->active_days > 0) {
115         if (!ping_value.empty())
116           ping_value += "&";
117         ping_value += "a=" + base::IntToString(ping_data->active_days);
118         pings_[id].active_days = ping_data->active_days;
119       }
120     }
121     if (!ping_value.empty())
122       parts.push_back("ping=" + net::EscapeQueryParamValue(ping_value, true));
123   }
124
125   std::string extra = full_url_.has_query() ? "&" : "?";
126   extra += "x=" + net::EscapeQueryParamValue(JoinString(parts, '&'), true);
127
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);
132     return false;
133   }
134   UMA_HISTOGRAM_PERCENTAGE("Extensions.UpdateCheckHitUrlSizeLimit", 0);
135
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);
139   return true;
140 }
141
142 bool ManifestFetchData::Includes(const std::string& extension_id) const {
143   return extension_ids_.find(extension_id) != extension_ids_.end();
144 }
145
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())
150     return false;
151   int value = 0;
152   if (type == ROLLCALL)
153     value = i->second.rollcall_days;
154   else if (type == ACTIVE)
155     value = i->second.active_days;
156   else
157     NOTREACHED();
158   return value == kNeverPinged || value > 0;
159 }
160
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());
164 }
165
166 }  // namespace extensions