Mac: Fix performance issues with remote CoreAnimation
[chromium-blink-merge.git] / components / domain_reliability / monitor.h
blob60f121abf614b1288b4ec38d1eaaaaf3c4cb0f43
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/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"
29 namespace base {
30 class ThreadChecker;
31 class Value;
32 } // namespace base
34 namespace net {
35 class URLRequest;
36 class URLRequestContext;
37 class URLRequestContextGetter;
38 } // namespace net
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 {
46 public:
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 scoped_refptr<base::SingleThreadTaskRunner> pref_thread,
53 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 scoped_refptr<base::SingleThreadTaskRunner> pref_thread,
59 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 virtual ~DomainReliabilityMonitor();
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 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
83 // Populates the monitor with contexts that were configured at compile time.
84 void AddBakedInConfigs();
86 // Sets whether the uploader will discard uploads. Must be called after
87 // |InitURLRequestContext|.
88 void SetDiscardUploads(bool discard_uploads);
90 // Should be called when |request| is about to follow a redirect. Will
91 // examine and possibly log the redirect request. Must be called after
92 // |SetDiscardUploads|.
93 void OnBeforeRedirect(net::URLRequest* request);
95 // Should be called when |request| is complete. Will examine and possibly
96 // log the (final) request. |started| should be true if the request was
97 // actually started before it was terminated. Must be called after
98 // |SetDiscardUploads|.
99 void OnCompleted(net::URLRequest* request, bool started);
101 // net::NetworkChangeNotifier::NetworkChangeObserver implementation:
102 virtual void OnNetworkChanged(
103 net::NetworkChangeNotifier::ConnectionType type) override;
105 // Called to remove browsing data. With CLEAR_BEACONS, leaves contexts in
106 // place but clears beacons (which betray browsing history); with
107 // CLEAR_CONTEXTS, removes all contexts (which can behave as cookies).
108 void ClearBrowsingData(DomainReliabilityClearMode mode);
110 // Gets a Value containing data that can be formatted into a web page for
111 // debugging purposes.
112 scoped_ptr<base::Value> GetWebUIData() const;
114 DomainReliabilityContext* AddContextForTesting(
115 scoped_ptr<const DomainReliabilityConfig> config);
117 size_t contexts_size_for_testing() const { return contexts_.size(); }
119 private:
120 friend class DomainReliabilityMonitorTest;
121 // Allow the Service to call |MakeWeakPtr|.
122 friend class DomainReliabilityServiceImpl;
124 typedef std::map<std::string, DomainReliabilityContext*> ContextMap;
126 struct DOMAIN_RELIABILITY_EXPORT RequestInfo {
127 RequestInfo();
128 explicit RequestInfo(const net::URLRequest& request);
129 ~RequestInfo();
131 bool AccessedNetwork() const;
133 GURL url;
134 net::URLRequestStatus status;
135 net::HttpResponseInfo response_info;
136 int load_flags;
137 net::LoadTimingInfo load_timing_info;
138 bool is_upload;
141 // Creates a context, adds it to the monitor, and returns a pointer to it.
142 // (The pointer is only valid until the Monitor is destroyed.)
143 DomainReliabilityContext* AddContext(
144 scoped_ptr<const DomainReliabilityConfig> config);
145 // Deletes all contexts from |contexts_| and clears the map.
146 void ClearContexts();
147 void OnRequestLegComplete(const RequestInfo& info);
149 DomainReliabilityContext* GetContextForHost(const std::string& host) const;
151 bool OnPrefThread() const {
152 return pref_task_runner_->BelongsToCurrentThread();
154 bool OnNetworkThread() const {
155 return network_task_runner_->BelongsToCurrentThread();
158 base::WeakPtr<DomainReliabilityMonitor> MakeWeakPtr();
160 scoped_ptr<MockableTime> time_;
161 base::TimeTicks last_network_change_time_;
162 const std::string upload_reporter_string_;
163 DomainReliabilityScheduler::Params scheduler_params_;
164 DomainReliabilityDispatcher dispatcher_;
165 scoped_ptr<DomainReliabilityUploader> uploader_;
166 ContextMap contexts_;
168 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner_;
169 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
171 bool moved_to_network_thread_;
172 bool discard_uploads_set_;
174 base::WeakPtrFactory<DomainReliabilityMonitor> weak_factory_;
176 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityMonitor);
179 } // namespace domain_reliability
181 #endif // COMPONENTS_DOMAIN_RELIABILITY_MONITOR_H_