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