[SyncFS] Add completion callback to PromoteDemotedChanges
[chromium-blink-merge.git] / net / android / network_change_notifier_delegate_android.cc
blobfbbaceddd948b73c460d471081b8f0735e462c03
1 // Copyright (c) 2012 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 "net/android/network_change_notifier_delegate_android.h"
7 #include "base/logging.h"
8 #include "jni/NetworkChangeNotifier_jni.h"
10 namespace net {
12 namespace {
14 // Converts a Java side connection type (integer) to
15 // the native side NetworkChangeNotifier::ConnectionType.
16 NetworkChangeNotifier::ConnectionType ConvertConnectionType(
17 jint connection_type) {
18 switch (connection_type) {
19 case NetworkChangeNotifier::CONNECTION_UNKNOWN:
20 case NetworkChangeNotifier::CONNECTION_ETHERNET:
21 case NetworkChangeNotifier::CONNECTION_WIFI:
22 case NetworkChangeNotifier::CONNECTION_2G:
23 case NetworkChangeNotifier::CONNECTION_3G:
24 case NetworkChangeNotifier::CONNECTION_4G:
25 case NetworkChangeNotifier::CONNECTION_NONE:
26 case NetworkChangeNotifier::CONNECTION_BLUETOOTH:
27 break;
28 default:
29 NOTREACHED() << "Unknown connection type received: " << connection_type;
30 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
32 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type);
35 } // namespace
37 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
38 : observers_(new ObserverListThreadSafe<Observer>()) {
39 JNIEnv* env = base::android::AttachCurrentThread();
40 java_network_change_notifier_.Reset(
41 Java_NetworkChangeNotifier_init(
42 env, base::android::GetApplicationContext()));
43 Java_NetworkChangeNotifier_addNativeObserver(
44 env, java_network_change_notifier_.obj(),
45 reinterpret_cast<intptr_t>(this));
46 SetCurrentConnectionType(
47 ConvertConnectionType(
48 Java_NetworkChangeNotifier_getCurrentConnectionType(
49 env, java_network_change_notifier_.obj())));
52 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
53 DCHECK(thread_checker_.CalledOnValidThread());
54 observers_->AssertEmpty();
55 JNIEnv* env = base::android::AttachCurrentThread();
56 Java_NetworkChangeNotifier_removeNativeObserver(
57 env, java_network_change_notifier_.obj(),
58 reinterpret_cast<intptr_t>(this));
61 NetworkChangeNotifier::ConnectionType
62 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const {
63 base::AutoLock auto_lock(connection_type_lock_);
64 return connection_type_;
67 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
68 JNIEnv* env,
69 jobject obj,
70 jint new_connection_type) {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 const ConnectionType actual_connection_type = ConvertConnectionType(
73 new_connection_type);
74 SetCurrentConnectionType(actual_connection_type);
75 observers_->Notify(&Observer::OnConnectionTypeChanged);
78 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
79 jobject) const {
80 DCHECK(thread_checker_.CalledOnValidThread());
81 return GetCurrentConnectionType();
84 void NetworkChangeNotifierDelegateAndroid::AddObserver(
85 Observer* observer) {
86 observers_->AddObserver(observer);
89 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(
90 Observer* observer) {
91 observers_->RemoveObserver(observer);
94 // static
95 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) {
96 return RegisterNativesImpl(env);
99 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType(
100 ConnectionType new_connection_type) {
101 base::AutoLock auto_lock(connection_type_lock_);
102 connection_type_ = new_connection_type;
105 void NetworkChangeNotifierDelegateAndroid::SetOnline() {
106 JNIEnv* env = base::android::AttachCurrentThread();
107 Java_NetworkChangeNotifier_forceConnectivityState(env, true);
110 void NetworkChangeNotifierDelegateAndroid::SetOffline() {
111 JNIEnv* env = base::android::AttachCurrentThread();
112 Java_NetworkChangeNotifier_forceConnectivityState(env, false);
115 } // namespace net