Commit e01ab251 authored by carlosk's avatar carlosk Committed by Commit bot

Add base persistence interfaces for Prefetching Offline Pages.

BUG=701939

Review-Url: https://codereview.chromium.org/2872933003
Cr-Commit-Position: refs/heads/master@{#472166}
parent b6e599fe
......@@ -11,6 +11,8 @@ static_library("prefetch") {
sources = [
"generate_page_bundle_request.cc",
"generate_page_bundle_request.h",
"prefetch_item.cc",
"prefetch_item.h",
"prefetch_request_fetcher.cc",
"prefetch_request_fetcher.h",
"prefetch_service.h",
......@@ -48,6 +50,7 @@ source_set("unit_tests") {
testonly = true
sources = [
"generate_page_bundle_request_unittest.cc",
"prefetch_item_unittest.cc",
"prefetch_request_fetcher_unittest.cc",
"prefetch_request_test_base.cc",
"prefetch_request_test_base.h",
......
// 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/offline_pages/core/prefetch/prefetch_item.h"
namespace offline_pages {
PrefetchItem::PrefetchItem() = default;
PrefetchItem::PrefetchItem(const PrefetchItem& other) = default;
PrefetchItem::~PrefetchItem(){};
bool PrefetchItem::operator==(const PrefetchItem& other) const {
return guid == other.guid && client_name_space == other.client_name_space &&
client_id == other.client_id && state == other.state &&
url == other.url && final_archived_url == other.final_archived_url &&
request_archive_attempt_count == other.request_archive_attempt_count &&
operation_name == other.operation_name &&
archive_body_name == other.archive_body_name &&
archive_body_length == other.archive_body_length &&
creation_time == other.creation_time &&
freshness_time == other.freshness_time &&
error_code == other.error_code;
}
bool PrefetchItem::operator!=(const PrefetchItem& other) const {
return !(*this == other);
}
} // namespace offline_pages
// 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.
#ifndef COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_ITEM_H_
#define COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_ITEM_H_
#include <string>
#include "base/time/time.h"
#include "components/offline_pages/core/prefetch/prefetch_types.h"
#include "url/gurl.h"
namespace offline_pages {
// Data object representing an item progressing through the prefetching process
// from the moment a URL is requested by a client until its processing is done,
// successfully or not.
// Instances of this class are in-memory representations of items in (or to be
// inserted into) the persistent prefetching data store.
struct PrefetchItem {
PrefetchItem();
explicit PrefetchItem(const PrefetchItem& other);
~PrefetchItem();
bool operator==(const PrefetchItem& other) const;
bool operator!=(const PrefetchItem& other) const;
// Primary key/ID for this prefetch item (See |base::GenerateGUID()|). This
// value will be reused when communicating with other systems accepting GUID
// identifiers for operations linked to this item.
std::string guid;
// Externally provided namespace and id values so that this item can be
// uniquely identified by the client requesting it. It is the client's
// responsibility to make sure the id is unique within the context of its
// assigned namespace.
std::string client_name_space;
std::string client_id;
// Current prefetching progress state.
PrefetchItemState state = PrefetchItemState::NEW_REQUEST;
// The URL of the page the client requested to be prefetched.
GURL url;
// The final URL whose page was actually included in a successfully created
// archive after redirects, if it was different than the |url|. It will be
// left empty if they are the same.
GURL final_archived_url;
// Number of times an attempt was made to generate an archive for this item.
int request_archive_attempt_count = 0;
// Name used to identify the archiving operation being executed by the
// prefetching service for processing this item's URL. It is used as the
// |operation_name| reported by an incoming GCM message and in the
// |GetOperationRequest.name| field of the respective GetOperation RPC.
std::string operation_name;
// The name specific to this item's archive that can be used to build the URL
// to allow the downloading of that archive. Will only be set when and if an
// archive was successfully created for this item. It will be kept empty
// otherwise.
std::string archive_body_name;
// The final size of the generated archive that contains this item's page
// snapshot. The archive might also include other articles in a bundle so the
// length is not necessarily directly related to this item's page contents.
// Will only be set when and if an archive was successfully created for this
// item. It holds a negative value otherwise.
int64_t archive_body_length = -1;
// Time when this item was inserted into the store with the URL to be
// prefetched.
base::Time creation_time;
// Time used for the expiration of the item depending on the applicable policy
// for its current state. It is initially set with the same value as
// |creation_time|. Its value is "refreshed" to the current time on some state
// transitions considered significant for the prefetching process.
base::Time freshness_time;
// The reason why the item was set to the FINISHED state. Should be
// disregarded until reaching that state.
PrefetchItemErrorCode error_code = PrefetchItemErrorCode::SUCCESS;
};
} // namespace offline_pages
#endif // COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_ITEM_H_
// 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/offline_pages/core/prefetch/prefetch_item.h"
#include "base/time/time.h"
#include "components/offline_pages/core/prefetch/prefetch_types.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace offline_pages {
TEST(PrefetchItemTest, OperatorEqualsAndCopyConstructor) {
PrefetchItem item1;
EXPECT_EQ(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1.guid = "A";
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.client_name_space = "B";
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.client_id = "C";
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.state = PrefetchItemState::AWAITING_GCM;
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.url = GURL("http://test.com");
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.final_archived_url = GURL("http://test.com/final");
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.request_archive_attempt_count = 10;
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.operation_name = "D";
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.archive_body_name = "E";
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.archive_body_length = 20;
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.creation_time = base::Time::FromJavaTime(1000L);
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.freshness_time = base::Time::FromJavaTime(2000L);
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
item1 = PrefetchItem();
item1.error_code = PrefetchItemErrorCode::EXPIRED;
EXPECT_NE(item1, PrefetchItem());
EXPECT_EQ(item1, PrefetchItem(item1));
}
} // namespace offline_pages
......@@ -59,6 +59,46 @@ struct RenderPageInfo {
base::Time render_time;
};
// List of states a prefetch item can be at during its progress through the
// prefetching process. They follow somewhat the order below, but some states
// might be skipped.
enum class PrefetchItemState {
// New request just received from the client.
NEW_REQUEST,
// The item has been included in a GeneratePageBundle RPC requesting the
// creation of an archive for its URL.
SENT_GENERATE_PAGE_BUNDLE,
// The archive was not immediately available (cached) upon the request and
// is now waiting for a GCM message notifying of its archiving operation
// completion.
AWAITING_GCM,
// The GCM message notifying of the archiving operation completion was
// received for this item.
RECEIVED_GCM,
// A GetOperation RPC was sent for this item to query for the final results
// of its archiving request.
SENT_GET_OPERATION,
// Information was received about a successfully created archive for this
// item that can now be downloaded.
RECEIVED_BUNDLE,
// This item's archive is currently being downloaded.
DOWNLOADING,
// Item has finished processing, successfully or otherwise, and is waiting to
// be processed for stats reporting to UMA.
FINISHED,
// UMA stats have been reported and the item is being kept just long enough
// to confirm that the same URL is not being repeatedly requested by its
// client.
ZOMBIE,
};
// Error codes used to identify the reason why a prefetch item has finished
// processing.
enum class PrefetchItemErrorCode {
SUCCESS,
EXPIRED,
};
} // namespace offline_pages
#endif // COMPONENTS_OFFLINE_PAGES_CORE_PREFETCH_PREFETCH_TYPES_H_
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