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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/metrics/field_trial.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 11 #include "components/data_usage/core/data_use.h" | 13 #include "components/data_usage/core/data_use.h" |
| 14 #include "components/variations/variations_associated_data.h" | |
| 12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 13 #include "jni/ExternalDataUseObserver_jni.h" | 16 #include "jni/ExternalDataUseObserver_jni.h" |
| 14 #include "third_party/re2/re2/re2.h" | 17 #include "third_party/re2/re2/re2.h" |
| 15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 16 | 19 |
| 17 using base::android::ConvertUTF8ToJavaString; | 20 using base::android::ConvertUTF8ToJavaString; |
| 18 using base::android::ToJavaArrayOfStrings; | 21 using base::android::ToJavaArrayOfStrings; |
| 19 | 22 |
| 23 namespace { | |
| 24 | |
| 25 const char kExternalDataUseObserverFieldTrial[] = "ExternalDataUseObserver"; | |
| 26 | |
| 27 // Default duration after which matching rules are periodically fetched. May be | |
| 28 // overridden by the field trial. | |
| 29 const int kDefaultFetchMatchingRulesDurationSeconds = 60 * 15; // 15 minutes | |
|
sclittle
2015/11/06 23:42:49
nit: add period at the end of the comment.
tbansal1
2015/11/07 01:32:43
Done.
| |
| 30 | |
| 31 // Default value of the minimum number of bytes that should be buffered before | |
| 32 // a data use report is submitted. May be overridden by the field trial. | |
| 33 const int64_t kDefaultDataUseReportMinBytes = 100 * 1000; // 100 KB. | |
|
sclittle
2015/11/06 23:42:48
nit: 1 KB = 1024 bytes
tbansal1
2015/11/07 01:32:43
Done.
| |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 20 namespace chrome { | 37 namespace chrome { |
| 21 | 38 |
| 22 namespace android { | 39 namespace android { |
| 23 | 40 |
| 24 ExternalDataUseObserver::ExternalDataUseObserver( | 41 ExternalDataUseObserver::ExternalDataUseObserver( |
| 25 data_usage::DataUseAggregator* data_use_aggregator, | 42 data_usage::DataUseAggregator* data_use_aggregator, |
| 26 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 43 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 27 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) | 44 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) |
| 28 : data_use_aggregator_(data_use_aggregator), | 45 : data_use_aggregator_(data_use_aggregator), |
| 29 matching_rules_fetch_pending_(false), | 46 matching_rules_fetch_pending_(false), |
| 30 submit_data_report_pending_(false), | 47 submit_data_report_pending_(false), |
| 31 registered_as_observer_(false), | 48 registered_as_observer_(false), |
| 32 io_task_runner_(io_task_runner), | 49 io_task_runner_(io_task_runner), |
| 33 ui_task_runner_(ui_task_runner), | 50 ui_task_runner_(ui_task_runner), |
| 34 previous_report_time_(base::Time::Now()), | 51 previous_report_time_(base::Time::Now()), |
| 52 last_matching_rules_fetch_time_(base::TimeTicks::Now()), | |
| 53 total_bytes_buffered_(0), | |
| 54 fetch_matching_rules_duration_(base::TimeDelta::FromSeconds( | |
| 55 kDefaultFetchMatchingRulesDurationSeconds)), | |
| 56 data_use_report_min_bytes_(kDefaultDataUseReportMinBytes), | |
| 35 io_weak_factory_(this), | 57 io_weak_factory_(this), |
| 36 ui_weak_factory_(this) { | 58 ui_weak_factory_(this) { |
| 37 DCHECK(data_use_aggregator_); | 59 DCHECK(data_use_aggregator_); |
| 38 DCHECK(io_task_runner_); | 60 DCHECK(io_task_runner_); |
| 39 DCHECK(ui_task_runner_); | 61 DCHECK(ui_task_runner_); |
| 62 | |
| 63 PopulateFieldTrialParams(); | |
| 64 | |
| 40 ui_task_runner_->PostTask( | 65 ui_task_runner_->PostTask( |
| 41 FROM_HERE, | 66 FROM_HERE, |
| 42 base::Bind(&ExternalDataUseObserver::CreateJavaObjectOnUIThread, | 67 base::Bind(&ExternalDataUseObserver::CreateJavaObjectOnUIThread, |
| 43 GetUIWeakPtr())); | 68 GetUIWeakPtr())); |
| 44 | 69 |
| 45 ui_task_runner_->PostTask( | 70 ui_task_runner_->PostTask( |
| 46 FROM_HERE, | 71 FROM_HERE, |
| 47 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, | 72 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, |
| 48 GetUIWeakPtr())); | 73 GetUIWeakPtr())); |
| 49 | 74 |
| 50 matching_rules_fetch_pending_ = true; | 75 matching_rules_fetch_pending_ = true; |
| 51 data_use_aggregator_->AddObserver(this); | 76 data_use_aggregator_->AddObserver(this); |
| 52 registered_as_observer_ = true; | 77 registered_as_observer_ = true; |
| 53 } | 78 } |
| 54 | 79 |
| 80 const std::string | |
| 81 ExternalDataUseObserver::GetExternalDataUseObserverFieldTrialName() const { | |
| 82 return kExternalDataUseObserverFieldTrial; | |
| 83 } | |
| 84 | |
| 85 void ExternalDataUseObserver::PopulateFieldTrialParams() { | |
| 86 std::string field_trial = GetExternalDataUseObserverFieldTrialName(); | |
| 87 | |
| 88 int32_t fetch_matching_rules_duration_seconds = -1; | |
| 89 std::string variation_value = variations::GetVariationParamValue( | |
| 90 field_trial, "fetch_matching_rules_duration_seconds"); | |
| 91 if (!variation_value.empty() && | |
| 92 base::StringToInt(variation_value, | |
| 93 &fetch_matching_rules_duration_seconds)) { | |
| 94 fetch_matching_rules_duration_ = | |
| 95 base::TimeDelta::FromSeconds(fetch_matching_rules_duration_seconds); | |
| 96 } | |
| 97 DCHECK_GE(fetch_matching_rules_duration_, base::TimeDelta()); | |
| 98 | |
| 99 uint64_t data_use_report_min_bytes = 0; | |
|
sclittle
2015/11/06 23:42:50
Use an int64_t here instead of a uint64_t.
tbansal1
2015/11/07 01:32:43
Done.
| |
| 100 variation_value = variations::GetVariationParamValue( | |
| 101 field_trial, "data_use_report_min_bytes"); | |
| 102 if (!variation_value.empty() && | |
| 103 base::StringToUint64(variation_value, &data_use_report_min_bytes)) { | |
| 104 data_use_report_min_bytes_ = data_use_report_min_bytes; | |
|
sclittle
2015/11/06 23:42:50
Please don't convert a uint64_t to an int64_t with
tbansal1
2015/11/07 01:32:43
Done.
| |
| 105 } | |
| 106 DCHECK_GE(data_use_report_min_bytes_, 0); | |
| 107 } | |
| 108 | |
| 55 void ExternalDataUseObserver::CreateJavaObjectOnUIThread() { | 109 void ExternalDataUseObserver::CreateJavaObjectOnUIThread() { |
| 56 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 110 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 57 JNIEnv* env = base::android::AttachCurrentThread(); | 111 JNIEnv* env = base::android::AttachCurrentThread(); |
| 58 j_external_data_use_observer_.Reset(Java_ExternalDataUseObserver_create( | 112 j_external_data_use_observer_.Reset(Java_ExternalDataUseObserver_create( |
| 59 env, base::android::GetApplicationContext(), | 113 env, base::android::GetApplicationContext(), |
| 60 reinterpret_cast<intptr_t>(this))); | 114 reinterpret_cast<intptr_t>(this))); |
| 61 DCHECK(!j_external_data_use_observer_.is_null()); | 115 DCHECK(!j_external_data_use_observer_.is_null()); |
| 62 } | 116 } |
| 63 | 117 |
| 64 ExternalDataUseObserver::~ExternalDataUseObserver() { | 118 ExternalDataUseObserver::~ExternalDataUseObserver() { |
|
sclittle
2015/11/06 23:42:49
To help prevent losing any information, could we p
tbansal1
2015/11/07 01:32:43
The data use reports are asynchronously submitted
| |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 66 | 120 |
| 67 JNIEnv* env = base::android::AttachCurrentThread(); | 121 JNIEnv* env = base::android::AttachCurrentThread(); |
| 68 if (!j_external_data_use_observer_.is_null()) { | 122 if (!j_external_data_use_observer_.is_null()) { |
| 69 Java_ExternalDataUseObserver_onDestroy(env, | 123 Java_ExternalDataUseObserver_onDestroy(env, |
| 70 j_external_data_use_observer_.obj()); | 124 j_external_data_use_observer_.obj()); |
| 71 } | 125 } |
| 72 if (registered_as_observer_) | 126 if (registered_as_observer_) |
| 73 data_use_aggregator_->RemoveObserver(this); | 127 data_use_aggregator_->RemoveObserver(this); |
| 74 } | 128 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 | 201 |
| 148 submit_data_report_pending_ = false; | 202 submit_data_report_pending_ = false; |
| 149 | 203 |
| 150 SubmitBufferedDataUseReport(); | 204 SubmitBufferedDataUseReport(); |
| 151 } | 205 } |
| 152 | 206 |
| 153 void ExternalDataUseObserver::OnDataUse( | 207 void ExternalDataUseObserver::OnDataUse( |
| 154 const std::vector<const data_usage::DataUse*>& data_use_sequence) { | 208 const std::vector<const data_usage::DataUse*>& data_use_sequence) { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 209 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 | 210 |
| 211 if (base::TimeTicks::Now() - last_matching_rules_fetch_time_ >= | |
| 212 fetch_matching_rules_duration_) { | |
| 213 last_matching_rules_fetch_time_ = base::TimeTicks::Now(); | |
|
sclittle
2015/11/06 23:42:50
Could you add a quick comment around here describi
tbansal1
2015/11/07 01:32:43
Done.
| |
| 214 ui_task_runner_->PostTask( | |
| 215 FROM_HERE, | |
| 216 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, | |
| 217 GetUIWeakPtr())); | |
| 218 } | |
| 219 | |
| 157 if (matching_rules_fetch_pending_) { | 220 if (matching_rules_fetch_pending_) { |
| 158 // TODO(tbansal): Buffer reports. | 221 // TODO(tbansal): Buffer reports. |
| 159 } | 222 } |
| 160 | 223 |
| 161 std::string label; | 224 std::string label; |
| 162 | 225 |
| 163 for (const data_usage::DataUse* data_use : data_use_sequence) { | 226 for (const data_usage::DataUse* data_use : data_use_sequence) { |
| 164 if (!Matches(data_use->url, &label)) | 227 if (!Matches(data_use->url, &label)) |
| 165 continue; | 228 continue; |
| 166 | 229 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 184 DCHECK_LE(0, data_use->rx_bytes); | 247 DCHECK_LE(0, data_use->rx_bytes); |
| 185 DCHECK_LE(0, data_use->tx_bytes); | 248 DCHECK_LE(0, data_use->tx_bytes); |
| 186 if (data_use->rx_bytes < 0 || data_use->tx_bytes < 0) | 249 if (data_use->rx_bytes < 0 || data_use->tx_bytes < 0) |
| 187 return; | 250 return; |
| 188 | 251 |
| 189 DataUseReportKey data_use_report_key = | 252 DataUseReportKey data_use_report_key = |
| 190 DataUseReportKey(label, data_use->connection_type, data_use->mcc_mnc); | 253 DataUseReportKey(label, data_use->connection_type, data_use->mcc_mnc); |
| 191 | 254 |
| 192 DataUseReport report = DataUseReport(start_time, end_time, data_use->rx_bytes, | 255 DataUseReport report = DataUseReport(start_time, end_time, data_use->rx_bytes, |
| 193 data_use->tx_bytes); | 256 data_use->tx_bytes); |
| 257 total_bytes_buffered_ += (data_use->rx_bytes + data_use->tx_bytes); | |
| 194 | 258 |
| 195 // Check if the |data_use_report_key| is already in the buffered reports. | 259 // Check if the |data_use_report_key| is already in the buffered reports. |
| 196 DataUseReports::iterator it = | 260 DataUseReports::iterator it = |
| 197 buffered_data_reports_.find(data_use_report_key); | 261 buffered_data_reports_.find(data_use_report_key); |
| 198 if (it == buffered_data_reports_.end()) { | 262 if (it == buffered_data_reports_.end()) { |
| 199 // Limit the buffer size. | 263 // Limit the buffer size. |
| 200 if (buffered_data_reports_.size() == kMaxBufferSize) { | 264 if (buffered_data_reports_.size() == kMaxBufferSize) { |
| 201 // TODO(tbansal): Add UMA to track impact of lost reports. | 265 // TODO(tbansal): Add UMA to track impact of lost reports. |
| 202 // Remove the first entry. | 266 // Remove the first entry. |
|
sclittle
2015/11/06 23:42:49
Why do you remove the first entry? They aren't in
tbansal1
2015/11/07 01:32:43
Done.
| |
| 267 total_bytes_buffered_ -= | |
| 268 (buffered_data_reports_.begin()->second.bytes_downloaded + | |
| 269 buffered_data_reports_.begin()->second.bytes_uploaded); | |
| 203 buffered_data_reports_.erase(buffered_data_reports_.begin()); | 270 buffered_data_reports_.erase(buffered_data_reports_.begin()); |
| 204 } | 271 } |
| 205 buffered_data_reports_.insert(std::make_pair(data_use_report_key, report)); | 272 buffered_data_reports_.insert(std::make_pair(data_use_report_key, report)); |
| 206 } else { | 273 } else { |
| 207 DataUseReport existing_report = DataUseReport(it->second); | 274 DataUseReport existing_report = DataUseReport(it->second); |
| 208 DataUseReport merged_report = DataUseReport( | 275 DataUseReport merged_report = DataUseReport( |
| 209 std::min(existing_report.start_time, report.start_time), | 276 std::min(existing_report.start_time, report.start_time), |
| 210 std::max(existing_report.end_time, report.end_time), | 277 std::max(existing_report.end_time, report.end_time), |
| 211 existing_report.bytes_downloaded + report.bytes_downloaded, | 278 existing_report.bytes_downloaded + report.bytes_downloaded, |
| 212 existing_report.bytes_uploaded + report.bytes_uploaded); | 279 existing_report.bytes_uploaded + report.bytes_uploaded); |
| 213 buffered_data_reports_.erase(it); | 280 buffered_data_reports_.erase(it); |
| 214 buffered_data_reports_.insert( | 281 buffered_data_reports_.insert( |
| 215 std::make_pair(data_use_report_key, merged_report)); | 282 std::make_pair(data_use_report_key, merged_report)); |
| 216 } | 283 } |
| 217 | 284 |
| 218 DCHECK_LE(buffered_data_reports_.size(), | 285 DCHECK_LE(buffered_data_reports_.size(), |
|
sclittle
2015/11/06 23:42:49
nit: fix indentation
tbansal1
2015/11/07 01:32:43
Done.
| |
| 219 static_cast<size_t>(kMaxBufferSize)); | 286 static_cast<size_t>(kMaxBufferSize)); |
| 220 } | 287 } |
| 221 | 288 |
| 222 void ExternalDataUseObserver::SubmitBufferedDataUseReport() { | 289 void ExternalDataUseObserver::SubmitBufferedDataUseReport() { |
| 223 DCHECK(thread_checker_.CalledOnValidThread()); | 290 DCHECK(thread_checker_.CalledOnValidThread()); |
| 224 | 291 |
| 225 if (submit_data_report_pending_ || buffered_data_reports_.empty()) | 292 if (submit_data_report_pending_ || buffered_data_reports_.empty()) |
| 226 return; | 293 return; |
| 227 | 294 |
| 295 if (total_bytes_buffered_ < data_use_report_min_bytes_) | |
| 296 return; | |
| 297 | |
| 228 // TODO(tbansal): Keep buffering until enough data has been received. | 298 // TODO(tbansal): Keep buffering until enough data has been received. |
| 229 | 299 |
| 230 // Send one data use report. | 300 // Send one data use report. |
| 231 DataUseReports::iterator it = buffered_data_reports_.begin(); | 301 DataUseReports::iterator it = buffered_data_reports_.begin(); |
| 232 DataUseReportKey key = it->first; | 302 DataUseReportKey key = it->first; |
| 233 DataUseReport report = it->second; | 303 DataUseReport report = it->second; |
| 234 | 304 |
| 235 // Remove the entry from the map. | 305 // Remove the entry from the map. |
| 236 buffered_data_reports_.erase(it); | 306 buffered_data_reports_.erase(it); |
| 307 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); | |
| 237 | 308 |
| 238 submit_data_report_pending_ = true; | 309 submit_data_report_pending_ = true; |
| 239 | 310 |
| 240 ui_task_runner_->PostTask( | 311 ui_task_runner_->PostTask( |
| 241 FROM_HERE, base::Bind(&ExternalDataUseObserver::ReportDataUseOnUIThread, | 312 FROM_HERE, base::Bind(&ExternalDataUseObserver::ReportDataUseOnUIThread, |
| 242 GetIOWeakPtr(), key, report)); | 313 GetIOWeakPtr(), key, report)); |
| 243 } | 314 } |
| 244 | 315 |
| 245 void ExternalDataUseObserver::ReportDataUseOnUIThread( | 316 void ExternalDataUseObserver::ReportDataUseOnUIThread( |
| 246 const DataUseReportKey& key, | 317 const DataUseReportKey& key, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 return label_; | 405 return label_; |
| 335 } | 406 } |
| 336 | 407 |
| 337 bool RegisterExternalDataUseObserver(JNIEnv* env) { | 408 bool RegisterExternalDataUseObserver(JNIEnv* env) { |
| 338 return RegisterNativesImpl(env); | 409 return RegisterNativesImpl(env); |
| 339 } | 410 } |
| 340 | 411 |
| 341 } // namespace android | 412 } // namespace android |
| 342 | 413 |
| 343 } // namespace chrome | 414 } // namespace chrome |
| OLD | NEW |