OLD | NEW |
| (Empty) |
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 "extensions/browser/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 "base/strings/stringprintf.h" | |
14 #include "net/base/escape.h" | |
15 | |
16 namespace { | |
17 | |
18 // Maximum length of an extension manifest update check url, since it is a GET | |
19 // request. We want to stay under 2K because of proxies, etc. | |
20 const int kExtensionsManifestMaxURLSize = 2000; | |
21 | |
22 } // namespace | |
23 | |
24 namespace extensions { | |
25 | |
26 ManifestFetchData::ManifestFetchData(const GURL& update_url, | |
27 int request_id, | |
28 const std::string& brand_code, | |
29 const std::string& base_query_params, | |
30 PingMode ping_mode) | |
31 : base_url_(update_url), | |
32 full_url_(update_url), | |
33 brand_code_(brand_code), | |
34 ping_mode_(ping_mode) { | |
35 std::string query = | |
36 full_url_.has_query() ? full_url_.query() + "&" : std::string(); | |
37 query += base_query_params; | |
38 GURL::Replacements replacements; | |
39 replacements.SetQueryStr(query); | |
40 full_url_ = full_url_.ReplaceComponents(replacements); | |
41 | |
42 request_ids_.insert(request_id); | |
43 } | |
44 | |
45 ManifestFetchData::~ManifestFetchData() { | |
46 } | |
47 | |
48 // The format for request parameters in update checks is: | |
49 // | |
50 // ?x=EXT1_INFO&x=EXT2_INFO | |
51 // | |
52 // where EXT1_INFO and EXT2_INFO are url-encoded strings of the form: | |
53 // | |
54 // id=EXTENSION_ID&v=VERSION&uc | |
55 // | |
56 // Provide ping data with the parameter ping=PING_DATA where PING_DATA | |
57 // looks like r=DAYS or a=DAYS for extensions in the Chrome extensions gallery. | |
58 // ('r' refers to 'roll call' ie installation, and 'a' refers to 'active'). | |
59 // These values will each be present at most once every 24 hours, and indicate | |
60 // the number of days since the last time it was present in an update check. | |
61 // | |
62 // So for two extensions like: | |
63 // Extension 1- id:aaaa version:1.1 | |
64 // Extension 2- id:bbbb version:2.0 | |
65 // | |
66 // the full update url would be: | |
67 // http://somehost/path?x=id%3Daaaa%26v%3D1.1%26uc&x=id%3Dbbbb%26v%3D2.0%26uc | |
68 // | |
69 // (Note that '=' is %3D and '&' is %26 when urlencoded.) | |
70 bool ManifestFetchData::AddExtension(const std::string& id, | |
71 const std::string& version, | |
72 const PingData* ping_data, | |
73 const std::string& update_url_data, | |
74 const std::string& install_source) { | |
75 if (extension_ids_.find(id) != extension_ids_.end()) { | |
76 NOTREACHED() << "Duplicate extension id " << id; | |
77 return false; | |
78 } | |
79 | |
80 // Compute the string we'd append onto the full_url_, and see if it fits. | |
81 std::vector<std::string> parts; | |
82 parts.push_back("id=" + id); | |
83 parts.push_back("v=" + version); | |
84 if (!install_source.empty()) | |
85 parts.push_back("installsource=" + install_source); | |
86 parts.push_back("uc"); | |
87 | |
88 if (!update_url_data.empty()) { | |
89 // Make sure the update_url_data string is escaped before using it so that | |
90 // there is no chance of overriding the id or v other parameter value | |
91 // we place into the x= value. | |
92 parts.push_back("ap=" + net::EscapeQueryParamValue(update_url_data, true)); | |
93 } | |
94 | |
95 // Append brand code, rollcall and active ping parameters. | |
96 if (ping_mode_ != NO_PING) { | |
97 if (!brand_code_.empty()) | |
98 parts.push_back(base::StringPrintf("brand=%s", brand_code_.c_str())); | |
99 | |
100 std::string ping_value; | |
101 pings_[id] = PingData(0, 0, false); | |
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 (ping_mode_ == PING_WITH_METRICS) { | |
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 | |
OLD | NEW |