1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_DATA_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_DATA_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "base/macros.h"
14 #include "base/optional.h"
15 #include "base/supports_user_data.h"
16 #include "net/base/network_change_notifier.h"
17 #include "net/nqe/effective_connection_type.h"
18 #include "url/gurl.h"
19 
20 namespace data_reduction_proxy {
21 
22 // DataReductionProxy-related data that can be put into UserData or other
23 // storage vehicles to associate this data with the object that owns it.
24 class DataReductionProxyData : public base::SupportsUserData::Data {
25  public:
26   DataReductionProxyData();
27   ~DataReductionProxyData() override;
28 
29   // Allow copying.
30   DataReductionProxyData(const DataReductionProxyData& other);
31 
32   // Whether the DataReductionProxy was used for this request or navigation.
33   // Also true if the user is the holdback experiment, and the request would
34   // otherwise be eligible to use the proxy.
used_data_reduction_proxy()35   bool used_data_reduction_proxy() const { return used_data_reduction_proxy_; }
set_used_data_reduction_proxy(bool used_data_reduction_proxy)36   void set_used_data_reduction_proxy(bool used_data_reduction_proxy) {
37     used_data_reduction_proxy_ = used_data_reduction_proxy;
38   }
39 
40   // Whether a lite page response was seen for the request or navigation.
lite_page_received()41   bool lite_page_received() const { return lite_page_received_; }
set_lite_page_received(bool lite_page_received)42   void set_lite_page_received(bool lite_page_received) {
43     lite_page_received_ = lite_page_received;
44   }
45 
46   // This response was fetched from cache, but the original request used DRP.
was_cached_data_reduction_proxy_response()47   bool was_cached_data_reduction_proxy_response() const {
48     return was_cached_data_reduction_proxy_response_;
49   }
set_was_cached_data_reduction_proxy_response(bool was_cached_data_reduction_proxy_response)50   void set_was_cached_data_reduction_proxy_response(
51       bool was_cached_data_reduction_proxy_response) {
52     was_cached_data_reduction_proxy_response_ =
53         was_cached_data_reduction_proxy_response;
54   }
55 
56   // The session key used for this request. Only set for main frame requests.
session_key()57   std::string session_key() const { return session_key_; }
set_session_key(const std::string & session_key)58   void set_session_key(const std::string& session_key) {
59     session_key_ = session_key;
60   }
61 
62   // The URL the frame is navigating to. This may change during the navigation
63   // when encountering a server redirect. Only set for main frame requests.
request_url()64   GURL request_url() const { return request_url_; }
set_request_url(const GURL & request_url)65   void set_request_url(const GURL& request_url) { request_url_ = request_url; }
66 
67   // The EffectiveConnectionType after the proxy is resolved. This is set for
68   // main frame requests only.
effective_connection_type()69   net::EffectiveConnectionType effective_connection_type() const {
70     return effective_connection_type_;
71   }
set_effective_connection_type(const net::EffectiveConnectionType & effective_connection_type)72   void set_effective_connection_type(
73       const net::EffectiveConnectionType& effective_connection_type) {
74     effective_connection_type_ = effective_connection_type;
75   }
76 
77   // The connection type (Wifi, 2G, 3G, 4G, None, etc) as reported by the
78   // NetworkChangeNotifier. Only set for main frame requests.
connection_type()79   net::NetworkChangeNotifier::ConnectionType connection_type() const {
80     return connection_type_;
81   }
set_connection_type(const net::NetworkChangeNotifier::ConnectionType connection_type)82   void set_connection_type(
83       const net::NetworkChangeNotifier::ConnectionType connection_type) {
84     connection_type_ = connection_type;
85   }
86 
87   // An identifier that is guaranteed to be unique to each page load during a
88   // data saver session. Only present on main frame requests.
page_id()89   const base::Optional<uint64_t>& page_id() const { return page_id_; }
set_page_id(uint64_t page_id)90   void set_page_id(uint64_t page_id) { page_id_ = page_id; }
91 
92   // Whether the blacklist prevented a preview.
black_listed()93   bool black_listed() const { return black_listed_; }
set_black_listed(bool black_listed)94   void set_black_listed(bool black_listed) { black_listed_ = black_listed; }
95 
96   // Create a brand new instance of DataReductionProxyData that could be used in
97   // a different thread. Several of deep copies may occur per navigation, so
98   // this is inexpensive.
99   std::unique_ptr<DataReductionProxyData> DeepCopy() const;
100 
101  private:
102   // Whether the DataReductionProxy was used for this request or navigation.
103   // Also true if the user is the holdback experiment, and the request would
104   // otherwise be eligible to use the proxy.
105   // Cached responses are not considered to have used DRP.
106   bool used_data_reduction_proxy_;
107 
108   // Whether a proxy-served lite page response was seen for the HTTP request or
109   // navigation.
110   bool lite_page_received_;
111 
112   // Whether the blacklist prevented a preview.
113   bool black_listed_;
114 
115   // This response was fetched from cache, but the original request used DRP.
116   bool was_cached_data_reduction_proxy_response_;
117 
118   // The session key used for this request or navigation.
119   std::string session_key_;
120 
121   // The URL the frame is navigating to. This may change during the navigation
122   // when encountering a server redirect.
123   GURL request_url_;
124 
125   // The EffectiveConnectionType when the request or navigation starts. This is
126   // set for main frame requests only.
127   net::EffectiveConnectionType effective_connection_type_;
128 
129   // The connection type (Wifi, 2G, 3G, 4G, None, etc) as reported by the
130   // NetworkChangeNotifier. Only set for main frame requests.
131   net::NetworkChangeNotifier::ConnectionType connection_type_;
132 
133   // An identifier that is guaranteed to be unique to each page load during a
134   // data saver session. Only present on main frame requests.
135   base::Optional<uint64_t> page_id_;
136 
137   DISALLOW_ASSIGN(DataReductionProxyData);
138 };
139 
140 }  // namespace data_reduction_proxy
141 
142 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_DATA_H_
143