• 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_CONTEXT_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
7 
8 #include <deque>
9 #include <vector>
10 
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "components/domain_reliability/beacon.h"
15 #include "components/domain_reliability/config.h"
16 #include "components/domain_reliability/domain_reliability_export.h"
17 #include "components/domain_reliability/scheduler.h"
18 
19 class GURL;
20 
21 namespace base {
22 class Value;
23 }
24 
25 namespace domain_reliability {
26 
27 class DomainReliabilityDispatcher;
28 class DomainReliabilityUploader;
29 class MockableTime;
30 
31 // The per-domain context for the Domain Reliability client; includes the
32 // domain's config and per-resource beacon queues.
33 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext {
34  public:
35   DomainReliabilityContext(
36       MockableTime* time,
37       const DomainReliabilityScheduler::Params& scheduler_params,
38       const std::string& upload_reporter_string,
39       DomainReliabilityDispatcher* dispatcher,
40       DomainReliabilityUploader* uploader,
41       scoped_ptr<const DomainReliabilityConfig> config);
42   ~DomainReliabilityContext();
43 
44   // Notifies the context of a beacon on its domain(s); may or may not save the
45   // actual beacon to be uploaded, depending on the sample rates in the config,
46   // but will increment one of the request counters in any case.
47   void OnBeacon(const GURL& url, const DomainReliabilityBeacon& beacon);
48 
49   // Called to clear browsing data, since beacons are like browsing history.
50   void ClearBeacons();
51 
52   // Gets a Value containing data that can be formatted into a web page for
53   // debugging purposes.
54   scoped_ptr<base::Value> GetWebUIData() const;
55 
56   void GetQueuedBeaconsForTesting(
57       std::vector<DomainReliabilityBeacon>* beacons_out) const;
58 
59   void GetRequestCountsForTesting(
60       size_t resource_index,
61       uint32* successful_requests_out,
62       uint32* failed_requests_out) const;
63 
config()64   const DomainReliabilityConfig& config() const { return *config_.get(); }
65 
66   // Maximum number of beacons queued per context; if more than this many are
67   // queued; the oldest beacons will be removed.
68   static const size_t kMaxQueuedBeacons;
69 
70  private:
71   class ResourceState;
72 
73   typedef std::deque<DomainReliabilityBeacon> BeaconDeque;
74   typedef ScopedVector<ResourceState> ResourceStateVector;
75   typedef ResourceStateVector::const_iterator ResourceStateIterator;
76 
77   void InitializeResourceStates();
78   void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay);
79   void StartUpload();
80   void OnUploadComplete(bool success);
81 
82   scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const;
83 
84   // Remembers the current state of the context when an upload starts. Can be
85   // called multiple times in a row (without |CommitUpload|) if uploads fail
86   // and are retried.
87   void MarkUpload();
88 
89   // Uses the state remembered by |MarkUpload| to remove successfully uploaded
90   // data but keep beacons and request counts added after the upload started.
91   void CommitUpload();
92 
93   void RollbackUpload();
94 
95   // Finds and removes the oldest beacon. DCHECKs if there is none. (Called
96   // when there are too many beacons queued.)
97   void RemoveOldestBeacon();
98 
99   scoped_ptr<const DomainReliabilityConfig> config_;
100   MockableTime* time_;
101   const std::string& upload_reporter_string_;
102   DomainReliabilityScheduler scheduler_;
103   DomainReliabilityDispatcher* dispatcher_;
104   DomainReliabilityUploader* uploader_;
105 
106   BeaconDeque beacons_;
107   size_t uploading_beacons_size_;
108   // Each ResourceState in |states_| corresponds to the Resource of the same
109   // index in the config.
110   ResourceStateVector states_;
111   base::TimeTicks upload_time_;
112   base::TimeTicks last_upload_time_;
113 
114   base::WeakPtrFactory<DomainReliabilityContext> weak_factory_;
115 
116   DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext);
117 };
118 
119 }  // namespace domain_reliability
120 
121 #endif  // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
122