blob: decf74d1c64ccc86bceb79cb259bddcb47f2fcc1 [file] [log] [blame]
// 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_PREVIEWS_CONTENT_PREVIEWS_USER_DATA_H_
#define COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_USER_DATA_H_
#include <stdint.h>
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/time/time.h"
#include "components/previews/core/previews_experiments.h"
#include "components/previews/core/previews_lite_page_redirect.h"
#include "content/public/common/previews_state.h"
namespace previews {
// A representation of previews information related to a navigation.
// TODO(ryansturm): rename this to remove UserData.
class PreviewsUserData {
public:
explicit PreviewsUserData(uint64_t page_id);
struct ServerLitePageInfo {
std::unique_ptr<ServerLitePageInfo> Clone() {
return std::make_unique<ServerLitePageInfo>(*this);
}
// The start time of the original navigation, that is, the one started by
// the user.
base::TimeTicks original_navigation_start = base::TimeTicks();
// The page id used for this preview.
uint64_t page_id = 0;
// The DRP session key used for this preview.
std::string drp_session_key = std::string();
// The current state of the preview.
ServerLitePageStatus status = ServerLitePageStatus::kUnknown;
};
~PreviewsUserData();
PreviewsUserData(const PreviewsUserData& other);
// A session unique ID related to this navigation.
uint64_t page_id() const { return page_id_; }
// The effective connection type value for the navigation.
net::EffectiveConnectionType navigation_ect() const {
return navigation_ect_;
}
void set_navigation_ect(net::EffectiveConnectionType navigation_ect) {
navigation_ect_ = navigation_ect;
}
// Whether the navigation was redirected from the original URL.
bool is_redirect() const { return is_redirect_; }
void set_is_redirect(bool is_redirect) { is_redirect_ = is_redirect; }
// Returns the data savings inflation percent to use for this navigation
// instead of the default if it is not 0.
int data_savings_inflation_percent() const {
return data_savings_inflation_percent_;
}
// Sets a data savings inflation percent value to use instead of the default
// if there is a committed preview. Note that this is expected to be used for
// specific preview types (such as NoScript) that don't have better data use
// estimation information.
void set_data_savings_inflation_percent(int inflation_percent) {
data_savings_inflation_percent_ = inflation_percent;
}
// Whether a lite page preview was prevented from being shown due to the
// blacklist.
bool black_listed_for_lite_page() const {
return black_listed_for_lite_page_;
}
void set_black_listed_for_lite_page(bool black_listed_for_lite_page) {
black_listed_for_lite_page_ = black_listed_for_lite_page;
}
// Returns whether the Cache-Control:no-transform directive has been
// detected for the request. Should not be called prior to receiving
// a committed response.
bool cache_control_no_transform_directive() const {
return cache_control_no_transform_directive_;
}
// Sets that the page load received the Cache-Control:no-transform
// directive. Expected to be set upon receiving a committed response.
void set_cache_control_no_transform_directive() {
cache_control_no_transform_directive_ = true;
}
// Whether there is a committed previews type.
bool HasCommittedPreviewsType() const {
return committed_previews_type_ != previews::PreviewsType::NONE;
}
// The committed previews type, if any. Otherwise PreviewsType::NONE.
previews::PreviewsType committed_previews_type() const {
return committed_previews_type_;
}
// Sets the committed previews type. Should only be called once.
void SetCommittedPreviewsType(previews::PreviewsType previews_type);
// Sets the committed previews type for testing. Can be called multiple times.
void SetCommittedPreviewsTypeForTesting(previews::PreviewsType previews_type);
bool offline_preview_used() const { return offline_preview_used_; }
// Whether an offline preview is being served.
void set_offline_preview_used(bool offline_preview_used) {
offline_preview_used_ = offline_preview_used;
}
// The PreviewsState that was allowed for the navigation.
content::PreviewsState allowed_previews_state() const {
return allowed_previews_state_;
}
void set_allowed_previews_state(
content::PreviewsState allowed_previews_state) {
allowed_previews_state_ = allowed_previews_state;
}
// The PreviewsState that was committed for the navigation.
content::PreviewsState committed_previews_state() const {
return committed_previews_state_;
}
void set_committed_previews_state(
content::PreviewsState committed_previews_state) {
committed_previews_state_ = committed_previews_state;
}
// Metadata for an attempted or committed Lite Page Redirect preview.
ServerLitePageInfo* server_lite_page_info() {
return server_lite_page_info_.get();
}
void set_server_lite_page_info(std::unique_ptr<ServerLitePageInfo> info) {
server_lite_page_info_ = std::move(info);
}
private:
// A session unique ID related to this navigation.
const uint64_t page_id_;
// The effective connection type at the time of navigation. This is the value
// to compare to the preview's triggering ect threshold.
net::EffectiveConnectionType navigation_ect_ =
net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
// The navigation was redirected from the original URL.
bool is_redirect_ = false;
// A previews data savings inflation percent for the navigation if not 0.
int data_savings_inflation_percent_ = 0;
// Whether the origin provided a no-transform directive.
bool cache_control_no_transform_directive_ = false;
// Whether an offline preview is being served.
bool offline_preview_used_ = false;
// Whether a lite page preview was prevented from being shown due to the
// blacklist.
bool black_listed_for_lite_page_ = false;
// The committed previews type, if any.
previews::PreviewsType committed_previews_type_ = PreviewsType::NONE;
// The PreviewsState that was allowed for the navigation.
content::PreviewsState allowed_previews_state_ =
content::PREVIEWS_UNSPECIFIED;
// The PreviewsState that was committed for the navigation.
content::PreviewsState committed_previews_state_ = content::PREVIEWS_OFF;
// Metadata for an attempted or committed Lite Page Redirect preview. See
// struct comments for more detail.
std::unique_ptr<ServerLitePageInfo> server_lite_page_info_;
DISALLOW_ASSIGN(PreviewsUserData);
};
} // namespace previews
#endif // COMPONENTS_PREVIEWS_CONTENT_PREVIEWS_USER_DATA_H_