| OLD | NEW |
| 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 #include "chrome/browser/android/net/external_estimate_provider_android.h" | 5 #include "chrome/browser/android/net/external_estimate_provider_android.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/android/context_utils.h" | 9 #include "base/android/context_utils.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "jni/ExternalEstimateProviderAndroid_jni.h" | 12 #include "jni/ExternalEstimateProviderAndroid_jni.h" |
| 13 | 13 |
| 14 namespace chrome { | 14 namespace chrome { |
| 15 namespace android { | 15 namespace android { |
| 16 | 16 |
| 17 ExternalEstimateProviderAndroid::ExternalEstimateProviderAndroid() | 17 ExternalEstimateProviderAndroid::ExternalEstimateProviderAndroid() |
| 18 : task_runner_(nullptr), | 18 : task_runner_(nullptr), |
| 19 delegate_(nullptr), | 19 delegate_(nullptr), |
| 20 weak_factory_(this) { | 20 weak_factory_(this) { |
| 21 if (base::MessageLoop::current()) | 21 if (base::MessageLoop::current()) |
| 22 task_runner_ = base::MessageLoop::current()->task_runner(); | 22 task_runner_ = base::MessageLoop::current()->task_runner(); |
| 23 JNIEnv* env = base::android::AttachCurrentThread(); | 23 JNIEnv* env = base::android::AttachCurrentThread(); |
| 24 j_external_estimate_provider_.Reset( | 24 j_external_estimate_provider_.Reset( |
| 25 Java_ExternalEstimateProviderAndroid_create( | 25 Java_ExternalEstimateProviderAndroid_create( |
| 26 env, base::android::GetApplicationContext(), | 26 env, base::android::GetApplicationContext(), |
| 27 reinterpret_cast<intptr_t>(this))); | 27 reinterpret_cast<intptr_t>(this))); |
| 28 DCHECK(!j_external_estimate_provider_.is_null()); | 28 DCHECK(!j_external_estimate_provider_.is_null()); |
| 29 no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env); | 29 no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env); |
| 30 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | |
| 31 } | 30 } |
| 32 | 31 |
| 33 ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() { | 32 ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | |
| 36 Java_ExternalEstimateProviderAndroid_destroy( | 34 Java_ExternalEstimateProviderAndroid_destroy( |
| 37 base::android::AttachCurrentThread(), | 35 base::android::AttachCurrentThread(), |
| 38 j_external_estimate_provider_.obj()); | 36 j_external_estimate_provider_.obj()); |
| 39 } | 37 } |
| 40 | 38 |
| 41 bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const { | 39 bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const { |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 JNIEnv* env = base::android::AttachCurrentThread(); | 41 JNIEnv* env = base::android::AttachCurrentThread(); |
| 44 int32_t milliseconds = | 42 int32_t milliseconds = |
| 45 Java_ExternalEstimateProviderAndroid_getRTTMilliseconds( | 43 Java_ExternalEstimateProviderAndroid_getRTTMilliseconds( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 delegate_ = delegate; | 97 delegate_ = delegate; |
| 100 } | 98 } |
| 101 | 99 |
| 102 void ExternalEstimateProviderAndroid::Update() const { | 100 void ExternalEstimateProviderAndroid::Update() const { |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | 101 DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 JNIEnv* env = base::android::AttachCurrentThread(); | 102 JNIEnv* env = base::android::AttachCurrentThread(); |
| 105 Java_ExternalEstimateProviderAndroid_requestUpdate( | 103 Java_ExternalEstimateProviderAndroid_requestUpdate( |
| 106 env, j_external_estimate_provider_.obj()); | 104 env, j_external_estimate_provider_.obj()); |
| 107 } | 105 } |
| 108 | 106 |
| 109 void ExternalEstimateProviderAndroid::OnConnectionTypeChanged( | |
| 110 net::NetworkChangeNotifier::ConnectionType type) { | |
| 111 Update(); | |
| 112 } | |
| 113 | |
| 114 void ExternalEstimateProviderAndroid:: | 107 void ExternalEstimateProviderAndroid:: |
| 115 NotifyExternalEstimateProviderAndroidUpdate( | 108 NotifyExternalEstimateProviderAndroidUpdate( |
| 116 JNIEnv* env, | 109 JNIEnv* env, |
| 117 const JavaParamRef<jobject>& obj) { | 110 const JavaParamRef<jobject>& obj) { |
| 118 if (!task_runner_) | 111 if (!task_runner_) |
| 119 return; | 112 return; |
| 120 | 113 |
| 121 // Note that creating a weak pointer is safe even on a background thread, | 114 // Note that creating a weak pointer is safe even on a background thread, |
| 122 // because this method is called from the same critical section as the Java | 115 // because this method is called from the same critical section as the Java |
| 123 // destroy() method (so this object can't be destroyed while we're in this | 116 // destroy() method (so this object can't be destroyed while we're in this |
| 124 // method), and the factory itself isn't bound to a thread, just the weak | 117 // method), and the factory itself isn't bound to a thread, just the weak |
| 125 // pointers are (but they are bound to the thread they're *dereferenced* on, | 118 // pointers are (but they are bound to the thread they're *dereferenced* on, |
| 126 // which is the task runner thread). Once we are outside of the critical | 119 // which is the task runner thread). Once we are outside of the critical |
| 127 // section, the weak pointer will be invalidated if the object is destroyed. | 120 // section, the weak pointer will be invalidated if the object is destroyed. |
| 128 task_runner_->PostTask( | 121 task_runner_->PostTask( |
| 129 FROM_HERE, | 122 FROM_HERE, |
| 130 base::Bind( | 123 base::Bind( |
| 131 &ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable, | 124 &ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable, |
| 132 weak_factory_.GetWeakPtr())); | 125 weak_factory_.GetWeakPtr())); |
| 133 } | 126 } |
| 134 | 127 |
| 135 void ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable() const { | 128 void ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable() const { |
| 136 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 137 if (delegate_) | 130 |
| 138 delegate_->OnUpdatedEstimateAvailable(); | 131 base::TimeDelta rtt; |
| 132 GetRTT(&rtt); |
| 133 |
| 134 int32_t downstream_throughput_kbps = -1; |
| 135 GetDownstreamThroughputKbps(&downstream_throughput_kbps); |
| 136 |
| 137 int32_t upstream_throughput_kbps = -1; |
| 138 GetUpstreamThroughputKbps(&upstream_throughput_kbps); |
| 139 |
| 140 if (delegate_) { |
| 141 delegate_->OnUpdatedEstimateAvailable(rtt, downstream_throughput_kbps, |
| 142 upstream_throughput_kbps); |
| 143 } |
| 139 } | 144 } |
| 140 | 145 |
| 141 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) { | 146 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) { |
| 142 return RegisterNativesImpl(env); | 147 return RegisterNativesImpl(env); |
| 143 } | 148 } |
| 144 | 149 |
| 145 } // namespace android | 150 } // namespace android |
| 146 } // namespace chrome | 151 } // namespace chrome |
| OLD | NEW |