QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / components / domain_reliability / monitor.h
blob5260f9f66a459d0a18e4bf623fd609449e184461
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/socket/connection_attempts.h"
29 #include "net/url_request/url_request_status.h"
31 namespace base {
32 class ThreadChecker;
33 class Value;
34 } // namespace base
36 namespace net {
37 class URLRequest;
38 class URLRequestContext;
39 class URLRequestContextGetter;
40 } // namespace net
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
47 : public net::NetworkChangeNotifier::NetworkChangeObserver,
48 DomainReliabilityContext::Factory {
49 public:
50 // Creates a Monitor. |local_state_pref_service| must live on |pref_thread|
51 // (which should be the current thread); |network_thread| is the thread
52 // on which requests will actually be monitored and reported.
53 DomainReliabilityMonitor(
54 const std::string& upload_reporter_string,
55 const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
56 const scoped_refptr<base::SingleThreadTaskRunner>& network_thread);
58 // Same, but specifies a mock interface for time functions for testing.
59 DomainReliabilityMonitor(
60 const std::string& upload_reporter_string,
61 const scoped_refptr<base::SingleThreadTaskRunner>& pref_thread,
62 const scoped_refptr<base::SingleThreadTaskRunner>& network_thread,
63 scoped_ptr<MockableTime> time);
65 // Must be called from the pref thread if |MoveToNetworkThread| was not
66 // called, or from the network thread if it was called.
67 ~DomainReliabilityMonitor() override;
69 // Must be called before |InitURLRequestContext| on the same thread on which
70 // the Monitor was constructed. Moves (most of) the Monitor to the network
71 // thread passed in the constructor.
72 void MoveToNetworkThread();
74 // All public methods below this point must be called on the network thread
75 // after |MoveToNetworkThread| is called on the pref thread.
77 // Initializes the Monitor's URLRequestContextGetter.
79 // Must be called on the network thread, after |MoveToNetworkThread|.
80 void InitURLRequestContext(net::URLRequestContext* url_request_context);
82 // Same, but for unittests where the Getter is readily available.
83 void InitURLRequestContext(
84 const scoped_refptr<net::URLRequestContextGetter>&
85 url_request_context_getter);
87 // Populates the monitor with contexts that were configured at compile time.
88 void AddBakedInConfigs();
90 // Sets whether the uploader will discard uploads. Must be called after
91 // |InitURLRequestContext|.
92 void SetDiscardUploads(bool discard_uploads);
94 // Should be called when |request| is about to follow a redirect. Will
95 // examine and possibly log the redirect request. Must be called after
96 // |SetDiscardUploads|.
97 void OnBeforeRedirect(net::URLRequest* request);
99 // Should be called when |request| is complete. Will examine and possibly
100 // log the (final) request. |started| should be true if the request was
101 // actually started before it was terminated. Must be called after
102 // |SetDiscardUploads|.
103 void OnCompleted(net::URLRequest* request, bool started);
105 // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
106 void OnNetworkChanged(
107 net::NetworkChangeNotifier::ConnectionType type) override;
109 // Called to remove browsing data. With CLEAR_BEACONS, leaves contexts in
110 // place but clears beacons (which betray browsing history); with
111 // CLEAR_CONTEXTS, removes all contexts (which can behave as cookies).
112 void ClearBrowsingData(DomainReliabilityClearMode mode);
114 // Gets a Value containing data that can be formatted into a web page for
115 // debugging purposes.
116 scoped_ptr<base::Value> GetWebUIData() const;
118 DomainReliabilityContext* AddContextForTesting(
119 scoped_ptr<const DomainReliabilityConfig> config);
121 size_t contexts_size_for_testing() const {
122 return context_manager_.contexts_size_for_testing();
125 // DomainReliabilityContext::Factory implementation:
126 scoped_ptr<DomainReliabilityContext> CreateContextForConfig(
127 scoped_ptr<const DomainReliabilityConfig> config) override;
129 private:
130 friend class DomainReliabilityMonitorTest;
131 // Allow the Service to call |MakeWeakPtr|.
132 friend class DomainReliabilityServiceImpl;
134 typedef std::map<std::string, DomainReliabilityContext*> ContextMap;
136 struct DOMAIN_RELIABILITY_EXPORT RequestInfo {
137 RequestInfo();
138 explicit RequestInfo(const net::URLRequest& request);
139 ~RequestInfo();
141 static bool ShouldReportRequest(const RequestInfo& request);
143 GURL url;
144 net::URLRequestStatus status;
145 net::HttpResponseInfo response_info;
146 int load_flags;
147 net::LoadTimingInfo load_timing_info;
148 net::ConnectionAttempts connection_attempts;
149 bool is_upload;
152 void OnRequestLegComplete(const RequestInfo& info);
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 base::TimeTicks last_network_change_time_;
165 const std::string upload_reporter_string_;
166 DomainReliabilityScheduler::Params scheduler_params_;
167 DomainReliabilityDispatcher dispatcher_;
168 scoped_ptr<DomainReliabilityUploader> uploader_;
169 DomainReliabilityContextManager context_manager_;
171 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner_;
172 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
174 bool moved_to_network_thread_;
175 bool discard_uploads_set_;
177 base::WeakPtrFactory<DomainReliabilityMonitor> weak_factory_;
179 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityMonitor);
182 } // namespace domain_reliability
184 #endif // COMPONENTS_DOMAIN_RELIABILITY_MONITOR_H_