OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/browser/android/background_sync_network_observer_android.h" | |
6 | |
7 #include "jni/BackgroundSyncNetworkObserver_jni.h" | |
8 | |
9 namespace content { | |
10 | |
11 // static | |
12 bool BackgroundSyncNetworkObserverAndroid::Observer::RegisterNetworkObserver( | |
13 JNIEnv* env) { | |
14 return RegisterNativesImpl(env); | |
15 } | |
16 | |
17 BackgroundSyncNetworkObserverAndroid::Observer::Observer( | |
18 base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback) | |
19 : callback_(callback) { | |
20 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
jkarlin
2015/09/16 20:13:26
It seems like the code here and below in this func
iclelland
2015/09/18 15:46:28
That's a great idea -- thanks for that! I've added
| |
21 // Attach a Java BackgroundSyncNetworkObserver object. Its lifetime will be | |
22 // scoped to the lifetime of this object. | |
23 JNIEnv* env = base::android::AttachCurrentThread(); | |
24 base::android::ScopedJavaLocalRef<jobject> obj = | |
25 Java_BackgroundSyncNetworkObserver_createObserver( | |
26 env, base::android::GetApplicationContext(), | |
27 reinterpret_cast<jlong>(this)); | |
28 observer_ = obj; | |
29 } | |
30 | |
31 BackgroundSyncNetworkObserverAndroid::Observer::~Observer() { | |
32 } | |
33 | |
34 void BackgroundSyncNetworkObserverAndroid::Observer:: | |
35 NotifyConnectionTypeChanged(JNIEnv* env, | |
36 jobject jcaller, | |
37 jint new_connection_type) { | |
38 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
39 BrowserThread::PostTask( | |
40 BrowserThread::IO, FROM_HERE, | |
41 base::Bind(callback_, | |
42 static_cast<net::NetworkChangeNotifier::ConnectionType>( | |
43 new_connection_type))); | |
44 } | |
45 | |
46 BackgroundSyncNetworkObserverAndroid::BackgroundSyncNetworkObserverAndroid( | |
47 const base::Closure& network_changed_callback) | |
48 : BackgroundSyncNetworkObserver(network_changed_callback), | |
49 weak_ptr_factory_(this) { | |
50 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
51 | |
52 // Remove the observer attached by the NetworkObserver constructor | |
53 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | |
54 | |
55 observer_ = new Observer( | |
56 base::Bind(&BackgroundSyncNetworkObserverAndroid::OnNetworkChanged, | |
57 weak_ptr_factory_.GetWeakPtr())); | |
58 } | |
59 | |
60 BackgroundSyncNetworkObserverAndroid::~BackgroundSyncNetworkObserverAndroid() { | |
61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
62 } | |
63 } // namespace content | |
OLD | NEW |