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

Side by Side Diff: chrome/browser/ssl/certificate_reporting_service.h

Issue 2483003003: Introduce CertificateReportingService class to handle certificate reports. (Closed)
Patch Set: Rebase Created 4 years, 1 month 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
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/ssl/certificate_reporting_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #ifndef CHROME_BROWSER_SSL_CERTIFICATE_REPORTING_SERVICE_H_
6 #define CHROME_BROWSER_SSL_CERTIFICATE_REPORTING_SERVICE_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/time/time.h"
17 #include "components/certificate_reporting/error_reporter.h"
18 #include "components/keyed_service/core/keyed_service.h"
19
20 namespace base {
21 class Clock;
22 }
23
24 // This service initiates uploads invalid certificate reports and retries any
estark 2016/11/17 14:51:59 extra word in this sentence?
meacer 2016/11/17 20:09:08 Missing, in fact :)
25 // failed uploads.
26 class CertificateReportingService : public KeyedService {
27 public:
28 // Represent a report to be sent.
29 struct Report {
30 int report_id;
31 base::Time creation_time;
32 std::string serialized_report;
33 Report() {}
estark 2016/11/17 14:51:59 do you need this constructor?
meacer 2016/11/17 20:09:08 [] operator of std::map needs it, but I modified t
34 Report(int report_id,
35 base::Time creation_time,
36 const std::string& serialized_report)
37 : report_id(report_id),
38 creation_time(creation_time),
39 serialized_report(serialized_report) {}
40 };
41
42 // This class contains a number of reports, sorted by the first time the
43 // report was to be sent. Oldest reports are at the end of the list. The
44 // number of reports are bounded by |max_size|. The implementation sorts all
45 // items in the list whenever a new item is added. This should be fine for
46 // small values of |max_size| (e.g. fewer than 100 items). In case this is not
47 // sufficient in the future, an array based implementation should be
48 // considered where the array is maintained as a heap.
49 class BoundedReportList {
50 public:
51 explicit BoundedReportList(size_t max_size);
52 ~BoundedReportList();
53
54 void Add(const Report& report);
55 void Clear();
56
57 const std::vector<Report>& items() const;
58
59 private:
60 // Maximum number of reports in the list. A newly added item is compared
61 // to the items in the list and only added when it's newer than the
62 // oldest item in the list.
estark 2016/11/17 14:51:59 This sentence "A newly added item is compared..."
meacer 2016/11/17 20:09:08 Correct, clarified the comment a bit.
63 const size_t max_size_;
64
65 std::vector<Report> items_;
66 base::ThreadChecker thread_checker_;
67 };
68
69 // A class to observe events by the service. Used for testing.
estark 2016/11/17 14:51:59 True confessions, I haven't read the tests yet...
meacer 2016/11/17 20:09:08 Looks like this isn't actually used yet, so I remo
70 class EventObserver {
71 public:
72 EventObserver() {}
73 virtual ~EventObserver() {}
74
75 // Called when sending of a report is attempted. If attempt was cancelled,
76 // |sent| is false. Otherwise, it's true.
estark 2016/11/17 14:51:59 sent => completed
meacer 2016/11/17 20:09:08 Done.
77 virtual void OnSendAttempt(bool completed) {}
78 // Called when sending a report is completed. If attempt was successful,
79 // |success| is true. Otherwise, it's false.
80 virtual void OnSendComplete(bool success) {}
81 // Called when reporter is created. This can happen when changing
82 // SafeBrowsing or extended reporting preferences.
83 virtual void OnCreated() {}
84 // The service is being reset because SafeBrowsing preferences have changed.
85 virtual void OnReset() {}
86 // The service is being reset because SafeBrowsing preferences have changed.
87 virtual void OnInitialized() {}
88 };
89
90 // Class that handles report uploads and implements the upload retry logic.
91 class Reporter : public base::RefCountedThreadSafe<Reporter> {
estark 2016/11/17 14:51:59 ALERT ALERT REFCOUNTED ALERT Glanced at the .cc f
meacer 2016/11/17 20:09:08 Yeah, this was no longer necessary and I removed i
92 public:
93 Reporter(
94 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_,
95 std::unique_ptr<BoundedReportList> retry_list,
96 base::Clock* clock,
97 base::TimeDelta max_item_age,
98 EventObserver* observer);
99
100 // Sends a report. If the send fails, the report will be added to the retry
101 // list.
102 void Send(const std::string& serialized_report);
103
104 // Sends all pending reports. Skips reports older than max_item_age_. Failed
estark 2016/11/17 14:51:59 Tiny nit: referring to a private member on a publi
105 // reports will be added to the retry list.
106 void SendPending();
estark 2016/11/17 14:51:59 No code change necessary, but for my own understan
meacer 2016/11/17 20:09:08 It will be called periodically by the Metrics serv
107
108 // Getter and setters for testing:
109 size_t inflight_report_count_for_testing() const;
110 BoundedReportList* GetQueueForTesting() const;
111 void SetEventObserverForTesting(EventObserver* observer);
112
113 private:
114 ~Reporter();
115 friend class base::RefCountedThreadSafe<Reporter>;
116
117 void SendInternal(const Report& report);
118 void ErrorCallback(int report_id, const GURL& url, int error);
119 void SuccessCallback(int report_id);
120 void OnSendComplete(bool success);
121
122 std::unique_ptr<certificate_reporting::ErrorReporter> error_reporter_;
123 std::unique_ptr<BoundedReportList> retry_list_;
124 base::Clock* test_clock_;
125 const base::TimeDelta max_item_age_;
126 EventObserver* event_observer_;
127 int current_report_id_;
128
129 std::map<int, Report> inflight_reports_;
130
131 DISALLOW_IMPLICIT_CONSTRUCTORS(Reporter);
132 };
133 };
134
135 #endif // CHROME_BROWSER_SSL_CERTIFICATE_REPORTING_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/BUILD.gn ('k') | chrome/browser/ssl/certificate_reporting_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698