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_DOMAIN_RELIABILITY_MONITOR_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_MONITOR_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/prefs/pref_member.h"
14 #include "base/time/time.h"
15 #include "components/domain_reliability/beacon.h"
16 #include "components/domain_reliability/clear_mode.h"
17 #include "components/domain_reliability/config.h"
18 #include "components/domain_reliability/context.h"
19 #include "components/domain_reliability/dispatcher.h"
20 #include "components/domain_reliability/domain_reliability_export.h"
21 #include "components/domain_reliability/scheduler.h"
22 #include "components/domain_reliability/uploader.h"
23 #include "components/domain_reliability/util.h"
24 #include "net/base/load_timing_info.h"
25 #include "net/http/http_response_info.h"
26 #include "net/url_request/url_request_status.h"
31 class SingleThreadTaskRunner
;
38 class URLRequestContext
;
39 class URLRequestContextGetter
;
42 namespace domain_reliability
{
44 // The top-level object that measures requests and hands off the measurements
45 // to the proper |DomainReliabilityContext|.
46 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor
{
48 // Creates a Monitor. |local_state_pref_service| must live on |pref_thread|
49 // (which should be the current thread); |network_thread| is the thread
50 // on which requests will actually be monitored and reported.
51 DomainReliabilityMonitor(
52 const std::string
& upload_reporter_string
,
53 scoped_refptr
<base::SingleThreadTaskRunner
> pref_thread
,
54 scoped_refptr
<base::SingleThreadTaskRunner
> network_thread
,
55 PrefService
* local_state_pref_service
,
56 const char* reporting_pref_name
);
58 // Same, but specifies a mock interface for time functions for testing.
59 DomainReliabilityMonitor(
60 const std::string
& upload_reporter_string
,
61 scoped_refptr
<base::SingleThreadTaskRunner
> pref_thread
,
62 scoped_refptr
<base::SingleThreadTaskRunner
> network_thread
,
63 PrefService
* local_state_pref_service
,
64 const char* reporting_pref_name
,
65 scoped_ptr
<MockableTime
> time
);
67 // Must be called from the pref thread if |MoveToNetworkThread| was not
68 // called, or from the network thread if it was called.
69 ~DomainReliabilityMonitor();
71 // Must be called before |InitURLRequestContext| on the same thread on which
72 // the Monitor was constructed. Moves (most of) the Monitor to the network
73 // thread passed in the constructor.
74 void MoveToNetworkThread();
76 // Must be called from the pref thread before the Monitor is destructed if
77 // |MoveToNetworkThread| was called.
78 void DestroyReportingPref();
80 // All public methods below this point must be called on the network thread:
82 // Initializes the Monitor's URLRequestContextGetter.
84 // Must be called on the network thread, after |MoveToNetworkThread|.
85 void InitURLRequestContext(net::URLRequestContext
* url_request_context
);
87 // Same, but for unittests where the Getter is readily available.
88 void InitURLRequestContext(
89 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter
);
91 // Populates the monitor with contexts that were configured at compile time.
92 void AddBakedInConfigs();
94 // Should be called when |request| is about to follow a redirect. Will
95 // examine and possibly log the redirect request.
96 void OnBeforeRedirect(net::URLRequest
* request
);
98 // Should be called when |request| is complete. Will examine and possibly
99 // log the (final) request. (|started| should be true if the request was
100 // actually started before it was terminated.)
101 void OnCompleted(net::URLRequest
* request
, bool started
);
103 // Called to remove browsing data. With CLEAR_BEACONS, leaves contexts in
104 // place but clears beacons (which betray browsing history); with
105 // CLEAR_CONTEXTS, removes all contexts (which can behave as cookies).
106 void ClearBrowsingData(DomainReliabilityClearMode mode
);
108 // Gets a Value containing data that can be formatted into a web page for
109 // debugging purposes.
110 scoped_ptr
<base::Value
> GetWebUIData() const;
112 DomainReliabilityContext
* AddContextForTesting(
113 scoped_ptr
<const DomainReliabilityConfig
> config
);
115 size_t contexts_size_for_testing() const { return contexts_
.size(); }
118 friend class DomainReliabilityMonitorTest
;
119 // Allow the Service to call |MakeWeakPtr|.
120 friend class DomainReliabilityServiceImpl
;
122 typedef std::map
<std::string
, DomainReliabilityContext
*> ContextMap
;
124 struct DOMAIN_RELIABILITY_EXPORT RequestInfo
{
126 explicit RequestInfo(const net::URLRequest
& request
);
129 bool AccessedNetwork() const;
132 net::URLRequestStatus status
;
133 net::HttpResponseInfo response_info
;
135 net::LoadTimingInfo load_timing_info
;
139 // Creates a context, adds it to the monitor, and returns a pointer to it.
140 // (The pointer is only valid until the Monitor is destroyed.)
141 DomainReliabilityContext
* AddContext(
142 scoped_ptr
<const DomainReliabilityConfig
> config
);
143 // Deletes all contexts from |contexts_| and clears the map.
144 void ClearContexts();
145 void OnRequestLegComplete(const RequestInfo
& info
);
147 DomainReliabilityContext
* GetContextForHost(const std::string
& host
) const;
149 void InitReportingPref(
150 PrefService
* local_state_pref_service
,
151 const char* reporting_pref_name
);
152 void OnReportingPrefChanged();
154 bool OnPrefThread() const {
155 return pref_task_runner_
->BelongsToCurrentThread();
157 bool OnNetworkThread() const {
158 return network_task_runner_
->BelongsToCurrentThread();
161 base::WeakPtr
<DomainReliabilityMonitor
> MakeWeakPtr();
163 scoped_ptr
<MockableTime
> time_
;
164 const std::string upload_reporter_string_
;
165 DomainReliabilityScheduler::Params scheduler_params_
;
166 DomainReliabilityDispatcher dispatcher_
;
167 scoped_ptr
<DomainReliabilityUploader
> uploader_
;
168 ContextMap contexts_
;
170 scoped_refptr
<base::SingleThreadTaskRunner
> pref_task_runner_
;
171 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
173 BooleanPrefMember reporting_pref_
;
174 bool moved_to_network_thread_
;
176 base::WeakPtrFactory
<DomainReliabilityMonitor
> weak_factory_
;
178 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityMonitor
);
181 } // namespace domain_reliability
183 #endif // COMPONENTS_DOMAIN_RELIABILITY_MONITOR_H_