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 static scoped_refptr<BackgroundSyncNetworkObserverAndroid::Observer> Create( |
| 40 base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> |
| 41 callback); |
| 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, on the IO thread, with the new connection type. |
| 49 void NotifyConnectionTypeChanged(JNIEnv* env, |
| 50 jobject jcaller, |
| 51 jint new_connection_type); |
| 52 |
| 53 private: |
| 54 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 55 friend class base::DeleteHelper< |
| 56 BackgroundSyncNetworkObserverAndroid::Observer>; |
| 57 |
| 58 Observer(base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> |
| 59 callback); |
| 60 void Init(); |
| 61 ~Observer(); |
| 62 |
| 63 // This callback is to be run on the IO thread whenever the connection type |
| 64 // changes. |
| 65 base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback_; |
| 66 base::android::ScopedJavaGlobalRef<jobject> j_observer_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 69 }; |
| 70 |
| 71 private: |
| 72 // Accessed on UI Thread |
| 73 scoped_refptr<Observer> observer_; |
| 74 |
| 75 base::WeakPtrFactory<BackgroundSyncNetworkObserverAndroid> weak_ptr_factory_; |
| 76 }; |
| 77 |
| 78 } // namespace content |
| 79 |
| 80 #endif // CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ |
OLD | NEW |