Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.background.healthreport; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.background.BackgroundService; |
michael@0 | 8 | import org.mozilla.gecko.background.common.GlobalConstants; |
michael@0 | 9 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 10 | import org.mozilla.gecko.background.healthreport.prune.HealthReportPruneService; |
michael@0 | 11 | import org.mozilla.gecko.background.healthreport.upload.HealthReportUploadService; |
michael@0 | 12 | import org.mozilla.gecko.background.healthreport.upload.ObsoleteDocumentTracker; |
michael@0 | 13 | |
michael@0 | 14 | import android.app.AlarmManager; |
michael@0 | 15 | import android.app.PendingIntent; |
michael@0 | 16 | import android.content.Context; |
michael@0 | 17 | import android.content.Intent; |
michael@0 | 18 | import android.content.SharedPreferences; |
michael@0 | 19 | import android.content.SharedPreferences.Editor; |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * A service which listens to broadcast intents from the system and from the |
michael@0 | 23 | * browser, registering or unregistering the background health report services with the |
michael@0 | 24 | * {@link AlarmManager}. |
michael@0 | 25 | */ |
michael@0 | 26 | public class HealthReportBroadcastService extends BackgroundService { |
michael@0 | 27 | public static final String LOG_TAG = HealthReportBroadcastService.class.getSimpleName(); |
michael@0 | 28 | public static final String WORKER_THREAD_NAME = LOG_TAG + "Worker"; |
michael@0 | 29 | |
michael@0 | 30 | public HealthReportBroadcastService() { |
michael@0 | 31 | super(WORKER_THREAD_NAME); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | protected SharedPreferences getSharedPreferences() { |
michael@0 | 35 | return this.getSharedPreferences(HealthReportConstants.PREFS_BRANCH, GlobalConstants.SHARED_PREFERENCES_MODE); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | public long getSubmissionPollInterval() { |
michael@0 | 39 | return getSharedPreferences().getLong(HealthReportConstants.PREF_SUBMISSION_INTENT_INTERVAL_MSEC, HealthReportConstants.DEFAULT_SUBMISSION_INTENT_INTERVAL_MSEC); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | public void setSubmissionPollInterval(final long interval) { |
michael@0 | 43 | getSharedPreferences().edit().putLong(HealthReportConstants.PREF_SUBMISSION_INTENT_INTERVAL_MSEC, interval).commit(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | public long getPrunePollInterval() { |
michael@0 | 47 | return getSharedPreferences().getLong(HealthReportConstants.PREF_PRUNE_INTENT_INTERVAL_MSEC, |
michael@0 | 48 | HealthReportConstants.DEFAULT_PRUNE_INTENT_INTERVAL_MSEC); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | public void setPrunePollInterval(final long interval) { |
michael@0 | 52 | getSharedPreferences().edit().putLong(HealthReportConstants.PREF_PRUNE_INTENT_INTERVAL_MSEC, |
michael@0 | 53 | interval).commit(); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Set or cancel an alarm to submit data for a profile. |
michael@0 | 58 | * |
michael@0 | 59 | * @param context |
michael@0 | 60 | * Android context. |
michael@0 | 61 | * @param profileName |
michael@0 | 62 | * to submit data for. |
michael@0 | 63 | * @param profilePath |
michael@0 | 64 | * to submit data for. |
michael@0 | 65 | * @param enabled |
michael@0 | 66 | * whether the user has enabled submitting health report data for |
michael@0 | 67 | * this profile. |
michael@0 | 68 | * @param serviceEnabled |
michael@0 | 69 | * whether submitting should be scheduled. If the user turns off |
michael@0 | 70 | * submitting, <code>enabled</code> could be false but we could need |
michael@0 | 71 | * to delete so <code>serviceEnabled</code> could be true. |
michael@0 | 72 | */ |
michael@0 | 73 | protected void toggleSubmissionAlarm(final Context context, String profileName, String profilePath, |
michael@0 | 74 | boolean enabled, boolean serviceEnabled) { |
michael@0 | 75 | final Class<?> serviceClass = HealthReportUploadService.class; |
michael@0 | 76 | Logger.info(LOG_TAG, (serviceEnabled ? "R" : "Unr") + "egistering " + |
michael@0 | 77 | serviceClass.getSimpleName() + "."); |
michael@0 | 78 | |
michael@0 | 79 | // PendingIntents are compared without reference to their extras. Therefore |
michael@0 | 80 | // even though we pass the profile details to the action, different |
michael@0 | 81 | // profiles will share the *same* pending intent. In a multi-profile future, |
michael@0 | 82 | // this will need to be addressed. See Bug 882182. |
michael@0 | 83 | final Intent service = new Intent(context, serviceClass); |
michael@0 | 84 | service.setAction("upload"); // PendingIntents "lose" their extras if no action is set. |
michael@0 | 85 | service.putExtra("uploadEnabled", enabled); |
michael@0 | 86 | service.putExtra("profileName", profileName); |
michael@0 | 87 | service.putExtra("profilePath", profilePath); |
michael@0 | 88 | final PendingIntent pending = PendingIntent.getService(context, 0, service, PendingIntent.FLAG_CANCEL_CURRENT); |
michael@0 | 89 | |
michael@0 | 90 | if (!serviceEnabled) { |
michael@0 | 91 | cancelAlarm(pending); |
michael@0 | 92 | return; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | final long pollInterval = getSubmissionPollInterval(); |
michael@0 | 96 | scheduleAlarm(pollInterval, pending); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | @Override |
michael@0 | 100 | protected void onHandleIntent(Intent intent) { |
michael@0 | 101 | Logger.setThreadLogTag(HealthReportConstants.GLOBAL_LOG_TAG); |
michael@0 | 102 | |
michael@0 | 103 | // Intent can be null. Bug 1025937. |
michael@0 | 104 | if (intent == null) { |
michael@0 | 105 | Logger.debug(LOG_TAG, "Short-circuiting on null intent."); |
michael@0 | 106 | return; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // The same intent can be handled by multiple methods so do not short-circuit evaluate. |
michael@0 | 110 | boolean handled = attemptHandleIntentForUpload(intent); |
michael@0 | 111 | handled = attemptHandleIntentForPrune(intent) ? true : handled; |
michael@0 | 112 | |
michael@0 | 113 | if (!handled) { |
michael@0 | 114 | Logger.warn(LOG_TAG, "Unhandled intent with action " + intent.getAction() + "."); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * Attempts to handle the given intent for FHR document upload. If it cannot, false is returned. |
michael@0 | 120 | * |
michael@0 | 121 | * @param intent must be non-null. |
michael@0 | 122 | */ |
michael@0 | 123 | private boolean attemptHandleIntentForUpload(final Intent intent) { |
michael@0 | 124 | if (HealthReportConstants.UPLOAD_FEATURE_DISABLED) { |
michael@0 | 125 | Logger.debug(LOG_TAG, "Health report upload feature is compile-time disabled; not handling intent."); |
michael@0 | 126 | return false; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | final String action = intent.getAction(); |
michael@0 | 130 | Logger.debug(LOG_TAG, "Health report upload feature is compile-time enabled; attempting to " + |
michael@0 | 131 | "handle intent with action " + action + "."); |
michael@0 | 132 | |
michael@0 | 133 | if (HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF.equals(action)) { |
michael@0 | 134 | handleUploadPrefIntent(intent); |
michael@0 | 135 | return true; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | if (Intent.ACTION_BOOT_COMPLETED.equals(action) || |
michael@0 | 139 | Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
michael@0 | 140 | BackgroundService.reflectContextToFennec(this, |
michael@0 | 141 | GlobalConstants.GECKO_PREFERENCES_CLASS, |
michael@0 | 142 | GlobalConstants.GECKO_BROADCAST_HEALTHREPORT_UPLOAD_PREF_METHOD); |
michael@0 | 143 | return true; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | return false; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | /** |
michael@0 | 150 | * Handle the intent sent by the browser when it wishes to notify us |
michael@0 | 151 | * of the value of the user preference. Look at the value and toggle the |
michael@0 | 152 | * alarm service accordingly. |
michael@0 | 153 | * |
michael@0 | 154 | * @param intent must be non-null. |
michael@0 | 155 | */ |
michael@0 | 156 | private void handleUploadPrefIntent(Intent intent) { |
michael@0 | 157 | if (!intent.hasExtra("enabled")) { |
michael@0 | 158 | Logger.warn(LOG_TAG, "Got " + HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF + " intent without enabled. Ignoring."); |
michael@0 | 159 | return; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | final boolean enabled = intent.getBooleanExtra("enabled", true); |
michael@0 | 163 | Logger.debug(LOG_TAG, intent.getStringExtra("branch") + "/" + |
michael@0 | 164 | intent.getStringExtra("pref") + " = " + |
michael@0 | 165 | (intent.hasExtra("enabled") ? enabled : "")); |
michael@0 | 166 | |
michael@0 | 167 | String profileName = intent.getStringExtra("profileName"); |
michael@0 | 168 | String profilePath = intent.getStringExtra("profilePath"); |
michael@0 | 169 | |
michael@0 | 170 | if (profileName == null || profilePath == null) { |
michael@0 | 171 | Logger.warn(LOG_TAG, "Got " + HealthReportConstants.ACTION_HEALTHREPORT_UPLOAD_PREF + " intent without profilePath or profileName. Ignoring."); |
michael@0 | 172 | return; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | Logger.pii(LOG_TAG, "Updating health report upload alarm for profile " + profileName + " at " + |
michael@0 | 176 | profilePath + "."); |
michael@0 | 177 | |
michael@0 | 178 | final SharedPreferences sharedPrefs = getSharedPreferences(); |
michael@0 | 179 | final ObsoleteDocumentTracker tracker = new ObsoleteDocumentTracker(sharedPrefs); |
michael@0 | 180 | final boolean hasObsoleteIds = tracker.hasObsoleteIds(); |
michael@0 | 181 | |
michael@0 | 182 | if (!enabled) { |
michael@0 | 183 | final Editor editor = sharedPrefs.edit(); |
michael@0 | 184 | editor.remove(HealthReportConstants.PREF_LAST_UPLOAD_DOCUMENT_ID); |
michael@0 | 185 | |
michael@0 | 186 | if (hasObsoleteIds) { |
michael@0 | 187 | Logger.debug(LOG_TAG, "Health report upload disabled; scheduling deletion of " + tracker.numberOfObsoleteIds() + " documents."); |
michael@0 | 188 | tracker.limitObsoleteIds(); |
michael@0 | 189 | } else { |
michael@0 | 190 | // Primarily intended for debugging and testing. |
michael@0 | 191 | Logger.debug(LOG_TAG, "Health report upload disabled and no deletes to schedule: clearing prefs."); |
michael@0 | 192 | editor.remove(HealthReportConstants.PREF_FIRST_RUN); |
michael@0 | 193 | editor.remove(HealthReportConstants.PREF_NEXT_SUBMISSION); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | editor.commit(); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | // The user can toggle us off or on, or we can have obsolete documents to |
michael@0 | 200 | // remove. |
michael@0 | 201 | final boolean serviceEnabled = hasObsoleteIds || enabled; |
michael@0 | 202 | toggleSubmissionAlarm(this, profileName, profilePath, enabled, serviceEnabled); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | /** |
michael@0 | 206 | * Attempts to handle the given intent for FHR data pruning. If it cannot, false is returned. |
michael@0 | 207 | * |
michael@0 | 208 | * @param intent must be non-null. |
michael@0 | 209 | */ |
michael@0 | 210 | private boolean attemptHandleIntentForPrune(final Intent intent) { |
michael@0 | 211 | final String action = intent.getAction(); |
michael@0 | 212 | Logger.debug(LOG_TAG, "Prune: Attempting to handle intent with action, " + action + "."); |
michael@0 | 213 | |
michael@0 | 214 | if (HealthReportConstants.ACTION_HEALTHREPORT_PRUNE.equals(action)) { |
michael@0 | 215 | handlePruneIntent(intent); |
michael@0 | 216 | return true; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | if (Intent.ACTION_BOOT_COMPLETED.equals(action) || |
michael@0 | 220 | Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
michael@0 | 221 | BackgroundService.reflectContextToFennec(this, |
michael@0 | 222 | GlobalConstants.GECKO_PREFERENCES_CLASS, |
michael@0 | 223 | GlobalConstants.GECKO_BROADCAST_HEALTHREPORT_PRUNE_METHOD); |
michael@0 | 224 | return true; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | return false; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | /** |
michael@0 | 231 | * @param intent must be non-null. |
michael@0 | 232 | */ |
michael@0 | 233 | private void handlePruneIntent(final Intent intent) { |
michael@0 | 234 | final String profileName = intent.getStringExtra("profileName"); |
michael@0 | 235 | final String profilePath = intent.getStringExtra("profilePath"); |
michael@0 | 236 | |
michael@0 | 237 | if (profileName == null || profilePath == null) { |
michael@0 | 238 | Logger.warn(LOG_TAG, "Got " + HealthReportConstants.ACTION_HEALTHREPORT_PRUNE + " intent " + |
michael@0 | 239 | "without profilePath or profileName. Ignoring."); |
michael@0 | 240 | return; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | final Class<?> serviceClass = HealthReportPruneService.class; |
michael@0 | 244 | final Intent service = new Intent(this, serviceClass); |
michael@0 | 245 | service.setAction("prune"); // Intents without actions have their extras removed. |
michael@0 | 246 | service.putExtra("profileName", profileName); |
michael@0 | 247 | service.putExtra("profilePath", profilePath); |
michael@0 | 248 | final PendingIntent pending = PendingIntent.getService(this, 0, service, |
michael@0 | 249 | PendingIntent.FLAG_CANCEL_CURRENT); |
michael@0 | 250 | |
michael@0 | 251 | // Set a regular alarm to start PruneService. Since the various actions that PruneService can |
michael@0 | 252 | // take occur on irregular intervals, we can be more efficient by only starting the Service |
michael@0 | 253 | // when one of these time limits runs out. However, subsequent Service invocations must then |
michael@0 | 254 | // be registered by the PruneService itself, which would fail if the PruneService crashes. |
michael@0 | 255 | // Thus, we set this regular (and slightly inefficient) alarm. |
michael@0 | 256 | Logger.info(LOG_TAG, "Registering " + serviceClass.getSimpleName() + "."); |
michael@0 | 257 | final long pollInterval = getPrunePollInterval(); |
michael@0 | 258 | scheduleAlarm(pollInterval, pending); |
michael@0 | 259 | } |
michael@0 | 260 | } |