Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/android/data_usage/external_data_use_observer.h" | 5 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 data_use_tab_model_(new DataUseTabModel( | 60 data_use_tab_model_(new DataUseTabModel( |
| 61 base::Bind(&ExternalDataUseObserverBridge::FetchMatchingRules, | 61 base::Bind(&ExternalDataUseObserverBridge::FetchMatchingRules, |
| 62 base::Unretained(external_data_use_observer_bridge_)), | 62 base::Unretained(external_data_use_observer_bridge_)), |
| 63 base::Bind( | 63 base::Bind( |
| 64 &ExternalDataUseObserverBridge::ShouldRegisterAsDataUseObserver, | 64 &ExternalDataUseObserverBridge::ShouldRegisterAsDataUseObserver, |
| 65 base::Unretained(external_data_use_observer_bridge_)))), | 65 base::Unretained(external_data_use_observer_bridge_)))), |
| 66 external_data_use_reporter_( | 66 external_data_use_reporter_( |
| 67 new ExternalDataUseReporter(kExternalDataUseObserverFieldTrial, | 67 new ExternalDataUseReporter(kExternalDataUseObserverFieldTrial, |
| 68 data_use_tab_model_, | 68 data_use_tab_model_, |
| 69 external_data_use_observer_bridge_)), | 69 external_data_use_observer_bridge_)), |
| 70 io_task_runner_(io_task_runner), | |
| 70 ui_task_runner_(ui_task_runner), | 71 ui_task_runner_(ui_task_runner), |
| 71 last_matching_rules_fetch_time_(base::TimeTicks::Now()), | 72 last_matching_rules_fetch_time_(base::TimeTicks::Now()), |
| 72 fetch_matching_rules_duration_( | 73 fetch_matching_rules_duration_( |
| 73 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), | 74 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), |
| 74 registered_as_data_use_observer_(false), | 75 registered_as_data_use_observer_(false), |
| 75 weak_factory_(this) { | 76 weak_factory_(this) { |
| 76 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 77 DCHECK(data_use_aggregator_); | 78 DCHECK(data_use_aggregator_); |
| 78 DCHECK(io_task_runner); | 79 DCHECK(io_task_runner_); |
| 79 DCHECK(ui_task_runner_); | 80 DCHECK(ui_task_runner_); |
| 80 | 81 |
| 81 // Initialize the ExternalDataUseReporter object. It is okay to use | 82 // Initialize the ExternalDataUseReporter object. It is okay to use |
| 82 // base::Unretained here since |external_data_use_reporter_| is owned by | 83 // base::Unretained here since |external_data_use_reporter_| is owned by |
| 83 // |this|, and is destroyed on UI thread when |this| is destroyed. | 84 // |this|, and is destroyed on UI thread when |this| is destroyed. |
| 84 ui_task_runner_->PostTask( | 85 ui_task_runner_->PostTask( |
| 85 FROM_HERE, base::Bind(&ExternalDataUseReporter::InitOnUIThread, | 86 FROM_HERE, base::Bind(&ExternalDataUseReporter::InitOnUIThread, |
| 86 base::Unretained(external_data_use_reporter_))); | 87 base::Unretained(external_data_use_reporter_))); |
| 87 | 88 |
| 88 // Initialize the ExternalDataUseObserverBridge object. It is okay to use | 89 // Initialize the ExternalDataUseObserverBridge object. It is okay to use |
| 89 // base::Unretained here since |external_data_use_observer_bridge_| is owned | 90 // base::Unretained here since |external_data_use_observer_bridge_| is owned |
| 90 // by |this|, and is destroyed on UI thread when |this| is destroyed. | 91 // by |this|, and is destroyed on UI thread when |this| is destroyed. |
| 91 ui_task_runner_->PostTask( | 92 ui_task_runner_->PostTask( |
| 92 FROM_HERE, | 93 FROM_HERE, |
| 93 base::Bind(&ExternalDataUseObserverBridge::Init, | 94 base::Bind(&ExternalDataUseObserverBridge::Init, |
| 94 base::Unretained(external_data_use_observer_bridge_), | 95 base::Unretained(external_data_use_observer_bridge_), |
| 95 io_task_runner, GetWeakPtr(), data_use_tab_model_)); | 96 io_task_runner_, GetWeakPtr(), data_use_tab_model_)); |
| 96 } | 97 } |
| 97 | 98 |
| 98 ExternalDataUseObserver::~ExternalDataUseObserver() { | 99 ExternalDataUseObserver::~ExternalDataUseObserver() { |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); | 100 DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 | 101 |
| 101 if (registered_as_data_use_observer_) | 102 if (registered_as_data_use_observer_) |
| 102 data_use_aggregator_->RemoveObserver(this); | 103 data_use_aggregator_->RemoveObserver(this); |
| 103 | 104 |
| 104 // Delete |external_data_use_reporter_| on the UI thread. | 105 // Delete |external_data_use_reporter_| on the UI thread. |
| 105 // |external_data_use_reporter_| should be deleted before | 106 // |external_data_use_reporter_| should be deleted before |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 119 external_data_use_observer_bridge_)) { | 120 external_data_use_observer_bridge_)) { |
| 120 NOTIMPLEMENTED() | 121 NOTIMPLEMENTED() |
| 121 << " ExternalDataUseObserverBridge was not deleted successfully"; | 122 << " ExternalDataUseObserverBridge was not deleted successfully"; |
| 122 } | 123 } |
| 123 } | 124 } |
| 124 | 125 |
| 125 void ExternalDataUseObserver::OnDataUse(const data_usage::DataUse& data_use) { | 126 void ExternalDataUseObserver::OnDataUse(const data_usage::DataUse& data_use) { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | 127 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 DCHECK(registered_as_data_use_observer_); | 128 DCHECK(registered_as_data_use_observer_); |
| 128 | 129 |
| 130 if (!data_use_list) { | |
| 131 data_use_list.reset(new std::deque<const data_usage::DataUse>()); | |
| 132 // Post a task to the same IO thread, that will get invoked when some of the | |
| 133 // data use objects are batched. | |
| 134 io_task_runner_->PostTask( | |
| 135 FROM_HERE, base::Bind(&ExternalDataUseObserver::OnDataUseBatchComplete, | |
| 136 GetWeakPtr())); | |
| 137 } | |
| 138 | |
| 139 DCHECK(data_use_list); | |
|
tbansal1
2016/07/21 04:40:30
This DCHECK is not needed because if it is null, t
Raj
2016/07/21 05:58:22
Done.
| |
| 140 data_use_list->push_back(data_use); | |
| 141 } | |
| 142 | |
| 143 void ExternalDataUseObserver::OnDataUseBatchComplete() { | |
|
tbansal1
2016/07/21 04:40:30
Add thread checker.
Raj
2016/07/21 05:58:22
Done.
| |
| 129 const base::TimeTicks now_ticks = base::TimeTicks::Now(); | 144 const base::TimeTicks now_ticks = base::TimeTicks::Now(); |
| 130 | 145 |
| 131 // If the time when the matching rules were last fetched is more than | 146 // If the time when the matching rules were last fetched is more than |
| 132 // |fetch_matching_rules_duration_|, fetch them again. | 147 // |fetch_matching_rules_duration_|, fetch them again. |
| 133 if (now_ticks - last_matching_rules_fetch_time_ >= | 148 if (now_ticks - last_matching_rules_fetch_time_ >= |
| 134 fetch_matching_rules_duration_) { | 149 fetch_matching_rules_duration_) { |
| 135 FetchMatchingRules(); | 150 FetchMatchingRules(); |
| 136 } | 151 } |
| 137 | 152 |
| 138 // It is okay to use base::Unretained here since |external_data_use_reporter_| | 153 // It is okay to use base::Unretained here since |external_data_use_reporter_| |
| 139 // is owned by |this|, and is destroyed on UI thread when |this| is destroyed. | 154 // is owned by |this|, and is destroyed on UI thread when |this| is destroyed. |
| 140 ui_task_runner_->PostTask( | 155 ui_task_runner_->PostTask( |
| 141 FROM_HERE, | 156 FROM_HERE, base::Bind(&ExternalDataUseReporter::OnDataUse, |
| 142 base::Bind(&ExternalDataUseReporter::OnDataUse, | 157 base::Unretained(external_data_use_reporter_), |
| 143 base::Unretained(external_data_use_reporter_), data_use)); | 158 base::Passed(&data_use_list))); |
|
tbansal1
2016/07/21 04:40:30
Add DCHECK(!data_use_list_);
Raj
2016/07/21 05:58:22
Done.
| |
| 144 } | 159 } |
| 145 | 160 |
| 146 void ExternalDataUseObserver::OnReportDataUseDone(bool success) { | 161 void ExternalDataUseObserver::OnReportDataUseDone(bool success) { |
| 147 DCHECK(thread_checker_.CalledOnValidThread()); | 162 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 | 163 |
| 149 // It is okay to use base::Unretained here since |external_data_use_reporter_| | 164 // It is okay to use base::Unretained here since |external_data_use_reporter_| |
| 150 // is owned by |this|, and is destroyed on UI thread when |this| is destroyed. | 165 // is owned by |this|, and is destroyed on UI thread when |this| is destroyed. |
| 151 ui_task_runner_->PostTask( | 166 ui_task_runner_->PostTask( |
| 152 FROM_HERE, | 167 FROM_HERE, |
| 153 base::Bind(&ExternalDataUseReporter::OnReportDataUseDone, | 168 base::Bind(&ExternalDataUseReporter::OnReportDataUseDone, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 } | 204 } |
| 190 | 205 |
| 191 DataUseTabModel* ExternalDataUseObserver::GetDataUseTabModel() const { | 206 DataUseTabModel* ExternalDataUseObserver::GetDataUseTabModel() const { |
| 192 DCHECK(thread_checker_.CalledOnValidThread()); | 207 DCHECK(thread_checker_.CalledOnValidThread()); |
| 193 return data_use_tab_model_; | 208 return data_use_tab_model_; |
| 194 } | 209 } |
| 195 | 210 |
| 196 } // namespace android | 211 } // namespace android |
| 197 | 212 |
| 198 } // namespace chrome | 213 } // namespace chrome |
| OLD | NEW |