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/single_thread_task_runner.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/base/network_change_notifier.h"
26 #include "net/http/http_response_info.h"
27 #include "net/url_request/url_request_status.h"
36 class URLRequestContext
;
37 class URLRequestContextGetter
;
40 namespace domain_reliability
{
42 // The top-level object that measures requests and hands off the measurements
43 // to the proper |DomainReliabilityContext|.
44 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor
45 : public net::NetworkChangeNotifier::NetworkChangeObserver
{
47 // Creates a Monitor. |local_state_pref_service| must live on |pref_thread|
48 // (which should be the current thread); |network_thread| is the thread
49 // on which requests will actually be monitored and reported.
50 DomainReliabilityMonitor(
51 const std::string
& upload_reporter_string
,
52 const scoped_refptr
<base::SingleThreadTaskRunner
>& pref_thread
,
53 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_thread
);
55 // Same, but specifies a mock interface for time functions for testing.
56 DomainReliabilityMonitor(
57 const std::string
& upload_reporter_string
,
58 const scoped_refptr
<base::SingleThreadTaskRunner
>& pref_thread
,
59 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_thread
,
60 scoped_ptr
<MockableTime
> time
);
62 // Must be called from the pref thread if |MoveToNetworkThread| was not
63 // called, or from the network thread if it was called.
64 ~DomainReliabilityMonitor() override
;
66 // Must be called before |InitURLRequestContext| on the same thread on which
67 // the Monitor was constructed. Moves (most of) the Monitor to the network
68 // thread passed in the constructor.
69 void MoveToNetworkThread();
71 // All public methods below this point must be called on the network thread
72 // after |MoveToNetworkThread| is called on the pref thread.
74 // Initializes the Monitor's URLRequestContextGetter.
76 // Must be called on the network thread, after |MoveToNetworkThread|.
77 void InitURLRequestContext(net::URLRequestContext
* url_request_context
);
79 // Same, but for unittests where the Getter is readily available.
80 void InitURLRequestContext(
81 const scoped_refptr
<net::URLRequestContextGetter
>&
82 url_request_context_getter
);
84 // Populates the monitor with contexts that were configured at compile time.
85 void AddBakedInConfigs();
87 // Sets whether the uploader will discard uploads. Must be called after
88 // |InitURLRequestContext|.
89 void SetDiscardUploads(bool discard_uploads
);
91 // Should be called when |request| is about to follow a redirect. Will
92 // examine and possibly log the redirect request. Must be called after
93 // |SetDiscardUploads|.
94 void OnBeforeRedirect(net::URLRequest
* request
);
96 // Should be called when |request| is complete. Will examine and possibly
97 // log the (final) request. |started| should be true if the request was
98 // actually started before it was terminated. Must be called after
99 // |SetDiscardUploads|.
100 void OnCompleted(net::URLRequest
* request
, bool started
);
102 // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
103 void OnNetworkChanged(
104 net::NetworkChangeNotifier::ConnectionType type
) override
;
106 // Called to remove browsing data. With CLEAR_BEACONS, leaves contexts in
107 // place but clears beacons (which betray browsing history); with
108 // CLEAR_CONTEXTS, removes all contexts (which can behave as cookies).
109 void ClearBrowsingData(DomainReliabilityClearMode mode
);
111 // Gets a Value containing data that can be formatted into a web page for
112 // debugging purposes.
113 scoped_ptr
<base::Value
> GetWebUIData() const;
115 DomainReliabilityContext
* AddContextForTesting(
116 scoped_ptr
<const DomainReliabilityConfig
> config
);
118 size_t contexts_size_for_testing() const { return contexts_
.size(); }
121 friend class DomainReliabilityMonitorTest
;
122 // Allow the Service to call |MakeWeakPtr|.
123 friend class DomainReliabilityServiceImpl
;
125 typedef std::map
<std::string
, DomainReliabilityContext
*> ContextMap
;
127 struct DOMAIN_RELIABILITY_EXPORT RequestInfo
{
129 explicit RequestInfo(const net::URLRequest
& request
);
132 bool AccessedNetwork() const;
135 net::URLRequestStatus status
;
136 net::HttpResponseInfo response_info
;
138 net::LoadTimingInfo load_timing_info
;
142 // Creates a context, adds it to the monitor, and returns a pointer to it.
143 // (The pointer is only valid until the Monitor is destroyed.)
144 DomainReliabilityContext
* AddContext(
145 scoped_ptr
<const DomainReliabilityConfig
> config
);
146 // Deletes all contexts from |contexts_| and clears the map.
147 void ClearContexts();
148 void OnRequestLegComplete(const RequestInfo
& info
);
150 DomainReliabilityContext
* GetContextForHost(const std::string
& host
) const;
152 bool OnPrefThread() const {
153 return pref_task_runner_
->BelongsToCurrentThread();
155 bool OnNetworkThread() const {
156 return network_task_runner_
->BelongsToCurrentThread();
159 base::WeakPtr
<DomainReliabilityMonitor
> MakeWeakPtr();
161 scoped_ptr
<MockableTime
> time_
;
162 base::TimeTicks last_network_change_time_
;
163 const std::string upload_reporter_string_
;
164 DomainReliabilityScheduler::Params scheduler_params_
;
165 DomainReliabilityDispatcher dispatcher_
;
166 scoped_ptr
<DomainReliabilityUploader
> uploader_
;
167 ContextMap contexts_
;
169 scoped_refptr
<base::SingleThreadTaskRunner
> pref_task_runner_
;
170 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
172 bool moved_to_network_thread_
;
173 bool discard_uploads_set_
;
175 base::WeakPtrFactory
<DomainReliabilityMonitor
> weak_factory_
;
177 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityMonitor
);
180 } // namespace domain_reliability
182 #endif // COMPONENTS_DOMAIN_RELIABILITY_MONITOR_H_