Commit 66a5a86c authored by Min Qin's avatar Min Qin Committed by Commit Bot

Add new in-progress classes for corresponding proto messages

Adding new classes to be used by the C++ code.
These classes will be serialized into the proto messages

Bug: 803135
Change-Id: Ib0cae24a8d0cc7ff8fee5a14d6c80070b092b8ff
Reviewed-on: https://chromium-review.googlesource.com/1050797
Commit-Queue: Min Qin <qinmin@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558074}
parent 030eab8a
......@@ -9,8 +9,12 @@ if (is_android) {
source_set("in_progress") {
sources = [
"download_db_entry.cc",
"download_db_entry.h",
"download_entry.cc",
"download_entry.h",
"download_info.cc",
"download_info.h",
"in_progress_cache.h",
"in_progress_cache_impl.cc",
"in_progress_cache_impl.h",
......
// Copyright 2018 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/download_db_entry.h"
namespace download {
DownloadDBEntry::DownloadDBEntry() = default;
DownloadDBEntry::DownloadDBEntry(const DownloadDBEntry& other) = default;
DownloadDBEntry::~DownloadDBEntry() = default;
bool DownloadDBEntry::operator==(const DownloadDBEntry& other) const {
return id == other.id && download_info == other.download_info;
}
} // namespace download
// Copyright 2018 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.
#ifndef COMPONENTS_DOWNLOAD_DOWNLOADER_IN_PROGRESS_DOWNLOAD_DB_ENTRY_H_
#define COMPONENTS_DOWNLOAD_DOWNLOADER_IN_PROGRESS_DOWNLOAD_DB_ENTRY_H_
#include <string>
#include "base/optional.h"
#include "components/download/downloader/in_progress/download_info.h"
namespace download {
// Representing one entry in the DownloadDB.
struct DownloadDBEntry {
public:
DownloadDBEntry();
DownloadDBEntry(const DownloadDBEntry& other);
~DownloadDBEntry();
bool operator==(const DownloadDBEntry& other) const;
// ID of the entry, this should be namespace + GUID of the download.
std::string id;
// Information about a regular download.
base::Optional<DownloadInfo> download_info;
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_DOWNLOADER_IN_PROGRESS_DOWNLOAD_DB_ENTRY_H_
// Copyright 2018 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/download_info.h"
namespace download {
DownloadInfo::DownloadInfo() = default;
DownloadInfo::DownloadInfo(const DownloadInfo& other) = default;
DownloadInfo::~DownloadInfo() = default;
bool DownloadInfo::operator==(const DownloadInfo& other) const {
return guid == other.guid && ukm_info == other.ukm_info &&
in_progress_info == other.in_progress_info;
}
} // namespace download
// Copyright 2018 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.
#ifndef COMPONENTS_DOWNLOAD_DOWNLOADER_IN_PROGRESS_DOWNLOAD_INFO_H_
#define COMPONENTS_DOWNLOAD_DOWNLOADER_IN_PROGRESS_DOWNLOAD_INFO_H_
#include <string>
#include "base/optional.h"
#include "components/download/downloader/in_progress/in_progress_info.h"
#include "components/download/downloader/in_progress/ukm_info.h"
namespace download {
// Contains needed information to reconstruct a download item.
struct DownloadInfo {
public:
DownloadInfo();
DownloadInfo(const DownloadInfo& other);
~DownloadInfo();
bool operator==(const DownloadInfo& other) const;
// Download GUID.
std::string guid;
// UKM information for reporting.
base::Optional<UkmInfo> ukm_info;
// In progress information for active download.
base::Optional<InProgressInfo> in_progress_info;
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_DOWNLOADER_IN_PROGRESS_DOWNLOAD_INFO_H_
......@@ -237,4 +237,53 @@ metadata_pb::UkmInfo InProgressConversions::UkmInfoToProto(
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
......@@ -6,7 +6,9 @@
#define COMPONENTS_DOWNLOAD_IN_PROGRESS_IN_PROGRESS_CONVERSIONS_H_
#include "base/macros.h"
#include "components/download/downloader/in_progress/download_db_entry.h"
#include "components/download/downloader/in_progress/download_entry.h"
#include "components/download/downloader/in_progress/download_info.h"
#include "components/download/downloader/in_progress/in_progress_info.h"
#include "components/download/downloader/in_progress/proto/download_entry.pb.h"
#include "components/download/downloader/in_progress/proto/download_source.pb.h"
......@@ -49,6 +51,18 @@ class InProgressConversions {
static metadata_pb::UkmInfo UkmInfoToProto(const UkmInfo& ukm_info);
static UkmInfo UkmInfoFromProto(const metadata_pb::UkmInfo& proto);
static metadata_pb::DownloadInfo DownloadInfoToProto(
const DownloadInfo& download_info);
static DownloadInfo DownloadInfoFromProto(
const metadata_pb::DownloadInfo& proto);
static metadata_pb::DownloadDBEntry DownloadDBEntryToProto(
const DownloadDBEntry& entry);
static DownloadDBEntry DownloadDBEntryFromProto(
const metadata_pb::DownloadDBEntry& proto);
};
} // namespace download
......
......@@ -9,6 +9,48 @@
namespace download {
namespace {
InProgressInfo CreateInProgressInfo() {
InProgressInfo info;
// InProgressInfo with valid fields.
info.current_path = base::FilePath(FILE_PATH_LITERAL("/tmp.crdownload"));
info.target_path = base::FilePath(FILE_PATH_LITERAL("/tmp"));
info.url_chain.emplace_back("http://foo");
info.url_chain.emplace_back("http://foo2");
info.end_time = base::Time::NowFromSystemTime().LocalMidnight();
info.etag = "A";
info.last_modified = "Wed, 1 Oct 2018 07:00:00 GMT";
info.received_bytes = 1000;
info.total_bytes = 10000;
info.state = DownloadItem::IN_PROGRESS;
info.danger_type = DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
info.interrupt_reason = DOWNLOAD_INTERRUPT_REASON_NONE;
info.transient = false;
info.paused = false;
info.hash = "abcdefg";
info.metered = true;
info.received_slices.emplace_back(0, 500, false);
info.received_slices.emplace_back(5000, 500, false);
info.request_origin = "request origin";
info.bytes_wasted = 1234;
info.fetch_error_body = true;
info.request_headers.emplace_back(
std::make_pair<std::string, std::string>("123", "456"));
info.request_headers.emplace_back(
std::make_pair<std::string, std::string>("ABC", "def"));
return info;
}
DownloadInfo CreateDownloadInfo() {
DownloadInfo info;
info.in_progress_info = CreateInProgressInfo();
info.ukm_info = UkmInfo(DownloadSource::FROM_RENDERER, 100);
return info;
}
} // namespace
class InProgressConversionsTest : public testing::Test,
public InProgressConversions {
public:
......@@ -87,31 +129,7 @@ TEST_F(InProgressConversionsTest, InProgressInfo) {
EXPECT_EQ(info, InProgressInfoFromProto(InProgressInfoToProto(info)));
// InProgressInfo with valid fields.
info.current_path = base::FilePath(FILE_PATH_LITERAL("/tmp.crdownload"));
info.target_path = base::FilePath(FILE_PATH_LITERAL("/tmp"));
info.url_chain.emplace_back("http://foo");
info.url_chain.emplace_back("http://foo2");
info.end_time = base::Time::NowFromSystemTime().LocalMidnight();
info.etag = "A";
info.last_modified = "Wed, 1 Oct 2018 07:00:00 GMT";
info.received_bytes = 1000;
info.total_bytes = 10000;
info.state = DownloadItem::IN_PROGRESS;
info.danger_type = DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
info.interrupt_reason = DOWNLOAD_INTERRUPT_REASON_NONE;
info.transient = false;
info.paused = false;
info.hash = "abcdefg";
info.metered = true;
info.received_slices.emplace_back(0, 500, false);
info.received_slices.emplace_back(5000, 500, false);
info.request_origin = "request origin";
info.bytes_wasted = 1234;
info.fetch_error_body = true;
info.request_headers.emplace_back(
std::make_pair<std::string, std::string>("123", "456"));
info.request_headers.emplace_back(
std::make_pair<std::string, std::string>("ABC", "def"));
info = CreateInProgressInfo();
EXPECT_EQ(info, InProgressInfoFromProto(InProgressInfoToProto(info)));
}
......@@ -119,4 +137,22 @@ TEST_F(InProgressConversionsTest, UkmInfo) {
UkmInfo info(DownloadSource::FROM_RENDERER, 100);
EXPECT_EQ(info, UkmInfoFromProto(UkmInfoToProto(info)));
}
TEST_F(InProgressConversionsTest, DownloadInfo) {
DownloadInfo info;
EXPECT_EQ(info, DownloadInfoFromProto(DownloadInfoToProto(info)));
info = CreateDownloadInfo();
EXPECT_EQ(info, DownloadInfoFromProto(DownloadInfoToProto(info)));
}
TEST_F(InProgressConversionsTest, DownloadDBEntry) {
DownloadDBEntry entry;
EXPECT_EQ(entry, DownloadDBEntryFromProto(DownloadDBEntryToProto(entry)));
entry.id = "abc";
entry.download_info = CreateDownloadInfo();
EXPECT_EQ(entry, DownloadDBEntryFromProto(DownloadDBEntryToProto(entry)));
}
} // namespace download
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment