Index: chrome/browser/android/data_usage/external_data_use_observer.cc |
diff --git a/chrome/browser/android/data_usage/external_data_use_observer.cc b/chrome/browser/android/data_usage/external_data_use_observer.cc |
index 2ffa3ff0914f875aed67afa3d6ea08f1c98c5686..a8c1f3c0f6fd2e653e093035fedf76ca27f06db0 100644 |
--- a/chrome/browser/android/data_usage/external_data_use_observer.cc |
+++ b/chrome/browser/android/data_usage/external_data_use_observer.cc |
@@ -8,7 +8,10 @@ |
#include "base/android/jni_string.h" |
#include "base/message_loop/message_loop.h" |
+#include "base/metrics/field_trial.h" |
+#include "base/strings/string_number_conversions.h" |
#include "components/data_usage/core/data_use.h" |
+#include "components/variations/variations_associated_data.h" |
#include "content/public/browser/browser_thread.h" |
#include "jni/ExternalDataUseObserver_jni.h" |
#include "third_party/re2/re2/re2.h" |
@@ -17,6 +20,47 @@ |
using base::android::ConvertUTF8ToJavaString; |
using base::android::ToJavaArrayOfStrings; |
+namespace { |
+ |
+const char kExternalDataUseObserverFieldTrial[] = "ExternalDataUseObserver"; |
+ |
+// Default duration after which matching rules are periodically fetched. May be |
+// overridden by the field trial. |
+const int kDefaultFetchMatchingRulesDurationSeconds = 60 * 15; // 15 minutes. |
+ |
+// Default value of the minimum number of bytes that should be buffered before |
+// a data use report is submitted. May be overridden by the field trial. |
+const int64_t kDefaultDataUseReportMinBytes = 100 * 1024; // 100 KB. |
+ |
+// Populates various parameters from the values specified in the field trial. |
+int32_t GetFetchMatchingRulesDurationSeconds() { |
+ int32_t duration_seconds = -1; |
+ std::string variation_value = variations::GetVariationParamValue( |
+ kExternalDataUseObserverFieldTrial, |
+ "fetch_matching_rules_duration_seconds"); |
+ if (!variation_value.empty() && |
+ base::StringToInt(variation_value, &duration_seconds)) { |
+ DCHECK_LE(0, duration_seconds); |
+ return duration_seconds; |
+ } |
+ return kDefaultFetchMatchingRulesDurationSeconds; |
+} |
+ |
+// Populates various parameters from the values specified in the field trial. |
+int64_t GetMinBytes() { |
+ int64_t min_bytes = -1; |
+ std::string variation_value = variations::GetVariationParamValue( |
+ kExternalDataUseObserverFieldTrial, "data_use_report_min_bytes"); |
+ if (!variation_value.empty() && |
+ base::StringToInt64(variation_value, &min_bytes)) { |
+ DCHECK_LE(0, min_bytes); |
+ return min_bytes; |
+ } |
+ return kDefaultDataUseReportMinBytes; |
+} |
+ |
+} // namespace |
+ |
namespace chrome { |
namespace android { |
@@ -32,11 +76,17 @@ ExternalDataUseObserver::ExternalDataUseObserver( |
io_task_runner_(io_task_runner), |
ui_task_runner_(ui_task_runner), |
previous_report_time_(base::Time::Now()), |
+ last_matching_rules_fetch_time_(base::TimeTicks::Now()), |
+ total_bytes_buffered_(0), |
+ fetch_matching_rules_duration_( |
+ base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), |
+ data_use_report_min_bytes_(GetMinBytes()), |
io_weak_factory_(this), |
ui_weak_factory_(this) { |
DCHECK(data_use_aggregator_); |
DCHECK(io_task_runner_); |
DCHECK(ui_task_runner_); |
+ |
ui_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&ExternalDataUseObserver::CreateJavaObjectOnUIThread, |
@@ -52,6 +102,11 @@ ExternalDataUseObserver::ExternalDataUseObserver( |
registered_as_observer_ = true; |
} |
+const std::string |
+ExternalDataUseObserver::GetExternalDataUseObserverFieldTrialName() const { |
+ return kExternalDataUseObserverFieldTrial; |
+} |
+ |
void ExternalDataUseObserver::CreateJavaObjectOnUIThread() { |
DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
JNIEnv* env = base::android::AttachCurrentThread(); |
@@ -154,6 +209,17 @@ void ExternalDataUseObserver::OnDataUse( |
const std::vector<const data_usage::DataUse*>& data_use_sequence) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ // If the time when the matching rules were last fetched is more than |
+ // |fetch_matching_rules_duration_|, fetch them again. |
+ if (base::TimeTicks::Now() - last_matching_rules_fetch_time_ >= |
+ fetch_matching_rules_duration_) { |
+ last_matching_rules_fetch_time_ = base::TimeTicks::Now(); |
+ ui_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&ExternalDataUseObserver::FetchMatchingRulesOnUIThread, |
+ GetUIWeakPtr())); |
+ } |
+ |
if (matching_rules_fetch_pending_) { |
// TODO(tbansal): Buffer reports. |
} |
@@ -191,6 +257,7 @@ void ExternalDataUseObserver::BufferDataUseReport( |
DataUseReport report = DataUseReport(start_time, end_time, data_use->rx_bytes, |
data_use->tx_bytes); |
+ total_bytes_buffered_ += (data_use->rx_bytes + data_use->tx_bytes); |
// Check if the |data_use_report_key| is already in the buffered reports. |
DataUseReports::iterator it = |
@@ -198,9 +265,9 @@ void ExternalDataUseObserver::BufferDataUseReport( |
if (it == buffered_data_reports_.end()) { |
// Limit the buffer size. |
if (buffered_data_reports_.size() == kMaxBufferSize) { |
+ 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.
|
// TODO(tbansal): Add UMA to track impact of lost reports. |
- // Remove the first entry. |
- buffered_data_reports_.erase(buffered_data_reports_.begin()); |
+ return; |
} |
buffered_data_reports_.insert(std::make_pair(data_use_report_key, report)); |
} else { |
@@ -215,8 +282,7 @@ void ExternalDataUseObserver::BufferDataUseReport( |
std::make_pair(data_use_report_key, merged_report)); |
} |
- DCHECK_LE(buffered_data_reports_.size(), |
- static_cast<size_t>(kMaxBufferSize)); |
+ DCHECK_LE(buffered_data_reports_.size(), static_cast<size_t>(kMaxBufferSize)); |
} |
void ExternalDataUseObserver::SubmitBufferedDataUseReport() { |
@@ -225,7 +291,8 @@ void ExternalDataUseObserver::SubmitBufferedDataUseReport() { |
if (submit_data_report_pending_ || buffered_data_reports_.empty()) |
return; |
- // TODO(tbansal): Keep buffering until enough data has been received. |
+ if (total_bytes_buffered_ < data_use_report_min_bytes_) |
+ return; |
// Send one data use report. |
DataUseReports::iterator it = buffered_data_reports_.begin(); |
@@ -234,6 +301,7 @@ void ExternalDataUseObserver::SubmitBufferedDataUseReport() { |
// Remove the entry from the map. |
buffered_data_reports_.erase(it); |
+ total_bytes_buffered_ -= (report.bytes_downloaded + report.bytes_uploaded); |
submit_data_report_pending_ = true; |