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_
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 #include "components/domain_reliability/uploader.h"
26 namespace domain_reliability
{
28 class DomainReliabilityDispatcher
;
29 class DomainReliabilityUploader
;
32 // The per-domain context for the Domain Reliability client; includes the
33 // domain's config and per-resource beacon queues.
34 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext
{
36 DomainReliabilityContext(
38 const DomainReliabilityScheduler::Params
& scheduler_params
,
39 const std::string
& upload_reporter_string
,
40 const base::TimeTicks
* last_network_change_time
,
41 DomainReliabilityDispatcher
* dispatcher
,
42 DomainReliabilityUploader
* uploader
,
43 scoped_ptr
<const DomainReliabilityConfig
> config
);
44 ~DomainReliabilityContext();
46 // Notifies the context of a beacon on its domain(s); may or may not save the
47 // actual beacon to be uploaded, depending on the sample rates in the config,
48 // but will increment one of the request counters in any case.
49 void OnBeacon(const GURL
& url
, const DomainReliabilityBeacon
& beacon
);
51 // Called to clear browsing data, since beacons are like browsing history.
54 // Gets a Value containing data that can be formatted into a web page for
55 // debugging purposes.
56 scoped_ptr
<base::Value
> GetWebUIData() const;
58 void GetQueuedBeaconsForTesting(
59 std::vector
<DomainReliabilityBeacon
>* beacons_out
) const;
61 void GetRequestCountsForTesting(
62 size_t resource_index
,
63 uint32
* successful_requests_out
,
64 uint32
* failed_requests_out
) const;
66 const DomainReliabilityConfig
& config() const { return *config_
.get(); }
68 // Maximum number of beacons queued per context; if more than this many are
69 // queued; the oldest beacons will be removed.
70 static const size_t kMaxQueuedBeacons
;
75 typedef std::deque
<DomainReliabilityBeacon
> BeaconDeque
;
76 typedef ScopedVector
<ResourceState
> ResourceStateVector
;
77 typedef ResourceStateVector::const_iterator ResourceStateIterator
;
79 void InitializeResourceStates();
80 void ScheduleUpload(base::TimeDelta min_delay
, base::TimeDelta max_delay
);
82 void OnUploadComplete(const DomainReliabilityUploader::UploadResult
& result
);
84 scoped_ptr
<const base::Value
> CreateReport(base::TimeTicks upload_time
) const;
86 // Remembers the current state of the context when an upload starts. Can be
87 // called multiple times in a row (without |CommitUpload|) if uploads fail
91 // Uses the state remembered by |MarkUpload| to remove successfully uploaded
92 // data but keep beacons and request counts added after the upload started.
95 void RollbackUpload();
97 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called
98 // when there are too many beacons queued.)
99 void RemoveOldestBeacon();
101 scoped_ptr
<const DomainReliabilityConfig
> config_
;
103 const std::string
& upload_reporter_string_
;
104 DomainReliabilityScheduler scheduler_
;
105 DomainReliabilityDispatcher
* dispatcher_
;
106 DomainReliabilityUploader
* uploader_
;
108 BeaconDeque beacons_
;
109 size_t uploading_beacons_size_
;
110 // Each ResourceState in |states_| corresponds to the Resource of the same
111 // index in the config.
112 ResourceStateVector states_
;
113 base::TimeTicks upload_time_
;
114 base::TimeTicks last_upload_time_
;
115 // The last network change time is not tracked per-context, so this is a
116 // pointer to that value in a wider (e.g. per-Monitor or unittest) scope.
117 const base::TimeTicks
* last_network_change_time_
;
119 base::WeakPtrFactory
<DomainReliabilityContext
> weak_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext
);
124 } // namespace domain_reliability
126 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_