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 static void DisableNotificationForTesting(); | |
|
jkarlin
2015/09/19 00:16:07
Since this is public please add a comment
iclelland
2015/09/21 18:49:30
Done.
| |
| 33 | |
| 34 // This class lives on the UI thread and mediates all access to the Java | |
| 35 // BackgroundSyncNetworkObserver, which it creates and owns. It is in turn | |
| 36 // owned by the BackgroundSyncNetworkObserverAndroid. | |
| 37 class Observer : public base::RefCountedThreadSafe< | |
| 38 BackgroundSyncNetworkObserverAndroid::Observer, | |
| 39 content::BrowserThread::DeleteOnUIThread> { | |
| 40 public: | |
| 41 static scoped_refptr<BackgroundSyncNetworkObserverAndroid::Observer> Create( | |
| 42 base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> | |
| 43 callback); | |
| 44 ~Observer(); | |
| 45 | |
| 46 static bool RegisterNetworkObserver(JNIEnv* env); | |
| 47 | |
| 48 // Called from BackgroundSyncNetworkObserver.java over JNI whenever the | |
| 49 // connection type changes. This updates the current connection type seen by | |
| 50 // this class and calls the |network_changed_callback| provided to the | |
| 51 // constructor, with the new connection type. | |
|
jkarlin
2015/09/19 00:16:07
append "on the IO thread" to the comment.
iclelland
2015/09/21 18:49:30
Done.
| |
| 52 void NotifyConnectionTypeChanged(JNIEnv* env, | |
| 53 jobject jcaller, | |
| 54 jint new_connection_type); | |
| 55 | |
| 56 private: | |
| 57 Observer(base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> | |
| 58 callback); | |
| 59 void Init(); | |
| 60 | |
| 61 base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback_; | |
|
jkarlin
2015/09/19 00:16:07
Add comment that callback_ is to be run on the IO
iclelland
2015/09/21 18:49:30
Done.
| |
| 62 base::android::ScopedJavaGlobalRef<jobject> j_observer_; | |
| 63 JNIEnv* env_; | |
|
jkarlin
2015/09/19 00:16:07
DISALLOW_COPY_AND_ASSIGN
iclelland
2015/09/21 18:49:30
Done.
| |
| 64 }; | |
| 65 | |
| 66 private: | |
| 67 // Accessed on UI Thread | |
| 68 scoped_refptr<Observer> observer_; | |
| 69 | |
| 70 base::WeakPtrFactory<BackgroundSyncNetworkObserverAndroid> weak_ptr_factory_; | |
| 71 }; | |
| 72 | |
| 73 } // namespace content | |
| 74 | |
| 75 #endif // CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ | |
| OLD | NEW |