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

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/BackgroundSyncLauncherService.java

Issue 1288593003: [BackgroundSync] Move read and write of shared preferences to an AsyncTask (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up Created 5 years, 4 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.content.browser; 5 package org.chromium.content.browser;
6 6
7 import android.app.IntentService; 7 import android.app.IntentService;
8 import android.content.Context; 8 import android.content.Context;
9 import android.content.Intent; 9 import android.content.Intent;
10 import android.net.ConnectivityManager; 10 import android.net.ConnectivityManager;
11 import android.net.NetworkInfo; 11 import android.net.NetworkInfo;
12 import android.preference.PreferenceManager;
13 import android.support.v4.content.WakefulBroadcastReceiver; 12 import android.support.v4.content.WakefulBroadcastReceiver;
14 13
15 import org.chromium.base.Log; 14 import org.chromium.base.Log;
16 import org.chromium.base.ThreadUtils; 15 import org.chromium.base.ThreadUtils;
17 import org.chromium.base.VisibleForTesting; 16 import org.chromium.base.VisibleForTesting;
18 import org.chromium.base.annotations.SuppressFBWarnings; 17 import org.chromium.base.annotations.SuppressFBWarnings;
19 import org.chromium.base.library_loader.LibraryProcessType; 18 import org.chromium.base.library_loader.LibraryProcessType;
20 import org.chromium.base.library_loader.ProcessInitException; 19 import org.chromium.base.library_loader.ProcessInitException;
21 import org.chromium.content.app.ContentApplication; 20 import org.chromium.content.app.ContentApplication;
22 21
23 /** 22 /**
24 * {@link BackgroundSyncLauncherService} monitors network connectivity and launc hes 23 * {@link BackgroundSyncLauncherService} monitors network connectivity and launc hes
25 * the browser when it goes online if the {@link BackgroundSyncLauncher} request ed it. 24 * the browser when it goes online if the {@link BackgroundSyncLauncher} request ed it.
26 */ 25 */
27 public class BackgroundSyncLauncherService extends IntentService { 26 public class BackgroundSyncLauncherService extends IntentService {
28 private static final String TAG = "cr.BgSyncLauncher"; 27 private static final String TAG = "cr.BgSyncLauncher";
29 28
30 /** 29 /**
31 * Receiver for network connection change broadcasts. If the device is onlin e and the browser 30 * Receiver for network connection change broadcasts. If the device is onlin e
32 * should be launched, it starts the BackgroundSyncLauncherService. 31 * and the browser isn't running it starts the BackgroundSyncLauncherService .
32 * The service will then launch the browser if necessary.
33 * 33 *
34 * This class is public so that it can be instantiated by the Android runtim e. 34 * This class is public so that it can be instantiated by the Android runtim e.
35 */ 35 */
36 public static class Receiver extends WakefulBroadcastReceiver { 36 public static class Receiver extends WakefulBroadcastReceiver {
37 @Override 37 @Override
38 public void onReceive(Context context, Intent intent) { 38 public void onReceive(Context context, Intent intent) {
39 // If online, the browser isn't running, and the browser has request ed 39 // If online, the browser isn't running, and the browser has request ed
40 // it be launched the next time the device is online, start the brow ser. 40 // it be launched the next time the device is online, start the brow ser.
41 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction( )) 41 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction( ))
42 && isOnline(context) && !BackgroundSyncLauncher.hasInstance( ) 42 && isOnline(context) && !BackgroundSyncLauncher.hasInstance( )) {
43 && BackgroundSyncLauncher.shouldLaunchWhenNextOnline(
44 PreferenceManager.getDefaultSharedPreferences(con text))) {
45 startService(context); 43 startService(context);
46 } 44 }
47 } 45 }
48 46
49 @VisibleForTesting 47 @VisibleForTesting
50 protected void startService(Context context) { 48 protected void startService(Context context) {
51 Intent serviceIntent = new Intent(context, BackgroundSyncLauncherSer vice.class); 49 Intent serviceIntent = new Intent(context, BackgroundSyncLauncherSer vice.class);
52 startWakefulService(context, serviceIntent); 50 startWakefulService(context, serviceIntent);
53 } 51 }
54 52
(...skipping 17 matching lines...) Expand all
72 @Override 70 @Override
73 public void run() { 71 public void run() {
74 onOnline(getApplicationContext()); 72 onOnline(getApplicationContext());
75 } 73 }
76 }); 74 });
77 } finally { 75 } finally {
78 WakefulBroadcastReceiver.completeWakefulIntent(intent); 76 WakefulBroadcastReceiver.completeWakefulIntent(intent);
79 } 77 }
80 } 78 }
81 79
82 private void onOnline(Context context) { 80 private void onOnline(final Context context) {
83 ThreadUtils.assertOnUiThread(); 81 ThreadUtils.assertOnUiThread();
84 82
85 // Start the browser. The browser's BackgroundSyncManager (for the activ e profile) will 83 BackgroundSyncLauncher.ShouldLaunchCallback callback =
86 // start, check the network, and run any necessary sync events. It runs without a wake lock. 84 new BackgroundSyncLauncher.ShouldLaunchCallback() {
87 // TODO(jkarlin): Protect the browser sync event with a wake lock. See c rbug.com/486020. 85 @Override
88 Log.v(TAG, "Starting Browser after coming online"); 86 public void run(Boolean shouldLaunch) {
89 launchBrowser(context); 87 if (shouldLaunch) {
mlamouri (slow - plz ping) 2015/08/21 09:40:54 nit: if (!shouldLaunch) return;
jkarlin 2015/08/21 12:03:11 Done.
88 // Start the browser. The browser's BackgroundSyncMa nager (for the
89 // active profile) will start, check the network, an d run any necessary
90 // sync events. It runs without a wake lock.
91 // TODO(jkarlin): Protect the browser sync event wit h a wake lock. See
92 // crbug.com/486020.
93 Log.v(TAG, "Starting Browser after coming online");
94 launchBrowser(context);
95 }
96 }
97 };
98 BackgroundSyncLauncher.shouldLaunchWhenNextOnline(context, callback);
90 } 99 }
91 100
92 @SuppressFBWarnings("DM_EXIT") 101 @SuppressFBWarnings("DM_EXIT")
93 private void launchBrowser(Context context) { 102 private void launchBrowser(Context context) {
94 ContentApplication.initCommandLine(context); 103 ContentApplication.initCommandLine(context);
95 try { 104 try {
96 BrowserStartupController.get(context, LibraryProcessType.PROCESS_BRO WSER) 105 BrowserStartupController.get(context, LibraryProcessType.PROCESS_BRO WSER)
97 .startBrowserProcessesSync(false); 106 .startBrowserProcessesSync(false);
98 } catch (ProcessInitException e) { 107 } catch (ProcessInitException e) {
99 Log.e(TAG, "ProcessInitException while starting the browser process" ); 108 Log.e(TAG, "ProcessInitException while starting the browser process" );
100 // Since the library failed to initialize nothing in the application 109 // Since the library failed to initialize nothing in the application
101 // can work, so kill the whole application not just the activity. 110 // can work, so kill the whole application not just the activity.
102 System.exit(-1); 111 System.exit(-1);
103 } 112 }
104 } 113 }
105 } 114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698