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 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser
vice.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser
vice.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 prefs::kSafeBrowsingIncidentsSent); | 164 prefs::kSafeBrowsingIncidentsSent); |
165 for (const auto& incident_type : types_to_remove) | 165 for (const auto& incident_type : types_to_remove) |
166 pref_update.Get()->RemoveWithoutPathExpansion(incident_type, NULL); | 166 pref_update.Get()->RemoveWithoutPathExpansion(incident_type, NULL); |
167 } | 167 } |
168 | 168 |
169 // Runs |callback| on the thread to which |thread_runner| belongs. The callback | 169 // Runs |callback| on the thread to which |thread_runner| belongs. The callback |
170 // is run immediately if this function is called on |thread_runner|'s thread. | 170 // is run immediately if this function is called on |thread_runner|'s thread. |
171 void AddIncidentOnOriginThread( | 171 void AddIncidentOnOriginThread( |
172 const AddIncidentCallback& callback, | 172 const AddIncidentCallback& callback, |
173 scoped_refptr<base::SingleThreadTaskRunner> thread_runner, | 173 scoped_refptr<base::SingleThreadTaskRunner> thread_runner, |
| 174 Profile* profile, |
174 scoped_ptr<Incident> incident) { | 175 scoped_ptr<Incident> incident) { |
175 if (thread_runner->BelongsToCurrentThread()) | 176 if (thread_runner->BelongsToCurrentThread()) { |
176 callback.Run(incident.Pass()); | 177 callback.Run(profile, incident.Pass()); |
177 else | 178 } else { |
| 179 // It is unsafe to bounce the profile from another thread. |
| 180 DCHECK(!profile); |
178 thread_runner->PostTask(FROM_HERE, | 181 thread_runner->PostTask(FROM_HERE, |
179 base::Bind(callback, base::Passed(&incident))); | 182 base::Bind(callback, nullptr, |
| 183 base::Passed(&incident))); |
| 184 } |
180 } | 185 } |
181 | 186 |
182 } // namespace | 187 } // namespace |
183 | 188 |
184 struct IncidentReportingService::ProfileContext { | 189 struct IncidentReportingService::ProfileContext { |
185 ProfileContext(); | 190 ProfileContext(); |
186 ~ProfileContext(); | 191 ~ProfileContext(); |
187 | 192 |
188 // The incidents collected for this profile pending creation and/or upload. | 193 // The incidents collected for this profile pending creation and/or upload. |
189 // Will contain null values for pruned incidents. | 194 // Will contain null values for pruned incidents. |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 // Cancel all internal asynchronous tasks. | 291 // Cancel all internal asynchronous tasks. |
287 weak_ptr_factory_.InvalidateWeakPtrs(); | 292 weak_ptr_factory_.InvalidateWeakPtrs(); |
288 | 293 |
289 CancelEnvironmentCollection(); | 294 CancelEnvironmentCollection(); |
290 CancelDownloadCollection(); | 295 CancelDownloadCollection(); |
291 CancelAllReportUploads(); | 296 CancelAllReportUploads(); |
292 | 297 |
293 STLDeleteValues(&profiles_); | 298 STLDeleteValues(&profiles_); |
294 } | 299 } |
295 | 300 |
296 AddIncidentCallback IncidentReportingService::GetAddIncidentCallback( | 301 AddIncidentCallback IncidentReportingService::GetAddIncidentCallback() { |
297 Profile* profile) { | |
298 // Force the context to be created so that incidents added before | |
299 // OnProfileAdded is called are held until the profile's preferences can be | |
300 // queried. | |
301 ignore_result(GetOrCreateProfileContext(profile)); | |
302 | |
303 return base::Bind(&IncidentReportingService::AddIncident, | 302 return base::Bind(&IncidentReportingService::AddIncident, |
304 receiver_weak_ptr_factory_.GetWeakPtr(), | 303 receiver_weak_ptr_factory_.GetWeakPtr()); |
305 profile); | |
306 } | 304 } |
307 | 305 |
308 scoped_ptr<TrackedPreferenceValidationDelegate> | 306 scoped_ptr<TrackedPreferenceValidationDelegate> |
309 IncidentReportingService::CreatePreferenceValidationDelegate(Profile* profile) { | 307 IncidentReportingService::CreatePreferenceValidationDelegate(Profile* profile) { |
310 DCHECK(thread_checker_.CalledOnValidThread()); | 308 DCHECK(thread_checker_.CalledOnValidThread()); |
311 | 309 |
312 if (profile->IsOffTheRecord()) | 310 if (profile->IsOffTheRecord()) |
313 return scoped_ptr<TrackedPreferenceValidationDelegate>(); | 311 return scoped_ptr<TrackedPreferenceValidationDelegate>(); |
314 return scoped_ptr<TrackedPreferenceValidationDelegate>( | 312 return scoped_ptr<TrackedPreferenceValidationDelegate>( |
315 new PreferenceValidationDelegate(GetAddIncidentCallback(profile))); | 313 new PreferenceValidationDelegate(profile, GetAddIncidentCallback())); |
316 } | 314 } |
317 | 315 |
318 void IncidentReportingService::RegisterDelayedAnalysisCallback( | 316 void IncidentReportingService::RegisterDelayedAnalysisCallback( |
319 const DelayedAnalysisCallback& callback) { | 317 const DelayedAnalysisCallback& callback) { |
320 DCHECK(thread_checker_.CalledOnValidThread()); | 318 DCHECK(thread_checker_.CalledOnValidThread()); |
321 | 319 |
322 // |callback| will be run on the blocking pool, so it will likely run the | 320 // |callback| will be run on the blocking pool, so it will likely run the |
323 // AddIncidentCallback there as well. Bounce the run of that callback back to | 321 // AddIncidentCallback there as well. Bounce the run of that callback back to |
324 // the current thread via AddIncidentOnOriginThread. | 322 // the current thread via AddIncidentOnOriginThread. |
325 delayed_analysis_callbacks_.RegisterCallback( | 323 delayed_analysis_callbacks_.RegisterCallback( |
326 base::Bind(callback, | 324 base::Bind(callback, |
327 base::Bind(&AddIncidentOnOriginThread, | 325 base::Bind(&AddIncidentOnOriginThread, |
328 GetAddIncidentCallback(NULL), | 326 GetAddIncidentCallback(), |
329 base::ThreadTaskRunnerHandle::Get()))); | 327 base::ThreadTaskRunnerHandle::Get()))); |
330 | 328 |
331 // Start running the callbacks if any profiles are participating in safe | 329 // Start running the callbacks if any profiles are participating in safe |
332 // browsing. If none are now, running will commence if/when a participaing | 330 // browsing. If none are now, running will commence if/when a participaing |
333 // profile is added. | 331 // profile is added. |
334 if (FindEligibleProfile()) | 332 if (FindEligibleProfile()) |
335 delayed_analysis_callbacks_.Start(); | 333 delayed_analysis_callbacks_.Start(); |
336 } | 334 } |
337 | 335 |
338 void IncidentReportingService::AddDownloadManager( | 336 void IncidentReportingService::AddDownloadManager( |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 } | 509 } |
512 } | 510 } |
513 } | 511 } |
514 return candidate; | 512 return candidate; |
515 } | 513 } |
516 | 514 |
517 void IncidentReportingService::AddIncident(Profile* profile, | 515 void IncidentReportingService::AddIncident(Profile* profile, |
518 scoped_ptr<Incident> incident) { | 516 scoped_ptr<Incident> incident) { |
519 DCHECK(thread_checker_.CalledOnValidThread()); | 517 DCHECK(thread_checker_.CalledOnValidThread()); |
520 | 518 |
521 ProfileContext* context = GetProfileContext(profile); | 519 // Ignore incidents from off-the-record profiles. |
522 // It is forbidden to call this function with a destroyed profile. | 520 if (profile && profile->IsOffTheRecord()) |
523 DCHECK(context); | 521 return; |
| 522 |
| 523 ProfileContext* context = GetOrCreateProfileContext(profile); |
524 // If this is a process-wide incident, the context must not indicate that the | 524 // If this is a process-wide incident, the context must not indicate that the |
525 // profile (which is NULL) has been added to the profile manager. | 525 // profile (which is NULL) has been added to the profile manager. |
526 DCHECK(profile || !context->added); | 526 DCHECK(profile || !context->added); |
527 | 527 |
528 LogIncidentDataType(RECEIVED, *incident); | 528 LogIncidentDataType(RECEIVED, *incident); |
529 | 529 |
530 // Drop the incident immediately if the profile has already been added to the | 530 // Drop the incident immediately if the profile has already been added to the |
531 // manager and is not participating in safe browsing. Preference evaluation is | 531 // manager and is not participating in safe browsing. Preference evaluation is |
532 // deferred until OnProfileAdded() otherwise. | 532 // deferred until OnProfileAdded() otherwise. |
533 if (context->added && | 533 if (context->added && |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 if (!profile->IsOffTheRecord()) | 958 if (!profile->IsOffTheRecord()) |
959 OnProfileDestroyed(profile); | 959 OnProfileDestroyed(profile); |
960 break; | 960 break; |
961 } | 961 } |
962 default: | 962 default: |
963 break; | 963 break; |
964 } | 964 } |
965 } | 965 } |
966 | 966 |
967 } // namespace safe_browsing | 967 } // namespace safe_browsing |
OLD | NEW |