File: manifest_fetch_data.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (308 lines) | stat: -rw-r--r-- 11,333 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/updater/manifest_fetch_data.h"

#include <iterator>
#include <tuple>
#include <vector>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/notreached.h"
#include "base/strings/escape.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/updater/extension_downloader_types.h"
#include "extensions/common/extension_id.h"

using extensions::mojom::ManifestLocation;

namespace extensions {

namespace {

// Maximum length of an extension manifest update check url, since it is a GET
// request. We want to stay under 2K because of proxies, etc.
const int kExtensionsManifestMaxURLSize = 2000;

// Strings to report the manifest location in Omaha update pings. Please use
// strings with no capitalization, spaces or underscores.
const char kInternalLocation[] = "internal";
const char kExternalLocation[] = "external";
const char kPolicyLocation[] = "policy";
const char kOtherLocation[] = "other";
const char kInvalidLocation[] = "invalid";

void AddEnabledStateToPing(std::string* ping_value,
                           const DownloadPingData* ping_data) {
  *ping_value += "&e=" + std::string(ping_data->is_enabled ? "1" : "0");
  if (!ping_data->is_enabled) {
    // Add a dr=<number> param for each bit set in disable reasons.
    for (int reason : ping_data->disable_reasons) {
      if (reason != disable_reason::DISABLE_UNKNOWN) {
        // Only append valid and known disable reasons.
        *ping_value += "&dr=" + base::NumberToString(reason);
      }
    }
  }
}

}  // namespace

ManifestFetchData::ExtensionData::ExtensionData() = default;

ManifestFetchData::ExtensionData::ExtensionData(const ExtensionData& other) =
    default;

ManifestFetchData::ExtensionData::ExtensionData(
    const base::Version& version,
    const std::string& update_url_data,
    const std::string& install_source,
    ManifestLocation extension_location)
    : version(version),
      update_url_data(update_url_data),
      install_source(install_source),
      extension_location(extension_location) {}

ManifestFetchData::ExtensionData::~ExtensionData() = default;

// static
std::string ManifestFetchData::GetSimpleLocationString(ManifestLocation loc) {
  std::string result = kInvalidLocation;
  switch (loc) {
    case ManifestLocation::kInternal:
      result = kInternalLocation;
      break;
    case ManifestLocation::kExternalPref:
    case ManifestLocation::kExternalPrefDownload:
    case ManifestLocation::kExternalRegistry:
      result = kExternalLocation;
      break;
    case ManifestLocation::kComponent:
    case ManifestLocation::kExternalComponent:
    case ManifestLocation::kUnpacked:
    case ManifestLocation::kCommandLine:
      result = kOtherLocation;
      break;
    case ManifestLocation::kExternalPolicyDownload:
    case ManifestLocation::kExternalPolicy:
      result = kPolicyLocation;
      break;
    case ManifestLocation::kInvalidLocation:
      NOTREACHED();
  }

  return result;
}

ManifestFetchData::ManifestFetchData(const GURL& update_url,
                                     int request_id,
                                     const std::string& brand_code,
                                     const std::string& base_query_params,
                                     PingMode ping_mode,
                                     DownloadFetchPriority fetch_priority)
    : base_url_(update_url),
      full_url_(update_url),
      brand_code_(brand_code),
      ping_mode_(ping_mode),
      fetch_priority_(fetch_priority) {
  UpdateFullUrl(base_query_params);
  request_ids_.insert(request_id);
}

ManifestFetchData::~ManifestFetchData() = default;

// The format for request parameters in update checks is:
//
//   ?x=EXT1_INFO&x=EXT2_INFO
//
// where EXT1_INFO and EXT2_INFO are url-encoded strings of the form:
//
//   id=EXTENSION_ID&v=VERSION&uc
//
// Provide ping data with the parameter ping=PING_DATA where PING_DATA
// looks like r=DAYS or a=DAYS for extensions in the Chrome extensions gallery.
// ('r' refers to 'roll call' ie installation, and 'a' refers to 'active').
// These values will each be present at most once every 24 hours, and indicate
// the number of days since the last time it was present in an update check.
//
// So for two extensions like:
//   Extension 1- id:aaaa version:1.1
//   Extension 2- id:bbbb version:2.0
//
// the full update url would be:
//   http://somehost/path?x=id%3Daaaa%26v%3D1.1%26uc&x=id%3Dbbbb%26v%3D2.0%26uc
//
// (Note that '=' is %3D and '&' is %26 when urlencoded.)
bool ManifestFetchData::AddExtension(const std::string& id,
                                     const std::string& version,
                                     const DownloadPingData* ping_data,
                                     const std::string& update_url_data,
                                     const std::string& install_source,
                                     ManifestLocation extension_location,
                                     DownloadFetchPriority fetch_priority) {
  DCHECK(!is_all_external_policy_download_ ||
         extension_location == ManifestLocation::kExternalPolicyDownload);
  if (base::Contains(extensions_data_, id)) {
    NOTREACHED() << "Duplicate extension id " << id;
  }

  if (fetch_priority_ != DownloadFetchPriority::kForeground) {
    fetch_priority_ = fetch_priority;
  }

  const std::string install_location =
      GetSimpleLocationString(extension_location);

  // Compute the string we'd append onto the full_url_, and see if it fits.
  std::vector<std::string> parts;
  parts.push_back("id=" + id);
  parts.push_back("v=" + version);
  if (!install_source.empty())
    parts.push_back("installsource=" + install_source);
  if (!install_location.empty())
    parts.push_back("installedby=" + install_location);
  parts.push_back("uc");

  if (!update_url_data.empty()) {
    // Make sure the update_url_data string is escaped before using it so that
    // there is no chance of overriding the id or v other parameter value
    // we place into the x= value.
    parts.push_back("ap=" + base::EscapeQueryParamValue(update_url_data, true));
  }

  // Append brand code, rollcall and active ping parameters.
  if (ping_mode_ != NO_PING) {
    if (!brand_code_.empty())
      parts.push_back(base::StringPrintf("brand=%s", brand_code_.c_str()));

    std::string ping_value;
    pings_.emplace(
        std::piecewise_construct, std::forward_as_tuple(id),
        std::forward_as_tuple(/*rollcall=*/0, /*active=*/0,
                              /*enabled=*/false, DisableReasonSet()));

    if (ping_data) {
      if (ping_data->rollcall_days == kNeverPinged ||
          ping_data->rollcall_days > 0) {
        ping_value += "r=" + base::NumberToString(ping_data->rollcall_days);
        if (ping_mode_ == PING_WITH_ENABLED_STATE)
          AddEnabledStateToPing(&ping_value, ping_data);
        pings_[id].rollcall_days = ping_data->rollcall_days;
        pings_[id].is_enabled = ping_data->is_enabled;
      }
      if (ping_data->active_days == kNeverPinged ||
          ping_data->active_days > 0) {
        if (!ping_value.empty())
          ping_value += "&";
        ping_value += "a=" + base::NumberToString(ping_data->active_days);
        pings_[id].active_days = ping_data->active_days;
      }
    }
    if (!ping_value.empty())
      parts.push_back("ping=" + base::EscapeQueryParamValue(ping_value, true));
  }

  std::string extra = full_url_.has_query() ? "&" : "?";
  extra +=
      "x=" + base::EscapeQueryParamValue(base::JoinString(parts, "&"), true);

  // Check against our max url size, exempting the first extension added.
  int new_size = full_url_.possibly_invalid_spec().size() + extra.size();
  if (!extensions_data_.empty() && new_size > kExtensionsManifestMaxURLSize) {
    return false;
  }

  // We have room so go ahead and add the extension.
  extensions_data_[id] = ExtensionData(base::Version(version), update_url_data,
                                       install_source, extension_location);
  full_url_ = GURL(full_url_.possibly_invalid_spec() + extra);
  return true;
}

void ManifestFetchData::AddAssociatedTask(ExtensionDownloaderTask task) {
  associated_tasks_.emplace_back(std::move(task));
}

void ManifestFetchData::UpdateFullUrl(const std::string& base_query_params) {
  std::string query =
      full_url_.has_query() ? full_url_.query() + "&" : std::string();
  query += base_query_params;
  GURL::Replacements replacements;
  replacements.SetQueryStr(query);
  full_url_ = full_url_.ReplaceComponents(replacements);
}

void ManifestFetchData::RemoveExtensions(const ExtensionIdSet& id_to_remove,
                                         const std::string& base_query_params) {
  const std::map<ExtensionId, ExtensionData> extensions_data =
      std::move(extensions_data_);
  extensions_data_.clear();
  full_url_ = base_url_;
  UpdateFullUrl(base_query_params);

  for (const auto& data : extensions_data) {
    const ExtensionId& extension_id = data.first;
    if (id_to_remove.count(extension_id))
      continue;
    const ExtensionData& extension_data = data.second;
    auto it = pings_.find(extension_id);
    const DownloadPingData* optional_ping_data =
        it != pings_.end() ? &(it->second) : nullptr;
    AddExtension(extension_id, extension_data.version.GetString(),
                 optional_ping_data, extension_data.update_url_data,
                 extension_data.install_source,
                 extension_data.extension_location, fetch_priority_);
  }
}

ExtensionIdSet ManifestFetchData::GetExtensionIds() const {
  ExtensionIdSet extension_ids;
  for (const auto& extension_data : extensions_data_)
    extension_ids.insert(extension_data.first);
  return extension_ids;
}

bool ManifestFetchData::Includes(const ExtensionId& extension_id) const {
  return base::Contains(extensions_data_, extension_id);
}

bool ManifestFetchData::DidPing(const ExtensionId& extension_id,
                                PingType type) const {
  auto i = pings_.find(extension_id);
  if (i == pings_.end())
    return false;
  int value = 0;
  if (type == ROLLCALL)
    value = i->second.rollcall_days;
  else if (type == ACTIVE)
    value = i->second.active_days;
  else
    NOTREACHED();
  return value == kNeverPinged || value > 0;
}

void ManifestFetchData::Merge(std::unique_ptr<ManifestFetchData> other) {
  DCHECK(full_url() == other->full_url());
  if (fetch_priority_ != DownloadFetchPriority::kForeground) {
    fetch_priority_ = other->fetch_priority_;
  }
  request_ids_.insert(other->request_ids_.begin(), other->request_ids_.end());
  associated_tasks_.insert(
      associated_tasks_.end(),
      std::make_move_iterator(other->associated_tasks_.begin()),
      std::make_move_iterator(other->associated_tasks_.end()));
}

void ManifestFetchData::set_is_all_external_policy_download() {
  is_all_external_policy_download_ = true;
}

std::vector<ExtensionDownloaderTask> ManifestFetchData::TakeAssociatedTasks() {
  return std::move(associated_tasks_);
}
}  // namespace extensions