1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/net/external_estimate_provider_android.h"
#include <stdint.h>
#include "base/message_loop/message_loop.h"
#include "content/public/browser/browser_thread.h"
#include "jni/ExternalEstimateProviderAndroid_jni.h"
namespace chrome {
namespace android {
ExternalEstimateProviderAndroid::ExternalEstimateProviderAndroid()
: task_runner_(nullptr), delegate_(nullptr), weak_factory_(this) {
if (base::MessageLoop::current())
task_runner_ = base::MessageLoop::current()->task_runner();
JNIEnv* env = base::android::AttachCurrentThread();
DCHECK(j_external_estimate_provider_.is_null());
j_external_estimate_provider_.Reset(
Java_ExternalEstimateProviderAndroid_create(
env, base::android::GetApplicationContext(),
reinterpret_cast<intptr_t>(this)));
DCHECK(!j_external_estimate_provider_.is_null());
no_value_ = Java_ExternalEstimateProviderAndroid_getNoValue(env);
net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
}
ExternalEstimateProviderAndroid::~ExternalEstimateProviderAndroid() {
DCHECK(thread_checker_.CalledOnValidThread());
net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
}
void ExternalEstimateProviderAndroid::RequestUpdate() const {
DCHECK(thread_checker_.CalledOnValidThread());
JNIEnv* env = base::android::AttachCurrentThread();
Java_ExternalEstimateProviderAndroid_requestUpdate(
env, j_external_estimate_provider_.obj());
}
bool ExternalEstimateProviderAndroid::GetRTT(base::TimeDelta* rtt) const {
DCHECK(thread_checker_.CalledOnValidThread());
JNIEnv* env = base::android::AttachCurrentThread();
int32_t milliseconds =
Java_ExternalEstimateProviderAndroid_getRTTMilliseconds(
env, j_external_estimate_provider_.obj());
DCHECK(milliseconds >= no_value_);
if (milliseconds == no_value_)
return false;
*rtt = base::TimeDelta::FromMilliseconds(milliseconds);
return true;
}
bool ExternalEstimateProviderAndroid::GetDownstreamThroughputKbps(
int32_t* downstream_throughput_kbps) const {
DCHECK(thread_checker_.CalledOnValidThread());
JNIEnv* env = base::android::AttachCurrentThread();
int32_t kbps =
Java_ExternalEstimateProviderAndroid_getDownstreamThroughputKbps(
env, j_external_estimate_provider_.obj());
DCHECK(kbps >= no_value_);
if (kbps == no_value_)
return false;
*downstream_throughput_kbps = kbps;
return true;
}
bool ExternalEstimateProviderAndroid::GetUpstreamThroughputKbps(
int32_t* upstream_throughput_kbps) const {
DCHECK(thread_checker_.CalledOnValidThread());
JNIEnv* env = base::android::AttachCurrentThread();
int32_t kbps = Java_ExternalEstimateProviderAndroid_getUpstreamThroughputKbps(
env, j_external_estimate_provider_.obj());
DCHECK(kbps >= no_value_);
if (kbps == no_value_)
return false;
*upstream_throughput_kbps = kbps;
return true;
}
bool ExternalEstimateProviderAndroid::GetTimeSinceLastUpdate(
base::TimeDelta* time_since_last_update) const {
DCHECK(thread_checker_.CalledOnValidThread());
JNIEnv* env = base::android::AttachCurrentThread();
int32_t seconds =
Java_ExternalEstimateProviderAndroid_getTimeSinceLastUpdateSeconds(
env, j_external_estimate_provider_.obj());
DCHECK(seconds >= no_value_);
if (seconds == no_value_) {
*time_since_last_update = base::TimeDelta::Max();
return false;
}
*time_since_last_update = base::TimeDelta::FromMilliseconds(seconds);
return true;
}
void ExternalEstimateProviderAndroid::
NotifyExternalEstimateProviderAndroidUpdate(JNIEnv* env, jobject obj) {
if (!task_runner_)
return;
task_runner_->PostTask(
FROM_HERE,
base::Bind(
&ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable,
weak_factory_.GetWeakPtr()));
}
void ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable() const {
DCHECK(thread_checker_.CalledOnValidThread());
if (delegate_)
delegate_->OnUpdatedEstimateAvailable();
}
void ExternalEstimateProviderAndroid::OnConnectionTypeChanged(
net::NetworkChangeNotifier::ConnectionType type) {
RequestUpdate();
}
bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) {
return RegisterNativesImpl(env);
}
void ExternalEstimateProviderAndroid::SetUpdatedEstimateDelegate(
net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate) {
delegate_ = delegate;
}
} // namespace android
} // namespace chrome
|