| 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 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 scoped_ptr<ClientIncidentReport> report) | 252 scoped_ptr<ClientIncidentReport> report) |
| 253 : report(report.Pass()) { | 253 : report(report.Pass()) { |
| 254 } | 254 } |
| 255 | 255 |
| 256 IncidentReportingService::UploadContext::~UploadContext() { | 256 IncidentReportingService::UploadContext::~UploadContext() { |
| 257 } | 257 } |
| 258 | 258 |
| 259 IncidentReportingService::IncidentReportingService( | 259 IncidentReportingService::IncidentReportingService( |
| 260 SafeBrowsingService* safe_browsing_service, | 260 SafeBrowsingService* safe_browsing_service, |
| 261 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) | 261 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) |
| 262 : database_manager_(safe_browsing_service ? | 262 : database_manager_(safe_browsing_service |
| 263 safe_browsing_service->database_manager() : NULL), | 263 ? safe_browsing_service->database_manager() |
| 264 : NULL), |
| 264 url_request_context_getter_(request_context_getter), | 265 url_request_context_getter_(request_context_getter), |
| 265 collect_environment_data_fn_(&CollectEnvironmentData), | 266 collect_environment_data_fn_(&CollectEnvironmentData), |
| 266 environment_collection_task_runner_( | 267 environment_collection_task_runner_( |
| 267 content::BrowserThread::GetBlockingPool() | 268 content::BrowserThread::GetBlockingPool() |
| 268 ->GetTaskRunnerWithShutdownBehavior( | 269 ->GetTaskRunnerWithShutdownBehavior( |
| 269 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 270 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 270 environment_collection_pending_(), | 271 environment_collection_pending_(), |
| 271 collation_timeout_pending_(), | 272 collation_timeout_pending_(), |
| 272 collation_timer_(FROM_HERE, | 273 collation_timer_(FROM_HERE, |
| 273 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs), | 274 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs), |
| 274 this, | 275 this, |
| 275 &IncidentReportingService::OnCollationTimeout), | 276 &IncidentReportingService::OnCollationTimeout), |
| 276 delayed_analysis_callbacks_( | 277 delayed_analysis_callbacks_( |
| 277 base::TimeDelta::FromMilliseconds(kDefaultCallbackIntervalMs), | 278 base::TimeDelta::FromMilliseconds(kDefaultCallbackIntervalMs), |
| 278 content::BrowserThread::GetBlockingPool() | 279 content::BrowserThread::GetBlockingPool() |
| 279 ->GetTaskRunnerWithShutdownBehavior( | 280 ->GetTaskRunnerWithShutdownBehavior( |
| 280 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 281 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 282 download_metadata_manager_(content::BrowserThread::GetBlockingPool()), |
| 281 receiver_weak_ptr_factory_(this), | 283 receiver_weak_ptr_factory_(this), |
| 282 weak_ptr_factory_(this) { | 284 weak_ptr_factory_(this) { |
| 283 notification_registrar_.Add(this, | 285 notification_registrar_.Add(this, |
| 284 chrome::NOTIFICATION_PROFILE_ADDED, | 286 chrome::NOTIFICATION_PROFILE_ADDED, |
| 285 content::NotificationService::AllSources()); | 287 content::NotificationService::AllSources()); |
| 286 notification_registrar_.Add(this, | 288 notification_registrar_.Add(this, |
| 287 chrome::NOTIFICATION_PROFILE_DESTROYED, | 289 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 288 content::NotificationService::AllSources()); | 290 content::NotificationService::AllSources()); |
| 291 DownloadProtectionService* download_protection_service = |
| 292 (safe_browsing_service ? |
| 293 safe_browsing_service->download_protection_service() : |
| 294 NULL); |
| 295 if (download_protection_service) { |
| 296 client_download_request_subscription_ = |
| 297 download_protection_service->RegisterClientDownloadRequestCallback( |
| 298 base::Bind(&IncidentReportingService::OnClientDownloadRequest, |
| 299 base::Unretained(this))); |
| 300 } |
| 289 } | 301 } |
| 290 | 302 |
| 291 IncidentReportingService::~IncidentReportingService() { | 303 IncidentReportingService::~IncidentReportingService() { |
| 292 CancelIncidentCollection(); | 304 CancelIncidentCollection(); |
| 293 | 305 |
| 294 // Cancel all internal asynchronous tasks. | 306 // Cancel all internal asynchronous tasks. |
| 295 weak_ptr_factory_.InvalidateWeakPtrs(); | 307 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 296 | 308 |
| 297 CancelEnvironmentCollection(); | 309 CancelEnvironmentCollection(); |
| 298 CancelDownloadCollection(); | 310 CancelDownloadCollection(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 GetAddIncidentCallback(NULL), | 348 GetAddIncidentCallback(NULL), |
| 337 base::ThreadTaskRunnerHandle::Get()))); | 349 base::ThreadTaskRunnerHandle::Get()))); |
| 338 | 350 |
| 339 // Start running the callbacks if any profiles are participating in safe | 351 // Start running the callbacks if any profiles are participating in safe |
| 340 // browsing. If none are now, running will commence if/when a participaing | 352 // browsing. If none are now, running will commence if/when a participaing |
| 341 // profile is added. | 353 // profile is added. |
| 342 if (FindEligibleProfile()) | 354 if (FindEligibleProfile()) |
| 343 delayed_analysis_callbacks_.Start(); | 355 delayed_analysis_callbacks_.Start(); |
| 344 } | 356 } |
| 345 | 357 |
| 358 void IncidentReportingService::AddDownloadManager( |
| 359 content::DownloadManager* download_manager) { |
| 360 download_metadata_manager_.AddDownloadManager(download_manager); |
| 361 } |
| 362 |
| 346 IncidentReportingService::IncidentReportingService( | 363 IncidentReportingService::IncidentReportingService( |
| 347 SafeBrowsingService* safe_browsing_service, | 364 SafeBrowsingService* safe_browsing_service, |
| 348 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 365 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 349 base::TimeDelta delayed_task_interval, | 366 base::TimeDelta delayed_task_interval, |
| 350 const scoped_refptr<base::TaskRunner>& delayed_task_runner) | 367 const scoped_refptr<base::TaskRunner>& delayed_task_runner) |
| 351 : database_manager_(safe_browsing_service ? | 368 : database_manager_(safe_browsing_service |
| 352 safe_browsing_service->database_manager() : NULL), | 369 ? safe_browsing_service->database_manager() |
| 370 : NULL), |
| 353 url_request_context_getter_(request_context_getter), | 371 url_request_context_getter_(request_context_getter), |
| 354 collect_environment_data_fn_(&CollectEnvironmentData), | 372 collect_environment_data_fn_(&CollectEnvironmentData), |
| 355 environment_collection_task_runner_( | 373 environment_collection_task_runner_( |
| 356 content::BrowserThread::GetBlockingPool() | 374 content::BrowserThread::GetBlockingPool() |
| 357 ->GetTaskRunnerWithShutdownBehavior( | 375 ->GetTaskRunnerWithShutdownBehavior( |
| 358 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 376 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 359 environment_collection_pending_(), | 377 environment_collection_pending_(), |
| 360 collation_timeout_pending_(), | 378 collation_timeout_pending_(), |
| 361 collation_timer_(FROM_HERE, | 379 collation_timer_(FROM_HERE, |
| 362 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs), | 380 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs), |
| 363 this, | 381 this, |
| 364 &IncidentReportingService::OnCollationTimeout), | 382 &IncidentReportingService::OnCollationTimeout), |
| 365 delayed_analysis_callbacks_(delayed_task_interval, delayed_task_runner), | 383 delayed_analysis_callbacks_(delayed_task_interval, delayed_task_runner), |
| 384 download_metadata_manager_(content::BrowserThread::GetBlockingPool()), |
| 366 receiver_weak_ptr_factory_(this), | 385 receiver_weak_ptr_factory_(this), |
| 367 weak_ptr_factory_(this) { | 386 weak_ptr_factory_(this) { |
| 368 notification_registrar_.Add(this, | 387 notification_registrar_.Add(this, |
| 369 chrome::NOTIFICATION_PROFILE_ADDED, | 388 chrome::NOTIFICATION_PROFILE_ADDED, |
| 370 content::NotificationService::AllSources()); | 389 content::NotificationService::AllSources()); |
| 371 notification_registrar_.Add(this, | 390 notification_registrar_.Add(this, |
| 372 chrome::NOTIFICATION_PROFILE_DESTROYED, | 391 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 373 content::NotificationService::AllSources()); | 392 content::NotificationService::AllSources()); |
| 374 } | 393 } |
| 375 | 394 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 } | 449 } |
| 431 | 450 |
| 432 // Take another stab at finding the most recent download if a report is being | 451 // Take another stab at finding the most recent download if a report is being |
| 433 // assembled and one hasn't been found yet (the LastDownloadFinder operates | 452 // assembled and one hasn't been found yet (the LastDownloadFinder operates |
| 434 // only on profiles that have been added to the ProfileManager). | 453 // only on profiles that have been added to the ProfileManager). |
| 435 BeginDownloadCollection(); | 454 BeginDownloadCollection(); |
| 436 } | 455 } |
| 437 | 456 |
| 438 scoped_ptr<LastDownloadFinder> IncidentReportingService::CreateDownloadFinder( | 457 scoped_ptr<LastDownloadFinder> IncidentReportingService::CreateDownloadFinder( |
| 439 const LastDownloadFinder::LastDownloadCallback& callback) { | 458 const LastDownloadFinder::LastDownloadCallback& callback) { |
| 440 return LastDownloadFinder::Create(callback).Pass(); | 459 return LastDownloadFinder::Create( |
| 460 base::Bind(&DownloadMetadataManager::GetDownloadDetails, |
| 461 base::Unretained(&download_metadata_manager_)), |
| 462 callback).Pass(); |
| 441 } | 463 } |
| 442 | 464 |
| 443 scoped_ptr<IncidentReportUploader> IncidentReportingService::StartReportUpload( | 465 scoped_ptr<IncidentReportUploader> IncidentReportingService::StartReportUpload( |
| 444 const IncidentReportUploader::OnResultCallback& callback, | 466 const IncidentReportUploader::OnResultCallback& callback, |
| 445 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 467 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 446 const ClientIncidentReport& report) { | 468 const ClientIncidentReport& report) { |
| 447 return IncidentReportUploaderImpl::UploadReport( | 469 return IncidentReportUploaderImpl::UploadReport( |
| 448 callback, request_context_getter, report).Pass(); | 470 callback, request_context_getter, report).Pass(); |
| 449 } | 471 } |
| 450 | 472 |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 948 DCHECK(it != uploads_.end()); | 970 DCHECK(it != uploads_.end()); |
| 949 scoped_ptr<UploadContext> upload(context); // == *it | 971 scoped_ptr<UploadContext> upload(context); // == *it |
| 950 *it = uploads_.back(); | 972 *it = uploads_.back(); |
| 951 uploads_.weak_erase(uploads_.end() - 1); | 973 uploads_.weak_erase(uploads_.end() - 1); |
| 952 | 974 |
| 953 if (result == IncidentReportUploader::UPLOAD_SUCCESS) | 975 if (result == IncidentReportUploader::UPLOAD_SUCCESS) |
| 954 HandleResponse(*upload); | 976 HandleResponse(*upload); |
| 955 // else retry? | 977 // else retry? |
| 956 } | 978 } |
| 957 | 979 |
| 980 void IncidentReportingService::OnClientDownloadRequest( |
| 981 content::DownloadItem* download, |
| 982 const ClientDownloadRequest* request) { |
| 983 if (!download->GetBrowserContext()->IsOffTheRecord()) |
| 984 download_metadata_manager_.SetRequest(download, request); |
| 985 } |
| 986 |
| 958 void IncidentReportingService::Observe( | 987 void IncidentReportingService::Observe( |
| 959 int type, | 988 int type, |
| 960 const content::NotificationSource& source, | 989 const content::NotificationSource& source, |
| 961 const content::NotificationDetails& details) { | 990 const content::NotificationDetails& details) { |
| 962 switch (type) { | 991 switch (type) { |
| 963 case chrome::NOTIFICATION_PROFILE_ADDED: { | 992 case chrome::NOTIFICATION_PROFILE_ADDED: { |
| 964 Profile* profile = content::Source<Profile>(source).ptr(); | 993 Profile* profile = content::Source<Profile>(source).ptr(); |
| 965 if (!profile->IsOffTheRecord()) | 994 if (!profile->IsOffTheRecord()) |
| 966 OnProfileAdded(profile); | 995 OnProfileAdded(profile); |
| 967 break; | 996 break; |
| 968 } | 997 } |
| 969 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 998 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 970 Profile* profile = content::Source<Profile>(source).ptr(); | 999 Profile* profile = content::Source<Profile>(source).ptr(); |
| 971 if (!profile->IsOffTheRecord()) | 1000 if (!profile->IsOffTheRecord()) |
| 972 OnProfileDestroyed(profile); | 1001 OnProfileDestroyed(profile); |
| 973 break; | 1002 break; |
| 974 } | 1003 } |
| 975 default: | 1004 default: |
| 976 break; | 1005 break; |
| 977 } | 1006 } |
| 978 } | 1007 } |
| 979 | 1008 |
| 980 } // namespace safe_browsing | 1009 } // namespace safe_browsing |
| OLD | NEW |