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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 | 63 |
| 64 // Called by Java when new matching rules have been fetched. This may be | 64 // Called by Java when new matching rules have been fetched. This may be |
| 65 // called on a different thread. |app_package_name| is the package name of | 65 // called on a different thread. |app_package_name| is the package name of |
| 66 // the app that should be matched. |domain_path_regex| is the regex to be used | 66 // the app that should be matched. |domain_path_regex| is the regex to be used |
| 67 // for matching URLs. |label| is the label that must be applied to data | 67 // for matching URLs. |label| is the label that must be applied to data |
| 68 // reports corresponding to the matching rule, and must | 68 // reports corresponding to the matching rule, and must |
| 69 // uniquely identify the matching rule. Each element in |label| must have | 69 // uniquely identify the matching rule. Each element in |label| must have |
| 70 // non-zero length. The three vectors should have equal length. The vectors | 70 // non-zero length. The three vectors should have equal length. The vectors |
| 71 // may be empty which implies that no matching rules are active. Must be | 71 // may be empty which implies that no matching rules are active. Must be |
| 72 // called on UI thread. | 72 // called on UI thread. |
| 73 void FetchMatchingRulesCallback( | 73 void FetchMatchingRulesDone( |
| 74 JNIEnv* env, | 74 JNIEnv* env, |
| 75 jobject obj, | 75 jobject obj, |
| 76 const base::android::JavaParamRef<jobjectArray>& app_package_name, | 76 const base::android::JavaParamRef<jobjectArray>& app_package_name, |
| 77 const base::android::JavaParamRef<jobjectArray>& domain_path_regex, | 77 const base::android::JavaParamRef<jobjectArray>& domain_path_regex, |
| 78 const base::android::JavaParamRef<jobjectArray>& label); | 78 const base::android::JavaParamRef<jobjectArray>& label); |
| 79 | 79 |
| 80 // Called by Java when the reporting of data usage has finished. This may be | 80 // Called by Java when the reporting of data usage has finished. This may be |
| 81 // called on a different thread. |success| is true if the request was | 81 // called on a different thread. |success| is true if the request was |
| 82 // successfully submitted to the external data use observer by Java. Must be | 82 // successfully submitted to the external data use observer by Java. Must be |
| 83 // called on UI thread. | 83 // called on UI thread. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 98 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, BufferSize); | 98 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, BufferSize); |
| 99 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, | 99 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, |
| 100 PeriodicFetchMatchingRules); | 100 PeriodicFetchMatchingRules); |
| 101 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, BufferDataUseReports); | 101 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, BufferDataUseReports); |
| 102 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, Variations); | 102 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, Variations); |
| 103 | 103 |
| 104 // DataUseReportKey is a unique identifier for a data use report. | 104 // DataUseReportKey is a unique identifier for a data use report. |
| 105 struct DataUseReportKey { | 105 struct DataUseReportKey { |
| 106 DataUseReportKey(const std::string& label, | 106 DataUseReportKey(const std::string& label, |
| 107 net::NetworkChangeNotifier::ConnectionType connection_type, | 107 net::NetworkChangeNotifier::ConnectionType connection_type, |
| 108 const std::string& mcc_mnc) | 108 const std::string& mcc_mnc); |
| 109 : label(label), connection_type(connection_type), mcc_mnc(mcc_mnc) {} | 109 virtual ~DataUseReportKey(); |
|
sclittle
2015/11/10 19:21:57
nit: this doesn't have to be virtual, and the clas
tbansal1
2015/11/11 21:18:38
Done.
| |
| 110 | 110 |
| 111 bool operator==(const DataUseReportKey& other) const { | 111 bool operator==(const DataUseReportKey& other) const; |
| 112 return (label == other.label && | |
| 113 connection_type == other.connection_type && | |
| 114 mcc_mnc == other.mcc_mnc); | |
| 115 } | |
| 116 | |
| 117 virtual ~DataUseReportKey() {} | |
| 118 | 112 |
| 119 // Label provided by the matching rules. | 113 // Label provided by the matching rules. |
| 120 const std::string label; | 114 const std::string label; |
| 121 | 115 |
| 122 // Type of network used by the request. | 116 // Type of network used by the request. |
| 123 const net::NetworkChangeNotifier::ConnectionType connection_type; | 117 const net::NetworkChangeNotifier::ConnectionType connection_type; |
| 124 | 118 |
| 125 // mcc_mnc operator of the provider of the SIM as obtained from | 119 // mcc_mnc operator of the provider of the SIM as obtained from |
| 126 // TelephonyManager#getNetworkOperator() Java API in Android. | 120 // TelephonyManager#getNetworkOperator() Java API in Android. |
| 127 const std::string mcc_mnc; | 121 const std::string mcc_mnc; |
| 128 }; | 122 }; |
| 129 | 123 |
| 130 // DataUseReport is paired with a DataUseReportKey object. DataUseReport | 124 // DataUseReport is paired with a DataUseReportKey object. DataUseReport |
| 131 // contains the bytes send/received during a specific interval. Only the bytes | 125 // contains the bytes send/received during a specific interval. Only the bytes |
| 132 // from the data use reports that have the |label|, |connection_type|, and | 126 // from the data use reports that have the |label|, |connection_type|, and |
| 133 // |mcc_mnc| specified in the corresponding DataUseReportKey object are | 127 // |mcc_mnc| specified in the corresponding DataUseReportKey object are |
| 134 // counted in the DataUseReport. | 128 // counted in the DataUseReport. |
| 135 struct DataUseReport { | 129 struct DataUseReport { |
| 136 // |start_time| and |end_time| are the start and end timestamps (in UTC | 130 // |start_time| and |end_time| are the start and end timestamps (in UTC |
| 137 // since the standard Java epoch of 1970-01-01 00:00:00) of the interval | 131 // since the standard Java epoch of 1970-01-01 00:00:00) of the interval |
| 138 // that this data report covers. |bytes_downloaded| and |bytes_uploaded| are | 132 // that this data report covers. |bytes_downloaded| and |bytes_uploaded| are |
| 139 // the total bytes received and send during this interval. | 133 // the total bytes received and send during this interval. |
| 140 DataUseReport(const base::Time& start_time, | 134 DataUseReport(const base::Time& start_time, |
| 141 const base::Time& end_time, | 135 const base::Time& end_time, |
| 142 int64_t bytes_downloaded, | 136 int64_t bytes_downloaded, |
| 143 int64_t bytes_uploaded) | 137 int64_t bytes_uploaded); |
| 144 : start_time(start_time), | 138 virtual ~DataUseReport(); |
|
sclittle
2015/11/10 19:21:57
nit: this doesn't have to be virtual either, and c
tbansal1
2015/11/11 21:18:38
Done.
| |
| 145 end_time(end_time), | |
| 146 bytes_downloaded(bytes_downloaded), | |
| 147 bytes_uploaded(bytes_uploaded) {} | |
| 148 | |
| 149 virtual ~DataUseReport() {} | |
| 150 | 139 |
| 151 // Start time of |this| data report (in UTC since the standard Java epoch of | 140 // Start time of |this| data report (in UTC since the standard Java epoch of |
| 152 // 1970-01-01 00:00:00). | 141 // 1970-01-01 00:00:00). |
| 153 const base::Time start_time; | 142 const base::Time start_time; |
| 154 | 143 |
| 155 // End time of |this| data report (in UTC since the standard Java epoch of | 144 // End time of |this| data report (in UTC since the standard Java epoch of |
| 156 // 1970-01-01 00:00:00) | 145 // 1970-01-01 00:00:00) |
| 157 const base::Time end_time; | 146 const base::Time end_time; |
| 158 | 147 |
| 159 // Number of bytes downloaded and uploaded by Chromium from |start_time| to | 148 // Number of bytes downloaded and uploaded by Chromium from |start_time| to |
| 160 // |end_time|. | 149 // |end_time|. |
| 161 const int64_t bytes_downloaded; | 150 const int64_t bytes_downloaded; |
| 162 const int64_t bytes_uploaded; | 151 const int64_t bytes_uploaded; |
| 163 }; | 152 }; |
| 164 | 153 |
| 165 // Class that implements hash operator on DataUseReportKey. | 154 // Class that implements hash operator on DataUseReportKey. |
| 166 class DataUseReportKeyHash { | 155 class DataUseReportKeyHash { |
| 167 public: | 156 public: |
| 168 // A simple heuristical hash function that satisifes the property that two | 157 // A simple heuristical hash function that satisifes the property that two |
| 169 // equal data structures have the same hash value. The hash is computed by | 158 // equal data structures have the same hash value. The hash is computed by |
| 170 // hashing individual variables and combining them using prime numbers. | 159 // hashing individual variables and combining them using prime numbers. |
| 171 // Prime numbers are used for multiplication because the number of buckets | 160 // Prime numbers are used for multiplication because the number of buckets |
|
sclittle
2015/11/10 19:21:57
nit: Perhaps the justification for using prime num
tbansal1
2015/11/11 21:18:38
Done.
| |
| 172 // used by map is always an even number. Using a prime number ensures that | 161 // used by map is always an even number. Using a prime number ensures that |
| 173 // for two different DataUseReportKey objects (say |j| and |k|), if the | 162 // for two different DataUseReportKey objects (say |j| and |k|), if the |
| 174 // hash value of |k.label| is equal to hash value of |j.mcc_mnc|, then |j| | 163 // hash value of |k.label| is equal to hash value of |j.mcc_mnc|, then |j| |
| 175 // and |k| map to different buckets. Large prime numbers are used so that | 164 // and |k| map to different buckets. Large prime numbers are used so that |
| 176 // hash value is spread over a larger range. | 165 // hash value is spread over a larger range. |
| 177 size_t operator()(const DataUseReportKey& k) const { | 166 size_t operator()(const DataUseReportKey& k) const; |
| 178 std::hash<std::string> hash_function; | |
| 179 size_t hash = 1; | |
| 180 hash = hash * 23 + hash_function(k.label); | |
| 181 hash = hash * 43 + k.connection_type; | |
| 182 hash = hash * 83 + hash_function(k.mcc_mnc); | |
| 183 return hash; | |
| 184 } | |
| 185 }; | 167 }; |
| 186 | 168 |
| 187 typedef base::hash_map<DataUseReportKey, DataUseReport, DataUseReportKeyHash> | 169 typedef base::hash_map<DataUseReportKey, DataUseReport, DataUseReportKeyHash> |
| 188 DataUseReports; | 170 DataUseReports; |
| 189 | 171 |
| 190 // Stores the matching rules. | 172 // Stores the matching rules. |
| 191 class MatchingRule { | 173 class MatchingRule { |
| 192 public: | 174 public: |
| 193 MatchingRule(const std::string& app_package_name, | 175 MatchingRule(const std::string& app_package_name, |
| 194 scoped_ptr<re2::RE2> pattern, | 176 scoped_ptr<re2::RE2> pattern, |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 216 static const size_t kMaxBufferSize = 100; | 198 static const size_t kMaxBufferSize = 100; |
| 217 | 199 |
| 218 // Creates Java object. Must be called on the UI thread. | 200 // Creates Java object. Must be called on the UI thread. |
| 219 void CreateJavaObjectOnUIThread(); | 201 void CreateJavaObjectOnUIThread(); |
| 220 | 202 |
| 221 // data_usage::DataUseAggregator::Observer implementation: | 203 // data_usage::DataUseAggregator::Observer implementation: |
| 222 void OnDataUse(const std::vector<const data_usage::DataUse*>& | 204 void OnDataUse(const std::vector<const data_usage::DataUse*>& |
| 223 data_use_sequence) override; | 205 data_use_sequence) override; |
| 224 | 206 |
| 225 // Fetches matching rules from Java. Must be called on the UI thread. Returns | 207 // Fetches matching rules from Java. Must be called on the UI thread. Returns |
| 226 // result asynchronously on UI thread via FetchMatchingRulesCallback. | 208 // result asynchronously on UI thread via FetchMatchingRulesDone. |
| 227 void FetchMatchingRulesOnUIThread() const; | 209 void FetchMatchingRulesOnUIThread() const; |
| 228 | 210 |
| 229 // Called by FetchMatchingRulesCallback on IO thread when new matching rules | 211 // Called by FetchMatchingRulesDone on IO thread when new matching rules |
| 230 // Adds |data_use| to buffered reports. |data_use| is the data use report | 212 // Adds |data_use| to buffered reports. |data_use| is the data use report |
| 231 // received from DataUseAggregator. |data_use| should not be null. |label| is | 213 // received from DataUseAggregator. |data_use| should not be null. |label| is |
| 232 // a non-empty label that applies to |data_use|. |start_time| and |end_time| | 214 // a non-empty label that applies to |data_use|. |start_time| and |end_time| |
| 233 // are the start, and end times of the interval during which bytes reported in | 215 // are the start, and end times of the interval during which bytes reported in |
| 234 // |data_use| went over the network. | 216 // |data_use| went over the network. |
| 235 void BufferDataUseReport(const data_usage::DataUse* data_use, | 217 void BufferDataUseReport(const data_usage::DataUse* data_use, |
| 236 const std::string& label, | 218 const std::string& label, |
| 237 const base::Time& start_time, | 219 const base::Time& start_time, |
| 238 const base::Time& end_time); | 220 const base::Time& end_time); |
| 239 | 221 |
| 240 // Submits the first data report among the buffered data reports in | 222 // Submits the first data report among the buffered data reports in |
| 241 // |buffered_data_reports_|. Since an unordered_map is used to buffer the | 223 // |buffered_data_reports_|. Since an unordered_map is used to buffer the |
| 242 // reports, the order of reports may change. The reports are buffered in an | 224 // reports, the order of reports may change. The reports are buffered in an |
| 243 // arbitrary order and there are no guarantees that the next report to be | 225 // arbitrary order and there are no guarantees that the next report to be |
| 244 // submitted is the oldest one buffered. | 226 // submitted is the oldest one buffered. |
| 245 void SubmitBufferedDataUseReport(); | 227 void SubmitBufferedDataUseReport(); |
| 246 | 228 |
| 247 // Called by |FetchMatchingRulesCallback| on IO thread when new matching rules | 229 // Called by FetchMatchingRulesDone on IO thread when new matching rules have |
| 248 // have been fetched. | 230 // been fetched. |
| 249 void FetchMatchingRulesCallbackOnIOThread( | 231 void FetchMatchingRulesDoneOnIOThread( |
| 250 const std::vector<std::string>& app_package_name, | 232 const std::vector<std::string>& app_package_name, |
| 251 const std::vector<std::string>& domain_path_regex, | 233 const std::vector<std::string>& domain_path_regex, |
| 252 const std::vector<std::string>& label); | 234 const std::vector<std::string>& label); |
| 253 | 235 |
| 254 // Reports data use to Java. Must be called on the UI thread. Returns | 236 // Reports data use to Java. Must be called on the UI thread. Returns |
| 255 // result asynchronously on UI thread via OnReportDataUseDone. | 237 // result asynchronously on UI thread via OnReportDataUseDone. |
| 256 void ReportDataUseOnUIThread(const DataUseReportKey& key, | 238 void ReportDataUseOnUIThread(const DataUseReportKey& key, |
| 257 const DataUseReport& report) const; | 239 const DataUseReport& report) const; |
| 258 | 240 |
| 259 // Called by OnReportDataUseDone on IO thread when a data use report has | 241 // Called by OnReportDataUseDone on IO thread when a data use report has |
| 260 // been submitted. | 242 // been submitted. |
| 261 void OnReportDataUseDoneOnIOThread(bool success); | 243 void OnReportDataUseDoneOnIOThread(bool success); |
| 262 | 244 |
| 263 // Called by FetchMatchingRulesCallbackIO to register multiple | 245 // Called by FetchMatchingRulesDoneOnIOThread to register multiple |
| 264 // case-insensitive regular expressions. If the url of the data use request | 246 // case-insensitive regular expressions. If the url of the data use request |
| 265 // matches any of the regular expression, the observation is passed to the | 247 // matches any of the regular expression, the observation is passed to the |
| 266 // Java listener. | 248 // Java listener. |
| 267 void RegisterURLRegexes(const std::vector<std::string>& app_package_name, | 249 void RegisterURLRegexes(const std::vector<std::string>& app_package_name, |
| 268 const std::vector<std::string>& domain_path_regex, | 250 const std::vector<std::string>& domain_path_regex, |
| 269 const std::vector<std::string>& label); | 251 const std::vector<std::string>& label); |
| 270 | 252 |
| 271 // Returns true if the |gurl| matches the registered regular expressions. | 253 // Returns true if the |gurl| matches the registered regular expressions. |
| 272 // |label| must not be null. If a match is found, the |label| is set to the | 254 // |label| must not be null. If a match is found, the |label| is set to the |
| 273 // matching rule's label. | 255 // matching rule's label. |
| 274 bool Matches(const GURL& gurl, std::string* label) const; | 256 bool Matches(const GURL& gurl, std::string* label) const; |
| 275 | 257 |
| 276 // Return the weak pointer to |this| to be used on IO and UI thread, | 258 // Return the weak pointer to |this| to be used on IO and UI thread, |
| 277 // respectively. | 259 // respectively. |
| 278 base::WeakPtr<ExternalDataUseObserver> GetIOWeakPtr(); | 260 base::WeakPtr<ExternalDataUseObserver> GetIOWeakPtr(); |
| 279 base::WeakPtr<ExternalDataUseObserver> GetUIWeakPtr(); | 261 base::WeakPtr<ExternalDataUseObserver> GetUIWeakPtr(); |
| 280 | 262 |
| 281 // Aggregator that sends data use observations to |this|. | 263 // Aggregator that sends data use observations to |this|. |
| 282 data_usage::DataUseAggregator* data_use_aggregator_; | 264 data_usage::DataUseAggregator* data_use_aggregator_; |
| 283 | 265 |
| 284 // Java listener that provides regular expressions to |this|. The regular | 266 // Java listener that provides regular expressions to |this|. The regular |
| 285 // expressions are applied to the request URLs, and filtered data use is | 267 // expressions are applied to the request URLs, and filtered data use is |
| 286 // notified to |j_external_data_use_observer_|. | 268 // notified to |j_external_data_use_observer_|. |
| 287 base::android::ScopedJavaGlobalRef<jobject> j_external_data_use_observer_; | 269 base::android::ScopedJavaGlobalRef<jobject> j_external_data_use_observer_; |
| 288 | 270 |
| 289 // True if callback from |FetchMatchingRulesCallback| is currently pending. | 271 // True if callback from |FetchMatchingRulesDone| is currently pending. |
| 290 bool matching_rules_fetch_pending_; | 272 bool matching_rules_fetch_pending_; |
| 291 | 273 |
| 292 // True if callback from |SubmitDataUseReportCallback| is currently pending. | 274 // True if callback from |SubmitDataUseReportCallback| is currently pending. |
| 293 bool submit_data_report_pending_; | 275 bool submit_data_report_pending_; |
| 294 | 276 |
| 295 // Contains matching rules. | 277 // Contains matching rules. |
| 296 ScopedVector<MatchingRule> matching_rules_; | 278 ScopedVector<MatchingRule> matching_rules_; |
| 297 | 279 |
| 298 // Buffered data reports that need to be submitted to the Java data use | 280 // Buffered data reports that need to be submitted to the Java data use |
| 299 // observer. | 281 // observer. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 DISALLOW_COPY_AND_ASSIGN(ExternalDataUseObserver); | 319 DISALLOW_COPY_AND_ASSIGN(ExternalDataUseObserver); |
| 338 }; | 320 }; |
| 339 | 321 |
| 340 bool RegisterExternalDataUseObserver(JNIEnv* env); | 322 bool RegisterExternalDataUseObserver(JNIEnv* env); |
| 341 | 323 |
| 342 } // namespace android | 324 } // namespace android |
| 343 | 325 |
| 344 } // namespace chrome | 326 } // namespace chrome |
| 345 | 327 |
| 346 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ | 328 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ |
| OLD | NEW |