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