Chromium Code Reviews| 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 #ifndef CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ | |
| 6 #define CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ | |
| 7 | |
| 8 #include "base/android/jni_android.h" | |
| 9 #include "base/android/scoped_java_ref.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "content/browser/background_sync/background_sync_network_observer.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 // BackgroundSyncNetworkObserverAndroid is a specialized | |
| 17 // BackgroundSyncNetworkObserver which is backed by a NetworkChangeNotifier | |
| 18 // that listens for network events even when the browser is paused, unlike the | |
| 19 // standard NetworkChangeNotifier. This ensures that sync events can be fired | |
| 20 // even when the browser is backgrounded, and other network observers are | |
| 21 // disabled. | |
| 22 class BackgroundSyncNetworkObserverAndroid | |
| 23 : public BackgroundSyncNetworkObserver { | |
| 24 public: | |
| 25 // Creates a BackgroundSyncNetworkObserver. |network_changed_callback| is | |
| 26 // called via PostMessage when the network connection changes. | |
| 27 BackgroundSyncNetworkObserverAndroid( | |
| 28 const base::Closure& network_changed_callback); | |
| 29 | |
| 30 ~BackgroundSyncNetworkObserverAndroid() override; | |
| 31 | |
| 32 // This class lives on the UI thread and mediates all access to the Java | |
| 33 // BackgroundSyncNetworkObserver, which it creates and owns. It is in turn | |
| 34 // owned by the BackgroundSyncNetworkObserverAndroid. | |
| 35 class Observer : public base::RefCountedThreadSafe< | |
| 36 BackgroundSyncNetworkObserverAndroid::Observer, | |
| 37 content::BrowserThread::DeleteOnUIThread> { | |
| 38 public: | |
| 39 Observer(base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> | |
| 40 callback); | |
| 41 ~Observer(); | |
| 42 | |
| 43 static bool RegisterNetworkObserver(JNIEnv* env); | |
| 44 | |
| 45 // Called from BackgroundSyncNetworkObserver.java over JNI whenever the | |
| 46 // connection type changes. This updates the current connection type seen by | |
| 47 // this class and calls the |network_changed_callback| provided to the | |
| 48 // constructor, with the new connection type. | |
| 49 void NotifyConnectionTypeChanged(JNIEnv* env, | |
| 50 jobject jcaller, | |
| 51 jint new_connection_type); | |
| 52 | |
| 53 private: | |
| 54 base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback_; | |
| 55 base::android::ScopedJavaLocalRef<jobject> observer_; | |
|
Yaron
2015/09/16 22:04:36
Nit: rename j_observer_ (to help distinguish betwe
iclelland
2015/09/18 15:46:28
Done.
| |
| 56 }; | |
| 57 | |
| 58 private: | |
| 59 // Accessed on UI Thread | |
| 60 scoped_refptr<Observer> observer_; | |
| 61 | |
| 62 base::WeakPtrFactory<BackgroundSyncNetworkObserverAndroid> weak_ptr_factory_; | |
| 63 }; | |
| 64 | |
| 65 } // namespace content | |
| 66 | |
| 67 #endif // CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ | |
| OLD | NEW |