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 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ | 6 #define CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 const base::android::JavaParamRef<jobjectArray>& app_package_name, | 73 const base::android::JavaParamRef<jobjectArray>& app_package_name, |
| 74 const base::android::JavaParamRef<jobjectArray>& domain_path_regex, | 74 const base::android::JavaParamRef<jobjectArray>& domain_path_regex, |
| 75 const base::android::JavaParamRef<jobjectArray>& label); | 75 const base::android::JavaParamRef<jobjectArray>& label); |
| 76 | 76 |
| 77 // Called by Java when the reporting of data usage has finished. This may be | 77 // Called by Java when the reporting of data usage has finished. This may be |
| 78 // called on a different thread. |success| is true if the request was | 78 // called on a different thread. |success| is true if the request was |
| 79 // successfully submitted to the external data use observer by Java. Must be | 79 // successfully submitted to the external data use observer by Java. Must be |
| 80 // called on UI thread. | 80 // called on UI thread. |
| 81 void OnReportDataUseDone(JNIEnv* env, jobject obj, bool success); | 81 void OnReportDataUseDone(JNIEnv* env, jobject obj, bool success); |
| 82 | 82 |
| 83 // Returns the external data use observer field trial name. | |
| 84 const std::string GetExternalDataUseObserverFieldTrialName() const; | |
|
sclittle
2015/11/06 23:42:50
Make this a static constant.
tbansal1
2015/11/07 01:32:43
This function would be used by Raj to control othe
sclittle
2015/11/07 02:05:32
Could it just be a publicly visible static constan
tbansal1
2015/11/07 02:33:02
Done.
| |
| 85 | |
| 83 private: | 86 private: |
| 84 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, SingleRegex); | 87 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, SingleRegex); |
| 85 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, TwoRegex); | 88 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, TwoRegex); |
| 86 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, MultipleRegex); | 89 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, MultipleRegex); |
| 87 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, ChangeRegex); | 90 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, ChangeRegex); |
| 88 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, | 91 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, |
| 89 AtMostOneDataUseSubmitRequest); | 92 AtMostOneDataUseSubmitRequest); |
| 90 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, MultipleMatchingRules); | 93 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, MultipleMatchingRules); |
| 91 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, ReportsMergedCorrectly); | 94 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, ReportsMergedCorrectly); |
| 92 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, | 95 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, |
| 93 TimestampsMergedCorrectly); | 96 TimestampsMergedCorrectly); |
| 94 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, HashFunction); | 97 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, HashFunction); |
| 95 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, BufferSize); | 98 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, BufferSize); |
| 99 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, | |
| 100 PeriodicFetchMatchingRules); | |
| 101 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, BufferDataUseReports); | |
| 102 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, Variations); | |
| 96 | 103 |
| 97 // DataUseReportKey is a unique identifier for a data use report. | 104 // DataUseReportKey is a unique identifier for a data use report. |
| 98 struct DataUseReportKey { | 105 struct DataUseReportKey { |
| 99 DataUseReportKey(const std::string& label, | 106 DataUseReportKey(const std::string& label, |
| 100 net::NetworkChangeNotifier::ConnectionType connection_type, | 107 net::NetworkChangeNotifier::ConnectionType connection_type, |
| 101 const std::string& mcc_mnc) | 108 const std::string& mcc_mnc) |
| 102 : label(label), connection_type(connection_type), mcc_mnc(mcc_mnc) {} | 109 : label(label), connection_type(connection_type), mcc_mnc(mcc_mnc) {} |
| 103 | 110 |
| 104 DataUseReportKey(const DataUseReportKey& other) | 111 DataUseReportKey(const DataUseReportKey& other) |
| 105 : label(other.label), | 112 : label(other.label), |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 123 // mcc_mnc operator of the provider of the SIM as obtained from | 130 // mcc_mnc operator of the provider of the SIM as obtained from |
| 124 // TelephonyManager#getNetworkOperator() Java API in Android. | 131 // TelephonyManager#getNetworkOperator() Java API in Android. |
| 125 const std::string mcc_mnc; | 132 const std::string mcc_mnc; |
| 126 }; | 133 }; |
| 127 | 134 |
| 128 // DataUseReport is paired with a DataUseReportKey object. DataUseReport | 135 // DataUseReport is paired with a DataUseReportKey object. DataUseReport |
| 129 // contains the bytes send/received during a specific interval. Only the bytes | 136 // contains the bytes send/received during a specific interval. Only the bytes |
| 130 // from the data use reports that have the |label|, |connection_type|, and | 137 // from the data use reports that have the |label|, |connection_type|, and |
| 131 // |mcc_mnc| specified in the corresponding DataUseReportKey object are | 138 // |mcc_mnc| specified in the corresponding DataUseReportKey object are |
| 132 // counted in the DataUseReport. | 139 // counted in the DataUseReport. |
| 133 struct DataUseReport { | 140 struct DataUseReport { |
|
sclittle
2015/11/06 23:42:50
For a future CL: This DataUseReport stuff should r
tbansal1
2015/11/07 01:32:43
Agree with moving the implementation to .cc but wh
sclittle
2015/11/07 02:05:32
Good point, it might make sense to move them to se
tbansal1
2015/11/07 02:33:02
Acknowledged.
| |
| 134 // |start_time| and |end_time| are the start and end timestamps (in UTC | 141 // |start_time| and |end_time| are the start and end timestamps (in UTC |
| 135 // since the standard Java epoch of 1970-01-01 00:00:00) of the interval | 142 // since the standard Java epoch of 1970-01-01 00:00:00) of the interval |
| 136 // that this data report covers. |bytes_downloaded| and |bytes_uploaded| are | 143 // that this data report covers. |bytes_downloaded| and |bytes_uploaded| are |
| 137 // the total bytes received and send during this interval. | 144 // the total bytes received and send during this interval. |
| 138 DataUseReport(const base::Time& start_time, | 145 DataUseReport(const base::Time& start_time, |
| 139 const base::Time& end_time, | 146 const base::Time& end_time, |
| 140 int64_t bytes_downloaded, | 147 int64_t bytes_downloaded, |
| 141 int64_t bytes_uploaded) | 148 int64_t bytes_uploaded) |
| 142 : start_time(start_time), | 149 : start_time(start_time), |
| 143 end_time(end_time), | 150 end_time(end_time), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 size_t operator()(const DataUseReportKey& k) const { | 182 size_t operator()(const DataUseReportKey& k) const { |
| 176 std::hash<std::string> hash_function; | 183 std::hash<std::string> hash_function; |
| 177 size_t hash = 1; | 184 size_t hash = 1; |
| 178 hash = hash * 23 + hash_function(k.label); | 185 hash = hash * 23 + hash_function(k.label); |
| 179 hash = hash * 43 + k.connection_type; | 186 hash = hash * 43 + k.connection_type; |
| 180 hash = hash * 83 + hash_function(k.mcc_mnc); | 187 hash = hash * 83 + hash_function(k.mcc_mnc); |
| 181 return hash; | 188 return hash; |
| 182 } | 189 } |
| 183 }; | 190 }; |
| 184 | 191 |
| 185 typedef std::unordered_map<DataUseReportKey, | 192 typedef std::unordered_map<DataUseReportKey, |
|
sclittle
2015/11/06 23:42:50
Change this to use base::hash_map from https://cod
tbansal1
2015/11/07 01:32:43
Interesting. Fixed it.
| |
| 186 DataUseReport, | 193 DataUseReport, |
| 187 DataUseReportKeyHash> DataUseReports; | 194 DataUseReportKeyHash> DataUseReports; |
| 188 | 195 |
| 189 // Stores the matching rules. | 196 // Stores the matching rules. |
| 190 class MatchingRule { | 197 class MatchingRule { |
| 191 public: | 198 public: |
| 192 MatchingRule(const std::string& app_package_name, | 199 MatchingRule(const std::string& app_package_name, |
| 193 scoped_ptr<re2::RE2> pattern, | 200 scoped_ptr<re2::RE2> pattern, |
| 194 const std::string& label); | 201 const std::string& label); |
| 195 ~MatchingRule(); | 202 ~MatchingRule(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 207 // Opaque label that uniquely identifies this matching rule. | 214 // Opaque label that uniquely identifies this matching rule. |
| 208 const std::string label_; | 215 const std::string label_; |
| 209 | 216 |
| 210 DISALLOW_COPY_AND_ASSIGN(MatchingRule); | 217 DISALLOW_COPY_AND_ASSIGN(MatchingRule); |
| 211 }; | 218 }; |
| 212 | 219 |
| 213 // Maximum buffer size. If an entry needs to be added to the buffer that has | 220 // Maximum buffer size. If an entry needs to be added to the buffer that has |
| 214 // size |kMaxBufferSize|, then the oldest entry will be removed. | 221 // size |kMaxBufferSize|, then the oldest entry will be removed. |
| 215 static const size_t kMaxBufferSize = 100; | 222 static const size_t kMaxBufferSize = 100; |
| 216 | 223 |
| 224 // Populates various parameters from the values specified in the field trial. | |
| 225 void PopulateFieldTrialParams(); | |
|
sclittle
2015/11/06 23:42:50
Could this just be a few anonymous functions in th
tbansal1
2015/11/07 01:32:43
Done.
| |
| 226 | |
| 217 // Creates Java object. Must be called on the UI thread. | 227 // Creates Java object. Must be called on the UI thread. |
| 218 void CreateJavaObjectOnUIThread(); | 228 void CreateJavaObjectOnUIThread(); |
| 219 | 229 |
| 220 // data_usage::DataUseAggregator::Observer implementation: | 230 // data_usage::DataUseAggregator::Observer implementation: |
| 221 void OnDataUse(const std::vector<const data_usage::DataUse*>& | 231 void OnDataUse(const std::vector<const data_usage::DataUse*>& |
| 222 data_use_sequence) override; | 232 data_use_sequence) override; |
| 223 | 233 |
| 224 // Fetches matching rules from Java. Must be called on the UI thread. Returns | 234 // Fetches matching rules from Java. Must be called on the UI thread. Returns |
| 225 // result asynchronously on UI thread via FetchMatchingRulesCallback. | 235 // result asynchronously on UI thread via FetchMatchingRulesCallback. |
| 226 void FetchMatchingRulesOnUIThread() const; | 236 void FetchMatchingRulesOnUIThread() const; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 315 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 306 | 316 |
| 307 // |ui_task_runner_| is used to call Java code on UI thread. This ensures | 317 // |ui_task_runner_| is used to call Java code on UI thread. This ensures |
| 308 // that Java code is safely called only on a single thread, and eliminates | 318 // that Java code is safely called only on a single thread, and eliminates |
| 309 // the need for locks in Java. | 319 // the need for locks in Java. |
| 310 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 320 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 311 | 321 |
| 312 // Time when the data use reports were last received from DataUseAggregator. | 322 // Time when the data use reports were last received from DataUseAggregator. |
| 313 base::Time previous_report_time_; | 323 base::Time previous_report_time_; |
| 314 | 324 |
| 325 // Time when the matching rules were last fetched. | |
| 326 base::TimeTicks last_matching_rules_fetch_time_; | |
| 327 | |
| 328 // Total number of bytes transmitted or received across all the buffered | |
| 329 // reports. | |
| 330 int64_t total_bytes_buffered_; | |
| 331 | |
| 332 // Duration after which matching rules are periodically fetched. | |
| 333 base::TimeDelta fetch_matching_rules_duration_; | |
| 334 | |
| 335 // Minimum number of bytes that should be buffered before a data use report is | |
| 336 // submitted. | |
| 337 int64_t data_use_report_min_bytes_; | |
| 338 | |
| 315 base::ThreadChecker thread_checker_; | 339 base::ThreadChecker thread_checker_; |
| 316 | 340 |
| 317 // |io_weak_factory_| and |ui_weak_factory_| are used for posting tasks on the | 341 // |io_weak_factory_| and |ui_weak_factory_| are used for posting tasks on the |
| 318 // IO and UI thread, respectively. | 342 // IO and UI thread, respectively. |
| 319 base::WeakPtrFactory<ExternalDataUseObserver> io_weak_factory_; | 343 base::WeakPtrFactory<ExternalDataUseObserver> io_weak_factory_; |
| 320 base::WeakPtrFactory<ExternalDataUseObserver> ui_weak_factory_; | 344 base::WeakPtrFactory<ExternalDataUseObserver> ui_weak_factory_; |
| 321 | 345 |
| 322 DISALLOW_COPY_AND_ASSIGN(ExternalDataUseObserver); | 346 DISALLOW_COPY_AND_ASSIGN(ExternalDataUseObserver); |
| 323 }; | 347 }; |
| 324 | 348 |
| 325 bool RegisterExternalDataUseObserver(JNIEnv* env); | 349 bool RegisterExternalDataUseObserver(JNIEnv* env); |
| 326 | 350 |
| 327 } // namespace android | 351 } // namespace android |
| 328 | 352 |
| 329 } // namespace chrome | 353 } // namespace chrome |
| 330 | 354 |
| 331 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ | 355 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ |
| OLD | NEW |