mobile/android/base/background/healthreport/prune/PrunePolicyDatabaseStorage.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.

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.prune;
michael@0 6
michael@0 7 import org.mozilla.gecko.background.common.log.Logger;
michael@0 8 import org.mozilla.gecko.background.healthreport.Environment;
michael@0 9 import org.mozilla.gecko.background.healthreport.EnvironmentBuilder;
michael@0 10 import org.mozilla.gecko.background.healthreport.HealthReportDatabaseStorage;
michael@0 11 import org.mozilla.gecko.background.healthreport.ProfileInformationCache;
michael@0 12
michael@0 13 import android.content.ContentProviderClient;
michael@0 14 import android.content.Context;
michael@0 15
michael@0 16 /**
michael@0 17 * Abstracts over the Storage instance behind the PrunePolicy. The underlying storage instance is
michael@0 18 * a {@link HealthReportDatabaseStorage} instance. Since our cleanup routine vacuums, auto_vacuum
michael@0 19 * can be disabled. It is enabled by default, however, turning it off requires an expensive vacuum
michael@0 20 * so we wait until our first {@link cleanup} call since we are vacuuming anyway.
michael@0 21 */
michael@0 22 public class PrunePolicyDatabaseStorage implements PrunePolicyStorage {
michael@0 23 public static final String LOG_TAG = PrunePolicyDatabaseStorage.class.getSimpleName();
michael@0 24
michael@0 25 private final Context context;
michael@0 26 private final String profilePath;
michael@0 27
michael@0 28 private ContentProviderClient client;
michael@0 29 private HealthReportDatabaseStorage storage;
michael@0 30
michael@0 31 private int currentEnvironmentID; // So we don't prune the current environment.
michael@0 32
michael@0 33 public PrunePolicyDatabaseStorage(final Context context, final String profilePath) {
michael@0 34 this.context = context;
michael@0 35 this.profilePath = profilePath;
michael@0 36
michael@0 37 this.currentEnvironmentID = -1;
michael@0 38 }
michael@0 39
michael@0 40 public void pruneEvents(final int count) {
michael@0 41 getStorage().pruneEvents(count);
michael@0 42 }
michael@0 43
michael@0 44 public void pruneEnvironments(final int count) {
michael@0 45 getStorage().pruneEnvironments(count);
michael@0 46
michael@0 47 // Re-populate the DB and environment cache with the current environment in the unlikely event
michael@0 48 // that it was deleted.
michael@0 49 this.currentEnvironmentID = -1;
michael@0 50 getCurrentEnvironmentID();
michael@0 51 }
michael@0 52
michael@0 53 /**
michael@0 54 * Deletes data recorded before the given time. Note that if this method fails to retrieve the
michael@0 55 * current environment from the profile cache, it will not delete data so be sure to prune by
michael@0 56 * other methods (e.g. {@link pruneEvents}) as well.
michael@0 57 */
michael@0 58 public int deleteDataBefore(final long time) {
michael@0 59 return getStorage().deleteDataBefore(time, getCurrentEnvironmentID());
michael@0 60 }
michael@0 61
michael@0 62 public void cleanup() {
michael@0 63 final HealthReportDatabaseStorage storage = getStorage();
michael@0 64 // The change to auto_vacuum will only take affect after a vacuum.
michael@0 65 storage.disableAutoVacuuming();
michael@0 66 storage.vacuum();
michael@0 67 }
michael@0 68
michael@0 69 public int getEventCount() {
michael@0 70 return getStorage().getEventCount();
michael@0 71 }
michael@0 72
michael@0 73 public int getEnvironmentCount() {
michael@0 74 return getStorage().getEnvironmentCount();
michael@0 75 }
michael@0 76
michael@0 77 public void close() {
michael@0 78 if (client != null) {
michael@0 79 client.release();
michael@0 80 client = null;
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 /**
michael@0 85 * Retrieves the {@link HealthReportDatabaseStorage} associated with the profile of the policy.
michael@0 86 * For efficiency, the underlying {@link ContentProviderClient} and
michael@0 87 * {@link HealthReportDatabaseStorage} are cached for later invocations. However, this means a
michael@0 88 * call to this method MUST be accompanied by a call to {@link close}. Throws
michael@0 89 * {@link IllegalStateException} if the storage instance could not be retrieved - note that the
michael@0 90 * {@link ContentProviderClient} instance will not be closed in this case and
michael@0 91 * {@link releaseClient} should still be called.
michael@0 92 */
michael@0 93 protected HealthReportDatabaseStorage getStorage() {
michael@0 94 if (storage != null) {
michael@0 95 return storage;
michael@0 96 }
michael@0 97
michael@0 98 client = EnvironmentBuilder.getContentProviderClient(context);
michael@0 99 if (client == null) {
michael@0 100 // TODO: Record prune failures and submit as part of FHR upload.
michael@0 101 Logger.warn(LOG_TAG, "Unable to get ContentProviderClient - throwing.");
michael@0 102 throw new IllegalStateException("Unable to get ContentProviderClient.");
michael@0 103 }
michael@0 104
michael@0 105 try {
michael@0 106 storage = EnvironmentBuilder.getStorage(client, profilePath);
michael@0 107 if (storage == null) {
michael@0 108 // TODO: Record prune failures and submit as part of FHR upload.
michael@0 109 Logger.warn(LOG_TAG,"Unable to get HealthReportDatabaseStorage for " + profilePath +
michael@0 110 " - throwing.");
michael@0 111 throw new IllegalStateException("Unable to get HealthReportDatabaseStorage for " +
michael@0 112 profilePath + " (== null).");
michael@0 113 }
michael@0 114 } catch (ClassCastException ex) {
michael@0 115 // TODO: Record prune failures and submit as part of FHR upload.
michael@0 116 Logger.warn(LOG_TAG,"Unable to get HealthReportDatabaseStorage for " + profilePath +
michael@0 117 profilePath + " (ClassCastException).");
michael@0 118 throw new IllegalStateException("Unable to get HealthReportDatabaseStorage for " +
michael@0 119 profilePath + ".", ex);
michael@0 120 }
michael@0 121
michael@0 122 return storage;
michael@0 123 }
michael@0 124
michael@0 125 protected int getCurrentEnvironmentID() {
michael@0 126 if (currentEnvironmentID < 0) {
michael@0 127 final ProfileInformationCache cache = new ProfileInformationCache(profilePath);
michael@0 128 if (!cache.restoreUnlessInitialized()) {
michael@0 129 throw new IllegalStateException("Current environment unknown.");
michael@0 130 }
michael@0 131 final Environment env = EnvironmentBuilder.getCurrentEnvironment(cache);
michael@0 132 currentEnvironmentID = env.register();
michael@0 133 }
michael@0 134 return currentEnvironmentID;
michael@0 135 }
michael@0 136 }

mercurial