summaryrefslogtreecommitdiff
path: root/chromium/components/download/downloader/in_progress/in_progress_conversions.cc
blob: 5620961d2c6f383075fd8385f8861235390665fb (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/download/downloader/in_progress/in_progress_conversions.h"

#include <utility>
#include "base/logging.h"
#include "base/pickle.h"

namespace download {

DownloadEntry InProgressConversions::DownloadEntryFromProto(
    const metadata_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;
}

metadata_pb::DownloadEntry InProgressConversions::DownloadEntryToProto(
    const DownloadEntry& entry) {
  metadata_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 InProgressConversions::DownloadSourceFromProto(
    metadata_pb::DownloadSource download_source) {
  switch (download_source) {
    case metadata_pb::DownloadSource::UNKNOWN:
      return DownloadSource::UNKNOWN;
    case metadata_pb::DownloadSource::NAVIGATION:
      return DownloadSource::NAVIGATION;
    case metadata_pb::DownloadSource::DRAG_AND_DROP:
      return DownloadSource::DRAG_AND_DROP;
    case metadata_pb::DownloadSource::FROM_RENDERER:
      return DownloadSource::FROM_RENDERER;
    case metadata_pb::DownloadSource::EXTENSION_API:
      return DownloadSource::EXTENSION_API;
    case metadata_pb::DownloadSource::EXTENSION_INSTALLER:
      return DownloadSource::EXTENSION_INSTALLER;
    case metadata_pb::DownloadSource::INTERNAL_API:
      return DownloadSource::INTERNAL_API;
    case metadata_pb::DownloadSource::WEB_CONTENTS_API:
      return DownloadSource::WEB_CONTENTS_API;
    case metadata_pb::DownloadSource::OFFLINE_PAGE:
      return DownloadSource::OFFLINE_PAGE;
    case metadata_pb::DownloadSource::CONTEXT_MENU:
      return DownloadSource::CONTEXT_MENU;
  }
  NOTREACHED();
  return DownloadSource::UNKNOWN;
}

// static
metadata_pb::DownloadSource InProgressConversions::DownloadSourceToProto(
    DownloadSource download_source) {
  switch (download_source) {
    case DownloadSource::UNKNOWN:
      return metadata_pb::DownloadSource::UNKNOWN;
    case DownloadSource::NAVIGATION:
      return metadata_pb::DownloadSource::NAVIGATION;
    case DownloadSource::DRAG_AND_DROP:
      return metadata_pb::DownloadSource::DRAG_AND_DROP;
    case DownloadSource::FROM_RENDERER:
      return metadata_pb::DownloadSource::FROM_RENDERER;
    case DownloadSource::EXTENSION_API:
      return metadata_pb::DownloadSource::EXTENSION_API;
    case DownloadSource::EXTENSION_INSTALLER:
      return metadata_pb::DownloadSource::EXTENSION_INSTALLER;
    case DownloadSource::INTERNAL_API:
      return metadata_pb::DownloadSource::INTERNAL_API;
    case DownloadSource::WEB_CONTENTS_API:
      return metadata_pb::DownloadSource::WEB_CONTENTS_API;
    case DownloadSource::OFFLINE_PAGE:
      return metadata_pb::DownloadSource::OFFLINE_PAGE;
    case DownloadSource::CONTEXT_MENU:
      return metadata_pb::DownloadSource::CONTEXT_MENU;
  }
  NOTREACHED();
  return metadata_pb::DownloadSource::UNKNOWN;
}

std::vector<DownloadEntry> InProgressConversions::DownloadEntriesFromProto(
    const metadata_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;
}

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

// static
metadata_pb::HttpRequestHeader InProgressConversions::HttpRequestHeaderToProto(
    const std::pair<std::string, std::string>& header) {
  metadata_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>
InProgressConversions::HttpRequestHeaderFromProto(
    const metadata_pb::HttpRequestHeader& proto) {
  if (proto.key().empty())
    return std::pair<std::string, std::string>();

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

// static
metadata_pb::InProgressInfo InProgressConversions::InProgressInfoToProto(
    const InProgressInfo& in_progress_info) {
  metadata_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_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_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_end_time(
      in_progress_info.end_time.ToDeltaSinceWindowsEpoch().InMilliseconds());
  for (size_t i = 0; i < in_progress_info.received_slices.size(); ++i) {
    metadata_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_request_origin(in_progress_info.request_origin);
  proto.set_bytes_wasted(in_progress_info.bytes_wasted);
  return proto;
}

// static
InProgressInfo InProgressConversions::InProgressInfoFromProto(
    const metadata_pb::InProgressInfo& proto) {
  InProgressInfo info;
  for (const auto& url : proto.url_chain())
    info.url_chain.emplace_back(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.total_bytes = proto.total_bytes();
  base::PickleIterator current_path(
      base::Pickle(proto.current_path().data(), proto.current_path().size()));
  info.current_path.ReadFromPickle(&current_path);
  base::PickleIterator target_path(
      base::Pickle(proto.target_path().data(), proto.target_path().size()));
  info.target_path.ReadFromPickle(&target_path);
  info.received_bytes = proto.received_bytes();
  info.end_time = base::Time::FromDeltaSinceWindowsEpoch(
      base::TimeDelta::FromMilliseconds(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.request_origin = proto.request_origin();
  info.bytes_wasted = proto.bytes_wasted();
  return info;
}

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

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

DownloadInfo InProgressConversions::DownloadInfoFromProto(
    const metadata_pb::DownloadInfo& proto) {
  DownloadInfo info;
  info.guid = proto.guid();
  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;
}

metadata_pb::DownloadInfo InProgressConversions::DownloadInfoToProto(
    const DownloadInfo& info) {
  metadata_pb::DownloadInfo proto;
  proto.set_guid(info.guid);
  if (info.ukm_info.has_value()) {
    auto ukm_info = std::make_unique<metadata_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<metadata_pb::InProgressInfo>(
        InProgressInfoToProto(info.in_progress_info.value()));
    proto.set_allocated_in_progress_info(in_progress_info.release());
  }
  return proto;
}

DownloadDBEntry InProgressConversions::DownloadDBEntryFromProto(
    const metadata_pb::DownloadDBEntry& proto) {
  DownloadDBEntry entry;
  entry.id = proto.id();
  if (proto.has_download_info())
    entry.download_info = DownloadInfoFromProto(proto.download_info());
  return entry;
}

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

}  // namespace download