Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / android / net / external_estimate_provider_android.h
blob8fe6729e9698241595401bcb51e1877e4fdbdef9
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 #ifndef CHROME_BROWSER_ANDROID_NET_EXTERNAL_ESTIMATE_PROVIDER_ANDROID_H_
6 #define CHROME_BROWSER_ANDROID_NET_EXTERNAL_ESTIMATE_PROVIDER_ANDROID_H_
8 #include <jni.h>
9 #include <stdint.h>
11 #include "base/android/scoped_java_ref.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/time.h"
18 #include "net/base/external_estimate_provider.h"
19 #include "net/base/network_change_notifier.h"
21 namespace chrome {
22 namespace android {
24 // Native class that calls Java code exposed by
25 // ExternalEstimateProviderAndroidHelper.java. Provides network quality
26 // estimates as provided by Android. Estimates are automatically updated on a
27 // network change event.
28 class ExternalEstimateProviderAndroid
29 : public net::NetworkChangeNotifier::ConnectionTypeObserver,
30 public net::ExternalEstimateProvider {
31 public:
32 // Constructs and initializes the underlying provider.
33 ExternalEstimateProviderAndroid();
35 ~ExternalEstimateProviderAndroid() override;
37 // net::ExternalEstimateProvider implementation:
38 bool GetRTT(base::TimeDelta* rtt) const override;
39 bool GetDownstreamThroughputKbps(
40 int32_t* downstream_throughput_kbps) const override;
41 bool GetUpstreamThroughputKbps(
42 int32_t* upstream_throughput_kbps) const override;
43 bool GetTimeSinceLastUpdate(
44 base::TimeDelta* time_since_last_update) const override;
45 void SetUpdatedEstimateDelegate(
46 net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate)
47 override;
48 void Update() const override;
50 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
51 void OnConnectionTypeChanged(
52 net::NetworkChangeNotifier::ConnectionType type) override;
54 // Called by Java when the external estimate provider has an updated value.
55 // This may be called on a thread different from |task_runner_|.
56 void NotifyExternalEstimateProviderAndroidUpdate(JNIEnv* env, jobject obj);
58 protected:
59 // Notifies the delegate that a new update to external estimate is available.
60 // Protected for testing.
61 void NotifyUpdatedEstimateAvailable() const;
63 private:
64 // Value returned if valid value is unavailable.
65 int32_t no_value_ = -1;
67 base::android::ScopedJavaGlobalRef<jobject> j_external_estimate_provider_;
69 // Task runner that accesses ExternalEstimateProviderAndroid members.
70 scoped_refptr<base::TaskRunner> task_runner_;
72 // Notified every time there is an update available from the network quality
73 // provider.
74 // TODO(tbansal): Add the function that is called by Java side when an update
75 // is available.
76 net::ExternalEstimateProvider::UpdatedEstimateDelegate* delegate_;
78 base::ThreadChecker thread_checker_;
80 // Used for posting tasks.
81 base::WeakPtrFactory<ExternalEstimateProviderAndroid> weak_factory_;
83 DISALLOW_COPY_AND_ASSIGN(ExternalEstimateProviderAndroid);
86 bool RegisterExternalEstimateProviderAndroid(JNIEnv* env);
88 } // namespace android
89 } // namespace chrome
91 #endif // CHROME_BROWSER_ANDROID_NET_EXTERNAL_ESTIMATE_PROVIDER_ANDROID_H_