Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: chrome/browser/safe_browsing/incident_reporting/incident_reporting_service.cc

Issue 1870003002: Convert //chrome/browser/safe_browsing from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and address comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <stddef.h> 8 #include <stddef.h>
9
9 #include <algorithm> 10 #include <algorithm>
10 #include <utility> 11 #include <utility>
11 #include <vector> 12 #include <vector>
12 13
13 #include "base/feature_list.h" 14 #include "base/feature_list.h"
14 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
15 #include "base/metrics/field_trial.h" 17 #include "base/metrics/field_trial.h"
16 #include "base/metrics/histogram.h" 18 #include "base/metrics/histogram.h"
17 #include "base/process/process_info.h" 19 #include "base/process/process_info.h"
18 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
19 #include "base/stl_util.h" 21 #include "base/stl_util.h"
20 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
21 #include "base/thread_task_runner_handle.h" 23 #include "base/thread_task_runner_handle.h"
22 #include "base/threading/sequenced_worker_pool.h" 24 #include "base/threading/sequenced_worker_pool.h"
23 #include "build/build_config.h" 25 #include "build/build_config.h"
24 #include "chrome/browser/chrome_notification_types.h" 26 #include "chrome/browser/chrome_notification_types.h"
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 145
144 struct IncidentReportingService::ProfileContext { 146 struct IncidentReportingService::ProfileContext {
145 ProfileContext(); 147 ProfileContext();
146 ~ProfileContext(); 148 ~ProfileContext();
147 149
148 // Returns true if the profile has incidents to be uploaded or cleared. 150 // Returns true if the profile has incidents to be uploaded or cleared.
149 bool HasIncidents() const; 151 bool HasIncidents() const;
150 152
151 // The incidents collected for this profile pending creation and/or upload. 153 // The incidents collected for this profile pending creation and/or upload.
152 // Will contain null values for pruned incidents. 154 // Will contain null values for pruned incidents.
153 std::vector<scoped_ptr<Incident>> incidents; 155 std::vector<std::unique_ptr<Incident>> incidents;
154 156
155 // The incidents data of which should be cleared. 157 // The incidents data of which should be cleared.
156 std::vector<scoped_ptr<Incident>> incidents_to_clear; 158 std::vector<std::unique_ptr<Incident>> incidents_to_clear;
157 159
158 // State storage for this profile; null until PROFILE_ADDED notification is 160 // State storage for this profile; null until PROFILE_ADDED notification is
159 // received. 161 // received.
160 scoped_ptr<StateStore> state_store; 162 std::unique_ptr<StateStore> state_store;
161 163
162 // False until PROFILE_ADDED notification is received. 164 // False until PROFILE_ADDED notification is received.
163 bool added; 165 bool added;
164 166
165 private: 167 private:
166 DISALLOW_COPY_AND_ASSIGN(ProfileContext); 168 DISALLOW_COPY_AND_ASSIGN(ProfileContext);
167 }; 169 };
168 170
169 class IncidentReportingService::UploadContext { 171 class IncidentReportingService::UploadContext {
170 public: 172 public:
171 typedef std::map<ProfileContext*, std::vector<PersistentIncidentState>> 173 typedef std::map<ProfileContext*, std::vector<PersistentIncidentState>>
172 PersistentIncidentStateCollection; 174 PersistentIncidentStateCollection;
173 175
174 explicit UploadContext(scoped_ptr<ClientIncidentReport> report); 176 explicit UploadContext(std::unique_ptr<ClientIncidentReport> report);
175 ~UploadContext(); 177 ~UploadContext();
176 178
177 // The report being uploaded. 179 // The report being uploaded.
178 scoped_ptr<ClientIncidentReport> report; 180 std::unique_ptr<ClientIncidentReport> report;
179 181
180 // The uploader in use. This is NULL until the CSD killswitch is checked. 182 // The uploader in use. This is NULL until the CSD killswitch is checked.
181 scoped_ptr<IncidentReportUploader> uploader; 183 std::unique_ptr<IncidentReportUploader> uploader;
182 184
183 // A mapping of profile contexts to the data to be persisted upon successful 185 // A mapping of profile contexts to the data to be persisted upon successful
184 // upload. 186 // upload.
185 PersistentIncidentStateCollection profiles_to_state; 187 PersistentIncidentStateCollection profiles_to_state;
186 188
187 private: 189 private:
188 DISALLOW_COPY_AND_ASSIGN(UploadContext); 190 DISALLOW_COPY_AND_ASSIGN(UploadContext);
189 }; 191 };
190 192
191 // An IncidentReceiver that is weakly-bound to the service and transparently 193 // An IncidentReceiver that is weakly-bound to the service and transparently
192 // bounces process-wide incidents back to the main thread for handling. 194 // bounces process-wide incidents back to the main thread for handling.
193 class IncidentReportingService::Receiver : public IncidentReceiver { 195 class IncidentReportingService::Receiver : public IncidentReceiver {
194 public: 196 public:
195 explicit Receiver(const base::WeakPtr<IncidentReportingService>& service); 197 explicit Receiver(const base::WeakPtr<IncidentReportingService>& service);
196 ~Receiver() override; 198 ~Receiver() override;
197 199
198 // IncidentReceiver methods: 200 // IncidentReceiver methods:
199 void AddIncidentForProfile(Profile* profile, 201 void AddIncidentForProfile(Profile* profile,
200 scoped_ptr<Incident> incident) override; 202 std::unique_ptr<Incident> incident) override;
201 void AddIncidentForProcess(scoped_ptr<Incident> incident) override; 203 void AddIncidentForProcess(std::unique_ptr<Incident> incident) override;
202 void ClearIncidentForProcess(scoped_ptr<Incident> incident) override; 204 void ClearIncidentForProcess(std::unique_ptr<Incident> incident) override;
203 205
204 private: 206 private:
205 static void AddIncidentOnMainThread( 207 static void AddIncidentOnMainThread(
206 const base::WeakPtr<IncidentReportingService>& service, 208 const base::WeakPtr<IncidentReportingService>& service,
207 Profile* profile, 209 Profile* profile,
208 scoped_ptr<Incident> incident); 210 std::unique_ptr<Incident> incident);
209 static void ClearIncidentOnMainThread( 211 static void ClearIncidentOnMainThread(
210 const base::WeakPtr<IncidentReportingService>& service, 212 const base::WeakPtr<IncidentReportingService>& service,
211 Profile* profile, 213 Profile* profile,
212 scoped_ptr<Incident> incident); 214 std::unique_ptr<Incident> incident);
213 215
214 base::WeakPtr<IncidentReportingService> service_; 216 base::WeakPtr<IncidentReportingService> service_;
215 scoped_refptr<base::SingleThreadTaskRunner> thread_runner_; 217 scoped_refptr<base::SingleThreadTaskRunner> thread_runner_;
216 218
217 DISALLOW_COPY_AND_ASSIGN(Receiver); 219 DISALLOW_COPY_AND_ASSIGN(Receiver);
218 }; 220 };
219 221
220 IncidentReportingService::Receiver::Receiver( 222 IncidentReportingService::Receiver::Receiver(
221 const base::WeakPtr<IncidentReportingService>& service) 223 const base::WeakPtr<IncidentReportingService>& service)
222 : service_(service), 224 : service_(service),
223 thread_runner_(base::ThreadTaskRunnerHandle::Get()) { 225 thread_runner_(base::ThreadTaskRunnerHandle::Get()) {
224 } 226 }
225 227
226 IncidentReportingService::Receiver::~Receiver() { 228 IncidentReportingService::Receiver::~Receiver() {
227 } 229 }
228 230
229 void IncidentReportingService::Receiver::AddIncidentForProfile( 231 void IncidentReportingService::Receiver::AddIncidentForProfile(
230 Profile* profile, 232 Profile* profile,
231 scoped_ptr<Incident> incident) { 233 std::unique_ptr<Incident> incident) {
232 DCHECK(thread_runner_->BelongsToCurrentThread()); 234 DCHECK(thread_runner_->BelongsToCurrentThread());
233 DCHECK(profile); 235 DCHECK(profile);
234 AddIncidentOnMainThread(service_, profile, std::move(incident)); 236 AddIncidentOnMainThread(service_, profile, std::move(incident));
235 } 237 }
236 238
237 void IncidentReportingService::Receiver::AddIncidentForProcess( 239 void IncidentReportingService::Receiver::AddIncidentForProcess(
238 scoped_ptr<Incident> incident) { 240 std::unique_ptr<Incident> incident) {
239 if (thread_runner_->BelongsToCurrentThread()) { 241 if (thread_runner_->BelongsToCurrentThread()) {
240 AddIncidentOnMainThread(service_, nullptr, std::move(incident)); 242 AddIncidentOnMainThread(service_, nullptr, std::move(incident));
241 } else { 243 } else {
242 thread_runner_->PostTask( 244 thread_runner_->PostTask(
243 FROM_HERE, 245 FROM_HERE,
244 base::Bind(&IncidentReportingService::Receiver::AddIncidentOnMainThread, 246 base::Bind(&IncidentReportingService::Receiver::AddIncidentOnMainThread,
245 service_, nullptr, base::Passed(&incident))); 247 service_, nullptr, base::Passed(&incident)));
246 } 248 }
247 } 249 }
248 250
249 void IncidentReportingService::Receiver::ClearIncidentForProcess( 251 void IncidentReportingService::Receiver::ClearIncidentForProcess(
250 scoped_ptr<Incident> incident) { 252 std::unique_ptr<Incident> incident) {
251 if (thread_runner_->BelongsToCurrentThread()) { 253 if (thread_runner_->BelongsToCurrentThread()) {
252 ClearIncidentOnMainThread(service_, nullptr, std::move(incident)); 254 ClearIncidentOnMainThread(service_, nullptr, std::move(incident));
253 } else { 255 } else {
254 thread_runner_->PostTask( 256 thread_runner_->PostTask(
255 FROM_HERE, 257 FROM_HERE,
256 base::Bind( 258 base::Bind(
257 &IncidentReportingService::Receiver::ClearIncidentOnMainThread, 259 &IncidentReportingService::Receiver::ClearIncidentOnMainThread,
258 service_, nullptr, base::Passed(&incident))); 260 service_, nullptr, base::Passed(&incident)));
259 } 261 }
260 } 262 }
261 263
262 bool IncidentReportingService::HasIncidentsToUpload() const { 264 bool IncidentReportingService::HasIncidentsToUpload() const {
263 for (const auto& profile_and_context : profiles_) { 265 for (const auto& profile_and_context : profiles_) {
264 if (!profile_and_context.second->incidents.empty()) 266 if (!profile_and_context.second->incidents.empty())
265 return true; 267 return true;
266 } 268 }
267 return false; 269 return false;
268 } 270 }
269 271
270 // static 272 // static
271 void IncidentReportingService::Receiver::AddIncidentOnMainThread( 273 void IncidentReportingService::Receiver::AddIncidentOnMainThread(
272 const base::WeakPtr<IncidentReportingService>& service, 274 const base::WeakPtr<IncidentReportingService>& service,
273 Profile* profile, 275 Profile* profile,
274 scoped_ptr<Incident> incident) { 276 std::unique_ptr<Incident> incident) {
275 if (service) 277 if (service)
276 service->AddIncident(profile, std::move(incident)); 278 service->AddIncident(profile, std::move(incident));
277 else 279 else
278 LogIncidentDataType(DISCARDED, *incident); 280 LogIncidentDataType(DISCARDED, *incident);
279 } 281 }
280 282
281 // static 283 // static
282 void IncidentReportingService::Receiver::ClearIncidentOnMainThread( 284 void IncidentReportingService::Receiver::ClearIncidentOnMainThread(
283 const base::WeakPtr<IncidentReportingService>& service, 285 const base::WeakPtr<IncidentReportingService>& service,
284 Profile* profile, 286 Profile* profile,
285 scoped_ptr<Incident> incident) { 287 std::unique_ptr<Incident> incident) {
286 if (service) 288 if (service)
287 service->ClearIncident(profile, std::move(incident)); 289 service->ClearIncident(profile, std::move(incident));
288 } 290 }
289 291
290 IncidentReportingService::ProfileContext::ProfileContext() : added(false) { 292 IncidentReportingService::ProfileContext::ProfileContext() : added(false) {
291 } 293 }
292 294
293 IncidentReportingService::ProfileContext::~ProfileContext() { 295 IncidentReportingService::ProfileContext::~ProfileContext() {
294 for (const auto& incident : incidents) { 296 for (const auto& incident : incidents) {
295 if (incident) 297 if (incident)
296 LogIncidentDataType(DISCARDED, *incident); 298 LogIncidentDataType(DISCARDED, *incident);
297 } 299 }
298 } 300 }
299 301
300 bool IncidentReportingService::ProfileContext::HasIncidents() const { 302 bool IncidentReportingService::ProfileContext::HasIncidents() const {
301 return !incidents.empty() || !incidents_to_clear.empty(); 303 return !incidents.empty() || !incidents_to_clear.empty();
302 } 304 }
303 305
304 IncidentReportingService::UploadContext::UploadContext( 306 IncidentReportingService::UploadContext::UploadContext(
305 scoped_ptr<ClientIncidentReport> report) 307 std::unique_ptr<ClientIncidentReport> report)
306 : report(std::move(report)) {} 308 : report(std::move(report)) {}
307 309
308 IncidentReportingService::UploadContext::~UploadContext() { 310 IncidentReportingService::UploadContext::~UploadContext() {
309 } 311 }
310 312
311 // static 313 // static
312 bool IncidentReportingService::IsEnabledForProfile(Profile* profile) { 314 bool IncidentReportingService::IsEnabledForProfile(Profile* profile) {
313 if (profile->IsOffTheRecord()) 315 if (profile->IsOffTheRecord())
314 return false; 316 return false;
315 if (!profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)) 317 if (!profile->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled))
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 // Cancel all internal asynchronous tasks. 380 // Cancel all internal asynchronous tasks.
379 weak_ptr_factory_.InvalidateWeakPtrs(); 381 weak_ptr_factory_.InvalidateWeakPtrs();
380 382
381 CancelEnvironmentCollection(); 383 CancelEnvironmentCollection();
382 CancelDownloadCollection(); 384 CancelDownloadCollection();
383 CancelAllReportUploads(); 385 CancelAllReportUploads();
384 386
385 STLDeleteValues(&profiles_); 387 STLDeleteValues(&profiles_);
386 } 388 }
387 389
388 scoped_ptr<IncidentReceiver> IncidentReportingService::GetIncidentReceiver() { 390 std::unique_ptr<IncidentReceiver>
389 return make_scoped_ptr(new Receiver(receiver_weak_ptr_factory_.GetWeakPtr())); 391 IncidentReportingService::GetIncidentReceiver() {
392 return base::WrapUnique(
393 new Receiver(receiver_weak_ptr_factory_.GetWeakPtr()));
390 } 394 }
391 395
392 scoped_ptr<TrackedPreferenceValidationDelegate> 396 std::unique_ptr<TrackedPreferenceValidationDelegate>
393 IncidentReportingService::CreatePreferenceValidationDelegate(Profile* profile) { 397 IncidentReportingService::CreatePreferenceValidationDelegate(Profile* profile) {
394 DCHECK(thread_checker_.CalledOnValidThread()); 398 DCHECK(thread_checker_.CalledOnValidThread());
395 399
396 if (profile->IsOffTheRecord()) 400 if (profile->IsOffTheRecord())
397 return scoped_ptr<TrackedPreferenceValidationDelegate>(); 401 return std::unique_ptr<TrackedPreferenceValidationDelegate>();
398 return scoped_ptr<TrackedPreferenceValidationDelegate>( 402 return std::unique_ptr<TrackedPreferenceValidationDelegate>(
399 new PreferenceValidationDelegate(profile, GetIncidentReceiver())); 403 new PreferenceValidationDelegate(profile, GetIncidentReceiver()));
400 } 404 }
401 405
402 void IncidentReportingService::RegisterDelayedAnalysisCallback( 406 void IncidentReportingService::RegisterDelayedAnalysisCallback(
403 const DelayedAnalysisCallback& callback) { 407 const DelayedAnalysisCallback& callback) {
404 DCHECK(thread_checker_.CalledOnValidThread()); 408 DCHECK(thread_checker_.CalledOnValidThread());
405 409
406 // |callback| will be run on the blocking pool. The receiver will bounce back 410 // |callback| will be run on the blocking pool. The receiver will bounce back
407 // to the origin thread if needed. 411 // to the origin thread if needed.
408 delayed_analysis_callbacks_.RegisterCallback( 412 delayed_analysis_callbacks_.RegisterCallback(
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 // Environment collection is deferred until at least one profile for which the 552 // Environment collection is deferred until at least one profile for which the
549 // service is enabled is added. Re-initiate collection now in case this is the 553 // service is enabled is added. Re-initiate collection now in case this is the
550 // first such profile. 554 // first such profile.
551 BeginEnvironmentCollection(); 555 BeginEnvironmentCollection();
552 // Take another stab at finding the most recent download if a report is being 556 // Take another stab at finding the most recent download if a report is being
553 // assembled and one hasn't been found yet (the LastDownloadFinder operates 557 // assembled and one hasn't been found yet (the LastDownloadFinder operates
554 // only on profiles that have been added to the ProfileManager). 558 // only on profiles that have been added to the ProfileManager).
555 BeginDownloadCollection(); 559 BeginDownloadCollection();
556 } 560 }
557 561
558 scoped_ptr<LastDownloadFinder> IncidentReportingService::CreateDownloadFinder( 562 std::unique_ptr<LastDownloadFinder>
563 IncidentReportingService::CreateDownloadFinder(
559 const LastDownloadFinder::LastDownloadCallback& callback) { 564 const LastDownloadFinder::LastDownloadCallback& callback) {
560 return LastDownloadFinder::Create( 565 return LastDownloadFinder::Create(
561 base::Bind(&DownloadMetadataManager::GetDownloadDetails, 566 base::Bind(&DownloadMetadataManager::GetDownloadDetails,
562 base::Unretained(&download_metadata_manager_)), 567 base::Unretained(&download_metadata_manager_)),
563 callback); 568 callback);
564 } 569 }
565 570
566 scoped_ptr<IncidentReportUploader> IncidentReportingService::StartReportUpload( 571 std::unique_ptr<IncidentReportUploader>
572 IncidentReportingService::StartReportUpload(
567 const IncidentReportUploader::OnResultCallback& callback, 573 const IncidentReportUploader::OnResultCallback& callback,
568 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, 574 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
569 const ClientIncidentReport& report) { 575 const ClientIncidentReport& report) {
570 return IncidentReportUploaderImpl::UploadReport( 576 return IncidentReportUploaderImpl::UploadReport(
571 callback, request_context_getter, report); 577 callback, request_context_getter, report);
572 } 578 }
573 579
574 bool IncidentReportingService::IsProcessingReport() const { 580 bool IncidentReportingService::IsProcessingReport() const {
575 return report_ != NULL; 581 return report_ != NULL;
576 } 582 }
(...skipping 15 matching lines...) Expand all
592 } 598 }
593 599
594 void IncidentReportingService::OnProfileDestroyed(Profile* profile) { 600 void IncidentReportingService::OnProfileDestroyed(Profile* profile) {
595 DCHECK(thread_checker_.CalledOnValidThread()); 601 DCHECK(thread_checker_.CalledOnValidThread());
596 602
597 ProfileContextCollection::iterator it = profiles_.find(profile); 603 ProfileContextCollection::iterator it = profiles_.find(profile);
598 if (it == profiles_.end()) 604 if (it == profiles_.end())
599 return; 605 return;
600 606
601 // Take ownership of the context. 607 // Take ownership of the context.
602 scoped_ptr<ProfileContext> context(it->second); 608 std::unique_ptr<ProfileContext> context(it->second);
603 it->second = nullptr; 609 it->second = nullptr;
604 610
605 // TODO(grt): Persist incidents for upload on future profile load. 611 // TODO(grt): Persist incidents for upload on future profile load.
606 612
607 // Remove the association with this profile context from all pending uploads. 613 // Remove the association with this profile context from all pending uploads.
608 for (const auto& upload : uploads_) 614 for (const auto& upload : uploads_)
609 upload->profiles_to_state.erase(context.get()); 615 upload->profiles_to_state.erase(context.get());
610 616
611 // Forget about this profile. Incidents not yet sent for upload are lost. 617 // Forget about this profile. Incidents not yet sent for upload are lost.
612 // No new incidents will be accepted for it. 618 // No new incidents will be accepted for it.
(...skipping 21 matching lines...) Expand all
634 } 640 }
635 // Otherwise, store this one as a candidate and keep looking (in case we 641 // Otherwise, store this one as a candidate and keep looking (in case we
636 // find one with Extended Reporting enabled). 642 // find one with Extended Reporting enabled).
637 candidate = scan->first; 643 candidate = scan->first;
638 } 644 }
639 645
640 return candidate; 646 return candidate;
641 } 647 }
642 648
643 void IncidentReportingService::AddIncident(Profile* profile, 649 void IncidentReportingService::AddIncident(Profile* profile,
644 scoped_ptr<Incident> incident) { 650 std::unique_ptr<Incident> incident) {
645 DCHECK(thread_checker_.CalledOnValidThread()); 651 DCHECK(thread_checker_.CalledOnValidThread());
646 652
647 // Ignore incidents from off-the-record profiles. 653 // Ignore incidents from off-the-record profiles.
648 if (profile && profile->IsOffTheRecord()) 654 if (profile && profile->IsOffTheRecord())
649 return; 655 return;
650 656
651 ProfileContext* context = GetOrCreateProfileContext(profile); 657 ProfileContext* context = GetOrCreateProfileContext(profile);
652 // If this is a process-wide incident, the context must not indicate that the 658 // If this is a process-wide incident, the context must not indicate that the
653 // profile (which is NULL) has been added to the profile manager. 659 // profile (which is NULL) has been added to the profile manager.
654 DCHECK(profile || !context->added); 660 DCHECK(profile || !context->added);
(...skipping 21 matching lines...) Expand all
676 } 682 }
677 last_incident_time_ = base::TimeTicks::Now(); 683 last_incident_time_ = base::TimeTicks::Now();
678 684
679 // Persist the incident data. 685 // Persist the incident data.
680 686
681 // Start assembling a new report if this is the first incident ever or the 687 // Start assembling a new report if this is the first incident ever or the
682 // first since the last upload. 688 // first since the last upload.
683 BeginReportProcessing(); 689 BeginReportProcessing();
684 } 690 }
685 691
686 void IncidentReportingService::ClearIncident(Profile* profile, 692 void IncidentReportingService::ClearIncident(
687 scoped_ptr<Incident> incident) { 693 Profile* profile,
694 std::unique_ptr<Incident> incident) {
688 ProfileContext* context = GetOrCreateProfileContext(profile); 695 ProfileContext* context = GetOrCreateProfileContext(profile);
689 context->incidents_to_clear.push_back(std::move(incident)); 696 context->incidents_to_clear.push_back(std::move(incident));
690 // Begin processing to handle cleared incidents following collation. 697 // Begin processing to handle cleared incidents following collation.
691 BeginReportProcessing(); 698 BeginReportProcessing();
692 } 699 }
693 700
694 void IncidentReportingService::BeginReportProcessing() { 701 void IncidentReportingService::BeginReportProcessing() {
695 DCHECK(thread_checker_.CalledOnValidThread()); 702 DCHECK(thread_checker_.CalledOnValidThread());
696 703
697 // Creates a new report if needed. 704 // Creates a new report if needed.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 if (environment_collection_pending_ || report_->has_environment() || 758 if (environment_collection_pending_ || report_->has_environment() ||
752 !HasIncidentsToUpload() || !FindEligibleProfile()) { 759 !HasIncidentsToUpload() || !FindEligibleProfile()) {
753 return; 760 return;
754 } 761 }
755 762
756 environment_collection_begin_ = base::TimeTicks::Now(); 763 environment_collection_begin_ = base::TimeTicks::Now();
757 ClientIncidentReport_EnvironmentData* environment_data = 764 ClientIncidentReport_EnvironmentData* environment_data =
758 new ClientIncidentReport_EnvironmentData(); 765 new ClientIncidentReport_EnvironmentData();
759 environment_collection_pending_ = 766 environment_collection_pending_ =
760 environment_collection_task_runner_->PostTaskAndReply( 767 environment_collection_task_runner_->PostTaskAndReply(
761 FROM_HERE, 768 FROM_HERE, base::Bind(collect_environment_data_fn_, environment_data),
762 base::Bind(collect_environment_data_fn_, environment_data),
763 base::Bind(&IncidentReportingService::OnEnvironmentDataCollected, 769 base::Bind(&IncidentReportingService::OnEnvironmentDataCollected,
764 weak_ptr_factory_.GetWeakPtr(), 770 weak_ptr_factory_.GetWeakPtr(),
765 base::Passed(make_scoped_ptr(environment_data)))); 771 base::Passed(base::WrapUnique(environment_data))));
766 772
767 // Posting the task will fail if the runner has been shut down. This should 773 // Posting the task will fail if the runner has been shut down. This should
768 // never happen since the blocking pool is shut down after this service. 774 // never happen since the blocking pool is shut down after this service.
769 DCHECK(environment_collection_pending_); 775 DCHECK(environment_collection_pending_);
770 } 776 }
771 777
772 bool IncidentReportingService::WaitingForEnvironmentCollection() { 778 bool IncidentReportingService::WaitingForEnvironmentCollection() {
773 return environment_collection_pending_; 779 return environment_collection_pending_;
774 } 780 }
775 781
776 void IncidentReportingService::CancelEnvironmentCollection() { 782 void IncidentReportingService::CancelEnvironmentCollection() {
777 environment_collection_begin_ = base::TimeTicks(); 783 environment_collection_begin_ = base::TimeTicks();
778 environment_collection_pending_ = false; 784 environment_collection_pending_ = false;
779 if (report_) 785 if (report_)
780 report_->clear_environment(); 786 report_->clear_environment();
781 } 787 }
782 788
783 void IncidentReportingService::OnEnvironmentDataCollected( 789 void IncidentReportingService::OnEnvironmentDataCollected(
784 scoped_ptr<ClientIncidentReport_EnvironmentData> environment_data) { 790 std::unique_ptr<ClientIncidentReport_EnvironmentData> environment_data) {
785 DCHECK(thread_checker_.CalledOnValidThread()); 791 DCHECK(thread_checker_.CalledOnValidThread());
786 DCHECK(environment_collection_pending_); 792 DCHECK(environment_collection_pending_);
787 DCHECK(report_ && !report_->has_environment()); 793 DCHECK(report_ && !report_->has_environment());
788 environment_collection_pending_ = false; 794 environment_collection_pending_ = false;
789 795
790 // CurrentProcessInfo::CreationTime() is missing on some platforms. 796 // CurrentProcessInfo::CreationTime() is missing on some platforms.
791 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) 797 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX)
792 base::TimeDelta uptime = 798 base::TimeDelta uptime =
793 first_incident_time_ - base::CurrentProcessInfo::CreationTime(); 799 first_incident_time_ - base::CurrentProcessInfo::CreationTime();
794 environment_data->mutable_process()->set_uptime_msec(uptime.InMilliseconds()); 800 environment_data->mutable_process()->set_uptime_msec(uptime.InMilliseconds());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 } 854 }
849 855
850 void IncidentReportingService::CancelDownloadCollection() { 856 void IncidentReportingService::CancelDownloadCollection() {
851 last_download_finder_.reset(); 857 last_download_finder_.reset();
852 last_download_begin_ = base::TimeTicks(); 858 last_download_begin_ = base::TimeTicks();
853 if (report_) 859 if (report_)
854 report_->clear_download(); 860 report_->clear_download();
855 } 861 }
856 862
857 void IncidentReportingService::OnLastDownloadFound( 863 void IncidentReportingService::OnLastDownloadFound(
858 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download, 864 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download,
859 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> 865 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails>
860 last_non_binary_download) { 866 last_non_binary_download) {
861 DCHECK(thread_checker_.CalledOnValidThread()); 867 DCHECK(thread_checker_.CalledOnValidThread());
862 DCHECK(report_); 868 DCHECK(report_);
863 869
864 UMA_HISTOGRAM_TIMES("SBIRS.FindDownloadedBinaryTime", 870 UMA_HISTOGRAM_TIMES("SBIRS.FindDownloadedBinaryTime",
865 base::TimeTicks::Now() - last_download_begin_); 871 base::TimeTicks::Now() - last_download_begin_);
866 last_download_begin_ = base::TimeTicks(); 872 last_download_begin_ = base::TimeTicks();
867 873
868 // Harvest the finder. 874 // Harvest the finder.
869 last_download_finder_.reset(); 875 last_download_finder_.reset();
(...skipping 12 matching lines...) Expand all
882 void IncidentReportingService::ProcessIncidentsIfCollectionComplete() { 888 void IncidentReportingService::ProcessIncidentsIfCollectionComplete() {
883 DCHECK(report_); 889 DCHECK(report_);
884 // Bail out if there are still outstanding collection tasks. Completion of any 890 // Bail out if there are still outstanding collection tasks. Completion of any
885 // of these will start another upload attempt. 891 // of these will start another upload attempt.
886 if (WaitingForEnvironmentCollection() || WaitingToCollateIncidents() || 892 if (WaitingForEnvironmentCollection() || WaitingToCollateIncidents() ||
887 WaitingForMostRecentDownload()) { 893 WaitingForMostRecentDownload()) {
888 return; 894 return;
889 } 895 }
890 896
891 // Take ownership of the report and clear things for future reports. 897 // Take ownership of the report and clear things for future reports.
892 scoped_ptr<ClientIncidentReport> report(std::move(report_)); 898 std::unique_ptr<ClientIncidentReport> report(std::move(report_));
893 first_incident_time_ = base::Time(); 899 first_incident_time_ = base::Time();
894 last_incident_time_ = base::TimeTicks(); 900 last_incident_time_ = base::TimeTicks();
895 901
896 ClientIncidentReport_EnvironmentData_Process* process = 902 ClientIncidentReport_EnvironmentData_Process* process =
897 report->mutable_environment()->mutable_process(); 903 report->mutable_environment()->mutable_process();
898 904
899 process->set_metrics_consent( 905 process->set_metrics_consent(
900 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled()); 906 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled());
901 907
902 // Find the profile that benefits from the strongest protections. 908 // Find the profile that benefits from the strongest protections.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1011 IncidentReportUploader::NUM_UPLOAD_RESULTS); 1017 IncidentReportUploader::NUM_UPLOAD_RESULTS);
1012 } 1018 }
1013 return; 1019 return;
1014 } 1020 }
1015 1021
1016 UMA_HISTOGRAM_COUNTS_100("SBIRS.IncidentCount", count); 1022 UMA_HISTOGRAM_COUNTS_100("SBIRS.IncidentCount", count);
1017 1023
1018 // Perform final synchronous collection tasks for the report. 1024 // Perform final synchronous collection tasks for the report.
1019 DoExtensionCollection(report->mutable_extension_data()); 1025 DoExtensionCollection(report->mutable_extension_data());
1020 1026
1021 scoped_ptr<UploadContext> context(new UploadContext(std::move(report))); 1027 std::unique_ptr<UploadContext> context(new UploadContext(std::move(report)));
1022 context->profiles_to_state.swap(profiles_to_state); 1028 context->profiles_to_state.swap(profiles_to_state);
1023 if (!database_manager_.get()) { 1029 if (!database_manager_.get()) {
1024 // No database manager during testing. Take ownership of the context and 1030 // No database manager during testing. Take ownership of the context and
1025 // continue processing. 1031 // continue processing.
1026 UploadContext* temp_context = context.get(); 1032 UploadContext* temp_context = context.get();
1027 uploads_.push_back(std::move(context)); 1033 uploads_.push_back(std::move(context));
1028 IncidentReportingService::OnKillSwitchResult(temp_context, false); 1034 IncidentReportingService::OnKillSwitchResult(temp_context, false);
1029 } else { 1035 } else {
1030 if (content::BrowserThread::PostTaskAndReplyWithResult( 1036 if (content::BrowserThread::PostTaskAndReplyWithResult(
1031 content::BrowserThread::IO, 1037 content::BrowserThread::IO,
(...skipping 22 matching lines...) Expand all
1054 DCHECK(thread_checker_.CalledOnValidThread()); 1060 DCHECK(thread_checker_.CalledOnValidThread());
1055 if (!is_killswitch_on) { 1061 if (!is_killswitch_on) {
1056 // Initiate the upload. 1062 // Initiate the upload.
1057 context->uploader = StartReportUpload( 1063 context->uploader = StartReportUpload(
1058 base::Bind(&IncidentReportingService::OnReportUploadResult, 1064 base::Bind(&IncidentReportingService::OnReportUploadResult,
1059 weak_ptr_factory_.GetWeakPtr(), context), 1065 weak_ptr_factory_.GetWeakPtr(), context),
1060 url_request_context_getter_, *context->report); 1066 url_request_context_getter_, *context->report);
1061 if (!context->uploader) { 1067 if (!context->uploader) {
1062 OnReportUploadResult(context, 1068 OnReportUploadResult(context,
1063 IncidentReportUploader::UPLOAD_INVALID_REQUEST, 1069 IncidentReportUploader::UPLOAD_INVALID_REQUEST,
1064 scoped_ptr<ClientIncidentResponse>()); 1070 std::unique_ptr<ClientIncidentResponse>());
1065 } 1071 }
1066 } else { 1072 } else {
1067 OnReportUploadResult(context, 1073 OnReportUploadResult(context, IncidentReportUploader::UPLOAD_SUPPRESSED,
1068 IncidentReportUploader::UPLOAD_SUPPRESSED, 1074 std::unique_ptr<ClientIncidentResponse>());
1069 scoped_ptr<ClientIncidentResponse>());
1070 } 1075 }
1071 } 1076 }
1072 1077
1073 void IncidentReportingService::HandleResponse(const UploadContext& context) { 1078 void IncidentReportingService::HandleResponse(const UploadContext& context) {
1074 // Mark each incident as reported in its corresponding profile's state store. 1079 // Mark each incident as reported in its corresponding profile's state store.
1075 for (const auto& context_and_states : context.profiles_to_state) { 1080 for (const auto& context_and_states : context.profiles_to_state) {
1076 StateStore::Transaction transaction( 1081 StateStore::Transaction transaction(
1077 context_and_states.first->state_store.get()); 1082 context_and_states.first->state_store.get());
1078 for (const auto& state : context_and_states.second) 1083 for (const auto& state : context_and_states.second)
1079 transaction.MarkAsReported(state.type, state.key, state.digest); 1084 transaction.MarkAsReported(state.type, state.key, state.digest);
1080 } 1085 }
1081 } 1086 }
1082 1087
1083 void IncidentReportingService::OnReportUploadResult( 1088 void IncidentReportingService::OnReportUploadResult(
1084 UploadContext* context, 1089 UploadContext* context,
1085 IncidentReportUploader::Result result, 1090 IncidentReportUploader::Result result,
1086 scoped_ptr<ClientIncidentResponse> response) { 1091 std::unique_ptr<ClientIncidentResponse> response) {
1087 DCHECK(thread_checker_.CalledOnValidThread()); 1092 DCHECK(thread_checker_.CalledOnValidThread());
1088 1093
1089 UMA_HISTOGRAM_ENUMERATION( 1094 UMA_HISTOGRAM_ENUMERATION(
1090 "SBIRS.UploadResult", result, IncidentReportUploader::NUM_UPLOAD_RESULTS); 1095 "SBIRS.UploadResult", result, IncidentReportUploader::NUM_UPLOAD_RESULTS);
1091 1096
1092 // The upload is no longer outstanding, so take ownership of the context (from 1097 // The upload is no longer outstanding, so take ownership of the context (from
1093 // the collection of outstanding uploads) in this scope. 1098 // the collection of outstanding uploads) in this scope.
1094 auto it = std::find_if(uploads_.begin(), uploads_.end(), 1099 auto it =
1095 [context] (const scoped_ptr<UploadContext>& value) { 1100 std::find_if(uploads_.begin(), uploads_.end(),
1096 return value.get() == context; 1101 [context](const std::unique_ptr<UploadContext>& value) {
1097 }); 1102 return value.get() == context;
1103 });
1098 DCHECK(it != uploads_.end()); 1104 DCHECK(it != uploads_.end());
1099 scoped_ptr<UploadContext> upload(std::move(*it)); 1105 std::unique_ptr<UploadContext> upload(std::move(*it));
1100 uploads_.erase(it); 1106 uploads_.erase(it);
1101 1107
1102 if (result == IncidentReportUploader::UPLOAD_SUCCESS) 1108 if (result == IncidentReportUploader::UPLOAD_SUCCESS)
1103 HandleResponse(*upload); 1109 HandleResponse(*upload);
1104 // else retry? 1110 // else retry?
1105 } 1111 }
1106 1112
1107 void IncidentReportingService::OnClientDownloadRequest( 1113 void IncidentReportingService::OnClientDownloadRequest(
1108 content::DownloadItem* download, 1114 content::DownloadItem* download,
1109 const ClientDownloadRequest* request) { 1115 const ClientDownloadRequest* request) {
(...skipping 19 matching lines...) Expand all
1129 if (!profile->IsOffTheRecord()) 1135 if (!profile->IsOffTheRecord())
1130 OnProfileDestroyed(profile); 1136 OnProfileDestroyed(profile);
1131 break; 1137 break;
1132 } 1138 }
1133 default: 1139 default:
1134 break; 1140 break;
1135 } 1141 }
1136 } 1142 }
1137 1143
1138 } // namespace safe_browsing 1144 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698