da989cd33b4732fe402273b0a080dd3467c17ffb
[platform/framework/web/crosswalk.git] / src / components / domain_reliability / monitor.h
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/time/time.h"
12 #include "components/domain_reliability/beacon.h"
13 #include "components/domain_reliability/config.h"
14 #include "components/domain_reliability/context.h"
15 #include "components/domain_reliability/dispatcher.h"
16 #include "components/domain_reliability/domain_reliability_export.h"
17 #include "components/domain_reliability/scheduler.h"
18 #include "components/domain_reliability/uploader.h"
19 #include "components/domain_reliability/util.h"
20 #include "net/base/host_port_pair.h"
21 #include "net/base/load_timing_info.h"
22 #include "net/url_request/url_request_status.h"
23
24 namespace net {
25 class URLRequest;
26 class URLRequestContext;
27 class URLRequestContextGetter;
28 }
29
30 namespace domain_reliability {
31
32 // The top-level per-profile object that measures requests and hands off the
33 // measurements to the proper |DomainReliabilityContext|.  Referenced by the
34 // |ChromeNetworkDelegate|, which calls the On* methods.
35 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityMonitor {
36  public:
37   // NB: We don't take a URLRequestContextGetter because we already live on the
38   // I/O thread.
39   explicit DomainReliabilityMonitor(
40       net::URLRequestContext* url_request_context);
41   DomainReliabilityMonitor(
42       net::URLRequestContext* url_request_context,
43       scoped_ptr<MockableTime> time);
44   ~DomainReliabilityMonitor();
45
46   // Should be called from the profile's NetworkDelegate on the corresponding
47   // events:
48   void OnBeforeRedirect(net::URLRequest* request);
49   void OnCompleted(net::URLRequest* request, bool started);
50
51   // Creates a context for testing, adds it to the monitor, and returns a
52   // pointer to it. (The pointer is only valid until the MOnitor is destroyed.)
53   DomainReliabilityContext* AddContextForTesting(
54       scoped_ptr<const DomainReliabilityConfig> config);
55
56  private:
57   friend class DomainReliabilityMonitorTest;
58
59   struct DOMAIN_RELIABILITY_EXPORT RequestInfo {
60     RequestInfo();
61     RequestInfo(const net::URLRequest& request);
62     ~RequestInfo();
63
64     GURL url;
65     net::URLRequestStatus status;
66     int response_code;
67     net::HostPortPair socket_address;
68     net::LoadTimingInfo load_timing_info;
69     bool was_cached;
70   };
71
72   void OnRequestLegComplete(const RequestInfo& info);
73
74   scoped_ptr<MockableTime> time_;
75   scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
76   DomainReliabilityScheduler::Params scheduler_params_;
77   DomainReliabilityDispatcher dispatcher_;
78   scoped_ptr<DomainReliabilityUploader> uploader_;
79   std::map<std::string, DomainReliabilityContext*> contexts_;
80
81   DISALLOW_COPY_AND_ASSIGN(DomainReliabilityMonitor);
82 };
83
84 }  // namespace domain_reliability
85
86 #endif  // COMPONENTS_DOMAIN_RELIABILITY_MONITOR_H_