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 #include "chrome/browser/android/datausage/external_data_use_observer_android.h" | |
6 | |
7 #include "base/android/jni_string.h" | |
8 #include "jni/ExternalDataUseObserverAndroid_jni.h" | |
9 #include "third_party/re2/re2/re2.h" | |
10 #include "url/gurl.h" | |
11 | |
12 using base::android::ConvertUTF8ToJavaString; | |
13 | |
14 namespace chrome { | |
15 | |
16 namespace android { | |
17 | |
18 ExternalDataUseObserverAndroid::ExternalDataUseObserverAndroid( | |
19 data_usage::DataUseAggregator* data_use_aggregator) | |
20 : data_use_aggregator_(data_use_aggregator) { | |
21 DCHECK(data_use_aggregator_); | |
22 | |
23 JNIEnv* env = base::android::AttachCurrentThread(); | |
24 DCHECK(j_external_data_use_observer_.is_null()); | |
newt (away)
2015/10/13 19:05:21
This DCHECK is superfluous. Of course the member v
tbansal1
2015/10/13 21:30:21
Done.
| |
25 j_external_data_use_observer_.Reset( | |
26 Java_ExternalDataUseObserverAndroid_create( | |
27 env, base::android::GetApplicationContext(), | |
28 reinterpret_cast<intptr_t>(this))); | |
29 DCHECK(!j_external_data_use_observer_.is_null()); | |
30 } | |
31 | |
32 ExternalDataUseObserverAndroid::~ExternalDataUseObserverAndroid() { | |
33 DCHECK(thread_checker_.CalledOnValidThread()); | |
34 | |
35 if (url_patterns_.size() > 0) | |
36 data_use_aggregator_->RemoveObserver(this); | |
37 } | |
38 | |
39 bool ExternalDataUseObserverAndroid::Matches(const GURL& gurl) const { | |
40 if (!gurl.is_valid() || gurl.is_empty()) | |
41 return false; | |
42 | |
43 for (size_t i = 0; i < url_patterns_.size(); ++i) { | |
sclittle
2015/10/13 19:46:56
nit: If you change |url_patterns_| to a ScopedVect
tbansal1
2015/10/13 21:30:21
Done.
| |
44 if (re2::RE2::FullMatch(gurl.spec(), *(url_patterns_.at(i)))) | |
45 return true; | |
46 } | |
47 | |
48 return false; | |
49 } | |
50 | |
51 void ExternalDataUseObserverAndroid::OnDataUse( | |
52 const std::vector<data_usage::DataUse>& data_use_sequence) { | |
53 DCHECK(thread_checker_.CalledOnValidThread()); | |
54 DCHECK(!j_external_data_use_observer_.is_null()); | |
55 | |
56 JNIEnv* env = base::android::AttachCurrentThread(); | |
57 | |
58 for (const auto& data_use : data_use_sequence) { | |
59 if (!Matches(data_use.url)) | |
60 continue; | |
61 | |
62 // TODO(tbansal): Use buffering to avoid frequent JNI calls. | |
63 std::string tag = ""; // data_use.tab_id; | |
64 int64_t bytes_downloaded = data_use.rx_bytes; | |
65 int64_t bytes_uploaded = data_use.tx_bytes; | |
66 Java_ExternalDataUseObserverAndroid_onDataUse( | |
67 env, j_external_data_use_observer_.obj(), | |
68 ConvertUTF8ToJavaString(env, tag).obj(), bytes_downloaded, | |
69 bytes_uploaded); | |
70 } | |
71 } | |
72 | |
73 void ExternalDataUseObserverAndroid::RegisterURLRegexes( | |
74 const std::vector<std::string>& url_regexes) { | |
75 DCHECK(thread_checker_.CalledOnValidThread()); | |
76 | |
77 bool registered_as_observer = url_patterns_.size() > 0; | |
78 if (!registered_as_observer && url_regexes.size() > 0) { | |
79 registered_as_observer = true; | |
80 data_use_aggregator_->AddObserver(this); | |
81 } | |
82 | |
83 url_patterns_.clear(); | |
84 | |
85 re2::RE2::Options options(re2::RE2::DefaultOptions); | |
86 options.set_case_sensitive(false); | |
87 | |
88 for (const std::string& url_regex : url_regexes) { | |
89 if (url_regex.empty()) | |
90 continue; | |
91 scoped_ptr<re2::RE2> pattern(new re2::RE2(url_regex, options)); | |
92 if (!pattern->ok()) | |
93 continue; | |
94 url_patterns_.push_back(pattern.Pass()); | |
95 } | |
96 | |
97 // Unregister as an observer if no regular expressions were received. | |
98 if (url_patterns_.size() == 0 && registered_as_observer) | |
99 data_use_aggregator_->RemoveObserver(this); | |
100 } | |
101 | |
102 bool RegisterExternalDataUseObserverAndroid(JNIEnv* env) { | |
103 return RegisterNativesImpl(env); | |
104 } | |
105 | |
106 } // namespace android | |
107 | |
108 } // namespace chrome | |
OLD | NEW |