• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "components/domain_reliability/beacon.h"
14 #include "components/domain_reliability/clear_mode.h"
15 #include "components/domain_reliability/config.h"
16 #include "components/domain_reliability/context.h"
17 #include "components/domain_reliability/dispatcher.h"
18 #include "components/domain_reliability/domain_reliability_export.h"
19 #include "components/domain_reliability/scheduler.h"
20 #include "components/domain_reliability/uploader.h"
21 #include "components/domain_reliability/util.h"
22 #include "net/base/load_timing_info.h"
23 #include "net/http/http_response_info.h"
24 #include "net/url_request/url_request_status.h"
25 
26 namespace base {
27 class SingleThreadTaskRunner;
28 class ThreadChecker;
29 }  // namespace base
30 
31 namespace net {
32 class URLRequest;
33 class URLRequestContext;
34 class URLRequestContextGetter;
35 }  // namespace net
36 
37 namespace domain_reliability {
38 
39 // The top-level object that measures requests and hands off the measurements
40 // to the proper |DomainReliabilityContext|.
41 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor {
42  public:
43   explicit DomainReliabilityMonitor(const std::string& upload_reporter_string);
44   DomainReliabilityMonitor(const std::string& upload_reporter_string,
45                            scoped_ptr<MockableTime> time);
46   ~DomainReliabilityMonitor();
47 
48   // Initializes the Monitor.
49   void Init(
50       net::URLRequestContext* url_request_context,
51       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
52 
53   // Same, but for unittests where the Getter is readily available.
54   void Init(
55       scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
56 
57   // Populates the monitor with contexts that were configured at compile time.
58   void AddBakedInConfigs();
59 
60   // Should be called when |request| is about to follow a redirect. Will
61   // examine and possibly log the redirect request.
62   void OnBeforeRedirect(net::URLRequest* request);
63 
64   // Should be called when |request| is complete. Will examine and possibly
65   // log the (final) request. (|started| should be true if the request was
66   // actually started before it was terminated.)
67   void OnCompleted(net::URLRequest* request, bool started);
68 
69   // Called to remove browsing data. With CLEAR_BEACONS, leaves contexts in
70   // place but clears beacons (which betray browsing history); with
71   // CLEAR_CONTEXTS, removes all contexts (which can behave as cookies).
72   void ClearBrowsingData(DomainReliabilityClearMode mode);
73 
74   DomainReliabilityContext* AddContextForTesting(
75       scoped_ptr<const DomainReliabilityConfig> config);
76 
contexts_size_for_testing()77   size_t contexts_size_for_testing() const { return contexts_.size(); }
was_cleared_for_testing()78   bool was_cleared_for_testing() const { return was_cleared_; }
cleared_mode_for_testing()79   DomainReliabilityClearMode cleared_mode_for_testing() const {
80     return cleared_mode_;
81   }
82 
83  private:
84   friend class DomainReliabilityMonitorTest;
85   // Allow the Service to call |MakeWeakPtr|.
86   friend class DomainReliabilityServiceImpl;
87 
88   typedef std::map<std::string, DomainReliabilityContext*> ContextMap;
89 
90   struct DOMAIN_RELIABILITY_EXPORT RequestInfo {
91     RequestInfo();
92     explicit RequestInfo(const net::URLRequest& request);
93     ~RequestInfo();
94 
95     bool AccessedNetwork() const;
96 
97     GURL url;
98     net::URLRequestStatus status;
99     net::HttpResponseInfo response_info;
100     int load_flags;
101     net::LoadTimingInfo load_timing_info;
102     bool is_upload;
103   };
104 
105   // Creates a context, adds it to the monitor, and returns a pointer to it.
106   // (The pointer is only valid until the Monitor is destroyed.)
107   DomainReliabilityContext* AddContext(
108       scoped_ptr<const DomainReliabilityConfig> config);
109   // Deletes all contexts from |contexts_| and clears the map.
110   void ClearContexts();
111   void OnRequestLegComplete(const RequestInfo& info);
112 
113   base::WeakPtr<DomainReliabilityMonitor> MakeWeakPtr();
114 
115   scoped_ptr<base::ThreadChecker> thread_checker_;
116   scoped_ptr<MockableTime> time_;
117   const std::string upload_reporter_string_;
118   DomainReliabilityScheduler::Params scheduler_params_;
119   DomainReliabilityDispatcher dispatcher_;
120   scoped_ptr<DomainReliabilityUploader> uploader_;
121   ContextMap contexts_;
122 
123   bool was_cleared_;
124   DomainReliabilityClearMode cleared_mode_;
125 
126   base::WeakPtrFactory<DomainReliabilityMonitor> weak_factory_;
127 
128   DISALLOW_COPY_AND_ASSIGN(DomainReliabilityMonitor);
129 };
130 
131 }  // namespace domain_reliability
132 
133 #endif  // COMPONENTS_DOMAIN_RELIABILITY_MONITOR_H_
134