forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland of [Background Sync] Trigger Background Sync events when Chrom…
…e is backgrounded on Android Original review: https://codereview.chromium.org/1294603003/ (Committed as https://crrev.com/a5fb97d6fb9c3831994aadebea4664e2acc3c356) Reverted in https://codereview.chromium.org/1376563003/ (as https://crrev.com/c532c674b657426cb733c2b735bbf5741bcbea48) BUG=511129 Review URL: https://codereview.chromium.org/1383663004 Cr-Commit-Position: refs/heads/master@{#352019}
- Loading branch information
Showing
16 changed files
with
393 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
content/browser/android/background_sync_network_observer_android.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "content/browser/android/background_sync_network_observer_android.h" | ||
|
||
#include "jni/BackgroundSyncNetworkObserver_jni.h" | ||
|
||
namespace content { | ||
|
||
// static | ||
bool BackgroundSyncNetworkObserverAndroid::Observer::RegisterNetworkObserver( | ||
JNIEnv* env) { | ||
return RegisterNativesImpl(env); | ||
} | ||
|
||
// static | ||
scoped_refptr<BackgroundSyncNetworkObserverAndroid::Observer> | ||
BackgroundSyncNetworkObserverAndroid::Observer::Create( | ||
base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback) { | ||
DCHECK_CURRENTLY_ON(BrowserThread::IO); | ||
scoped_refptr<BackgroundSyncNetworkObserverAndroid::Observer> observer( | ||
new BackgroundSyncNetworkObserverAndroid::Observer(callback)); | ||
BrowserThread::PostTask( | ||
BrowserThread::UI, FROM_HERE, | ||
base::Bind(&BackgroundSyncNetworkObserverAndroid::Observer::Init, | ||
observer)); | ||
return observer; | ||
} | ||
|
||
void BackgroundSyncNetworkObserverAndroid::Observer::Init() { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
// Attach a Java BackgroundSyncNetworkObserver object. Its lifetime will be | ||
// scoped to the lifetime of this object. | ||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
base::android::ScopedJavaGlobalRef<jobject> obj( | ||
Java_BackgroundSyncNetworkObserver_createObserver( | ||
env, base::android::GetApplicationContext(), | ||
reinterpret_cast<jlong>(this))); | ||
j_observer_.Reset(obj); | ||
} | ||
|
||
BackgroundSyncNetworkObserverAndroid::Observer::~Observer() { | ||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
Java_BackgroundSyncNetworkObserver_removeObserver( | ||
env, j_observer_.obj(), reinterpret_cast<jlong>(this)); | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
j_observer_.Release(); | ||
} | ||
|
||
void BackgroundSyncNetworkObserverAndroid::Observer:: | ||
NotifyConnectionTypeChanged(JNIEnv* env, | ||
jobject jcaller, | ||
jint new_connection_type) { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
BrowserThread::PostTask( | ||
BrowserThread::IO, FROM_HERE, | ||
base::Bind(callback_, | ||
static_cast<net::NetworkChangeNotifier::ConnectionType>( | ||
new_connection_type))); | ||
} | ||
|
||
BackgroundSyncNetworkObserverAndroid::Observer::Observer( | ||
base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback) | ||
: callback_(callback) { | ||
DCHECK_CURRENTLY_ON(BrowserThread::IO); | ||
} | ||
|
||
BackgroundSyncNetworkObserverAndroid::BackgroundSyncNetworkObserverAndroid( | ||
const base::Closure& network_changed_callback) | ||
: BackgroundSyncNetworkObserver(network_changed_callback), | ||
weak_ptr_factory_(this) { | ||
DCHECK_CURRENTLY_ON(BrowserThread::IO); | ||
|
||
// Remove the observer attached by the NetworkObserver constructor | ||
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | ||
|
||
observer_ = Observer::Create( | ||
base::Bind(&BackgroundSyncNetworkObserverAndroid::OnNetworkChanged, | ||
weak_ptr_factory_.GetWeakPtr())); | ||
} | ||
|
||
BackgroundSyncNetworkObserverAndroid::~BackgroundSyncNetworkObserverAndroid() { | ||
DCHECK_CURRENTLY_ON(BrowserThread::IO); | ||
} | ||
} // namespace content |
80 changes: 80 additions & 0 deletions
80
content/browser/android/background_sync_network_observer_android.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ | ||
#define CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ | ||
|
||
#include "base/android/jni_android.h" | ||
#include "base/android/scoped_java_ref.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "content/browser/background_sync/background_sync_network_observer.h" | ||
#include "content/public/browser/browser_thread.h" | ||
|
||
namespace content { | ||
|
||
// BackgroundSyncNetworkObserverAndroid is a specialized | ||
// BackgroundSyncNetworkObserver which is backed by a NetworkChangeNotifier | ||
// that listens for network events even when the browser is paused, unlike the | ||
// standard NetworkChangeNotifier. This ensures that sync events can be fired | ||
// even when the browser is backgrounded, and other network observers are | ||
// disabled. | ||
class BackgroundSyncNetworkObserverAndroid | ||
: public BackgroundSyncNetworkObserver { | ||
public: | ||
// Creates a BackgroundSyncNetworkObserver. |network_changed_callback| is | ||
// called via PostMessage when the network connection changes. | ||
BackgroundSyncNetworkObserverAndroid( | ||
const base::Closure& network_changed_callback); | ||
|
||
~BackgroundSyncNetworkObserverAndroid() override; | ||
|
||
// This class lives on the UI thread and mediates all access to the Java | ||
// BackgroundSyncNetworkObserver, which it creates and owns. It is in turn | ||
// owned by the BackgroundSyncNetworkObserverAndroid. | ||
class Observer : public base::RefCountedThreadSafe< | ||
BackgroundSyncNetworkObserverAndroid::Observer, | ||
content::BrowserThread::DeleteOnUIThread> { | ||
public: | ||
static scoped_refptr<BackgroundSyncNetworkObserverAndroid::Observer> Create( | ||
base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> | ||
callback); | ||
|
||
static bool RegisterNetworkObserver(JNIEnv* env); | ||
|
||
// Called from BackgroundSyncNetworkObserver.java over JNI whenever the | ||
// connection type changes. This updates the current connection type seen by | ||
// this class and calls the |network_changed_callback| provided to the | ||
// constructor, on the IO thread, with the new connection type. | ||
void NotifyConnectionTypeChanged(JNIEnv* env, | ||
jobject jcaller, | ||
jint new_connection_type); | ||
|
||
private: | ||
friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | ||
friend class base::DeleteHelper< | ||
BackgroundSyncNetworkObserverAndroid::Observer>; | ||
|
||
Observer(base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> | ||
callback); | ||
void Init(); | ||
~Observer(); | ||
|
||
// This callback is to be run on the IO thread whenever the connection type | ||
// changes. | ||
base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback_; | ||
base::android::ScopedJavaGlobalRef<jobject> j_observer_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(Observer); | ||
}; | ||
|
||
private: | ||
// Accessed on UI Thread | ||
scoped_refptr<Observer> observer_; | ||
|
||
base::WeakPtrFactory<BackgroundSyncNetworkObserverAndroid> weak_ptr_factory_; | ||
}; | ||
|
||
} // namespace content | ||
|
||
#endif // CONTENT_BROWSER_ANDROID_BACKGROUND_SYNC_NETWORK_OBSERVER_ANDROID_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.