OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/rappor/rappor_recorder.h" |
| 6 |
| 7 #include "base/at_exit.h" |
| 8 #include "base/debug/leak_annotations.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/synchronization/lock.h" |
| 12 #include "components/rappor/rappor.h" |
| 13 |
| 14 namespace rappor { |
| 15 |
| 16 // Initialize rappor gathering system. |
| 17 base::LazyInstance<RapporRecorder>::Leaky g_rappor_recorder = |
| 18 LAZY_INSTANCE_INITIALIZER; |
| 19 |
| 20 // static |
| 21 void RapporRecorder::DeleteRappors(Rappors* rappors) { |
| 22 for (Rappors::iterator it = rappors->begin(); rappors->end() != it; ++it) { |
| 23 delete *it; |
| 24 } |
| 25 rappors->clear(); |
| 26 } |
| 27 |
| 28 void RapporRecorder::CollectRappors(Rappors* output) { |
| 29 base::AutoLock auto_lock(lock_); |
| 30 |
| 31 for (RapporMap::iterator it = rappors_.begin(); rappors_.end() != it; ++it) { |
| 32 DCHECK_EQ(it->first, it->second->parameters()->rappor_name); |
| 33 output->push_back(it->second); |
| 34 } |
| 35 rappors_.clear(); |
| 36 } |
| 37 |
| 38 void RapporRecorder::RecordSamples(const RapporParameters* parameters, |
| 39 const std::vector<std::string>& samples) { |
| 40 base::AutoLock auto_lock(lock_); |
| 41 |
| 42 Rappor* rappor = GetRappor(parameters); |
| 43 rappor->AddSamples(samples); |
| 44 } |
| 45 |
| 46 Rappor* RapporRecorder::GetRappor(const RapporParameters* parameters) { |
| 47 RapporMap::iterator it = rappors_.find(parameters->rappor_name); |
| 48 if (rappors_.end() != it) { |
| 49 Rappor* rappor = it->second; |
| 50 DCHECK_EQ(parameters, rappor->parameters()); |
| 51 return rappor; |
| 52 } |
| 53 |
| 54 Rappor* new_rappor = new Rappor(parameters); |
| 55 rappors_[parameters->rappor_name] = new_rappor; |
| 56 return new_rappor; |
| 57 } |
| 58 |
| 59 RapporRecorder::RapporRecorder() {} |
| 60 |
| 61 RapporRecorder::~RapporRecorder() { |
| 62 Rappors rappors; |
| 63 CollectRappors(&rappors); |
| 64 DeleteRappors(&rappors); |
| 65 } |
| 66 |
| 67 } // namespace rappor |
OLD | NEW |