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. | |
| 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 * 1024; // 100 KB. | |
| 34 | |
| 35 // Populates various parameters from the values specified in the field trial. | |
| 36 int32_t GetFetchMatchingRulesDurationSeconds() { | |
| 37 int32_t duration_seconds = -1; | |
| 38 std::string variation_value = variations::GetVariationParamValue( | |
| 39 kExternalDataUseObserverFieldTrial, | |
| 40 "fetch_matching_rules_duration_seconds"); | |
| 41 if (!variation_value.empty() && | |
| 42 base::StringToInt(variation_value, &duration_seconds)) { | |
| 43 DCHECK_LE(0, duration_seconds); | |
| 44 return duration_seconds; | |
| 45 } | |
| 46 return kDefaultFetchMatchingRulesDurationSeconds; | |
| 47 } | |
| 48 | |
| 49 // Populates various parameters from the values specified in the field trial. | |
| 50 int64_t GetMinBytes() { | |
| 51 int64_t min_bytes = -1; | |
| 52 std::string variation_value = variations::GetVariationParamValue( | |
| 53 kExternalDataUseObserverFieldTrial, "data_use_report_min_bytes"); | |
| 54 if (!variation_value.empty() && | |
| 55 base::StringToInt64(variation_value, &min_bytes)) { | |
| 56 DCHECK_LE(0, min_bytes); | |
| 57 return min_bytes; | |
| 58 } | |
| 59 return kDefaultDataUseReportMinBytes; | |
| 60 } | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 20 namespace chrome { | 64 namespace chrome { |
| 21 | 65 |
| 22 namespace android { | 66 namespace android { |
| 23 | 67 |
| 24 ExternalDataUseObserver::ExternalDataUseObserver( | 68 ExternalDataUseObserver::ExternalDataUseObserver( |
| 25 data_usage::DataUseAggregator* data_use_aggregator, | 69 data_usage::DataUseAggregator* data_use_aggregator, |
| 26 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 70 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 27 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) | 71 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) |
| 28 : data_use_aggregator_(data_use_aggregator), | 72 : data_use_aggregator_(data_use_aggregator), |
| 29 matching_rules_fetch_pending_(false), | 73 matching_rules_fetch_pending_(false), |
| 30 submit_data_report_pending_(false), | 74 submit_data_report_pending_(false), |
| 31 registered_as_observer_(false), | 75 registered_as_observer_(false), |
| 32 io_task_runner_(io_task_runner), | 76 io_task_runner_(io_task_runner), |
| 33 ui_task_runner_(ui_task_runner), | 77 ui_task_runner_(ui_task_runner), |
| 34 previous_report_time_(base::Time::Now()), | 78 previous_report_time_(base::Time::Now()), |
| 79 last_matching_rules_fetch_time_(base::TimeTicks::Now()), | |
| 80 total_bytes_buffered_(0), | |
| 81 fetch_matching_rules_duration_( | |
| 82 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), | |
| 83 data_use_report_min_bytes_(GetMinBytes()), | |
| 35 io_weak_factory_(this), | 84 io_weak_factory_(this), |
| 36 ui_weak_factory_(this) { | 85 ui_weak_factory_(this) { |
| 37 DCHECK(data_use_aggregator_); | 86 DCHECK(data_use_aggregator_); |
| 38 DCHECK(io_task_runner_); | 87 DCHECK(io_task_runner_); |
| 39 DCHECK(ui_task_runner_); | 88 DCHECK(ui_task_runner_); |
| 89 | |
| 40 ui_task_runner_->PostTask( | 90 ui_task_runner_->PostTask( |
| 41 FROM_HERE, | 91 FROM_HERE, |
| 42 base::Bind(&ExternalDataUseObserver::CreateJavaObjectOnUIThread, | 92 base::Bind(&ExternalDataUseObserver::CreateJavaObjectOnUIThread, |
| 43 GetUIWeakPtr())); | 93 GetUIWeakPtr())); |
| 44 | 94 |
| 45 ui_task_runner_->PostTask( | 95 ui_task_runner_->PostTask( |
| 46 FROM_HERE, | 96 FROM_HERE, |
| 47 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, | 97 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, |
| 48 GetUIWeakPtr())); | 98 GetUIWeakPtr())); |
| 49 | 99 |
| 50 matching_rules_fetch_pending_ = true; | 100 matching_rules_fetch_pending_ = true; |
| 51 data_use_aggregator_->AddObserver(this); | 101 data_use_aggregator_->AddObserver(this); |
| 52 registered_as_observer_ = true; | 102 registered_as_observer_ = true; |
| 53 } | 103 } |
| 54 | 104 |
| 105 const std::string | |
| 106 ExternalDataUseObserver::GetExternalDataUseObserverFieldTrialName() const { | |
| 107 return kExternalDataUseObserverFieldTrial; | |
| 108 } | |
| 109 | |
| 55 void ExternalDataUseObserver::CreateJavaObjectOnUIThread() { | 110 void ExternalDataUseObserver::CreateJavaObjectOnUIThread() { |
| 56 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 111 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 57 JNIEnv* env = base::android::AttachCurrentThread(); | 112 JNIEnv* env = base::android::AttachCurrentThread(); |
| 58 j_external_data_use_observer_.Reset(Java_ExternalDataUseObserver_create( | 113 j_external_data_use_observer_.Reset(Java_ExternalDataUseObserver_create( |
| 59 env, base::android::GetApplicationContext(), | 114 env, base::android::GetApplicationContext(), |
| 60 reinterpret_cast<intptr_t>(this))); | 115 reinterpret_cast<intptr_t>(this))); |
| 61 DCHECK(!j_external_data_use_observer_.is_null()); | 116 DCHECK(!j_external_data_use_observer_.is_null()); |
| 62 } | 117 } |
| 63 | 118 |
| 64 ExternalDataUseObserver::~ExternalDataUseObserver() { | 119 ExternalDataUseObserver::~ExternalDataUseObserver() { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 | 202 |
| 148 submit_data_report_pending_ = false; | 203 submit_data_report_pending_ = false; |
| 149 | 204 |
| 150 SubmitBufferedDataUseReport(); | 205 SubmitBufferedDataUseReport(); |
| 151 } | 206 } |
| 152 | 207 |
| 153 void ExternalDataUseObserver::OnDataUse( | 208 void ExternalDataUseObserver::OnDataUse( |
| 154 const std::vector<const data_usage::DataUse*>& data_use_sequence) { | 209 const std::vector<const data_usage::DataUse*>& data_use_sequence) { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 | 211 |
| 212 // If the time when the matching rules were last fetched is more than | |
| 213 // |fetch_matching_rules_duration_|, fetch them again. | |
| 214 if (base::TimeTicks::Now() - last_matching_rules_fetch_time_ >= | |
| 215 fetch_matching_rules_duration_) { | |
| 216 last_matching_rules_fetch_time_ = base::TimeTicks::Now(); | |
| 217 ui_task_runner_->PostTask( | |
| 218 FROM_HERE, | |
| 219 base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, | |
| 220 GetUIWeakPtr())); | |
| 221 } | |
| 222 | |
| 157 if (matching_rules_fetch_pending_) { | 223 if (matching_rules_fetch_pending_) { |
| 158 // TODO(tbansal): Buffer reports. | 224 // TODO(tbansal): Buffer reports. |
| 159 } | 225 } |
| 160 | 226 |
| 161 std::string label; | 227 std::string label; |
| 162 | 228 |
| 163 for (const data_usage::DataUse* data_use : data_use_sequence) { | 229 for (const data_usage::DataUse* data_use : data_use_sequence) { |
| 164 if (!Matches(data_use->url, &label)) | 230 if (!Matches(data_use->url, &label)) |
| 165 continue; | 231 continue; |
| 166 | 232 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 184 DCHECK_LE(0, data_use->rx_bytes); | 250 DCHECK_LE(0, data_use->rx_bytes); |
| 185 DCHECK_LE(0, data_use->tx_bytes); | 251 DCHECK_LE(0, data_use->tx_bytes); |
| 186 if (data_use->rx_bytes < 0 || data_use->tx_bytes < 0) | 252 if (data_use->rx_bytes < 0 || data_use->tx_bytes < 0) |
| 187 return; | 253 return; |
| 188 | 254 |
| 189 DataUseReportKey data_use_report_key = | 255 DataUseReportKey data_use_report_key = |
| 190 DataUseReportKey(label, data_use->connection_type, data_use->mcc_mnc); | 256 DataUseReportKey(label, data_use->connection_type, data_use->mcc_mnc); |
| 191 | 257 |
| 192 DataUseReport report = DataUseReport(start_time, end_time, data_use->rx_bytes, | 258 DataUseReport report = DataUseReport(start_time, end_time, data_use->rx_bytes, |
| 193 data_use->tx_bytes); | 259 data_use->tx_bytes); |
| 260 total_bytes_buffered_ += (data_use->rx_bytes + data_use->tx_bytes); | |
| 194 | 261 |
| 195 // Check if the |data_use_report_key| is already in the buffered reports. | 262 // Check if the |data_use_report_key| is already in the buffered reports. |
| 196 DataUseReports::iterator it = | 263 DataUseReports::iterator it = |
| 197 buffered_data_reports_.find(data_use_report_key); | 264 buffered_data_reports_.find(data_use_report_key); |
| 198 if (it == buffered_data_reports_.end()) { | 265 if (it == buffered_data_reports_.end()) { |
| 199 // Limit the buffer size. | 266 // Limit the buffer size. |
| 200 if (buffered_data_reports_.size() == kMaxBufferSize) { | 267 if (buffered_data_reports_.size() == kMaxBufferSize) { |
| 268 total_bytes_buffered_ -= (data_use->rx_bytes + data_use->tx_bytes); | |
|
sclittle
2015/11/07 02:05:32
nit: Instead of adding the bytes above and subtrac
tbansal1
2015/11/07 02:33:03
Done.
| |
| 201 // TODO(tbansal): Add UMA to track impact of lost reports. | 269 // TODO(tbansal): Add UMA to track impact of lost reports. |
| 202 // Remove the first entry. | 270 return; |
| 203 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(), static_cast<size_t>(kMaxBufferSize)); |
| 219 static_cast<size_t>(kMaxBufferSize)); | |
| 220 } | 286 } |
| 221 | 287 |
| 222 void ExternalDataUseObserver::SubmitBufferedDataUseReport() { | 288 void ExternalDataUseObserver::SubmitBufferedDataUseReport() { |
| 223 DCHECK(thread_checker_.CalledOnValidThread()); | 289 DCHECK(thread_checker_.CalledOnValidThread()); |
| 224 | 290 |
| 225 if (submit_data_report_pending_ || buffered_data_reports_.empty()) | 291 if (submit_data_report_pending_ || buffered_data_reports_.empty()) |
| 226 return; | 292 return; |
| 227 | 293 |
| 228 // TODO(tbansal): Keep buffering until enough data has been received. | 294 if (total_bytes_buffered_ < data_use_report_min_bytes_) |
| 295 return; | |
| 229 | 296 |
| 230 // Send one data use report. | 297 // Send one data use report. |
| 231 DataUseReports::iterator it = buffered_data_reports_.begin(); | 298 DataUseReports::iterator it = buffered_data_reports_.begin(); |
| 232 DataUseReportKey key = it->first; | 299 DataUseReportKey key = it->first; |
| 233 DataUseReport report = it->second; | 300 DataUseReport report = it->second; |
| 234 | 301 |
| 235 // Remove the entry from the map. | 302 // Remove the entry from the map. |
| 236 buffered_data_reports_.erase(it); | 303 buffered_data_reports_.erase(it); |
| 304 total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); | |
| 237 | 305 |
| 238 submit_data_report_pending_ = true; | 306 submit_data_report_pending_ = true; |
| 239 | 307 |
| 240 ui_task_runner_->PostTask( | 308 ui_task_runner_->PostTask( |
| 241 FROM_HERE, base::Bind(&ExternalDataUseObserver::ReportDataUseOnUIThread, | 309 FROM_HERE, base::Bind(&ExternalDataUseObserver::ReportDataUseOnUIThread, |
| 242 GetIOWeakPtr(), key, report)); | 310 GetIOWeakPtr(), key, report)); |
| 243 } | 311 } |
| 244 | 312 |
| 245 void ExternalDataUseObserver::ReportDataUseOnUIThread( | 313 void ExternalDataUseObserver::ReportDataUseOnUIThread( |
| 246 const DataUseReportKey& key, | 314 const DataUseReportKey& key, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 return label_; | 402 return label_; |
| 335 } | 403 } |
| 336 | 404 |
| 337 bool RegisterExternalDataUseObserver(JNIEnv* env) { | 405 bool RegisterExternalDataUseObserver(JNIEnv* env) { |
| 338 return RegisterNativesImpl(env); | 406 return RegisterNativesImpl(env); |
| 339 } | 407 } |
| 340 | 408 |
| 341 } // namespace android | 409 } // namespace android |
| 342 | 410 |
| 343 } // namespace chrome | 411 } // namespace chrome |
| OLD | NEW |