Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: components/domain_reliability/context.h

Issue 252613002: Domain Reliability: More security review. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: ;_; Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ 5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ 6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
7 7
8 #include <deque> 8 #include <deque>
9 #include <map> 9 #include <vector>
10 10
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h" 13 #include "base/time/time.h"
15 #include "components/domain_reliability/beacon.h"
16 #include "components/domain_reliability/config.h" 14 #include "components/domain_reliability/config.h"
17 #include "components/domain_reliability/domain_reliability_export.h" 15 #include "components/domain_reliability/domain_reliability_export.h"
18 #include "components/domain_reliability/scheduler.h" 16 #include "components/domain_reliability/scheduler.h"
19 #include "components/domain_reliability/uploader.h" 17 #include "components/domain_reliability/uploader.h"
20 #include "components/domain_reliability/util.h" 18 #include "components/domain_reliability/util.h"
21 19
22 class GURL; 20 class GURL;
23 21
24 namespace domain_reliability { 22 namespace domain_reliability {
25 23
24 struct DomainReliabilityBeacon;
26 class DomainReliabilityDispatcher; 25 class DomainReliabilityDispatcher;
27 class MockableTime; 26 class MockableTime;
28 27
29 // The per-domain context for the Domain Reliability client; includes the 28 // The per-domain context for the Domain Reliability client; includes the
30 // domain's config and per-resource beacon queues. 29 // domain's config and per-resource beacon queues.
31 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext { 30 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext {
32 public: 31 public:
33 DomainReliabilityContext( 32 DomainReliabilityContext(
34 MockableTime* time, 33 MockableTime* time,
35 const DomainReliabilityScheduler::Params& scheduler_params, 34 const DomainReliabilityScheduler::Params& scheduler_params,
35 const std::string& upload_reporter_string,
36 DomainReliabilityDispatcher* dispatcher, 36 DomainReliabilityDispatcher* dispatcher,
37 DomainReliabilityUploader* uploader, 37 DomainReliabilityUploader* uploader,
38 scoped_ptr<const DomainReliabilityConfig> config); 38 scoped_ptr<const DomainReliabilityConfig> config);
39 virtual ~DomainReliabilityContext(); 39 ~DomainReliabilityContext();
40 40
41 void AddBeacon(const DomainReliabilityBeacon& beacon, const GURL& url); 41 // Notifies the context of a beacon on its domain(s); may or may not save the
42 // actual beacon to be uploaded, depending on the sample rates in the config,
43 // but will increment one of the request counters in any case.
44 void OnBeacon(const GURL& url, const DomainReliabilityBeacon& beacon);
42 45
43 void GetQueuedDataForTesting( 46 void GetQueuedDataForTesting(
44 int resource_index, 47 size_t resource_index,
45 std::vector<DomainReliabilityBeacon>* beacons_out, 48 std::vector<DomainReliabilityBeacon>* beacons_out,
46 int* successful_requests_out, 49 uint32* successful_requests_out,
47 int* failed_requests_out) const; 50 uint32* failed_requests_out) const;
48 51
49 const DomainReliabilityConfig& config() { return *config_.get(); } 52 const DomainReliabilityConfig& config() { return *config_.get(); }
50 53
51 // Maximum number of beacons queued per context; if more than this many are 54 // Maximum number of beacons queued per context; if more than this many are
52 // queued; the oldest beacons will be removed. 55 // queued; the oldest beacons will be removed.
53 static const int kMaxQueuedBeacons; 56 static const size_t kMaxQueuedBeacons;
54 57
55 private: 58 private:
56 // Resource-specific state (queued beacons and request counts). 59 class ResourceState;
57 class ResourceState {
58 public:
59 ResourceState(DomainReliabilityContext* context,
60 const DomainReliabilityConfig::Resource* config);
61 ~ResourceState();
62
63 scoped_ptr<base::Value> ToValue(base::TimeTicks upload_time) const;
64
65 // Remembers the current state of the resource data when an upload starts.
66 void MarkUpload();
67
68 // Uses the state remembered by |MarkUpload| to remove successfully uploaded
69 // data but keep beacons and request counts added after the upload started.
70 void CommitUpload();
71
72 // Gets the start time of the oldest beacon, if there are any. Returns true
73 // and sets |oldest_start_out| if so; otherwise, returns false.
74 bool GetOldestBeaconStart(base::TimeTicks* oldest_start_out) const;
75 // Removes the oldest beacon. DCHECKs if there isn't one.
76 void RemoveOldestBeacon();
77
78 DomainReliabilityContext* context;
79 const DomainReliabilityConfig::Resource* config;
80
81 std::deque<DomainReliabilityBeacon> beacons;
82 int successful_requests;
83 int failed_requests;
84
85 // State saved during uploads; if an upload succeeds, these are used to
86 // remove uploaded data from the beacon list and request counters.
87 size_t uploading_beacons_size;
88 int uploading_successful_requests;
89 int uploading_failed_requests;
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(ResourceState);
93 };
94 60
95 typedef ScopedVector<ResourceState> ResourceStateVector; 61 typedef ScopedVector<ResourceState> ResourceStateVector;
96 typedef ResourceStateVector::const_iterator ResourceStateIterator; 62 typedef ResourceStateVector::const_iterator ResourceStateIterator;
97 63
98 void InitializeResourceStates(); 64 void InitializeResourceStates();
99 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay); 65 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay);
100 void StartUpload(); 66 void StartUpload();
101 void OnUploadComplete(bool success); 67 void OnUploadComplete(bool success);
102 68
103 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const; 69 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const;
104 70
105 // Remembers the current state of the context when an upload starts. 71 // Remembers the current state of the context when an upload starts. Can be
72 // called multiple times in a row (without |CommitUpload|) if uploads fail
73 // and are retried.
106 void MarkUpload(); 74 void MarkUpload();
107 75
108 // Uses the state remembered by |MarkUpload| to remove successfully uploaded 76 // Uses the state remembered by |MarkUpload| to remove successfully uploaded
109 // data but keep beacons and request counts added after the upload started. 77 // data but keep beacons and request counts added after the upload started.
78 // N.B.: There is no equivalent "RollbackUpload" that needs to be called on a
79 // failed upload.
110 void CommitUpload(); 80 void CommitUpload();
111 81
112 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called 82 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called
113 // when there are too many beacons queued.) 83 // when there are too many beacons queued.)
114 void RemoveOldestBeacon(); 84 void RemoveOldestBeacon();
115 85
116 scoped_ptr<const DomainReliabilityConfig> config_; 86 scoped_ptr<const DomainReliabilityConfig> config_;
117 MockableTime* time_; 87 MockableTime* time_;
88 const std::string& upload_reporter_string_;
118 DomainReliabilityScheduler scheduler_; 89 DomainReliabilityScheduler scheduler_;
119 DomainReliabilityDispatcher* dispatcher_; 90 DomainReliabilityDispatcher* dispatcher_;
120 DomainReliabilityUploader* uploader_; 91 DomainReliabilityUploader* uploader_;
121 92
122 // Each ResourceState in |states_| corresponds to the Resource of the same 93 // Each ResourceState in |states_| corresponds to the Resource of the same
123 // index in the config. 94 // index in the config.
124 ResourceStateVector states_; 95 ResourceStateVector states_;
125 int beacon_count_; 96 size_t beacon_count_;
126 int uploading_beacon_count_; 97 size_t uploading_beacon_count_;
127 base::TimeTicks upload_time_; 98 base::TimeTicks upload_time_;
128 base::TimeTicks last_upload_time_; 99 base::TimeTicks last_upload_time_;
129 100
130 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_; 101 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_;
131 102
132 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext); 103 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext);
133 }; 104 };
134 105
135 } // namespace domain_reliability 106 } // namespace domain_reliability
136 107
137 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ 108 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
OLDNEW
« no previous file with comments | « components/domain_reliability/config_unittest.cc ('k') | components/domain_reliability/context.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698