Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / android / net / external_estimate_provider_android.cc
blob2d8c2dbe80ba822a734d8a79d0fbb80eabef2a0d
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.
5 #include "chrome/browser/android/net/external_estimate_provider_android.h"
7 #include <stdint.h>
9 #include "base/message_loop/message_loop.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "jni/ExternalEstimateProviderAndroid_jni.h"
13 namespace chrome {
14 namespace android {
16 ExternalEstimateProviderAndroid::ExternalEstimateProviderAndroid()
17 : task_runner_(nullptr), delegate_(nullptr), weak_factory_(this) {
18 if (base::MessageLoop::current())
19 task_runner_ = base::MessageLoop::current()->task_runner();
20 JNIEnv* env = base::android::AttachCurrentThread();
21 DCHECK(j_external_estimate_provider_.is_null());
22 j_external_estimate_provider_.Reset(
23 Java_ExternalEstimateProviderAndroid_create(
24 env, base::android::GetApplicationContext(),
25 reinterpret_cast<intptr_t>(this)));
26 DCHECK(!j_external_estimate_provider_.is_null());
27 no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env);
28 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
31 ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() {
32 DCHECK(thread_checker_.CalledOnValidThread());
33 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
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());
43 bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const {
44 DCHECK(thread_checker_.CalledOnValidThread());
45 JNIEnv* env = base::android::AttachCurrentThread();
46 int32_t milliseconds =
47 Java_ExternalEstimateProviderAndroid_getRTTMilliseconds(
48 env, j_external_estimate_provider_.obj());
49 DCHECK(milliseconds >= no_value_);
50 if (milliseconds == no_value_)
51 return false;
52 *rtt = base::TimeDelta::FromMilliseconds(milliseconds);
53 return true;
56 bool ExternalEstimateProviderAndroid::GetDownstreamThroughputKbps(
57 int32_t* downstream_throughput_kbps) const {
58 DCHECK(thread_checker_.CalledOnValidThread());
59 JNIEnv* env = base::android::AttachCurrentThread();
60 int32_t kbps =
61 Java_ExternalEstimateProviderAndroid_getDownstreamThroughputKbps(
62 env, j_external_estimate_provider_.obj());
63 DCHECK(kbps >= no_value_);
64 if (kbps == no_value_)
65 return false;
66 *downstream_throughput_kbps = kbps;
67 return true;
70 bool ExternalEstimateProviderAndroid::GetUpstreamThroughputKbps(
71 int32_t* upstream_throughput_kbps) const {
72 DCHECK(thread_checker_.CalledOnValidThread());
73 JNIEnv* env = base::android::AttachCurrentThread();
74 int32_t kbps = Java_ExternalEstimateProviderAndroid_getUpstreamThroughputKbps(
75 env, j_external_estimate_provider_.obj());
76 DCHECK(kbps >= no_value_);
77 if (kbps == no_value_)
78 return false;
79 *upstream_throughput_kbps = kbps;
80 return true;
83 bool ExternalEstimateProviderAndroid::GetTimeSinceLastUpdate(
84 base::TimeDelta* time_since_last_update) const {
85 DCHECK(thread_checker_.CalledOnValidThread());
86 JNIEnv* env = base::android::AttachCurrentThread();
87 int32_t seconds =
88 Java_ExternalEstimateProviderAndroid_getTimeSinceLastUpdateSeconds(
89 env, j_external_estimate_provider_.obj());
90 DCHECK(seconds >= no_value_);
91 if (seconds == no_value_) {
92 *time_since_last_update = base::TimeDelta::Max();
93 return false;
95 *time_since_last_update = base::TimeDelta::FromMilliseconds(seconds);
96 return true;
99 void ExternalEstimateProviderAndroid::
100 NotifyExternalEstimateProviderAndroidUpdate(JNIEnv* env, jobject obj) {
101 if (!task_runner_)
102 return;
103 task_runner_->PostTask(
104 FROM_HERE,
105 base::Bind(
106 &ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable,
107 weak_factory_.GetWeakPtr()));
110 void ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable() const {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 if (delegate_)
113 delegate_->OnUpdatedEstimateAvailable();
116 void ExternalEstimateProviderAndroid::OnConnectionTypeChanged(
117 net::NetworkChangeNotifier::ConnectionType type) {
118 RequestUpdate();
121 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) {
122 return RegisterNativesImpl(env);
125 void ExternalEstimateProviderAndroid::SetUpdatedEstimateDelegate(
126 net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate) {
127 delegate_ = delegate;
130 } // namespace android
131 } // namespace chrome