summaryrefslogtreecommitdiffstats
path: root/chrome/browser/android/net/external_estimate_provider_android.cc
blob: cf5b000a8da25b5c3634c5826ca54d9ee234d608 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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/android/context_utils.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();
  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);
  Java_ExternalEstimateProviderAndroid_destroy(
      base::android::AttachCurrentThread(),
      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_GE(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_GE(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_GE(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_GE(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::SetUpdatedEstimateDelegate(
    net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate) {
  delegate_ = delegate;
}

void ExternalEstimateProviderAndroid::Update() const {
  DCHECK(thread_checker_.CalledOnValidThread());
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_ExternalEstimateProviderAndroid_requestUpdate(
      env, j_external_estimate_provider_.obj());
}

void ExternalEstimateProviderAndroid::OnConnectionTypeChanged(
    net::NetworkChangeNotifier::ConnectionType type) {
  Update();
}

void ExternalEstimateProviderAndroid::
    NotifyExternalEstimateProviderAndroidUpdate(
        JNIEnv* env,
        const JavaParamRef<jobject>& obj) {
  if (!task_runner_)
    return;

  // Note that creating a weak pointer is safe even on a background thread,
  // because this method is called from the same critical section as the Java
  // destroy() method (so this object can't be destroyed while we're in this
  // method), and the factory itself isn't bound to a thread, just the weak
  // pointers are (but they are bound to the thread they're *dereferenced* on,
  // which is the task runner thread). Once we are outside of the critical
  // section, the weak pointer will be invalidated if the object is destroyed.
  task_runner_->PostTask(
      FROM_HERE,
      base::Bind(
          &ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable,
          weak_factory_.GetWeakPtr()));
}

void ExternalEstimateProviderAndroid::NotifyUpdatedEstimateAvailable() const {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (delegate_)
    delegate_->OnUpdatedEstimateAvailable();
}

bool RegisterExternalEstimateProviderAndroid(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace android
}  // namespace chrome