Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(524)

Side by Side Diff: content/browser/android/background_sync_network_observer_android.cc

Issue 1294603003: [BackgroundSync] Trigger Background Sync events when Chrome is backgrounded on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hold JNI references in a dedicated class on UI thread Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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) {
jkarlin 2015/09/16 17:39:44 DCHECK on UI thread
iclelland 2015/09/16 19:14:16 Done. Well, actually checks for IO thread now, sin
20 // Attach a Java BackgroundSyncNetworkObserver object. Its lifetime will be
21 // scoped to the lifetime of this object.
22 JNIEnv* env = base::android::AttachCurrentThread();
23 base::android::ScopedJavaLocalRef<jobject> obj =
24 Java_BackgroundSyncNetworkObserver_createObserver(
25 env, base::android::GetApplicationContext(),
26 reinterpret_cast<jlong>(this));
27 observer_ = obj;
28 }
29
30 void BackgroundSyncNetworkObserverAndroid::Observer::
31 NotifyConnectionTypeChanged(JNIEnv* env,
32 jobject jcaller,
33 jint new_connection_type) {
jkarlin 2015/09/16 17:39:44 DCHECK on UI thread
iclelland 2015/09/16 19:14:16 Done. See the corresponding change to the Java Bac
34 BrowserThread::PostTask(
35 BrowserThread::IO, FROM_HERE,
36 base::Bind(callback_,
37 static_cast<net::NetworkChangeNotifier::ConnectionType>(
38 new_connection_type)));
39 }
40
41 BackgroundSyncNetworkObserverAndroid::BackgroundSyncNetworkObserverAndroid(
42 const base::Closure& network_changed_callback)
43 : BackgroundSyncNetworkObserver(network_changed_callback),
44 weak_ptr_factory_(this) {
45 DCHECK_CURRENTLY_ON(BrowserThread::IO);
46
47 // Remove the observer attached by the NetworkObserver constructor
48 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
49
50 BrowserThread::PostTask(
51 BrowserThread::UI, FROM_HERE,
52 base::Bind(
53 &BackgroundSyncNetworkObserverAndroid::InitializeOnUIThread,
54 weak_ptr_factory_.GetWeakPtr(),
jkarlin 2015/09/16 17:39:44 You can't use a weakptr on a different thread. He
iclelland 2015/09/16 17:47:38 Right -- I didn't think I was, by doing it this wa
iclelland 2015/09/16 19:14:16 Done.
55
56 base::Bind(&BackgroundSyncNetworkObserverAndroid::OnNetworkChanged,
57 weak_ptr_factory_.GetWeakPtr())));
58 }
59
60 void BackgroundSyncNetworkObserverAndroid::InitializeOnUIThread(
61 base::Callback<void(net::NetworkChangeNotifier::ConnectionType)> callback) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI);
63 // Attach a native observer on the UI thread
64 observer_ = new Observer(callback);
65 }
66
67 BackgroundSyncNetworkObserverAndroid::~BackgroundSyncNetworkObserverAndroid() {
68 DCHECK_CURRENTLY_ON(BrowserThread::IO);
69 }
70 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/android/background_sync_network_observer_android.h ('k') | content/browser/android/browser_jni_registrar.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698