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