| 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|. The callback may outlive the service, but |
| 61 AddIncidentCallback GetAddIncidentCallback(); | 73 // will no longer have any effect after the service is deleted. The callback |
| 74 // must not be run after |profile| has been destroyed. |
| 75 AddIncidentCallback GetAddIncidentCallback(Profile* profile); |
| 62 | 76 |
| 63 // Returns a preference validation delegate that adds incidents to the service | 77 // Returns a preference validation delegate that adds incidents to the service |
| 64 // for validation failures. | 78 // for validation failures in |profile|. The delegate may outlive the service, |
| 79 // but incidents reported by it will no longer have any effect after the |
| 80 // service is deleted. The lifetime of the delegate should not extend beyond |
| 81 // that of the profile it services. |
| 65 scoped_ptr<TrackedPreferenceValidationDelegate> | 82 scoped_ptr<TrackedPreferenceValidationDelegate> |
| 66 CreatePreferenceValidationDelegate(); | 83 CreatePreferenceValidationDelegate(Profile* profile); |
| 67 | 84 |
| 68 protected: | 85 protected: |
| 69 // A pointer to a function that populates a protobuf with environment data. | 86 // A pointer to a function that populates a protobuf with environment data. |
| 70 typedef void (*CollectEnvironmentDataFn)( | 87 typedef void (*CollectEnvironmentDataFn)( |
| 71 ClientIncidentReport_EnvironmentData*); | 88 ClientIncidentReport_EnvironmentData*); |
| 72 | 89 |
| 73 // Sets the function called by the service to collect environment data and the | 90 // 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 | 91 // task runner on which it is called. Used by unit tests to provide a fake |
| 75 // environment data collector. | 92 // environment data collector. |
| 76 void SetCollectEnvironmentHook( | 93 void SetCollectEnvironmentHook( |
| 77 CollectEnvironmentDataFn collect_environment_data_hook, | 94 CollectEnvironmentDataFn collect_environment_data_hook, |
| 78 const scoped_refptr<base::TaskRunner>& task_runner); | 95 const scoped_refptr<base::TaskRunner>& task_runner); |
| 79 | 96 |
| 97 // Handles the creation of a new profile. Creates a new context for |profile| |
| 98 // if one does not exist, and drops any received incidents for the profile if |
| 99 // the profile is not participating in safe browsing. Overridden by unit tests |
| 100 // to inject incidents prior to creation. |
| 101 virtual void OnProfileCreated(Profile* profile); |
| 102 |
| 80 // Initiates an upload. Overridden by unit tests to provide a fake uploader. | 103 // Initiates an upload. Overridden by unit tests to provide a fake uploader. |
| 81 virtual scoped_ptr<IncidentReportUploader> StartReportUpload( | 104 virtual scoped_ptr<IncidentReportUploader> StartReportUpload( |
| 82 const IncidentReportUploader::OnResultCallback& callback, | 105 const IncidentReportUploader::OnResultCallback& callback, |
| 83 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 106 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 84 const ClientIncidentReport& report); | 107 const ClientIncidentReport& report); |
| 85 | 108 |
| 86 private: | 109 private: |
| 110 struct ProfileContext; |
| 87 class UploadContext; | 111 class UploadContext; |
| 88 | 112 |
| 113 // A mapping of profiles to contexts holding state about received incidents. |
| 114 typedef std::map<Profile*, ProfileContext*> ProfileContextCollection; |
| 115 |
| 116 // Returns the context for |profile|, creating it if it does not exist. |
| 117 ProfileContext* GetOrCreateProfileContext(Profile* profile); |
| 118 |
| 119 // Returns the context for |profile|, or NULL if it is unknown. |
| 120 ProfileContext* GetProfileContext(Profile* profile); |
| 121 |
| 122 // Handles the destruction of a profile. Incidents reported for the profile |
| 123 // but not yet uploaded are dropped. |
| 124 void OnProfileDestroyed(Profile* profile); |
| 125 |
| 89 // Adds |incident_data| to the service. The incident_time_msec field is | 126 // 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. | 127 // populated with the current time if the caller has not already done so. |
| 91 void AddIncident(scoped_ptr<ClientIncidentReport_IncidentData> incident_data); | 128 void AddIncident(Profile* profile, |
| 129 scoped_ptr<ClientIncidentReport_IncidentData> incident_data); |
| 92 | 130 |
| 93 // Starts a task to collect environment data in the blocking pool. | 131 // Starts a task to collect environment data in the blocking pool. |
| 94 void BeginEnvironmentCollection(); | 132 void BeginEnvironmentCollection(); |
| 95 | 133 |
| 96 // Cancels any pending environment collection task and drops any data that has | 134 // Cancels any pending environment collection task and drops any data that has |
| 97 // already been collected. | 135 // already been collected. |
| 98 void CancelEnvironmentCollection(); | 136 void CancelEnvironmentCollection(); |
| 99 | 137 |
| 100 // A callback invoked on the UI thread when environment data collection is | 138 // A callback invoked on the UI thread when environment data collection is |
| 101 // complete. Incident report processing continues, either by waiting for the | 139 // complete. Incident report processing continues, either by waiting for the |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 // download. | 153 // download. |
| 116 void CollectDownloadDetails( | 154 void CollectDownloadDetails( |
| 117 ClientIncidentReport_DownloadDetails* download_details); | 155 ClientIncidentReport_DownloadDetails* download_details); |
| 118 | 156 |
| 119 // Record the incidents that were reported for future pruning. | 157 // Record the incidents that were reported for future pruning. |
| 120 void RecordReportedIncidents(); | 158 void RecordReportedIncidents(); |
| 121 | 159 |
| 122 // Prunes incidents that have previously been reported. | 160 // Prunes incidents that have previously been reported. |
| 123 void PruneReportedIncidents(ClientIncidentReport* report); | 161 void PruneReportedIncidents(ClientIncidentReport* report); |
| 124 | 162 |
| 125 // Uploads an incident report if all data collection is complete. | 163 // Uploads an incident report if all data collection is complete. Incidents |
| 164 // originating from profiles that do not participate in safe browsing are |
| 165 // dropped. |
| 126 void UploadIfCollectionComplete(); | 166 void UploadIfCollectionComplete(); |
| 127 | 167 |
| 128 // Cancels all uploads, discarding all reports and responses in progress. | 168 // Cancels all uploads, discarding all reports and responses in progress. |
| 129 void CancelAllReportUploads(); | 169 void CancelAllReportUploads(); |
| 130 | 170 |
| 131 // Continues an upload after checking for the CSD whitelist killswitch. | 171 // Continues an upload after checking for the CSD whitelist killswitch. |
| 132 void OnKillSwitchResult(UploadContext* context, bool is_killswitch_on); | 172 void OnKillSwitchResult(UploadContext* context, bool is_killswitch_on); |
| 133 | 173 |
| 134 // Performs processing for a report after succesfully receiving a response. | 174 // Performs processing for a report after succesfully receiving a response. |
| 135 void HandleResponse(scoped_ptr<ClientIncidentReport> report, | 175 void HandleResponse(scoped_ptr<ClientIncidentReport> report, |
| 136 scoped_ptr<ClientIncidentResponse> response); | 176 scoped_ptr<ClientIncidentResponse> response); |
| 137 | 177 |
| 138 // IncidentReportUploader::OnResultCallback implementation. | 178 // IncidentReportUploader::OnResultCallback implementation. |
| 139 void OnReportUploadResult(UploadContext* context, | 179 void OnReportUploadResult(UploadContext* context, |
| 140 IncidentReportUploader::Result result, | 180 IncidentReportUploader::Result result, |
| 141 scoped_ptr<ClientIncidentResponse> response); | 181 scoped_ptr<ClientIncidentResponse> response); |
| 142 | 182 |
| 183 // content::NotificationObserver methods. |
| 184 virtual void Observe(int type, |
| 185 const content::NotificationSource& source, |
| 186 const content::NotificationDetails& details) OVERRIDE; |
| 187 |
| 143 base::ThreadChecker thread_checker_; | 188 base::ThreadChecker thread_checker_; |
| 144 | 189 |
| 145 // The safe browsing database manager, through which the whitelist killswitch | 190 // The safe browsing database manager, through which the whitelist killswitch |
| 146 // is checked. | 191 // is checked. |
| 147 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | 192 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; |
| 148 | 193 |
| 149 // Accessor for an URL context with which reports will be sent. | 194 // Accessor for an URL context with which reports will be sent. |
| 150 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 195 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 151 | 196 |
| 152 // A pointer to a function that collects environment data. The function will | 197 // A pointer to a function that collects environment data. The function will |
| 153 // be run by |environment_collection_task_runner_|. This is ordinarily | 198 // be run by |environment_collection_task_runner_|. This is ordinarily |
| 154 // CollectEnvironmentData, but may be overridden by tests; see | 199 // CollectEnvironmentData, but may be overridden by tests; see |
| 155 // SetCollectEnvironmentHook. | 200 // SetCollectEnvironmentHook. |
| 156 CollectEnvironmentDataFn collect_environment_data_fn_; | 201 CollectEnvironmentDataFn collect_environment_data_fn_; |
| 157 | 202 |
| 158 // The task runner on which environment collection takes place. This is | 203 // 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 | 204 // ordinarily a runner in the browser's blocking pool that will skip the |
| 160 // collection task at shutdown if it has not yet started. | 205 // collection task at shutdown if it has not yet started. |
| 161 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; | 206 scoped_refptr<base::TaskRunner> environment_collection_task_runner_; |
| 162 | 207 |
| 163 // True when the service has been enabled. | 208 // Registrar for observing profile lifecycle notifications. |
| 164 bool enabled_; | 209 content::NotificationRegistrar notification_registrar_; |
| 165 | 210 |
| 166 // True when the asynchronous environment collection task has been fired off | 211 // True when the asynchronous environment collection task has been fired off |
| 167 // but has not yet completed. | 212 // but has not yet completed. |
| 168 bool environment_collection_pending_; | 213 bool environment_collection_pending_; |
| 169 | 214 |
| 170 // True when an incident has been received and the service is waiting for the | 215 // True when an incident has been received and the service is waiting for the |
| 171 // upload_timer_ to fire. | 216 // upload_timer_ to fire. |
| 172 bool collection_timeout_pending_; | 217 bool collection_timeout_pending_; |
| 173 | 218 |
| 174 // A timer upon the firing of which the service will report received | 219 // A timer upon the firing of which the service will report received |
| 175 // incidents. | 220 // incidents. |
| 176 base::DelayTimer<IncidentReportingService> upload_timer_; | 221 base::DelayTimer<IncidentReportingService> upload_timer_; |
| 177 | 222 |
| 178 // The report currently being assembled. This becomes non-NULL when an initial | 223 // 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 | 224 // incident is reported, and returns to NULL when the report is sent for |
| 180 // upload. | 225 // upload. |
| 181 scoped_ptr<ClientIncidentReport> report_; | 226 scoped_ptr<ClientIncidentReport> report_; |
| 182 | 227 |
| 183 // The time at which the initial incident is reported. | 228 // The time at which the initial incident is reported. |
| 184 base::Time first_incident_time_; | 229 base::Time first_incident_time_; |
| 185 | 230 |
| 186 // The time at which the last incident is reported. | 231 // The time at which the last incident is reported. |
| 187 base::TimeTicks last_incident_time_; | 232 base::TimeTicks last_incident_time_; |
| 188 | 233 |
| 189 // The time at which environmental data collection was initiated. | 234 // The time at which environmental data collection was initiated. |
| 190 base::TimeTicks environment_collection_begin_; | 235 base::TimeTicks environment_collection_begin_; |
| 191 | 236 |
| 237 // Context data for all on-the-record profiles. |
| 238 ProfileContextCollection profiles_; |
| 239 |
| 192 // The collection of uploads in progress. | 240 // The collection of uploads in progress. |
| 193 ScopedVector<UploadContext> uploads_; | 241 ScopedVector<UploadContext> uploads_; |
| 194 | 242 |
| 195 // A factory for handing out weak pointers for AddIncident callbacks. | 243 // A factory for handing out weak pointers for AddIncident callbacks. |
| 196 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; | 244 base::WeakPtrFactory<IncidentReportingService> receiver_weak_ptr_factory_; |
| 197 | 245 |
| 198 // A factory for handing out weak pointers for internal asynchronous tasks | 246 // A factory for handing out weak pointers for internal asynchronous tasks |
| 199 // that are posted during normal processing (e.g., environment collection, | 247 // that are posted during normal processing (e.g., environment collection, |
| 200 // safe browsing database checks, and report uploads). | 248 // safe browsing database checks, and report uploads). |
| 201 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; | 249 base::WeakPtrFactory<IncidentReportingService> weak_ptr_factory_; |
| 202 | 250 |
| 203 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); | 251 DISALLOW_COPY_AND_ASSIGN(IncidentReportingService); |
| 204 }; | 252 }; |
| 205 | 253 |
| 206 } // namespace safe_browsing | 254 } // namespace safe_browsing |
| 207 | 255 |
| 208 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ | 256 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_SERVICE_H_ |
| OLD | NEW |