Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: chrome/browser/android/data_usage/external_data_use_observer.h

Issue 1412813007: ExternalDataUseObserver JNI integration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed sclittle comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include <string> 11 #include <string>
12 #include <unordered_map>
13 #include <vector> 12 #include <vector>
14 13
15 #include "base/android/jni_array.h" 14 #include "base/android/jni_array.h"
16 #include "base/android/scoped_java_ref.h" 15 #include "base/android/scoped_java_ref.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/gtest_prod_util.h" 17 #include "base/gtest_prod_util.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_vector.h" 20 #include "base/memory/scoped_vector.h"
21 #include "base/memory/weak_ptr.h" 21 #include "base/memory/weak_ptr.h"
22 #include "base/single_thread_task_runner.h" 22 #include "base/single_thread_task_runner.h"
23 #include "base/thread_task_runner_handle.h" 23 #include "base/thread_task_runner_handle.h"
24 #include "base/threading/thread_checker.h" 24 #include "base/threading/thread_checker.h"
25 #include "base/time/time.h" 25 #include "base/time/time.h"
26 #include "components/data_usage/core/data_use_aggregator.h" 26 #include "components/data_usage/core/data_use_aggregator.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
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;
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)
sclittle 2015/11/07 02:05:32 nit: This copy constructor is unnecessary, since i
tbansal1 2015/11/07 02:33:03 Done.
105 : label(other.label), 112 : label(other.label),
106 connection_type(other.connection_type), 113 connection_type(other.connection_type),
107 mcc_mnc(other.mcc_mnc) {} 114 mcc_mnc(other.mcc_mnc) {}
108 115
109 bool operator==(const DataUseReportKey& other) const { 116 bool operator==(const DataUseReportKey& other) const {
110 return (label == other.label && 117 return (label == other.label &&
111 connection_type == other.connection_type && 118 connection_type == other.connection_type &&
112 mcc_mnc == other.mcc_mnc); 119 mcc_mnc == other.mcc_mnc);
113 } 120 }
114 121
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 base::hash_map<DataUseReportKey, DataUseReport, DataUseReportKeyHash>
186 DataUseReport, 193 DataUseReports;
187 DataUseReportKeyHash> DataUseReports;
188 194
189 // Stores the matching rules. 195 // Stores the matching rules.
190 class MatchingRule { 196 class MatchingRule {
191 public: 197 public:
192 MatchingRule(const std::string& app_package_name, 198 MatchingRule(const std::string& app_package_name,
193 scoped_ptr<re2::RE2> pattern, 199 scoped_ptr<re2::RE2> pattern,
194 const std::string& label); 200 const std::string& label);
195 ~MatchingRule(); 201 ~MatchingRule();
196 202
197 const re2::RE2* pattern() const; 203 const re2::RE2* pattern() const;
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 311 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
306 312
307 // |ui_task_runner_| is used to call Java code on UI thread. This ensures 313 // |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 314 // that Java code is safely called only on a single thread, and eliminates
309 // the need for locks in Java. 315 // the need for locks in Java.
310 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 316 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
311 317
312 // Time when the data use reports were last received from DataUseAggregator. 318 // Time when the data use reports were last received from DataUseAggregator.
313 base::Time previous_report_time_; 319 base::Time previous_report_time_;
314 320
321 // Time when the matching rules were last fetched.
322 base::TimeTicks last_matching_rules_fetch_time_;
323
324 // Total number of bytes transmitted or received across all the buffered
325 // reports.
326 int64_t total_bytes_buffered_;
327
328 // Duration after which matching rules are periodically fetched.
329 const base::TimeDelta fetch_matching_rules_duration_;
330
331 // Minimum number of bytes that should be buffered before a data use report is
332 // submitted.
333 const int64_t data_use_report_min_bytes_;
334
315 base::ThreadChecker thread_checker_; 335 base::ThreadChecker thread_checker_;
316 336
317 // |io_weak_factory_| and |ui_weak_factory_| are used for posting tasks on the 337 // |io_weak_factory_| and |ui_weak_factory_| are used for posting tasks on the
318 // IO and UI thread, respectively. 338 // IO and UI thread, respectively.
319 base::WeakPtrFactory<ExternalDataUseObserver> io_weak_factory_; 339 base::WeakPtrFactory<ExternalDataUseObserver> io_weak_factory_;
320 base::WeakPtrFactory<ExternalDataUseObserver> ui_weak_factory_; 340 base::WeakPtrFactory<ExternalDataUseObserver> ui_weak_factory_;
321 341
322 DISALLOW_COPY_AND_ASSIGN(ExternalDataUseObserver); 342 DISALLOW_COPY_AND_ASSIGN(ExternalDataUseObserver);
323 }; 343 };
324 344
325 bool RegisterExternalDataUseObserver(JNIEnv* env); 345 bool RegisterExternalDataUseObserver(JNIEnv* env);
326 346
327 } // namespace android 347 } // namespace android
328 348
329 } // namespace chrome 349 } // namespace chrome
330 350
331 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_ 351 #endif // CHROME_BROWSER_ANDROID_DATA_USAGE_EXTERNAL_DATA_USE_OBSERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698