OLD | NEW |
---|---|
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 CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ |
6 #define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | |
11 | |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
16 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
17 #include "base/time/time.h" | 19 #include "base/time/time.h" |
18 #include "base/timer/timer.h" | 20 #include "base/timer/timer.h" |
19 #include "chrome/browser/safe_browsing/add_incident_callback.h" | 21 #include "chrome/browser/safe_browsing/add_incident_callback.h" |
20 #include "chrome/browser/safe_browsing/incident_report_uploader.h" | 22 #include "chrome/browser/safe_browsing/incident_report_uploader.h" |
23 #include "content/public/browser/notification_observer.h" | |
24 #include "content/public/browser/notification_registrar.h" | |
21 | 25 |
26 class Profile; | |
22 class SafeBrowsingDatabaseManager; | 27 class SafeBrowsingDatabaseManager; |
23 class SafeBrowsingService; | 28 class SafeBrowsingService; |
24 class TrackedPreferenceValidationDelegate; | 29 class TrackedPreferenceValidationDelegate; |
25 | 30 |
26 namespace base { | 31 namespace base { |
27 class TaskRunner; | 32 class TaskRunner; |
28 } | 33 } |
29 | 34 |
35 namespace content { | |
36 class NotificationDetails; | |
37 class NotificationSource; | |
38 } | |
39 | |
30 namespace net { | 40 namespace net { |
31 class URLRequestContextGetter; | 41 class URLRequestContextGetter; |
32 } | 42 } |
33 | 43 |
34 namespace safe_browsing { | 44 namespace safe_browsing { |
35 | 45 |
36 class ClientIncidentReport; | 46 class ClientIncidentReport; |
37 class ClientIncidentReport_DownloadDetails; | 47 class ClientIncidentReport_DownloadDetails; |
38 class ClientIncidentReport_EnvironmentData; | 48 class ClientIncidentReport_EnvironmentData; |
39 class ClientIncidentReport_IncidentData; | 49 class ClientIncidentReport_IncidentData; |
40 | 50 |
41 // A class that manages the collection of incidents and submission of incident | 51 // A class that manages the collection of incidents and submission of incident |
42 // reports to the safe browsing client-side detection service. The service | 52 // reports to the safe browsing client-side detection service. The service |
43 // begins operation when an incident is reported via the AddIncident method. | 53 // begins operation when an incident is reported via the AddIncident method. |
44 // Following this, the service collects environmental data and waits a bit. | 54 // Incidents reported from a profile that is loading are held until the profile |
55 // is fully created. Incidents originating from profiles that do not participate | |
56 // in safe browsing are dropped. Following the addition of an incident that is | |
57 // not dropped, the service collects environmental data and waits a bit. | |
45 // Additional incidents that arrive during this time are collated with the | 58 // Additional incidents that arrive during this time are collated with the |
46 // initial incident. Finally, already-reported incidents are pruned and any | 59 // initial incident. Finally, already-reported incidents are pruned and any |
47 // remaining are uploaded in an incident report. | 60 // remaining are uploaded in an incident report. |
48 class IncidentReportingService { | 61 class IncidentReportingService : public content::NotificationObserver { |
49 public: | 62 public: |
50 IncidentReportingService(SafeBrowsingService* safe_browsing_service, | 63 IncidentReportingService(SafeBrowsingService* safe_browsing_service, |
51 const scoped_refptr<net::URLRequestContextGetter>& | 64 const scoped_refptr<net::URLRequestContextGetter>& |
52 request_context_getter); | 65 request_context_getter); |
66 | |
67 // All incident collection, data collection, and uploads in progress are | |
68 // dropped at destruction. | |
53 virtual ~IncidentReportingService(); | 69 virtual ~IncidentReportingService(); |
54 | 70 |
55 // Enables or disables the service. When disabling, incident or data | |
56 // collection in progress is dropped. | |
57 void SetEnabled(bool enabled); | |
58 | |
59 // Returns a callback by which external components can add an incident to the | 71 // Returns a callback by which external components can add an incident to the |
60 // service. | 72 // service on behalf of |profile|. |
mattm
2014/06/18 21:07:18
comment should mention expectations regarding usin
grt (UTC plus 2)
2014/06/19 02:24:39
Done.
| |
61 AddIncidentCallback GetAddIncidentCallback(); | 73 AddIncidentCallback GetAddIncidentCallback(Profile* profile); |
62 | 74 |
63 // Returns a preference validation delegate that adds incidents to the service | 75 // Returns a preference validation delegate that adds incidents to the service |
64 // for validation failures. | 76 // for validation failures in |profile|. |
mattm
2014/06/18 21:07:18
same
grt (UTC plus 2)
2014/06/19 02:24:39
Done.
| |
65 scoped_ptr<TrackedPreferenceValidationDelegate> | 77 scoped_ptr<TrackedPreferenceValidationDelegate> |
66 CreatePreferenceValidationDelegate(); | 78 CreatePreferenceValidationDelegate(Profile* profile); |
67 | 79 |
68 protected: | 80 protected: |
69 // A pointer to a function that populates a protobuf with environment data. | 81 // A pointer to a function that populates a protobuf with environment data. |
70 typedef void (*CollectEnvironmentDataFn)( | 82 typedef void (*CollectEnvironmentDataFn)( |
71 ClientIncidentReport_EnvironmentData*); | 83 ClientIncidentReport_EnvironmentData*); |
72 | 84 |
73 // Sets the function called by the service to collect environment data and the | 85 // Sets the function called by the service to collect environment data and the |
74 // task runner on which it is called. Used by unit tests to provide a fake | 86 // task runner on which it is called. Used by unit tests to provide a fake |
75 // environment data collector. | 87 // environment data collector. |
76 void SetCollectEnvironmentHook( | 88 void SetCollectEnvironmentHook( |
77 CollectEnvironmentDataFn collect_environment_data_hook, | 89 CollectEnvironmentDataFn collect_environment_data_hook, |
78 const scoped_refptr<base::TaskRunner>& task_runner); | 90 const scoped_refptr<base::TaskRunner>& task_runner); |
79 | 91 |
80 // Initiates an upload. Overridden by unit tests to provide a fake uploader. | 92 // Initiates an upload. Overridden by unit tests to provide a fake uploader. |
81 virtual scoped_ptr<IncidentReportUploader> StartReportUpload( | 93 virtual scoped_ptr<IncidentReportUploader> StartReportUpload( |
82 const IncidentReportUploader::OnResultCallback& callback, | 94 const IncidentReportUploader::OnResultCallback& callback, |
83 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 95 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
84 const ClientIncidentReport& report); | 96 const ClientIncidentReport& report); |
85 | 97 |
86 private: | 98 private: |
99 struct ProfileContext; | |
87 class UploadContext; | 100 class UploadContext; |
88 | 101 |
102 // A mapping of profiles to contexts holding state about received incidents. | |
103 typedef std::map<Profile*, ProfileContext*> ProfileContextCollection; | |
104 | |
105 // Returns the context for |profile|, creating it if it does not exist. | |
106 ProfileContext* GetOrCreateProfileContext(Profile* profile); | |
107 | |
108 // Returns the context for |profile|, or NULL if it is unknown. | |
109 ProfileContext* GetProfileContext(Profile* profile); | |
110 | |
111 // Handles the creation of a new profile. Creates a new context for |profile| | |
112 // if one does not exist, and drops any received incidents for the profile if | |
113 // the profile is not participating in safe browsing. | |
114 void OnProfileCreated(Profile* profile); | |
115 | |
116 // Handles the destruction of a profile. Incidents reported for the profile | |
117 // but not yet uploaded are dropped. | |
118 void OnProfileDestroyed(Profile* profile); | |
119 | |
89 // Adds |incident_data| to the service. The incident_time_msec field is | 120 // Adds |incident_data| to the service. The incident_time_msec field is |
90 // populated with the current time if the caller has not already done so. | 121 // populated with the current time if the caller has not already done so. |
91 void AddIncident(scoped_ptr<ClientIncidentReport_IncidentData> incident_data); | 122 void AddIncident(Profile* profile, |
123 scoped_ptr<ClientIncidentReport_IncidentData> incident_data); | |
92 | 124 |
93 // Starts a task to collect environment data in the blocking pool. | 125 // Starts a task to collect environment data in the blocking pool. |
94 void BeginEnvironmentCollection(); | 126 void BeginEnvironmentCollection(); |
95 | 127 |
96 // Cancels any pending environment collection task and drops any data that has | 128 // Cancels any pending environment collection task and drops any data that has |
97 // already been collected. | 129 // already been collected. |
98 void CancelEnvironmentCollection(); | 130 void CancelEnvironmentCollection(); |
99 | 131 |
100 // A callback invoked on the UI thread when environment data collection is | 132 // A callback invoked on the UI thread when environment data collection is |
101 // complete. Incident report processing continues, either by waiting for the | 133 // complete. Incident report processing continues, either by waiting for the |
(...skipping 13 matching lines...) Expand all Loading... | |
115 // download. | 147 // download. |
116 void CollectDownloadDetails( | 148 void CollectDownloadDetails( |
117 ClientIncidentReport_DownloadDetails* download_details); | 149 ClientIncidentReport_DownloadDetails* download_details); |
118 | 150 |
119 // Record the incidents that were reported for future pruning. | 151 // Record the incidents that were reported for future pruning. |
120 void RecordReportedIncidents(); | 152 void RecordReportedIncidents(); |
121 | 153 |
122 // Prunes incidents that have previously been reported. | 154 // Prunes incidents that have previously been reported. |
123 void PruneReportedIncidents(ClientIncidentReport* report); | 155 void PruneReportedIncidents(ClientIncidentReport* report); |
124 | 156 |
125 // Uploads an incident report if all data collection is complete. | 157 // Uploads an incident report if all data collection is complete. Incidents |
158 // originating from profiles that do not participate in safe browsing are | |
159 // dropped. | |
126 void UploadIfCollectionComplete(); | 160 void UploadIfCollectionComplete(); |
127 | 161 |
128 // Cancels all uploads, discarding all reports and responses in progress. | 162 // Cancels all uploads, discarding all reports and responses in progress. |
129 void CancelAllReportUploads(); | 163 void CancelAllReportUploads(); |
130 | 164 |
131 // Continues an upload after checking for the CSD whitelist killswitch. | 165 // Continues an upload after checking for the CSD whitelist killswitch. |
132 void OnKillSwitchResult(UploadContext* context, bool is_killswitch_on); | 166 void OnKillSwitchResult(UploadContext* context, bool is_killswitch_on); |
133 | 167 |
134 // Performs processing for a report after succesfully receiving a response. | 168 // Performs processing for a report after succesfully receiving a response. |
135 void HandleResponse(scoped_ptr<ClientIncidentReport> report, | 169 void HandleResponse(scoped_ptr<ClientIncidentReport> report, |
136 scoped_ptr<ClientIncidentResponse> response); | 170 scoped_ptr<ClientIncidentResponse> response); |
137 | 171 |
138 // IncidentReportUploader::OnResultCallback implementation. | 172 // IncidentReportUploader::OnResultCallback implementation. |
139 void OnReportUploadResult(UploadContext* context, | 173 void OnReportUploadResult(UploadContext* context, |
140 IncidentReportUploader::Result result, | 174 IncidentReportUploader::Result result, |
141 scoped_ptr<ClientIncidentResponse> response); | 175 scoped_ptr<ClientIncidentResponse> response); |
142 | 176 |
177 // content::NotificationObserver methods. | |
178 virtual void Observe(int type, | |
179 const content::NotificationSource& source, | |
180 const content::NotificationDetails& details) OVERRIDE; | |
181 | |
143 base::ThreadChecker thread_checker_; | 182 base::ThreadChecker thread_checker_; |
144 | 183 |
145 // The safe browsing database manager, through which the whitelist killswitch | 184 // The safe browsing database manager, through which the whitelist killswitch |
146 // is checked. | 185 // is checked. |
147 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | 186 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; |
148 | 187 |
149 // Accessor for an URL context with which reports will be sent. | 188 // Accessor for an URL context with which reports will be sent. |
150 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 189 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
151 | 190 |
152 // A pointer to a function that collects environment data. The function will | 191 // A pointer to a function that collects environment data. The function will |
153 // be run by |environment_collection_task_runner_|. This is ordinarily | 192 // be run by |environment_collection_task_runner_|. This is ordinarily |
154 // CollectEnvironmentData, but may be overridden by tests; see | 193 // CollectEnvironmentData, but may be overridden by tests; see |
155 // SetCollectEnvironmentHook. | 194 // SetCollectEnvironmentHook. |
156 CollectEnvironmentDataFn collect_environment_data_fn_; | 195 CollectEnvironmentDataFn collect_environment_data_fn_; |
157 | 196 |
158 // The task runner on which environment collection takes place. This is | 197 // The task runner on which environment collection takes place. This is |
159 // ordinarily a runner in the browser's blocking pool that will skip the | 198 // ordinarily a runner in the browser's blocking pool that will skip the |
160 // collection task at shutdown if it has not yet started. | 199 // collection task at shutdown if it has not yet started. |
161 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; | 200 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; |
162 | 201 |
163 // True when the service has been enabled. | 202 // Registrar for observing profile lifecycle notifications. |
164 bool enabled_; | 203 content::NotificationRegistrar notification_registrar_; |
165 | 204 |
166 // True when the asynchronous environment collection task has been fired off | 205 // True when the asynchronous environment collection task has been fired off |
167 // but has not yet completed. | 206 // but has not yet completed. |
168 bool environment_collection_pending_; | 207 bool environment_collection_pending_; |
169 | 208 |
170 // True when an incident has been received and the service is waiting for the | 209 // True when an incident has been received and the service is waiting for the |
171 // upload_timer_ to fire. | 210 // upload_timer_ to fire. |
172 bool collection_timeout_pending_; | 211 bool collection_timeout_pending_; |
173 | 212 |
174 // A timer upon the firing of which the service will report received | 213 // A timer upon the firing of which the service will report received |
175 // incidents. | 214 // incidents. |
176 base::DelayTimer<IncidentReportingService> upload_timer_; | 215 base::DelayTimer<IncidentReportingService> upload_timer_; |
177 | 216 |
178 // The report currently being assembled. This becomes non-NULL when an initial | 217 // The report currently being assembled. This becomes non-NULL when an initial |
179 // incident is reported, and returns to NULL when the report is sent for | 218 // incident is reported, and returns to NULL when the report is sent for |
180 // upload. | 219 // upload. |
181 scoped_ptr<ClientIncidentReport> report_; | 220 scoped_ptr<ClientIncidentReport> report_; |
182 | 221 |
183 // The time at which the initial incident is reported. | 222 // The time at which the initial incident is reported. |
184 base::Time first_incident_time_; | 223 base::Time first_incident_time_; |
185 | 224 |
186 // The time at which the last incident is reported. | 225 // The time at which the last incident is reported. |
187 base::TimeTicks last_incident_time_; | 226 base::TimeTicks last_incident_time_; |
188 | 227 |
189 // The time at which environmental data collection was initiated. | 228 // The time at which environmental data collection was initiated. |
190 base::TimeTicks environment_collection_begin_; | 229 base::TimeTicks environment_collection_begin_; |
191 | 230 |
231 // Context data for all on-the-record profiles. | |
232 ProfileContextCollection profiles_; | |
233 | |
192 // The collection of uploads in progress. | 234 // The collection of uploads in progress. |
193 ScopedVector<UploadContext> uploads_; | 235 ScopedVector<UploadContext> uploads_; |
194 | 236 |
195 // A factory for handing out weak pointers for AddIncident callbacks. | 237 // A factory for handing out weak pointers for AddIncident callbacks. |
196 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; | 238 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; |
197 | 239 |
198 // A factory for handing out weak pointers for internal asynchronous tasks | 240 // A factory for handing out weak pointers for internal asynchronous tasks |
199 // that are posted during normal processing (e.g., environment collection, | 241 // that are posted during normal processing (e.g., environment collection, |
200 // safe browsing database checks, and report uploads). | 242 // safe browsing database checks, and report uploads). |
201 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; | 243 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; |
202 | 244 |
203 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); | 245 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); |
204 }; | 246 }; |
205 | 247 |
206 } // namespace safe_browsing | 248 } // namespace safe_browsing |
207 | 249 |
208 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ | 250 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ |
OLD | NEW |