File: download_db_conversions.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (373 lines) | stat: -rw-r--r-- 14,707 bytes parent folder | download | duplicates (6)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/download/database/download_db_conversions.h"

#include <utility>

#include "base/containers/span.h"
#include "base/notreached.h"
#include "base/pickle.h"
#include "base/time/time.h"
#include "components/download/public/common/download_features.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"

namespace download {
namespace {

// Converts base::Time to a timpstamp in milliseconds.
int64_t FromTimeToMilliseconds(base::Time time) {
  return time.ToDeltaSinceWindowsEpoch().InMilliseconds();
}

// Converts a time stamp in milliseconds to base::Time.
base::Time FromMillisecondsToTime(int64_t time_ms) {
  return base::Time::FromDeltaSinceWindowsEpoch(base::Milliseconds(time_ms));
}

}  // namespace

DownloadEntry DownloadDBConversions::DownloadEntryFromProto(
    const download_pb::DownloadEntry& proto) {
  DownloadEntry entry;
  entry.guid = proto.guid();
  entry.request_origin = proto.request_origin();
  entry.download_source = DownloadSourceFromProto(proto.download_source());
  entry.ukm_download_id = proto.ukm_download_id();
  entry.bytes_wasted = proto.bytes_wasted();
  entry.fetch_error_body = proto.fetch_error_body();
  for (const auto& header : proto.request_headers()) {
    entry.request_headers.emplace_back(HttpRequestHeaderFromProto(header));
  }
  return entry;
}

download_pb::DownloadEntry DownloadDBConversions::DownloadEntryToProto(
    const DownloadEntry& entry) {
  download_pb::DownloadEntry proto;
  proto.set_guid(entry.guid);
  proto.set_request_origin(entry.request_origin);
  proto.set_download_source(DownloadSourceToProto(entry.download_source));
  proto.set_ukm_download_id(entry.ukm_download_id);
  proto.set_bytes_wasted(entry.bytes_wasted);
  proto.set_fetch_error_body(entry.fetch_error_body);
  for (const auto& header : entry.request_headers) {
    auto* proto_header = proto.add_request_headers();
    *proto_header = HttpRequestHeaderToProto(header);
  }
  return proto;
}

// static
DownloadSource DownloadDBConversions::DownloadSourceFromProto(
    download_pb::DownloadSource download_source) {
  switch (download_source) {
    case download_pb::DownloadSource::UNKNOWN:
      return DownloadSource::UNKNOWN;
    case download_pb::DownloadSource::NAVIGATION:
      return DownloadSource::NAVIGATION;
    case download_pb::DownloadSource::DRAG_AND_DROP:
      return DownloadSource::DRAG_AND_DROP;
    case download_pb::DownloadSource::FROM_RENDERER:
      return DownloadSource::FROM_RENDERER;
    case download_pb::DownloadSource::EXTENSION_API:
      return DownloadSource::EXTENSION_API;
    case download_pb::DownloadSource::EXTENSION_INSTALLER:
      return DownloadSource::EXTENSION_INSTALLER;
    case download_pb::DownloadSource::INTERNAL_API:
      return DownloadSource::INTERNAL_API;
    case download_pb::DownloadSource::WEB_CONTENTS_API:
      return DownloadSource::WEB_CONTENTS_API;
    case download_pb::DownloadSource::OFFLINE_PAGE:
      return DownloadSource::OFFLINE_PAGE;
    case download_pb::DownloadSource::CONTEXT_MENU:
      return DownloadSource::CONTEXT_MENU;
    case download_pb::DownloadSource::RETRY:
      return DownloadSource::RETRY;
    case download_pb::DownloadSource::RETRY_FROM_BUBBLE:
      return DownloadSource::RETRY_FROM_BUBBLE;
    case download_pb::DownloadSource::TOOLBAR_MENU:
      return DownloadSource::TOOLBAR_MENU;
  }
  NOTREACHED();
}

// static
download_pb::DownloadSource DownloadDBConversions::DownloadSourceToProto(
    DownloadSource download_source) {
  switch (download_source) {
    case DownloadSource::UNKNOWN:
      return download_pb::DownloadSource::UNKNOWN;
    case DownloadSource::NAVIGATION:
      return download_pb::DownloadSource::NAVIGATION;
    case DownloadSource::DRAG_AND_DROP:
      return download_pb::DownloadSource::DRAG_AND_DROP;
    case DownloadSource::FROM_RENDERER:
      return download_pb::DownloadSource::FROM_RENDERER;
    case DownloadSource::EXTENSION_API:
      return download_pb::DownloadSource::EXTENSION_API;
    case DownloadSource::EXTENSION_INSTALLER:
      return download_pb::DownloadSource::EXTENSION_INSTALLER;
    case DownloadSource::INTERNAL_API:
      return download_pb::DownloadSource::INTERNAL_API;
    case DownloadSource::WEB_CONTENTS_API:
      return download_pb::DownloadSource::WEB_CONTENTS_API;
    case DownloadSource::OFFLINE_PAGE:
      return download_pb::DownloadSource::OFFLINE_PAGE;
    case DownloadSource::CONTEXT_MENU:
      return download_pb::DownloadSource::CONTEXT_MENU;
    case DownloadSource::RETRY:
      return download_pb::DownloadSource::RETRY;
    case DownloadSource::RETRY_FROM_BUBBLE:
      return download_pb::DownloadSource::RETRY_FROM_BUBBLE;
    case DownloadSource::TOOLBAR_MENU:
      return download_pb::DownloadSource::TOOLBAR_MENU;
  }
  NOTREACHED();
}

std::vector<DownloadEntry> DownloadDBConversions::DownloadEntriesFromProto(
    const download_pb::DownloadEntries& proto) {
  std::vector<DownloadEntry> entries;
  for (int i = 0; i < proto.entries_size(); i++)
    entries.push_back(DownloadEntryFromProto(proto.entries(i)));
  return entries;
}

download_pb::DownloadEntries DownloadDBConversions::DownloadEntriesToProto(
    const std::vector<DownloadEntry>& entries) {
  download_pb::DownloadEntries proto;
  for (size_t i = 0; i < entries.size(); i++) {
    download_pb::DownloadEntry* proto_entry = proto.add_entries();
    *proto_entry = DownloadEntryToProto(entries[i]);
  }
  return proto;
}

// static
download_pb::HttpRequestHeader DownloadDBConversions::HttpRequestHeaderToProto(
    const std::pair<std::string, std::string>& header) {
  download_pb::HttpRequestHeader proto;
  if (header.first.empty())
    return proto;

  proto.set_key(header.first);
  proto.set_value(header.second);
  return proto;
}

// static
std::pair<std::string, std::string>
DownloadDBConversions::HttpRequestHeaderFromProto(
    const download_pb::HttpRequestHeader& proto) {
  if (proto.key().empty())
    return std::pair<std::string, std::string>();

  return std::make_pair(proto.key(), proto.value());
}

// static
download_pb::InProgressInfo DownloadDBConversions::InProgressInfoToProto(
    const InProgressInfo& in_progress_info) {
  download_pb::InProgressInfo proto;
  for (size_t i = 0; i < in_progress_info.url_chain.size(); ++i)
    proto.add_url_chain(in_progress_info.url_chain[i].spec());
  proto.set_referrer_url(in_progress_info.referrer_url.spec());
  proto.set_serialized_embedder_download_data(
      in_progress_info.serialized_embedder_download_data);
  proto.set_tab_url(in_progress_info.tab_url.spec());
  proto.set_tab_referrer_url(in_progress_info.tab_referrer_url.spec());
  proto.set_fetch_error_body(in_progress_info.fetch_error_body);
  for (const auto& header : in_progress_info.request_headers) {
    auto* proto_header = proto.add_request_headers();
    *proto_header = HttpRequestHeaderToProto(header);
  }
  proto.set_etag(in_progress_info.etag);
  proto.set_last_modified(in_progress_info.last_modified);
  proto.set_mime_type(in_progress_info.mime_type);
  proto.set_original_mime_type(in_progress_info.original_mime_type);
  proto.set_total_bytes(in_progress_info.total_bytes);
  base::Pickle current_path;
  in_progress_info.current_path.WriteToPickle(&current_path);
  proto.set_current_path(current_path.data(), current_path.size());
  base::Pickle target_path;
  in_progress_info.target_path.WriteToPickle(&target_path);
  proto.set_target_path(target_path.data(), target_path.size());
  proto.set_received_bytes(in_progress_info.received_bytes);
  proto.set_start_time(
      in_progress_info.start_time.is_null()
          ? -1
          : FromTimeToMilliseconds(in_progress_info.start_time));
  proto.set_end_time(in_progress_info.end_time.is_null()
                         ? -1
                         : FromTimeToMilliseconds(in_progress_info.end_time));
  for (size_t i = 0; i < in_progress_info.received_slices.size(); ++i) {
    download_pb::ReceivedSlice* slice = proto.add_received_slices();
    slice->set_received_bytes(
        in_progress_info.received_slices[i].received_bytes);
    slice->set_offset(in_progress_info.received_slices[i].offset);
    slice->set_finished(in_progress_info.received_slices[i].finished);
  }
  proto.set_hash(in_progress_info.hash);
  proto.set_transient(in_progress_info.transient);
  proto.set_state(in_progress_info.state);
  proto.set_danger_type(in_progress_info.danger_type);
  proto.set_interrupt_reason(in_progress_info.interrupt_reason);
  proto.set_paused(in_progress_info.paused);
  proto.set_metered(in_progress_info.metered);
  proto.set_bytes_wasted(in_progress_info.bytes_wasted);
  proto.set_auto_resume_count(in_progress_info.auto_resume_count);
  proto.set_credentials_mode(
      static_cast<int32_t>(in_progress_info.credentials_mode));
  proto.set_range_request_from(in_progress_info.range_request_from);
  proto.set_range_request_to(in_progress_info.range_request_to);
  return proto;
}

// static
InProgressInfo DownloadDBConversions::InProgressInfoFromProto(
    const download_pb::InProgressInfo& proto) {
  InProgressInfo info;
  for (const auto& url : proto.url_chain())
    info.url_chain.emplace_back(url);
  info.referrer_url = GURL(proto.referrer_url());
  info.serialized_embedder_download_data =
      proto.serialized_embedder_download_data();
  info.tab_url = GURL(proto.tab_url());
  info.tab_referrer_url = GURL(proto.tab_referrer_url());
  info.fetch_error_body = proto.fetch_error_body();
  for (const auto& header : proto.request_headers())
    info.request_headers.emplace_back(HttpRequestHeaderFromProto(header));
  info.etag = proto.etag();
  info.last_modified = proto.last_modified();
  info.mime_type = proto.mime_type();
  info.original_mime_type = proto.original_mime_type();
  info.total_bytes = proto.total_bytes();
  base::Pickle current_path_pickle =
      base::Pickle::WithUnownedBuffer(base::as_byte_span(proto.current_path()));
  base::PickleIterator current_path(current_path_pickle);
  info.current_path.ReadFromPickle(&current_path);
  base::Pickle target_path_pickle =
      base::Pickle::WithUnownedBuffer(base::as_byte_span(proto.target_path()));
  base::PickleIterator target_path(target_path_pickle);
  info.target_path.ReadFromPickle(&target_path);
  info.received_bytes = proto.received_bytes();
  info.start_time = proto.start_time() == -1
                        ? base::Time()
                        : FromMillisecondsToTime(proto.start_time());
  info.end_time = proto.end_time() == -1
                      ? base::Time()
                      : FromMillisecondsToTime(proto.end_time());

  for (int i = 0; i < proto.received_slices_size(); ++i) {
    info.received_slices.emplace_back(proto.received_slices(i).offset(),
                                      proto.received_slices(i).received_bytes(),
                                      proto.received_slices(i).finished());
  }
  info.hash = proto.hash();
  info.transient = proto.transient();
  info.state = static_cast<DownloadItem::DownloadState>(proto.state());
  info.danger_type = static_cast<DownloadDangerType>(proto.danger_type());
  info.interrupt_reason =
      static_cast<DownloadInterruptReason>(proto.interrupt_reason());
  info.paused = proto.paused();
  info.metered = proto.metered();
  info.bytes_wasted = proto.bytes_wasted();
  info.auto_resume_count = proto.auto_resume_count();
  if (proto.has_credentials_mode()) {
    info.credentials_mode = static_cast<::network::mojom::CredentialsMode>(
        proto.credentials_mode());
  }
  if (proto.has_range_request_from())
    info.range_request_from = proto.range_request_from();
  if (proto.has_range_request_to())
    info.range_request_to = proto.range_request_to();

  return info;
}

UkmInfo DownloadDBConversions::UkmInfoFromProto(
    const download_pb::UkmInfo& proto) {
  UkmInfo info;
  info.download_source = DownloadSourceFromProto(proto.download_source());
  info.ukm_download_id = proto.ukm_download_id();
  return info;
}

download_pb::UkmInfo DownloadDBConversions::UkmInfoToProto(
    const UkmInfo& info) {
  download_pb::UkmInfo proto;
  proto.set_download_source(DownloadSourceToProto(info.download_source));
  proto.set_ukm_download_id(info.ukm_download_id);
  return proto;
}

DownloadInfo DownloadDBConversions::DownloadInfoFromProto(
    const download_pb::DownloadInfo& proto) {
  DownloadInfo info;
  info.guid = proto.guid();
  info.id = proto.id();
  if (proto.has_ukm_info())
    info.ukm_info = UkmInfoFromProto(proto.ukm_info());
  if (proto.has_in_progress_info())
    info.in_progress_info = InProgressInfoFromProto(proto.in_progress_info());
  return info;
}

download_pb::DownloadInfo DownloadDBConversions::DownloadInfoToProto(
    const DownloadInfo& info) {
  download_pb::DownloadInfo proto;
  proto.set_guid(info.guid);
  proto.set_id(info.id);
  if (info.ukm_info.has_value()) {
    auto ukm_info = std::make_unique<download_pb::UkmInfo>(
        UkmInfoToProto(info.ukm_info.value()));
    proto.set_allocated_ukm_info(ukm_info.release());
  }
  if (info.in_progress_info.has_value()) {
    auto in_progress_info = std::make_unique<download_pb::InProgressInfo>(
        InProgressInfoToProto(info.in_progress_info.value()));
    proto.set_allocated_in_progress_info(in_progress_info.release());
  }
  return proto;
}

DownloadDBEntry DownloadDBConversions::DownloadDBEntryFromProto(
    const download_pb::DownloadDBEntry& proto) {
  DownloadDBEntry entry;
  if (proto.has_download_info())
    entry.download_info = DownloadInfoFromProto(proto.download_info());
  return entry;
}

download_pb::DownloadDBEntry DownloadDBConversions::DownloadDBEntryToProto(
    const DownloadDBEntry& info) {
  download_pb::DownloadDBEntry proto;
  if (info.download_info.has_value()) {
    auto download_info = std::make_unique<download_pb::DownloadInfo>(
        DownloadInfoToProto(info.download_info.value()));
    proto.set_allocated_download_info(download_info.release());
  }
  return proto;
}

DownloadDBEntry DownloadDBConversions::DownloadDBEntryFromDownloadEntry(
    const DownloadEntry& entry) {
  DownloadDBEntry db_entry;
  DownloadInfo download_info;
  download_info.guid = entry.guid;

  UkmInfo ukm_info(entry.download_source, entry.ukm_download_id);

  InProgressInfo in_progress_info;
  in_progress_info.fetch_error_body = entry.fetch_error_body;
  in_progress_info.request_headers = entry.request_headers;

  download_info.ukm_info = ukm_info;
  download_info.in_progress_info = in_progress_info;
  db_entry.download_info = download_info;
  return db_entry;
}

}  // namespace download