Chromium Code Reviews| 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. |
|
robertshield
2014/08/05 21:26:28
s/one/such a profile/ to resolve mild pronoun ambi
| |
| 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 |
| 91 // Sets the function called by the service to collect environment data and the | 98 // 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 | 99 // task runner on which it is called. Used by unit tests to provide a fake |
| 93 // environment data collector. | 100 // environment data collector. |
| 94 void SetCollectEnvironmentHook( | 101 void SetCollectEnvironmentHook( |
| 95 CollectEnvironmentDataFn collect_environment_data_hook, | 102 CollectEnvironmentDataFn collect_environment_data_hook, |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 124 // Returns the context for |profile|, creating it if it does not exist. | 131 // Returns the context for |profile|, creating it if it does not exist. |
| 125 ProfileContext* GetOrCreateProfileContext(Profile* profile); | 132 ProfileContext* GetOrCreateProfileContext(Profile* profile); |
| 126 | 133 |
| 127 // Returns the context for |profile|, or NULL if it is unknown. | 134 // Returns the context for |profile|, or NULL if it is unknown. |
| 128 ProfileContext* GetProfileContext(Profile* profile); | 135 ProfileContext* GetProfileContext(Profile* profile); |
| 129 | 136 |
| 130 // Handles the destruction of a profile. Incidents reported for the profile | 137 // Handles the destruction of a profile. Incidents reported for the profile |
| 131 // but not yet uploaded are dropped. | 138 // but not yet uploaded are dropped. |
| 132 void OnProfileDestroyed(Profile* profile); | 139 void OnProfileDestroyed(Profile* profile); |
| 133 | 140 |
| 141 // Returns an initialized profile that participates in safe browsing. Profiles | |
| 142 // participating in extended safe browsing are preferred. | |
| 143 Profile* FindEligibleProfile() const; | |
| 144 | |
| 134 // Adds |incident_data| to the service. The incident_time_msec field is | 145 // 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. | 146 // populated with the current time if the caller has not already done so. |
| 136 void AddIncident(Profile* profile, | 147 void AddIncident(Profile* profile, |
| 137 scoped_ptr<ClientIncidentReport_IncidentData> incident_data); | 148 scoped_ptr<ClientIncidentReport_IncidentData> incident_data); |
| 138 | 149 |
| 150 // Begins processing a report. If processing is already underway, ensures that | |
| 151 // collection tasks have completed or are running. | |
| 152 void BeginReportProcessing(); | |
| 153 | |
| 154 // Begins the process of collating incidents by waiting for incidents to | |
| 155 // arrive. This function is idempotent. | |
| 156 void BeginIncidentCollation(); | |
| 157 | |
| 139 // Starts a task to collect environment data in the blocking pool. | 158 // Starts a task to collect environment data in the blocking pool. |
| 140 void BeginEnvironmentCollection(); | 159 void BeginEnvironmentCollection(); |
| 141 | 160 |
| 142 // Returns true if the environment collection task is outstanding. | 161 // Returns true if the environment collection task is outstanding. |
| 143 bool WaitingForEnvironmentCollection(); | 162 bool WaitingForEnvironmentCollection(); |
| 144 | 163 |
| 145 // Cancels any pending environment collection task and drops any data that has | 164 // Cancels any pending environment collection task and drops any data that has |
| 146 // already been collected. | 165 // already been collected. |
| 147 void CancelEnvironmentCollection(); | 166 void CancelEnvironmentCollection(); |
| 148 | 167 |
| 149 // A callback invoked on the UI thread when environment data collection is | 168 // A callback invoked on the UI thread when environment data collection is |
| 150 // complete. Incident report processing continues, either by waiting for the | 169 // complete. Incident report processing continues, either by waiting for the |
| 151 // collection timeout or by sending an incident report. | 170 // collection timeout or by sending an incident report. |
| 152 void OnEnvironmentDataCollected( | 171 void OnEnvironmentDataCollected( |
| 153 scoped_ptr<ClientIncidentReport_EnvironmentData> environment_data); | 172 scoped_ptr<ClientIncidentReport_EnvironmentData> environment_data); |
| 154 | 173 |
| 155 // Returns true if the service is waiting for additional incidents before | 174 // Returns true if the service is waiting for additional incidents before |
| 156 // uploading a report. | 175 // uploading a report. |
| 157 bool WaitingToCollateIncidents(); | 176 bool WaitingToCollateIncidents(); |
| 158 | 177 |
| 159 // Cancels the collection timeout. | 178 // Cancels the collection timeout. |
| 160 void CancelIncidentCollection(); | 179 void CancelIncidentCollection(); |
| 161 | 180 |
| 162 // A callback invoked on the UI thread after which incident collection has | 181 // A callback invoked on the UI thread after which incident collation has |
| 163 // completed. Incident report processing continues, either by waiting for | 182 // completed. Incident report processing continues, either by waiting for |
| 164 // environment data to arrive or by sending an incident report. | 183 // environment data or the most recent download to arrive or by sending an |
| 165 void OnCollectionTimeout(); | 184 // incident report. |
| 185 void OnCollationTimeout(); | |
| 166 | 186 |
| 167 // Starts the asynchronous process of finding the most recent executable | 187 // 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 | 188 // download if one is not currently being search for and/or has not already |
| 169 // been found. | 189 // been found. |
| 170 void BeginDownloadCollection(); | 190 void BeginDownloadCollection(); |
| 171 | 191 |
| 172 // True if the service is waiting to discover the most recent download either | 192 // 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 | 193 // because a task to do so is outstanding, or because one or more profiles |
| 174 // have yet to be added to the ProfileManager. | 194 // have yet to be added to the ProfileManager. |
| 175 bool WaitingForMostRecentDownload(); | 195 bool WaitingForMostRecentDownload(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; | 247 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; |
| 228 | 248 |
| 229 // Registrar for observing profile lifecycle notifications. | 249 // Registrar for observing profile lifecycle notifications. |
| 230 content::NotificationRegistrar notification_registrar_; | 250 content::NotificationRegistrar notification_registrar_; |
| 231 | 251 |
| 232 // True when the asynchronous environment collection task has been fired off | 252 // True when the asynchronous environment collection task has been fired off |
| 233 // but has not yet completed. | 253 // but has not yet completed. |
| 234 bool environment_collection_pending_; | 254 bool environment_collection_pending_; |
| 235 | 255 |
| 236 // True when an incident has been received and the service is waiting for the | 256 // True when an incident has been received and the service is waiting for the |
| 237 // upload_timer_ to fire. | 257 // collation_timer_ to fire. |
| 238 bool collection_timeout_pending_; | 258 bool collation_timeout_pending_; |
| 239 | 259 |
| 240 // A timer upon the firing of which the service will report received | 260 // A timer upon the firing of which the service will report received |
| 241 // incidents. | 261 // incidents. |
| 242 base::DelayTimer<IncidentReportingService> upload_timer_; | 262 base::DelayTimer<IncidentReportingService> collation_timer_; |
| 243 | 263 |
| 244 // The report currently being assembled. This becomes non-NULL when an initial | 264 // 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 | 265 // incident is reported, and returns to NULL when the report is sent for |
| 246 // upload. | 266 // upload. |
| 247 scoped_ptr<ClientIncidentReport> report_; | 267 scoped_ptr<ClientIncidentReport> report_; |
| 248 | 268 |
| 249 // The time at which the initial incident is reported. | 269 // The time at which the initial incident is reported. |
| 250 base::Time first_incident_time_; | 270 base::Time first_incident_time_; |
| 251 | 271 |
| 252 // The time at which the last incident is reported. | 272 // The time at which the last incident is reported. |
| 253 base::TimeTicks last_incident_time_; | 273 base::TimeTicks last_incident_time_; |
| 254 | 274 |
| 255 // The time at which environmental data collection was initiated. | 275 // The time at which environmental data collection was initiated. |
| 256 base::TimeTicks environment_collection_begin_; | 276 base::TimeTicks environment_collection_begin_; |
| 257 | 277 |
| 258 // The time at which download collection was initiated. | 278 // The time at which download collection was initiated. |
| 259 base::TimeTicks last_download_begin_; | 279 base::TimeTicks last_download_begin_; |
| 260 | 280 |
| 261 // Context data for all on-the-record profiles. | 281 // Context data for all on-the-record profiles plus the process-wide (NULL) |
| 282 // context. | |
| 262 ProfileContextCollection profiles_; | 283 ProfileContextCollection profiles_; |
| 263 | 284 |
| 285 // Callbacks registered for performing delayed analysis. | |
| 286 DelayedCallbackRunner delayed_analysis_callbacks_; | |
| 287 | |
| 264 // The collection of uploads in progress. | 288 // The collection of uploads in progress. |
| 265 ScopedVector<UploadContext> uploads_; | 289 ScopedVector<UploadContext> uploads_; |
| 266 | 290 |
| 267 // An object that asynchronously searches for the most recent binary download. | 291 // An object that asynchronously searches for the most recent binary download. |
| 268 // Non-NULL while such a search is outstanding. | 292 // Non-NULL while such a search is outstanding. |
| 269 scoped_ptr<LastDownloadFinder> last_download_finder_; | 293 scoped_ptr<LastDownloadFinder> last_download_finder_; |
| 270 | 294 |
| 271 // A factory for handing out weak pointers for AddIncident callbacks. | 295 // A factory for handing out weak pointers for AddIncident callbacks. |
| 272 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; | 296 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; |
| 273 | 297 |
| 274 // A factory for handing out weak pointers for internal asynchronous tasks | 298 // A factory for handing out weak pointers for internal asynchronous tasks |
| 275 // that are posted during normal processing (e.g., environment collection, | 299 // that are posted during normal processing (e.g., environment collection, |
| 276 // safe browsing database checks, and report uploads). | 300 // safe browsing database checks, and report uploads). |
| 277 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; | 301 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; |
| 278 | 302 |
| 279 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); | 303 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); |
| 280 }; | 304 }; |
| 281 | 305 |
| 282 } // namespace safe_browsing | 306 } // namespace safe_browsing |
| 283 | 307 |
| 284 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ | 308 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ |
| OLD | NEW |