Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / domain_reliability / context.h
blob8082ca173f25a8411159cd3676daffa4bc0ddbba
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_CONTEXT_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
8 #include <deque>
9 #include <vector>
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"
19 class GURL;
21 namespace base {
22 class Value;
25 namespace domain_reliability {
27 class DomainReliabilityDispatcher;
28 class DomainReliabilityUploader;
29 class MockableTime;
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();
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);
49 // Called to clear browsing data, since beacons are like browsing history.
50 void ClearBeacons();
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;
56 void GetQueuedBeaconsForTesting(
57 std::vector<DomainReliabilityBeacon>* beacons_out) const;
59 void GetRequestCountsForTesting(
60 size_t resource_index,
61 uint32* successful_requests_out,
62 uint32* failed_requests_out) const;
64 const DomainReliabilityConfig& config() const { return *config_.get(); }
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;
70 private:
71 class ResourceState;
73 typedef std::deque<DomainReliabilityBeacon> BeaconDeque;
74 typedef ScopedVector<ResourceState> ResourceStateVector;
75 typedef ResourceStateVector::const_iterator ResourceStateIterator;
77 void InitializeResourceStates();
78 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay);
79 void StartUpload();
80 void OnUploadComplete(bool success);
82 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const;
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();
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();
93 void RollbackUpload();
95 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called
96 // when there are too many beacons queued.)
97 void RemoveOldestBeacon();
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_;
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_;
114 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_;
116 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext);
119 } // namespace domain_reliability
121 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_