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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java

Issue 1699143002: [NTP Snippets] Schedule periodic fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_feature
Patch Set: fix bots Created 4 years, 9 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 2016 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 package org.chromium.chrome.browser.ntp.snippets;
6
7 import android.content.Context;
8
9 import com.google.android.gms.gcm.GcmNetworkManager;
10 import com.google.android.gms.gcm.PeriodicTask;
11 import com.google.android.gms.gcm.Task;
12
13 import org.chromium.base.Log;
14 import org.chromium.base.VisibleForTesting;
15 import org.chromium.base.annotations.CalledByNative;
16 import org.chromium.base.annotations.SuppressFBWarnings;
17 import org.chromium.chrome.browser.ChromeBackgroundService;
18 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils;
19 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler;
20
21 /**
22 * The {@link SnippetsLauncher} singleton is created and owned by the C++ browse r.
23 *
24 * Thread model: This class is to be run on the UI thread only.
25 */
26 public class SnippetsLauncher {
27 private static final String TAG = "SnippetsLauncher";
28
29 public static final String TASK_TAG = "FetchSnippets";
30
31 // The instance of SnippetsLauncher currently owned by a C++ SnippetsLaunche rAndroid, if any.
32 // If it is non-null then the browser is running.
33 private static SnippetsLauncher sInstance;
34
35 private GcmNetworkManager mScheduler;
36
37 private boolean mGCMEnabled = true;
38
39 /**
40 * Create a SnippetsLauncher object, which is owned by C++.
41 * @param context The app context.
42 */
43 @VisibleForTesting
44 @CalledByNative
45 public static SnippetsLauncher create(Context context) {
46 if (sInstance != null) {
47 throw new IllegalStateException("Already instantiated");
48 }
49
50 sInstance = new SnippetsLauncher(context);
51 return sInstance;
52 }
53
54 /**
55 * Called when the C++ counterpart is deleted.
56 */
57 @VisibleForTesting
58 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
59 @CalledByNative
60 public void destroy() {
61 assert sInstance == this;
62 sInstance = null;
63 }
64
65 /**
66 * Returns true if the native browser has started and created an instance of {@link
67 * SnippetsLauncher}.
68 */
69 public static boolean hasInstance() {
70 return sInstance != null;
71 }
72
73 protected SnippetsLauncher(Context context) {
74 checkGCM(context);
75 mScheduler = GcmNetworkManager.getInstance(context);
76 }
77
78 private boolean canUseGooglePlayServices(Context context) {
79 return ExternalAuthUtils.getInstance().canUseGooglePlayServices(
80 context, new UserRecoverableErrorHandler.Silent());
81 }
82
83 private void checkGCM(Context context) {
84 // Check to see if Play Services is up to date, and disable GCM if not.
85 if (!canUseGooglePlayServices(context)) {
86 mGCMEnabled = false;
87 Log.i(TAG, "Disabling SnippetsLauncher because Play Services is not up to date.");
88 }
89 }
90
91 @CalledByNative
92 private boolean schedule(int periodSeconds) {
93 if (!mGCMEnabled) return false;
94 Log.i(TAG, "Scheduling: " + periodSeconds);
95 // Google Play Services may not be up to date, if the application was no t installed through
96 // the Play Store. In this case, scheduling the task will fail silently.
97 PeriodicTask task = new PeriodicTask.Builder()
98 .setService(ChromeBackgroundService.class)
99 .setTag(TASK_TAG)
100 .setPeriod(periodSeconds)
101 .setRequiredNetwork(Task.NETWORK_STATE_UNMET ERED)
102 .setPersisted(true)
103 .setUpdateCurrent(true)
104 .build();
105 try {
106 mScheduler.schedule(task);
107 } catch (IllegalArgumentException e) {
108 // Disable GCM for the remainder of this session.
109 mGCMEnabled = false;
110 // Return false so that the failure will be logged.
111 return false;
112 }
113 return true;
114 }
115
116 @CalledByNative
117 private boolean unschedule() {
118 if (!mGCMEnabled) return false;
119 Log.i(TAG, "Unscheduling");
120 try {
121 mScheduler.cancelTask("SnippetsLauncher", ChromeBackgroundService.cl ass);
122 } catch (IllegalArgumentException e) {
123 // This occurs when SnippetsLauncherService is not found in the appl ication
124 // manifest. Disable GCM for the remainder of this session.
125 mGCMEnabled = false;
126 // Return false so that the failure will be logged.
127 return false;
128 }
129 return true;
130 }
131 }
132
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698