|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.background.healthreport.prune; |
|
6 |
|
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; |
|
11 |
|
12 import android.content.Intent; |
|
13 import android.content.SharedPreferences; |
|
14 import android.os.IBinder; |
|
15 |
|
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"; |
|
25 |
|
26 public HealthReportPruneService() { |
|
27 super(WORKER_THREAD_NAME); |
|
28 } |
|
29 |
|
30 @Override |
|
31 public IBinder onBind(Intent intent) { |
|
32 return null; |
|
33 } |
|
34 |
|
35 protected SharedPreferences getSharedPreferences() { |
|
36 return this.getSharedPreferences(HealthReportConstants.PREFS_BRANCH, GlobalConstants.SHARED_PREFERENCES_MODE); |
|
37 } |
|
38 |
|
39 @Override |
|
40 public void onHandleIntent(Intent intent) { |
|
41 Logger.setThreadLogTag(HealthReportConstants.GLOBAL_LOG_TAG); |
|
42 |
|
43 // Intent can be null. Bug 1025937. |
|
44 if (intent == null) { |
|
45 Logger.debug(LOG_TAG, "Short-circuiting on null intent."); |
|
46 return; |
|
47 } |
|
48 |
|
49 Logger.debug(LOG_TAG, "Handling prune intent."); |
|
50 |
|
51 if (!isIntentValid(intent)) { |
|
52 Logger.warn(LOG_TAG, "Intent not valid - returning."); |
|
53 return; |
|
54 } |
|
55 |
|
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 } |
|
62 |
|
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 } |
|
68 |
|
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; |
|
75 |
|
76 final String profileName = intent.getStringExtra("profileName"); |
|
77 if (profileName == null) { |
|
78 Logger.warn(LOG_TAG, "Got intent without profileName."); |
|
79 isValid = false; |
|
80 } |
|
81 |
|
82 final String profilePath = intent.getStringExtra("profilePath"); |
|
83 if (profilePath == null) { |
|
84 Logger.warn(LOG_TAG, "Got intent without profilePath."); |
|
85 isValid = false; |
|
86 } |
|
87 |
|
88 return isValid; |
|
89 } |
|
90 } |