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"
25 namespace domain_reliability
{
27 class DomainReliabilityDispatcher
;
28 class DomainReliabilityUploader
;
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
{
35 DomainReliabilityContext(
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.
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
;
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
);
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
89 // Uses the state remembered by |MarkUpload| to remove successfully uploaded
90 // data but keep beacons and request counts added after the upload started.
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_
;
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_