mobile/android/base/background/healthreport/EnvironmentBuilder.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/background/healthreport/EnvironmentBuilder.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,169 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.background.healthreport;
     1.9 +
    1.10 +import java.util.Iterator;
    1.11 +
    1.12 +import org.json.JSONObject;
    1.13 +import org.mozilla.gecko.AppConstants;
    1.14 +import org.mozilla.gecko.SysInfo;
    1.15 +import org.mozilla.gecko.background.common.GlobalConstants;
    1.16 +import org.mozilla.gecko.background.common.log.Logger;
    1.17 +
    1.18 +import android.content.ContentProvider;
    1.19 +import android.content.ContentProviderClient;
    1.20 +import android.content.ContentResolver;
    1.21 +import android.content.Context;
    1.22 +
    1.23 +/**
    1.24 + * Construct a HealthReport environment from the current running system.
    1.25 + */
    1.26 +public class EnvironmentBuilder {
    1.27 +  private static final String LOG_TAG = "GeckoEnvBuilder";
    1.28 +
    1.29 +  public static ContentProviderClient getContentProviderClient(Context context) {
    1.30 +    ContentResolver cr = context.getContentResolver();
    1.31 +    return cr.acquireContentProviderClient(HealthReportConstants.HEALTH_AUTHORITY);
    1.32 +  }
    1.33 +
    1.34 +  /**
    1.35 +   * Fetch the storage object associated with the provided
    1.36 +   * {@link ContentProviderClient}. If no storage instance can be found --
    1.37 +   * perhaps because the {@link ContentProvider} is running in a different
    1.38 +   * process -- returns <code>null</code>. On success, the returned
    1.39 +   * {@link HealthReportDatabaseStorage} instance is owned by the underlying
    1.40 +   * {@link HealthReportProvider} and thus does not need to be closed by the
    1.41 +   * caller.
    1.42 +   *
    1.43 +   * If the provider is not a {@link HealthReportProvider}, throws a
    1.44 +   * {@link ClassCastException}, because that would be disastrous.
    1.45 +   */
    1.46 +  public static HealthReportDatabaseStorage getStorage(ContentProviderClient cpc,
    1.47 +                                                       String profilePath) {
    1.48 +    ContentProvider pr = cpc.getLocalContentProvider();
    1.49 +    if (pr == null) {
    1.50 +      Logger.error(LOG_TAG, "Unable to retrieve local content provider. Running in a different process?");
    1.51 +      return null;
    1.52 +    }
    1.53 +    try {
    1.54 +      return ((HealthReportProvider) pr).getProfileStorage(profilePath);
    1.55 +    } catch (ClassCastException ex) {
    1.56 +      Logger.error(LOG_TAG, "ContentProvider not a HealthReportProvider!", ex);
    1.57 +      throw ex;
    1.58 +    }
    1.59 +  }
    1.60 +
    1.61 +  public static interface ProfileInformationProvider {
    1.62 +    public boolean isBlocklistEnabled();
    1.63 +    public boolean isTelemetryEnabled();
    1.64 +    public boolean isAcceptLangUserSet();
    1.65 +    public long getProfileCreationTime();
    1.66 +
    1.67 +    public String getDistributionString();
    1.68 +    public String getOSLocale();
    1.69 +    public String getAppLocale();
    1.70 +
    1.71 +    public JSONObject getAddonsJSON();
    1.72 +  }
    1.73 +
    1.74 +  protected static void populateEnvironment(Environment e,
    1.75 +                                            ProfileInformationProvider info) {
    1.76 +    e.cpuCount = SysInfo.getCPUCount();
    1.77 +    e.memoryMB = SysInfo.getMemSize();
    1.78 +
    1.79 +    e.appName = AppConstants.MOZ_APP_NAME;
    1.80 +    e.appID = AppConstants.MOZ_APP_ID;
    1.81 +    e.appVersion = AppConstants.MOZ_APP_VERSION;
    1.82 +    e.appBuildID = AppConstants.MOZ_APP_BUILDID;
    1.83 +    e.updateChannel = AppConstants.MOZ_UPDATE_CHANNEL;
    1.84 +    e.vendor = AppConstants.MOZ_APP_VENDOR;
    1.85 +    e.platformVersion = AppConstants.MOZILLA_VERSION;
    1.86 +    e.platformBuildID = AppConstants.MOZ_APP_BUILDID;
    1.87 +    e.xpcomabi = AppConstants.TARGET_XPCOM_ABI;
    1.88 +    e.os = "Android";
    1.89 +    e.architecture = SysInfo.getArchABI();       // Not just "arm".
    1.90 +    e.sysName = SysInfo.getName();
    1.91 +    e.sysVersion = SysInfo.getReleaseVersion();
    1.92 +
    1.93 +    e.profileCreation = (int) (info.getProfileCreationTime() / GlobalConstants.MILLISECONDS_PER_DAY);
    1.94 +
    1.95 +    // Corresponds to Gecko pref "extensions.blocklist.enabled".
    1.96 +    e.isBlocklistEnabled = (info.isBlocklistEnabled() ? 1 : 0);
    1.97 +
    1.98 +    // Corresponds to Gecko pref "toolkit.telemetry.enabled".
    1.99 +    e.isTelemetryEnabled = (info.isTelemetryEnabled() ? 1 : 0);
   1.100 +
   1.101 +    e.extensionCount = 0;
   1.102 +    e.pluginCount = 0;
   1.103 +    e.themeCount = 0;
   1.104 +
   1.105 +    JSONObject addons = info.getAddonsJSON();
   1.106 +    if (addons == null) {
   1.107 +      return;
   1.108 +    }
   1.109 +
   1.110 +    @SuppressWarnings("unchecked")
   1.111 +    Iterator<String> it = addons.keys();
   1.112 +    while (it.hasNext()) {
   1.113 +      String key = it.next();
   1.114 +      try {
   1.115 +        JSONObject addon = addons.getJSONObject(key);
   1.116 +        String type = addon.optString("type");
   1.117 +        Logger.pii(LOG_TAG, "Add-on " + key + " is a " + type);
   1.118 +        if ("extension".equals(type)) {
   1.119 +          ++e.extensionCount;
   1.120 +        } else if ("plugin".equals(type)) {
   1.121 +          ++e.pluginCount;
   1.122 +        } else if ("theme".equals(type)) {
   1.123 +          ++e.themeCount;
   1.124 +        } else if ("service".equals(type)) {
   1.125 +          // Later.
   1.126 +        } else {
   1.127 +          Logger.debug(LOG_TAG, "Unknown add-on type: " + type);
   1.128 +        }
   1.129 +      } catch (Exception ex) {
   1.130 +        Logger.warn(LOG_TAG, "Failed to process add-on " + key, ex);
   1.131 +      }
   1.132 +    }
   1.133 +
   1.134 +    e.addons = addons;
   1.135 +
   1.136 +    // v2 environment fields.
   1.137 +    e.distribution = info.getDistributionString();
   1.138 +    e.osLocale = info.getOSLocale();
   1.139 +    e.appLocale = info.getAppLocale();
   1.140 +    e.acceptLangSet = info.isAcceptLangUserSet() ? 1 : 0;
   1.141 +  }
   1.142 +
   1.143 +  /**
   1.144 +   * Returns an {@link Environment} not linked to a storage instance, but
   1.145 +   * populated with current field values.
   1.146 +   *
   1.147 +   * @param info a source of profile data
   1.148 +   * @return the new {@link Environment}
   1.149 +   */
   1.150 +  public static Environment getCurrentEnvironment(ProfileInformationProvider info) {
   1.151 +    Environment e = new Environment() {
   1.152 +      @Override
   1.153 +      public int register() {
   1.154 +        return 0;
   1.155 +      }
   1.156 +    };
   1.157 +    populateEnvironment(e, info);
   1.158 +    return e;
   1.159 +  }
   1.160 +
   1.161 +  /**
   1.162 +   * @return the current environment's ID in the provided storage layer
   1.163 +   */
   1.164 +  public static int registerCurrentEnvironment(final HealthReportStorage storage,
   1.165 +                                               final ProfileInformationProvider info) {
   1.166 +    Environment e = storage.getEnvironment();
   1.167 +    populateEnvironment(e, info);
   1.168 +    e.register();
   1.169 +    Logger.debug(LOG_TAG, "Registering current environment: " + e.getHash() + " = " + e.id);
   1.170 +    return e.id;
   1.171 +  }
   1.172 +}

mercurial