mobile/android/base/background/healthreport/prune/HealthReportPruneService.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.background.healthreport.prune;
     7 import org.mozilla.gecko.background.BackgroundService;
     8 import org.mozilla.gecko.background.common.GlobalConstants;
     9 import org.mozilla.gecko.background.common.log.Logger;
    10 import org.mozilla.gecko.background.healthreport.HealthReportConstants;
    12 import android.content.Intent;
    13 import android.content.SharedPreferences;
    14 import android.os.IBinder;
    16 /**
    17  * A <code>Service</code> to prune unnecessary or excessive health report data.
    18  *
    19  * We extend <code>IntentService</code>, rather than just <code>Service</code>,
    20  * because this gives us a worker thread to avoid excessive main-thread disk access.
    21  */
    22 public class HealthReportPruneService extends BackgroundService {
    23   public static final String LOG_TAG = HealthReportPruneService.class.getSimpleName();
    24   public static final String WORKER_THREAD_NAME = LOG_TAG + "Worker";
    26   public HealthReportPruneService() {
    27     super(WORKER_THREAD_NAME);
    28   }
    30   @Override
    31   public IBinder onBind(Intent intent) {
    32     return null;
    33   }
    35   protected SharedPreferences getSharedPreferences() {
    36     return this.getSharedPreferences(HealthReportConstants.PREFS_BRANCH, GlobalConstants.SHARED_PREFERENCES_MODE);
    37   }
    39   @Override
    40   public void onHandleIntent(Intent intent) {
    41     Logger.setThreadLogTag(HealthReportConstants.GLOBAL_LOG_TAG);
    43     // Intent can be null. Bug 1025937.
    44     if (intent == null) {
    45       Logger.debug(LOG_TAG, "Short-circuiting on null intent.");
    46       return;
    47     }
    49     Logger.debug(LOG_TAG, "Handling prune intent.");
    51     if (!isIntentValid(intent)) {
    52       Logger.warn(LOG_TAG, "Intent not valid - returning.");
    53       return;
    54     }
    56     final String profileName = intent.getStringExtra("profileName");
    57     final String profilePath = intent.getStringExtra("profilePath");
    58     Logger.debug(LOG_TAG, "Ticking for profile " + profileName + " at " + profilePath + ".");
    59     final PrunePolicy policy = getPrunePolicy(profilePath);
    60     policy.tick(System.currentTimeMillis());
    61   }
    63   // Generator function wraps constructor for testing purposes.
    64   protected PrunePolicy getPrunePolicy(final String profilePath) {
    65     final PrunePolicyStorage storage = new PrunePolicyDatabaseStorage(this, profilePath);
    66     return new PrunePolicy(storage, getSharedPreferences());
    67   }
    69   /**
    70    * @param intent must be non-null.
    71    * @return true if the supplied intent contains both profileName and profilePath.
    72    */
    73   private static boolean isIntentValid(final Intent intent) {
    74     boolean isValid = true;
    76     final String profileName = intent.getStringExtra("profileName");
    77     if (profileName == null) {
    78       Logger.warn(LOG_TAG, "Got intent without profileName.");
    79       isValid = false;
    80     }
    82     final String profilePath = intent.getStringExtra("profilePath");
    83     if (profilePath == null) {
    84       Logger.warn(LOG_TAG, "Got intent without profilePath.");
    85       isValid = false;
    86     }
    88     return isValid;
    89   }
    90 }

mercurial