Make castv2 performance test work.
[chromium-blink-merge.git] / components / domain_reliability / monitor.h
blob6fd0bfbff39c880e8e49aa479360d71459b63138
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_
8 #include <map>
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/context_manager.h"
20 #include "components/domain_reliability/dispatcher.h"
21 #include "components/domain_reliability/domain_reliability_export.h"
22 #include "components/domain_reliability/scheduler.h"
23 #include "components/domain_reliability/uploader.h"
24 #include "components/domain_reliability/util.h"
25 #include "net/base/load_timing_info.h"
26 #include "net/base/network_change_notifier.h"
27 #include "net/http/http_response_info.h"
28 #include "net/url_request/url_request_status.h"
30 namespace base {
31 class ThreadChecker;
32 class Value;
33 } // namespace base
35 namespace net {
36 class URLRequest;
37 class URLRequestContext;
38 class URLRequestContextGetter;
39 } // namespace net
41 namespace domain_reliability {
43 // The top-level object that measures requests and hands off the measurements
44 // to the proper |DomainReliabilityContext|.
45 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor
46 : public net::NetworkChangeNotifier::NetworkChangeObserver,
47 DomainReliabilityContext::Factory {
48 public:
49 // Creates a Monitor. |local_state_pref_service| must live on |pref_thread|
50 // (which should be the current thread); |network_thread| is the thread
51 // on which requests will actually be monitored and reported.
52 DomainReliabilityMonitor(
53 const std::string& upload_reporter_string,
54 const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
55 const scoped_refptr<base::SingleThreadTaskRunner>& network_thread);
57 // Same, but specifies a mock interface for time functions for testing.
58 DomainReliabilityMonitor(
59 const std::string& upload_reporter_string,
60 const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
61 const scoped_refptr<base::SingleThreadTaskRunner>& network_thread,
62 scoped_ptr<MockableTime> time);
64 // Must be called from the pref thread if |MoveToNetworkThread| was not
65 // called, or from the network thread if it was called.
66 ~DomainReliabilityMonitor() override;
68 // Must be called before |InitURLRequestContext| on the same thread on which
69 // the Monitor was constructed. Moves (most of) the Monitor to the network
70 // thread passed in the constructor.
71 void MoveToNetworkThread();
73 // All public methods below this point must be called on the network thread
74 // after |MoveToNetworkThread| is called on the pref thread.
76 // Initializes the Monitor's URLRequestContextGetter.
78 // Must be called on the network thread, after |MoveToNetworkThread|.
79 void InitURLRequestContext(net::URLRequestContext* url_request_context);
81 // Same, but for unittests where the Getter is readily available.
82 void InitURLRequestContext(
83 const scoped_refptr<net::URLRequestContextGetter>&
84 url_request_context_getter);
86 // Populates the monitor with contexts that were configured at compile time.
87 void AddBakedInConfigs();
89 // Sets whether the uploader will discard uploads. Must be called after
90 // |InitURLRequestContext|.
91 void SetDiscardUploads(bool discard_uploads);
93 // Should be called when |request| is about to follow a redirect. Will
94 // examine and possibly log the redirect request. Must be called after
95 // |SetDiscardUploads|.
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. Must be called after
101 // |SetDiscardUploads|.
102 void OnCompleted(net::URLRequest* request, bool started);
104 // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
105 void OnNetworkChanged(
106 net::NetworkChangeNotifier::ConnectionType type) override;
108 // Called to remove browsing data. With CLEAR_BEACONS, leaves contexts in
109 // place but clears beacons (which betray browsing history); with
110 // CLEAR_CONTEXTS, removes all contexts (which can behave as cookies).
111 void ClearBrowsingData(DomainReliabilityClearMode mode);
113 // Gets a Value containing data that can be formatted into a web page for
114 // debugging purposes.
115 scoped_ptr<base::Value> GetWebUIData() const;
117 DomainReliabilityContext* AddContextForTesting(
118 scoped_ptr<const DomainReliabilityConfig> config);
120 size_t contexts_size_for_testing() const {
121 return context_manager_.contexts_size_for_testing();
124 // DomainReliabilityContext::Factory implementation:
125 scoped_ptr<DomainReliabilityContext> CreateContextForConfig(
126 scoped_ptr<const DomainReliabilityConfig> config) override;
128 private:
129 friend class DomainReliabilityMonitorTest;
130 // Allow the Service to call |MakeWeakPtr|.
131 friend class DomainReliabilityServiceImpl;
133 typedef std::map<std::string, DomainReliabilityContext*> ContextMap;
135 struct DOMAIN_RELIABILITY_EXPORT RequestInfo {
136 RequestInfo();
137 explicit RequestInfo(const net::URLRequest& request);
138 ~RequestInfo();
140 bool AccessedNetwork() const;
142 GURL url;
143 net::URLRequestStatus status;
144 net::HttpResponseInfo response_info;
145 int load_flags;
146 net::LoadTimingInfo load_timing_info;
147 bool is_upload;
150 void OnRequestLegComplete(const RequestInfo& info);
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 DomainReliabilityContextManager context_manager_;
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_