| 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 data_use_list_->push_back(data_use); |
| 140 } |
| 141 |
| 142 void ExternalDataUseObserver::OnDataUseBatchComplete() { |
| 143 DCHECK(thread_checker_.CalledOnValidThread()); |
| 144 DCHECK(data_use_list_); |
| 145 |
| 129 const base::TimeTicks now_ticks = base::TimeTicks::Now(); | 146 const base::TimeTicks now_ticks = base::TimeTicks::Now(); |
| 130 | 147 |
| 131 // If the time when the matching rules were last fetched is more than | 148 // If the time when the matching rules were last fetched is more than |
| 132 // |fetch_matching_rules_duration_|, fetch them again. | 149 // |fetch_matching_rules_duration_|, fetch them again. |
| 133 if (now_ticks - last_matching_rules_fetch_time_ >= | 150 if (now_ticks - last_matching_rules_fetch_time_ >= |
| 134 fetch_matching_rules_duration_) { | 151 fetch_matching_rules_duration_) { |
| 135 FetchMatchingRules(); | 152 FetchMatchingRules(); |
| 136 } | 153 } |
| 137 | 154 |
| 138 // It is okay to use base::Unretained here since |external_data_use_reporter_| | 155 // 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. | 156 // is owned by |this|, and is destroyed on UI thread when |this| is destroyed. |
| 140 ui_task_runner_->PostTask( | 157 ui_task_runner_->PostTask( |
| 141 FROM_HERE, | 158 FROM_HERE, base::Bind(&ExternalDataUseReporter::OnDataUse, |
| 142 base::Bind(&ExternalDataUseReporter::OnDataUse, | 159 base::Unretained(external_data_use_reporter_), |
| 143 base::Unretained(external_data_use_reporter_), data_use)); | 160 base::Passed(&data_use_list_))); |
| 161 DCHECK(!data_use_list_); |
| 144 } | 162 } |
| 145 | 163 |
| 146 void ExternalDataUseObserver::OnReportDataUseDone(bool success) { | 164 void ExternalDataUseObserver::OnReportDataUseDone(bool success) { |
| 147 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 | 166 |
| 149 // It is okay to use base::Unretained here since |external_data_use_reporter_| | 167 // 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. | 168 // is owned by |this|, and is destroyed on UI thread when |this| is destroyed. |
| 151 ui_task_runner_->PostTask( | 169 ui_task_runner_->PostTask( |
| 152 FROM_HERE, | 170 FROM_HERE, |
| 153 base::Bind(&ExternalDataUseReporter::OnReportDataUseDone, | 171 base::Bind(&ExternalDataUseReporter::OnReportDataUseDone, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } | 207 } |
| 190 | 208 |
| 191 DataUseTabModel* ExternalDataUseObserver::GetDataUseTabModel() const { | 209 DataUseTabModel* ExternalDataUseObserver::GetDataUseTabModel() const { |
| 192 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 193 return data_use_tab_model_; | 211 return data_use_tab_model_; |
| 194 } | 212 } |
| 195 | 213 |
| 196 } // namespace android | 214 } // namespace android |
| 197 | 215 |
| 198 } // namespace chrome | 216 } // namespace chrome |
| OLD | NEW |