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/location.h" | 10 #include "base/location.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 Java_ExternalEstimateProviderAndroid_create( | 28 Java_ExternalEstimateProviderAndroid_create( |
29 env, base::android::GetApplicationContext(), | 29 env, base::android::GetApplicationContext(), |
30 reinterpret_cast<intptr_t>(this))); | 30 reinterpret_cast<intptr_t>(this))); |
31 DCHECK(!j_external_estimate_provider_.is_null()); | 31 DCHECK(!j_external_estimate_provider_.is_null()); |
32 no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env); | 32 no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env); |
33 } | 33 } |
34 | 34 |
35 ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() { | 35 ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() { |
36 DCHECK(thread_checker_.CalledOnValidThread()); | 36 DCHECK(thread_checker_.CalledOnValidThread()); |
37 Java_ExternalEstimateProviderAndroid_destroy( | 37 Java_ExternalEstimateProviderAndroid_destroy( |
38 base::android::AttachCurrentThread(), | 38 base::android::AttachCurrentThread(), j_external_estimate_provider_); |
39 j_external_estimate_provider_.obj()); | |
40 } | 39 } |
41 | 40 |
42 bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const { | 41 bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const { |
43 DCHECK(thread_checker_.CalledOnValidThread()); | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
44 JNIEnv* env = base::android::AttachCurrentThread(); | 43 JNIEnv* env = base::android::AttachCurrentThread(); |
45 int32_t milliseconds = | 44 int32_t milliseconds = |
46 Java_ExternalEstimateProviderAndroid_getRTTMilliseconds( | 45 Java_ExternalEstimateProviderAndroid_getRTTMilliseconds( |
47 env, j_external_estimate_provider_.obj()); | 46 env, j_external_estimate_provider_); |
48 DCHECK_GE(milliseconds, no_value_); | 47 DCHECK_GE(milliseconds, no_value_); |
49 if (milliseconds == no_value_) | 48 if (milliseconds == no_value_) |
50 return false; | 49 return false; |
51 *rtt = base::TimeDelta::FromMilliseconds(milliseconds); | 50 *rtt = base::TimeDelta::FromMilliseconds(milliseconds); |
52 return true; | 51 return true; |
53 } | 52 } |
54 | 53 |
55 bool ExternalEstimateProviderAndroid::GetDownstreamThroughputKbps( | 54 bool ExternalEstimateProviderAndroid::GetDownstreamThroughputKbps( |
56 int32_t* downstream_throughput_kbps) const { | 55 int32_t* downstream_throughput_kbps) const { |
57 DCHECK(thread_checker_.CalledOnValidThread()); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
58 JNIEnv* env = base::android::AttachCurrentThread(); | 57 JNIEnv* env = base::android::AttachCurrentThread(); |
59 int32_t kbps = | 58 int32_t kbps = |
60 Java_ExternalEstimateProviderAndroid_getDownstreamThroughputKbps( | 59 Java_ExternalEstimateProviderAndroid_getDownstreamThroughputKbps( |
61 env, j_external_estimate_provider_.obj()); | 60 env, j_external_estimate_provider_); |
62 DCHECK_GE(kbps, no_value_); | 61 DCHECK_GE(kbps, no_value_); |
63 if (kbps == no_value_) | 62 if (kbps == no_value_) |
64 return false; | 63 return false; |
65 *downstream_throughput_kbps = kbps; | 64 *downstream_throughput_kbps = kbps; |
66 return true; | 65 return true; |
67 } | 66 } |
68 | 67 |
69 bool ExternalEstimateProviderAndroid::GetUpstreamThroughputKbps( | 68 bool ExternalEstimateProviderAndroid::GetUpstreamThroughputKbps( |
70 int32_t* upstream_throughput_kbps) const { | 69 int32_t* upstream_throughput_kbps) const { |
71 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
72 JNIEnv* env = base::android::AttachCurrentThread(); | 71 JNIEnv* env = base::android::AttachCurrentThread(); |
73 int32_t kbps = Java_ExternalEstimateProviderAndroid_getUpstreamThroughputKbps( | 72 int32_t kbps = Java_ExternalEstimateProviderAndroid_getUpstreamThroughputKbps( |
74 env, j_external_estimate_provider_.obj()); | 73 env, j_external_estimate_provider_); |
75 DCHECK_GE(kbps, no_value_); | 74 DCHECK_GE(kbps, no_value_); |
76 if (kbps == no_value_) | 75 if (kbps == no_value_) |
77 return false; | 76 return false; |
78 *upstream_throughput_kbps = kbps; | 77 *upstream_throughput_kbps = kbps; |
79 return true; | 78 return true; |
80 } | 79 } |
81 | 80 |
82 bool ExternalEstimateProviderAndroid::GetTimeSinceLastUpdate( | 81 bool ExternalEstimateProviderAndroid::GetTimeSinceLastUpdate( |
83 base::TimeDelta* time_since_last_update) const { | 82 base::TimeDelta* time_since_last_update) const { |
84 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
85 JNIEnv* env = base::android::AttachCurrentThread(); | 84 JNIEnv* env = base::android::AttachCurrentThread(); |
86 int32_t seconds = | 85 int32_t seconds = |
87 Java_ExternalEstimateProviderAndroid_getTimeSinceLastUpdateSeconds( | 86 Java_ExternalEstimateProviderAndroid_getTimeSinceLastUpdateSeconds( |
88 env, j_external_estimate_provider_.obj()); | 87 env, j_external_estimate_provider_); |
89 DCHECK_GE(seconds, no_value_); | 88 DCHECK_GE(seconds, no_value_); |
90 if (seconds == no_value_) { | 89 if (seconds == no_value_) { |
91 *time_since_last_update = base::TimeDelta::Max(); | 90 *time_since_last_update = base::TimeDelta::Max(); |
92 return false; | 91 return false; |
93 } | 92 } |
94 *time_since_last_update = base::TimeDelta::FromMilliseconds(seconds); | 93 *time_since_last_update = base::TimeDelta::FromMilliseconds(seconds); |
95 return true; | 94 return true; |
96 } | 95 } |
97 | 96 |
98 void ExternalEstimateProviderAndroid::SetUpdatedEstimateDelegate( | 97 void ExternalEstimateProviderAndroid::SetUpdatedEstimateDelegate( |
99 net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate) { | 98 net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate) { |
100 delegate_ = delegate; | 99 delegate_ = delegate; |
101 } | 100 } |
102 | 101 |
103 void ExternalEstimateProviderAndroid::Update() const { | 102 void ExternalEstimateProviderAndroid::Update() const { |
104 DCHECK(thread_checker_.CalledOnValidThread()); | 103 DCHECK(thread_checker_.CalledOnValidThread()); |
105 JNIEnv* env = base::android::AttachCurrentThread(); | 104 JNIEnv* env = base::android::AttachCurrentThread(); |
106 Java_ExternalEstimateProviderAndroid_requestUpdate( | 105 Java_ExternalEstimateProviderAndroid_requestUpdate( |
107 env, j_external_estimate_provider_.obj()); | 106 env, j_external_estimate_provider_); |
108 } | 107 } |
109 | 108 |
110 void ExternalEstimateProviderAndroid:: | 109 void ExternalEstimateProviderAndroid:: |
111 NotifyExternalEstimateProviderAndroidUpdate( | 110 NotifyExternalEstimateProviderAndroidUpdate( |
112 JNIEnv* env, | 111 JNIEnv* env, |
113 const JavaParamRef<jobject>& obj) { | 112 const JavaParamRef<jobject>& obj) { |
114 if (!task_runner_) | 113 if (!task_runner_) |
115 return; | 114 return; |
116 | 115 |
117 // Note that creating a weak pointer is safe even on a background thread, | 116 // Note that creating a weak pointer is safe even on a background thread, |
(...skipping 27 matching lines...) Expand all Loading... |
145 upstream_throughput_kbps); | 144 upstream_throughput_kbps); |
146 } | 145 } |
147 } | 146 } |
148 | 147 |
149 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) { | 148 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) { |
150 return RegisterNativesImpl(env); | 149 return RegisterNativesImpl(env); |
151 } | 150 } |
152 | 151 |
153 } // namespace android | 152 } // namespace android |
154 } // namespace chrome | 153 } // namespace chrome |
OLD | NEW |