OLD | NEW |
| (Empty) |
1 // Copyright 2015 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/browsing_data/counters/browsing_data_counter.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "components/browsing_data/browsing_data_utils.h" | |
11 #include "components/browsing_data/pref_names.h" | |
12 #include "components/prefs/pref_service.h" | |
13 | |
14 namespace browsing_data { | |
15 | |
16 BrowsingDataCounter::BrowsingDataCounter(const std::string& pref_name) | |
17 : pref_name_(pref_name) {} | |
18 | |
19 BrowsingDataCounter::~BrowsingDataCounter() {} | |
20 | |
21 void BrowsingDataCounter::Init(PrefService* pref_service, | |
22 const Callback& callback) { | |
23 DCHECK(!initialized_); | |
24 callback_ = callback; | |
25 pref_service_ = pref_service; | |
26 pref_.Init(GetPrefName(), pref_service_, | |
27 base::Bind(&BrowsingDataCounter::Restart, base::Unretained(this))); | |
28 period_.Init( | |
29 browsing_data::prefs::kDeleteTimePeriod, pref_service_, | |
30 base::Bind(&BrowsingDataCounter::Restart, base::Unretained(this))); | |
31 | |
32 initialized_ = true; | |
33 OnInitialized(); | |
34 } | |
35 | |
36 void BrowsingDataCounter::OnInitialized() {} | |
37 | |
38 base::Time BrowsingDataCounter::GetPeriodStart() { | |
39 return CalculateBeginDeleteTime(static_cast<TimePeriod>(*period_)); | |
40 } | |
41 | |
42 void BrowsingDataCounter::Restart() { | |
43 DCHECK(initialized_); | |
44 | |
45 // If this data type was unchecked for deletion, we do not need to count it. | |
46 if (!pref_service_->GetBoolean(GetPrefName())) | |
47 return; | |
48 | |
49 callback_.Run(base::WrapUnique(new Result(this))); | |
50 | |
51 Count(); | |
52 } | |
53 | |
54 void BrowsingDataCounter::ReportResult(ResultInt value) { | |
55 DCHECK(initialized_); | |
56 callback_.Run(base::WrapUnique(new FinishedResult(this, value))); | |
57 } | |
58 | |
59 void BrowsingDataCounter::ReportResult(std::unique_ptr<Result> result) { | |
60 DCHECK(initialized_); | |
61 callback_.Run(std::move(result)); | |
62 } | |
63 | |
64 const std::string& BrowsingDataCounter::GetPrefName() const { | |
65 return pref_name_; | |
66 } | |
67 | |
68 PrefService* BrowsingDataCounter::GetPrefs() const { | |
69 return pref_service_; | |
70 } | |
71 | |
72 // BrowsingDataCounter::Result ------------------------------------------------- | |
73 | |
74 BrowsingDataCounter::Result::Result(const BrowsingDataCounter* source) | |
75 : source_(source) {} | |
76 | |
77 BrowsingDataCounter::Result::~Result() {} | |
78 | |
79 bool BrowsingDataCounter::Result::Finished() const { | |
80 return false; | |
81 } | |
82 | |
83 // BrowsingDataCounter::FinishedResult ----------------------------------------- | |
84 | |
85 BrowsingDataCounter::FinishedResult::FinishedResult( | |
86 const BrowsingDataCounter* source, | |
87 ResultInt value) | |
88 : Result(source), value_(value) {} | |
89 | |
90 BrowsingDataCounter::FinishedResult::~FinishedResult() {} | |
91 | |
92 bool BrowsingDataCounter::FinishedResult::Finished() const { | |
93 return true; | |
94 } | |
95 | |
96 BrowsingDataCounter::ResultInt BrowsingDataCounter::FinishedResult::Value() | |
97 const { | |
98 return value_; | |
99 } | |
100 | |
101 } // namespace browsing_data | |
OLD | NEW |