Chromium Code Reviews| 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()); | |
| 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 const std::string& gurl_spec = gurl.spec(); | |
| 44 | |
| 45 for (size_t i = 0; i < url_patterns_.size(); ++i) { | |
| 46 if (re2::RE2::FullMatch(gurl_spec, *(url_patterns_.at(i)))) | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 void ExternalDataUseObserverAndroid::OnDataUse( | |
| 54 const std::vector<data_usage::DataUse>& data_use_sequence) { | |
| 55 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 56 DCHECK(!j_external_data_use_observer_.is_null()); | |
| 57 | |
| 58 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 59 | |
| 60 for (const auto& data_use : data_use_sequence) { | |
| 61 if (!Matches(data_use.url)) | |
| 62 continue; | |
| 63 | |
| 64 // TODO(tbansal): Use buffering to avoid frequent JNI calls. | |
| 65 std::string tag = ""; // data_use.tab_id; | |
| 66 int64_t bytes_downloaded = data_use.rx_bytes; | |
| 67 int64_t bytes_uploaded = data_use.tx_bytes; | |
| 68 Java_ExternalDataUseObserverAndroid_onDataUse( | |
| 69 env, j_external_data_use_observer_.obj(), | |
| 70 ConvertUTF8ToJavaString(env, tag).obj(), bytes_downloaded, | |
| 71 bytes_uploaded); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void ExternalDataUseObserverAndroid::RegisterURLRegexes( | |
| 76 const std::vector<std::string>& url_regexes) { | |
| 77 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 78 | |
| 79 bool registered_as_observer = url_patterns_.size() > 0; | |
| 80 if (!registered_as_observer && url_regexes.size() > 0) { | |
| 81 registered_as_observer = true; | |
| 82 data_use_aggregator_->AddObserver(this); | |
| 83 } | |
| 84 | |
| 85 url_patterns_.clear(); | |
| 86 | |
| 87 for (const std::string& url_regex : url_regexes) { | |
| 88 if (url_regex.empty()) | |
| 89 continue; | |
| 90 re2::RE2::Options options(re2::RE2::DefaultOptions); | |
| 91 options.set_case_sensitive(false); | |
|
sclittle
2015/10/12 20:10:15
nit: Does it have to be case insensitive? I think
tbansal1
2015/10/12 22:39:14
yes, right now the regex support case insensitive
| |
| 92 scoped_ptr<re2::RE2> pattern(new re2::RE2(url_regex, options)); | |
|
sclittle
2015/10/12 20:10:15
What if the pattern is an invalid regex?
tbansal1
2015/10/12 22:39:14
Done.
| |
| 93 url_patterns_.push_back(pattern.Pass()); | |
| 94 } | |
| 95 | |
| 96 // Unregister as an observer if no regular expressions were received. | |
| 97 if (url_patterns_.size() == 0 && registered_as_observer) | |
| 98 data_use_aggregator_->RemoveObserver(this); | |
| 99 } | |
| 100 | |
| 101 bool RegisterExternalDataUseObserverAndroid(JNIEnv* env) { | |
| 102 return RegisterNativesImpl(env); | |
| 103 } | |
| 104 | |
| 105 } // namespace android | |
| 106 | |
| 107 } // namespace chrome | |
| OLD | NEW |