1 // Copyright 2014 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.
5 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_USAGE_STATS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_USAGE_STATS_H_
8 #include "base/callback.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/prefs/pref_member.h"
11 #include "base/threading/thread_checker.h"
12 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/base/network_change_notifier.h"
15 #include "net/proxy/proxy_service.h"
16 #include "net/url_request/url_request.h"
18 namespace data_reduction_proxy
{
20 class DataReductionProxyUsageStats
21 : public net::NetworkChangeNotifier::NetworkChangeObserver
{
23 // MessageLoopProxy instance is owned by io_thread. |params| outlives
24 // this class instance.
25 DataReductionProxyUsageStats(DataReductionProxyParams
* params
,
26 base::MessageLoopProxy
* ui_thread_proxy
);
27 virtual ~DataReductionProxyUsageStats();
29 // Sets the callback to be called on the UI thread when the unavailability
30 // status has changed.
31 void set_unavailable_callback(
32 const base::Callback
<void(bool)>& unavailable_callback
) {
33 unavailable_callback_
= unavailable_callback
;
36 // Callback intended to be called from |ChromeNetworkDelegate| when a
37 // request completes. This method is used to gather usage stats.
38 void OnUrlRequestCompleted(const net::URLRequest
* request
, bool started
);
40 // Records the last bypass reason to |bypass_type_| and sets
41 // |triggering_request_| to true. A triggering request is the first request to
42 // cause the current bypass.
43 void SetBypassType(net::ProxyService::DataReductionProxyBypassType type
);
45 // Given the |content_length| and associated |request|, records the
46 // number of bypassed bytes for that |request| into UMAs based on bypass type.
47 // |data_reduction_proxy_enabled| tells us the state of the
48 // kDataReductionProxyEnabled preference.
49 void RecordBypassedBytesHistograms(
50 net::URLRequest
& request
,
51 const BooleanPrefMember
& data_reduction_proxy_enabled
);
54 enum BypassedBytesType
{
55 NOT_BYPASSED
= 0, /* Not bypassed. */
56 SSL
, /* Bypass due to SSL. */
57 LOCAL_BYPASS_RULES
, /* Bypass due to client-side bypass rules. */
58 AUDIO_VIDEO
, /* Audio/Video bypass. */
59 TRIGGERING_REQUEST
, /* Triggering request bypass. */
60 NETWORK_ERROR
, /* Network error. */
61 BYPASSED_BYTES_TYPE_MAX
/* This must always be last.*/
64 // NetworkChangeNotifier::NetworkChangeObserver:
65 virtual void OnNetworkChanged(
66 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
;
68 // Counts requests that went through the data reduction proxy and counts
69 // requests that were eligible to go through the proxy.
70 void IncrementRequestCounts(bool actual
);
71 void ClearRequestCounts();
73 // Checks if the availability status of the data reduction proxy has changed,
74 // and notifies the UIThread via NotifyUnavailabilityOnUIThread if so. The
75 // data reduction proxy is considered unavailable if and only if no requests
76 // went through the proxy but some eligible requests were service by other
78 void MaybeNotifyUnavailability();
79 void NotifyUnavailabilityOnUIThread(bool unavailable
);
81 DataReductionProxyParams
* data_reduction_proxy_params_
;
82 // The last reason for bypass as determined by
83 // MaybeBypassProxyAndPrepareToRetry
84 net::ProxyService::DataReductionProxyBypassType last_bypass_type_
;
85 // True if the last request triggered the current bypass.
86 bool triggering_request_
;
87 base::MessageLoopProxy
* ui_thread_proxy_
;
89 // The following 2 fields are used to determine if data reduction proxy is
90 // unreachable. We keep a count of requests which should go through
91 // data request proxy, as well as those which actually do. The proxy is
92 // unreachable if no successful requests are made through it despite a
93 // non-zero number of requests being eligible.
95 // Count of requests which will be tried to be sent through data reduction
96 // proxy. The count is only based on the config and not the bad proxy list.
97 // Explicit bypasses are not part of this count. This is the desired behavior
98 // since otherwise both counts would be identical.
99 unsigned long eligible_num_requests_through_proxy_
;
101 // Count of successful requests through data reduction proxy.
102 unsigned long actual_num_requests_through_proxy_
;
104 // Whether or not the data reduction proxy is unavailable.
107 base::ThreadChecker thread_checker_
;
109 void RecordBypassedBytes(
110 net::ProxyService::DataReductionProxyBypassType bypass_type
,
111 BypassedBytesType bypassed_bytes_type
,
112 int64 content_length
);
114 // Called when the unavailability status has changed. Runs on the UI thread.
115 base::Callback
<void(bool)> unavailable_callback_
;
117 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyUsageStats
);
120 } // namespace data_reduction_proxy
122 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_USAGE_STATS_H_