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