OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_ANDROID_DATAUSAGE_EXTERNAL_DATA_USE_OBSERVER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_DATAUSAGE_EXTERNAL_DATA_USE_OBSERVER_H_ |
| 7 |
| 8 #include <jni.h> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/threading/thread_checker.h" |
| 17 #include "components/data_usage/core/data_use.h" |
| 18 #include "components/data_usage/core/data_use_aggregator.h" |
| 19 |
| 20 namespace re2 { |
| 21 class RE2; |
| 22 } |
| 23 |
| 24 namespace chrome { |
| 25 |
| 26 namespace android { |
| 27 |
| 28 // This class allows platform APIs that are external to Chromium to observe how |
| 29 // much data is used by Chromium on the current Android device. It creates and |
| 30 // owns a Java listener object that is notified of the data usage observations |
| 31 // of Chromium. This class receives regular expressions from the Java listener |
| 32 // object. It also registers as a data use observer with DataUseAggregator, |
| 33 // filters the received observations by applying the regex matching to the URLs |
| 34 // of the requests, and notifies the filtered data use to the Java listener. The |
| 35 // Java object in turn may notify the platform APIs of the data usage |
| 36 // observations. |
| 37 class ExternalDataUseObserver : public data_usage::DataUseAggregator::Observer { |
| 38 public: |
| 39 explicit ExternalDataUseObserver( |
| 40 data_usage::DataUseAggregator* data_use_aggregator); |
| 41 ~ExternalDataUseObserver() override; |
| 42 |
| 43 private: |
| 44 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, SingleRegex); |
| 45 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, TwoRegex); |
| 46 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, MultipleRegex); |
| 47 FRIEND_TEST_ALL_PREFIXES(ExternalDataUseObserverTest, ChangeRegex); |
| 48 |
| 49 // data_usage::DataUseAggregator::Observer implementation: |
| 50 void OnDataUse( |
| 51 const std::vector<data_usage::DataUse>& data_use_sequence) override; |
| 52 |
| 53 // Called by |j_external_data_use_observer_| to register multiple |
| 54 // case-insensitive regular expressions. If the url of the data use request |
| 55 // matches any of the regular expression, the observation is passed to the |
| 56 // Java listener. |
| 57 void RegisterURLRegexes(const std::vector<std::string>& url_regexes); |
| 58 |
| 59 // Returns true if the |gurl| matches the registered regular expressions. |
| 60 bool Matches(const GURL& gurl) const; |
| 61 |
| 62 // Aggregator that sends data use observations. |
| 63 data_usage::DataUseAggregator* data_use_aggregator_; |
| 64 |
| 65 // Java listener that provides regular expressions to this class. The |
| 66 // regular expressions are applied to the request URLs, and filtered |
| 67 // data use is notified to |j_external_data_use_observer_|. |
| 68 base::android::ScopedJavaGlobalRef<jobject> j_external_data_use_observer_; |
| 69 |
| 70 // Registered RE2 patterns that are matched against the URLs. |
| 71 ScopedVector<re2::RE2> url_patterns_; |
| 72 |
| 73 base::ThreadChecker thread_checker_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(ExternalDataUseObserver); |
| 76 }; |
| 77 |
| 78 bool RegisterExternalDataUseObserver(JNIEnv* env); |
| 79 |
| 80 } // namespace android |
| 81 |
| 82 } // namespace chrome |
| 83 |
| 84 #endif // CHROME_BROWSER_ANDROID_DATAUSAGE_EXTERNAL_DATA_USE_OBSERVER_H_ |
OLD | NEW |