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> | 10 #include <map> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "base/timer/timer.h" | 20 #include "base/timer/timer.h" |
21 #include "chrome/browser/safe_browsing/add_incident_callback.h" | 21 #include "chrome/browser/safe_browsing/add_incident_callback.h" |
| 22 #include "chrome/browser/safe_browsing/delayed_analysis_callback.h" |
| 23 #include "chrome/browser/safe_browsing/delayed_callback_runner.h" |
22 #include "chrome/browser/safe_browsing/incident_report_uploader.h" | 24 #include "chrome/browser/safe_browsing/incident_report_uploader.h" |
23 #include "chrome/browser/safe_browsing/last_download_finder.h" | 25 #include "chrome/browser/safe_browsing/last_download_finder.h" |
24 #include "content/public/browser/notification_observer.h" | 26 #include "content/public/browser/notification_observer.h" |
25 #include "content/public/browser/notification_registrar.h" | 27 #include "content/public/browser/notification_registrar.h" |
26 | 28 |
27 class Profile; | 29 class Profile; |
28 class SafeBrowsingDatabaseManager; | 30 class SafeBrowsingDatabaseManager; |
29 class SafeBrowsingService; | 31 class SafeBrowsingService; |
30 class TrackedPreferenceValidationDelegate; | 32 class TrackedPreferenceValidationDelegate; |
31 | 33 |
(...skipping 15 matching lines...) Expand all Loading... |
47 class ClientIncidentReport; | 49 class ClientIncidentReport; |
48 class ClientIncidentReport_DownloadDetails; | 50 class ClientIncidentReport_DownloadDetails; |
49 class ClientIncidentReport_EnvironmentData; | 51 class ClientIncidentReport_EnvironmentData; |
50 class ClientIncidentReport_IncidentData; | 52 class ClientIncidentReport_IncidentData; |
51 | 53 |
52 // A class that manages the collection of incidents and submission of incident | 54 // A class that manages the collection of incidents and submission of incident |
53 // reports to the safe browsing client-side detection service. The service | 55 // reports to the safe browsing client-side detection service. The service |
54 // begins operation when an incident is reported via the AddIncident method. | 56 // begins operation when an incident is reported via the AddIncident method. |
55 // Incidents reported from a profile that is loading are held until the profile | 57 // Incidents reported from a profile that is loading are held until the profile |
56 // is fully created. Incidents originating from profiles that do not participate | 58 // is fully created. Incidents originating from profiles that do not participate |
57 // in safe browsing are dropped. Following the addition of an incident that is | 59 // in safe browsing are dropped. Process-wide incidents are affiliated with a |
58 // not dropped, the service collects environmental data, finds the most recent | 60 // profile that participates in safe browsing when one becomes available. |
59 // binary download, and waits a bit. Additional incidents that arrive during | 61 // Following the addition of an incident that is not dropped, the service |
60 // this time are collated with the initial incident. Finally, already-reported | 62 // collects environmental data, finds the most recent binary download, and waits |
61 // incidents are pruned and any remaining are uploaded in an incident report. | 63 // a bit. Additional incidents that arrive during this time are collated with |
| 64 // the initial incident. Finally, already-reported incidents are pruned and any |
| 65 // remaining are uploaded in an incident report. |
62 class IncidentReportingService : public content::NotificationObserver { | 66 class IncidentReportingService : public content::NotificationObserver { |
63 public: | 67 public: |
64 IncidentReportingService(SafeBrowsingService* safe_browsing_service, | 68 IncidentReportingService(SafeBrowsingService* safe_browsing_service, |
65 const scoped_refptr<net::URLRequestContextGetter>& | 69 const scoped_refptr<net::URLRequestContextGetter>& |
66 request_context_getter); | 70 request_context_getter); |
67 | 71 |
68 // All incident collection, data collection, and uploads in progress are | 72 // All incident collection, data collection, and uploads in progress are |
69 // dropped at destruction. | 73 // dropped at destruction. |
70 virtual ~IncidentReportingService(); | 74 virtual ~IncidentReportingService(); |
71 | 75 |
72 // Returns a callback by which external components can add an incident to the | 76 // Returns a callback by which external components can add an incident to the |
73 // service on behalf of |profile|. The callback may outlive the service, but | 77 // service on behalf of |profile|. The callback may outlive the service, but |
74 // will no longer have any effect after the service is deleted. The callback | 78 // will no longer have any effect after the service is deleted. The callback |
75 // must not be run after |profile| has been destroyed. | 79 // must not be run after |profile| has been destroyed. |
76 AddIncidentCallback GetAddIncidentCallback(Profile* profile); | 80 AddIncidentCallback GetAddIncidentCallback(Profile* profile); |
77 | 81 |
78 // Returns a preference validation delegate that adds incidents to the service | 82 // Returns a preference validation delegate that adds incidents to the service |
79 // for validation failures in |profile|. The delegate may outlive the service, | 83 // for validation failures in |profile|. The delegate may outlive the service, |
80 // but incidents reported by it will no longer have any effect after the | 84 // but incidents reported by it will no longer have any effect after the |
81 // service is deleted. The lifetime of the delegate should not extend beyond | 85 // service is deleted. The lifetime of the delegate should not extend beyond |
82 // that of the profile it services. | 86 // that of the profile it services. |
83 scoped_ptr<TrackedPreferenceValidationDelegate> | 87 scoped_ptr<TrackedPreferenceValidationDelegate> |
84 CreatePreferenceValidationDelegate(Profile* profile); | 88 CreatePreferenceValidationDelegate(Profile* profile); |
85 | 89 |
| 90 // Registers |callback| to be run after some delay following process launch. |
| 91 void RegisterDelayedAnalysisCallback(const DelayedAnalysisCallback& callback); |
| 92 |
86 protected: | 93 protected: |
87 // A pointer to a function that populates a protobuf with environment data. | 94 // A pointer to a function that populates a protobuf with environment data. |
88 typedef void (*CollectEnvironmentDataFn)( | 95 typedef void (*CollectEnvironmentDataFn)( |
89 ClientIncidentReport_EnvironmentData*); | 96 ClientIncidentReport_EnvironmentData*); |
90 | 97 |
| 98 // For testing so that the TaskRunner used for delayed analysis callbacks can |
| 99 // be specified. |
| 100 IncidentReportingService( |
| 101 SafeBrowsingService* safe_browsing_service, |
| 102 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 103 base::TimeDelta delayed_task_interval, |
| 104 const scoped_refptr<base::TaskRunner>& delayed_task_runner); |
| 105 |
91 // Sets the function called by the service to collect environment data and the | 106 // Sets the function called by the service to collect environment data and the |
92 // task runner on which it is called. Used by unit tests to provide a fake | 107 // task runner on which it is called. Used by unit tests to provide a fake |
93 // environment data collector. | 108 // environment data collector. |
94 void SetCollectEnvironmentHook( | 109 void SetCollectEnvironmentHook( |
95 CollectEnvironmentDataFn collect_environment_data_hook, | 110 CollectEnvironmentDataFn collect_environment_data_hook, |
96 const scoped_refptr<base::TaskRunner>& task_runner); | 111 const scoped_refptr<base::TaskRunner>& task_runner); |
97 | 112 |
98 // Handles the addition of a new profile to the ProfileManager. Creates a new | 113 // Handles the addition of a new profile to the ProfileManager. Creates a new |
99 // context for |profile| if one does not exist, drops any received incidents | 114 // context for |profile| if one does not exist, drops any received incidents |
100 // for the profile if the profile is not participating in safe browsing, and | 115 // for the profile if the profile is not participating in safe browsing, and |
(...skipping 23 matching lines...) Expand all Loading... |
124 // Returns the context for |profile|, creating it if it does not exist. | 139 // Returns the context for |profile|, creating it if it does not exist. |
125 ProfileContext* GetOrCreateProfileContext(Profile* profile); | 140 ProfileContext* GetOrCreateProfileContext(Profile* profile); |
126 | 141 |
127 // Returns the context for |profile|, or NULL if it is unknown. | 142 // Returns the context for |profile|, or NULL if it is unknown. |
128 ProfileContext* GetProfileContext(Profile* profile); | 143 ProfileContext* GetProfileContext(Profile* profile); |
129 | 144 |
130 // Handles the destruction of a profile. Incidents reported for the profile | 145 // Handles the destruction of a profile. Incidents reported for the profile |
131 // but not yet uploaded are dropped. | 146 // but not yet uploaded are dropped. |
132 void OnProfileDestroyed(Profile* profile); | 147 void OnProfileDestroyed(Profile* profile); |
133 | 148 |
| 149 // Returns an initialized profile that participates in safe browsing. Profiles |
| 150 // participating in extended safe browsing are preferred. |
| 151 Profile* FindEligibleProfile() const; |
| 152 |
134 // Adds |incident_data| to the service. The incident_time_msec field is | 153 // Adds |incident_data| to the service. The incident_time_msec field is |
135 // populated with the current time if the caller has not already done so. | 154 // populated with the current time if the caller has not already done so. |
136 void AddIncident(Profile* profile, | 155 void AddIncident(Profile* profile, |
137 scoped_ptr<ClientIncidentReport_IncidentData> incident_data); | 156 scoped_ptr<ClientIncidentReport_IncidentData> incident_data); |
138 | 157 |
| 158 // Begins processing a report. If processing is already underway, ensures that |
| 159 // collection tasks have completed or are running. |
| 160 void BeginReportProcessing(); |
| 161 |
| 162 // Begins the process of collating incidents by waiting for incidents to |
| 163 // arrive. This function is idempotent. |
| 164 void BeginIncidentCollation(); |
| 165 |
139 // Starts a task to collect environment data in the blocking pool. | 166 // Starts a task to collect environment data in the blocking pool. |
140 void BeginEnvironmentCollection(); | 167 void BeginEnvironmentCollection(); |
141 | 168 |
142 // Returns true if the environment collection task is outstanding. | 169 // Returns true if the environment collection task is outstanding. |
143 bool WaitingForEnvironmentCollection(); | 170 bool WaitingForEnvironmentCollection(); |
144 | 171 |
145 // Cancels any pending environment collection task and drops any data that has | 172 // Cancels any pending environment collection task and drops any data that has |
146 // already been collected. | 173 // already been collected. |
147 void CancelEnvironmentCollection(); | 174 void CancelEnvironmentCollection(); |
148 | 175 |
149 // A callback invoked on the UI thread when environment data collection is | 176 // A callback invoked on the UI thread when environment data collection is |
150 // complete. Incident report processing continues, either by waiting for the | 177 // complete. Incident report processing continues, either by waiting for the |
151 // collection timeout or by sending an incident report. | 178 // collection timeout or by sending an incident report. |
152 void OnEnvironmentDataCollected( | 179 void OnEnvironmentDataCollected( |
153 scoped_ptr<ClientIncidentReport_EnvironmentData> environment_data); | 180 scoped_ptr<ClientIncidentReport_EnvironmentData> environment_data); |
154 | 181 |
155 // Returns true if the service is waiting for additional incidents before | 182 // Returns true if the service is waiting for additional incidents before |
156 // uploading a report. | 183 // uploading a report. |
157 bool WaitingToCollateIncidents(); | 184 bool WaitingToCollateIncidents(); |
158 | 185 |
159 // Cancels the collection timeout. | 186 // Cancels the collection timeout. |
160 void CancelIncidentCollection(); | 187 void CancelIncidentCollection(); |
161 | 188 |
162 // A callback invoked on the UI thread after which incident collection has | 189 // A callback invoked on the UI thread after which incident collation has |
163 // completed. Incident report processing continues, either by waiting for | 190 // completed. Incident report processing continues, either by waiting for |
164 // environment data to arrive or by sending an incident report. | 191 // environment data or the most recent download to arrive or by sending an |
165 void OnCollectionTimeout(); | 192 // incident report. |
| 193 void OnCollationTimeout(); |
166 | 194 |
167 // Starts the asynchronous process of finding the most recent executable | 195 // Starts the asynchronous process of finding the most recent executable |
168 // download if one is not currently being search for and/or has not already | 196 // download if one is not currently being search for and/or has not already |
169 // been found. | 197 // been found. |
170 void BeginDownloadCollection(); | 198 void BeginDownloadCollection(); |
171 | 199 |
172 // True if the service is waiting to discover the most recent download either | 200 // True if the service is waiting to discover the most recent download either |
173 // because a task to do so is outstanding, or because one or more profiles | 201 // because a task to do so is outstanding, or because one or more profiles |
174 // have yet to be added to the ProfileManager. | 202 // have yet to be added to the ProfileManager. |
175 bool WaitingForMostRecentDownload(); | 203 bool WaitingForMostRecentDownload(); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; | 255 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; |
228 | 256 |
229 // Registrar for observing profile lifecycle notifications. | 257 // Registrar for observing profile lifecycle notifications. |
230 content::NotificationRegistrar notification_registrar_; | 258 content::NotificationRegistrar notification_registrar_; |
231 | 259 |
232 // True when the asynchronous environment collection task has been fired off | 260 // True when the asynchronous environment collection task has been fired off |
233 // but has not yet completed. | 261 // but has not yet completed. |
234 bool environment_collection_pending_; | 262 bool environment_collection_pending_; |
235 | 263 |
236 // True when an incident has been received and the service is waiting for the | 264 // True when an incident has been received and the service is waiting for the |
237 // upload_timer_ to fire. | 265 // collation_timer_ to fire. |
238 bool collection_timeout_pending_; | 266 bool collation_timeout_pending_; |
239 | 267 |
240 // A timer upon the firing of which the service will report received | 268 // A timer upon the firing of which the service will report received |
241 // incidents. | 269 // incidents. |
242 base::DelayTimer<IncidentReportingService> upload_timer_; | 270 base::DelayTimer<IncidentReportingService> collation_timer_; |
243 | 271 |
244 // The report currently being assembled. This becomes non-NULL when an initial | 272 // The report currently being assembled. This becomes non-NULL when an initial |
245 // incident is reported, and returns to NULL when the report is sent for | 273 // incident is reported, and returns to NULL when the report is sent for |
246 // upload. | 274 // upload. |
247 scoped_ptr<ClientIncidentReport> report_; | 275 scoped_ptr<ClientIncidentReport> report_; |
248 | 276 |
249 // The time at which the initial incident is reported. | 277 // The time at which the initial incident is reported. |
250 base::Time first_incident_time_; | 278 base::Time first_incident_time_; |
251 | 279 |
252 // The time at which the last incident is reported. | 280 // The time at which the last incident is reported. |
253 base::TimeTicks last_incident_time_; | 281 base::TimeTicks last_incident_time_; |
254 | 282 |
255 // The time at which environmental data collection was initiated. | 283 // The time at which environmental data collection was initiated. |
256 base::TimeTicks environment_collection_begin_; | 284 base::TimeTicks environment_collection_begin_; |
257 | 285 |
258 // The time at which download collection was initiated. | 286 // The time at which download collection was initiated. |
259 base::TimeTicks last_download_begin_; | 287 base::TimeTicks last_download_begin_; |
260 | 288 |
261 // Context data for all on-the-record profiles. | 289 // Context data for all on-the-record profiles plus the process-wide (NULL) |
| 290 // context. |
262 ProfileContextCollection profiles_; | 291 ProfileContextCollection profiles_; |
263 | 292 |
| 293 // Callbacks registered for performing delayed analysis. |
| 294 DelayedCallbackRunner delayed_analysis_callbacks_; |
| 295 |
264 // The collection of uploads in progress. | 296 // The collection of uploads in progress. |
265 ScopedVector<UploadContext> uploads_; | 297 ScopedVector<UploadContext> uploads_; |
266 | 298 |
267 // An object that asynchronously searches for the most recent binary download. | 299 // An object that asynchronously searches for the most recent binary download. |
268 // Non-NULL while such a search is outstanding. | 300 // Non-NULL while such a search is outstanding. |
269 scoped_ptr<LastDownloadFinder> last_download_finder_; | 301 scoped_ptr<LastDownloadFinder> last_download_finder_; |
270 | 302 |
271 // A factory for handing out weak pointers for AddIncident callbacks. | 303 // A factory for handing out weak pointers for AddIncident callbacks. |
272 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; | 304 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; |
273 | 305 |
274 // A factory for handing out weak pointers for internal asynchronous tasks | 306 // A factory for handing out weak pointers for internal asynchronous tasks |
275 // that are posted during normal processing (e.g., environment collection, | 307 // that are posted during normal processing (e.g., environment collection, |
276 // safe browsing database checks, and report uploads). | 308 // safe browsing database checks, and report uploads). |
277 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; | 309 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; |
278 | 310 |
279 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); | 311 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); |
280 }; | 312 }; |
281 | 313 |
282 } // namespace safe_browsing | 314 } // namespace safe_browsing |
283 | 315 |
284 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ | 316 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ |
OLD | NEW |