| 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/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 DCHECK(!j_external_estimate_provider_.is_null()); | 26 DCHECK(!j_external_estimate_provider_.is_null()); |
| 27 no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env); | 27 no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env); |
| 28 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 28 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 29 } | 29 } |
| 30 | 30 |
| 31 ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() { | 31 ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() { |
| 32 DCHECK(thread_checker_.CalledOnValidThread()); | 32 DCHECK(thread_checker_.CalledOnValidThread()); |
| 33 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 33 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void ExternalEstimateProviderAndroid::RequestUpdate() const { | |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 38 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 39 Java_ExternalEstimateProviderAndroid_requestUpdate( | |
| 40 env, j_external_estimate_provider_.obj()); | |
| 41 } | |
| 42 | |
| 43 bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const { | 36 bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); | 37 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 JNIEnv* env = base::android::AttachCurrentThread(); | 38 JNIEnv* env = base::android::AttachCurrentThread(); |
| 46 int32_t milliseconds = | 39 int32_t milliseconds = |
| 47 Java_ExternalEstimateProviderAndroid_getRTTMilliseconds( | 40 Java_ExternalEstimateProviderAndroid_getRTTMilliseconds( |
| 48 env, j_external_estimate_provider_.obj()); | 41 env, j_external_estimate_provider_.obj()); |
| 49 DCHECK(milliseconds >= no_value_); | 42 DCHECK(milliseconds >= no_value_); |
| 50 if (milliseconds == no_value_) | 43 if (milliseconds == no_value_) |
| 51 return false; | 44 return false; |
| 52 *rtt = base::TimeDelta::FromMilliseconds(milliseconds); | 45 *rtt = base::TimeDelta::FromMilliseconds(milliseconds); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 env, j_external_estimate_provider_.obj()); | 82 env, j_external_estimate_provider_.obj()); |
| 90 DCHECK(seconds >= no_value_); | 83 DCHECK(seconds >= no_value_); |
| 91 if (seconds == no_value_) { | 84 if (seconds == no_value_) { |
| 92 *time_since_last_update = base::TimeDelta::Max(); | 85 *time_since_last_update = base::TimeDelta::Max(); |
| 93 return false; | 86 return false; |
| 94 } | 87 } |
| 95 *time_since_last_update = base::TimeDelta::FromMilliseconds(seconds); | 88 *time_since_last_update = base::TimeDelta::FromMilliseconds(seconds); |
| 96 return true; | 89 return true; |
| 97 } | 90 } |
| 98 | 91 |
| 92 void ExternalEstimateProviderAndroid::SetUpdatedEstimateDelegate( |
| 93 net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate) { |
| 94 delegate_ = delegate; |
| 95 } |
| 96 |
| 97 void ExternalEstimateProviderAndroid::Update() const { |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); |
| 99 JNIEnv* env = base::android::AttachCurrentThread(); |
| 100 Java_ExternalEstimateProviderAndroid_requestUpdate( |
| 101 env, j_external_estimate_provider_.obj()); |
| 102 } |
| 103 |
| 104 void ExternalEstimateProviderAndroid::OnConnectionTypeChanged( |
| 105 net::NetworkChangeNotifier::ConnectionType type) { |
| 106 Update(); |
| 107 } |
| 108 |
| 99 void ExternalEstimateProviderAndroid:: | 109 void ExternalEstimateProviderAndroid:: |
| 100 NotifyExternalEstimateProviderAndroidUpdate(JNIEnv* env, jobject obj) { | 110 NotifyExternalEstimateProviderAndroidUpdate(JNIEnv* env, jobject obj) { |
| 101 if (!task_runner_) | 111 if (!task_runner_) |
| 102 return; | 112 return; |
| 103 task_runner_->PostTask( | 113 task_runner_->PostTask( |
| 104 FROM_HERE, | 114 FROM_HERE, |
| 105 base::Bind( | 115 base::Bind( |
| 106 &ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable, | 116 &ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable, |
| 107 weak_factory_.GetWeakPtr())); | 117 weak_factory_.GetWeakPtr())); |
| 108 } | 118 } |
| 109 | 119 |
| 110 void ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable() const { | 120 void ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable() const { |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
| 112 if (delegate_) | 122 if (delegate_) |
| 113 delegate_->OnUpdatedEstimateAvailable(); | 123 delegate_->OnUpdatedEstimateAvailable(); |
| 114 } | 124 } |
| 115 | 125 |
| 116 void ExternalEstimateProviderAndroid::OnConnectionTypeChanged( | |
| 117 net::NetworkChangeNotifier::ConnectionType type) { | |
| 118 RequestUpdate(); | |
| 119 } | |
| 120 | |
| 121 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) { | 126 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) { |
| 122 return RegisterNativesImpl(env); | 127 return RegisterNativesImpl(env); |
| 123 } | 128 } |
| 124 | 129 |
| 125 void ExternalEstimateProviderAndroid::SetUpdatedEstimateDelegate( | |
| 126 net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate) { | |
| 127 delegate_ = delegate; | |
| 128 } | |
| 129 | |
| 130 } // namespace android | 130 } // namespace android |
| 131 } // namespace chrome | 131 } // namespace chrome |
| OLD | NEW |