1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/health/BrowserHealthReporter.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,156 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.health; 1.10 + 1.11 +import android.content.ContentProviderClient; 1.12 +import android.content.Context; 1.13 +import android.util.Log; 1.14 + 1.15 +import org.mozilla.gecko.GeckoAppShell; 1.16 +import org.mozilla.gecko.GeckoEvent; 1.17 +import org.mozilla.gecko.GeckoProfile; 1.18 + 1.19 +import org.mozilla.gecko.background.healthreport.EnvironmentBuilder; 1.20 +import org.mozilla.gecko.background.common.GlobalConstants; 1.21 +import org.mozilla.gecko.background.healthreport.HealthReportConstants; 1.22 +import org.mozilla.gecko.background.healthreport.HealthReportDatabaseStorage; 1.23 +import org.mozilla.gecko.background.healthreport.HealthReportGenerator; 1.24 + 1.25 +import org.mozilla.gecko.util.GeckoEventListener; 1.26 +import org.mozilla.gecko.util.ThreadUtils; 1.27 + 1.28 +import org.json.JSONException; 1.29 +import org.json.JSONObject; 1.30 + 1.31 +/** 1.32 + * BrowserHealthReporter is the browser's interface to the Firefox Health 1.33 + * Report report generator. 1.34 + * 1.35 + * Each instance registers Gecko event listeners, so keep a single instance 1.36 + * around for the life of the browser. Java callers should use this globally 1.37 + * available singleton. 1.38 + */ 1.39 +public class BrowserHealthReporter implements GeckoEventListener { 1.40 + private static final String LOGTAG = "GeckoHealthRep"; 1.41 + 1.42 + public static final String EVENT_REQUEST = "HealthReport:Request"; 1.43 + public static final String EVENT_RESPONSE = "HealthReport:Response"; 1.44 + 1.45 + protected final Context context; 1.46 + 1.47 + public BrowserHealthReporter() { 1.48 + GeckoAppShell.registerEventListener(EVENT_REQUEST, this); 1.49 + 1.50 + context = GeckoAppShell.getContext(); 1.51 + if (context == null) { 1.52 + throw new IllegalStateException("Null Gecko context"); 1.53 + } 1.54 + } 1.55 + 1.56 + public void uninit() { 1.57 + GeckoAppShell.unregisterEventListener(EVENT_REQUEST, this); 1.58 + } 1.59 + 1.60 + /** 1.61 + * Generate a new Health Report. 1.62 + * 1.63 + * This method performs IO, so call it from a background thread. 1.64 + * 1.65 + * @param since timestamp of first day to report (milliseconds since epoch). 1.66 + * @param lastPingTime timestamp when last health report was uploaded 1.67 + * (milliseconds since epoch). 1.68 + * @param profilePath path of the profile to generate report for. 1.69 + * @throws JSONException if JSON generation fails. 1.70 + * @throws IllegalStateException if the environment does not allow to generate a report. 1.71 + * @return non-null report. 1.72 + */ 1.73 + public JSONObject generateReport(long since, long lastPingTime, String profilePath) throws JSONException { 1.74 + // We abuse the life-cycle of an Android ContentProvider slightly by holding 1.75 + // onto a ContentProviderClient while we generate a payload. This keeps 1.76 + // our database storage alive, while also allowing us to share a database 1.77 + // connection with BrowserHealthRecorder and the uploader. 1.78 + // The ContentProvider owns all underlying Storage instances, so we don't 1.79 + // need to explicitly close them. 1.80 + ContentProviderClient client = EnvironmentBuilder.getContentProviderClient(context); 1.81 + if (client == null) { 1.82 + throw new IllegalStateException("Could not fetch Health Report content provider."); 1.83 + } 1.84 + 1.85 + try { 1.86 + // Storage instance is owned by HealthReportProvider, so we don't need 1.87 + // to close it. 1.88 + HealthReportDatabaseStorage storage = EnvironmentBuilder.getStorage(client, profilePath); 1.89 + if (storage == null) { 1.90 + throw new IllegalStateException("No storage in Health Reporter."); 1.91 + } 1.92 + 1.93 + HealthReportGenerator generator = new HealthReportGenerator(storage); 1.94 + JSONObject report = generator.generateDocument(since, lastPingTime, profilePath); 1.95 + if (report == null) { 1.96 + throw new IllegalStateException("Not enough profile information to generate report."); 1.97 + } 1.98 + return report; 1.99 + } finally { 1.100 + client.release(); 1.101 + } 1.102 + } 1.103 + 1.104 + /** 1.105 + * Get last time a health report was successfully uploaded. 1.106 + * 1.107 + * This is read from shared preferences, so call it from a background 1.108 + * thread. Bug 882182 tracks making this work with multiple profiles. 1.109 + * 1.110 + * @return milliseconds since the epoch, or 0 if never uploaded. 1.111 + */ 1.112 + protected long getLastUploadLocalTime() { 1.113 + return context 1.114 + .getSharedPreferences(HealthReportConstants.PREFS_BRANCH, 0) 1.115 + .getLong(HealthReportConstants.PREF_LAST_UPLOAD_LOCAL_TIME, 0L); 1.116 + } 1.117 + 1.118 + /** 1.119 + * Generate a new Health Report for the current Gecko profile. 1.120 + * 1.121 + * This method performs IO, so call it from a background thread. 1.122 + * 1.123 + * @throws JSONException if JSON generation fails. 1.124 + * @throws IllegalStateException if the environment does not allow to generate a report. 1.125 + * @return non-null Health Report. 1.126 + */ 1.127 + public JSONObject generateReport() throws JSONException { 1.128 + GeckoProfile profile = GeckoAppShell.getGeckoInterface().getProfile(); 1.129 + String profilePath = profile.getDir().getAbsolutePath(); 1.130 + 1.131 + long since = System.currentTimeMillis() - GlobalConstants.MILLISECONDS_PER_SIX_MONTHS; 1.132 + long lastPingTime = Math.max(getLastUploadLocalTime(), HealthReportConstants.EARLIEST_LAST_PING); 1.133 + 1.134 + return generateReport(since, lastPingTime, profilePath); 1.135 + } 1.136 + 1.137 + @Override 1.138 + public void handleMessage(String event, JSONObject message) { 1.139 + try { 1.140 + ThreadUtils.postToBackgroundThread(new Runnable() { 1.141 + @Override 1.142 + public void run() { 1.143 + JSONObject report = null; 1.144 + try { 1.145 + report = generateReport(); // non-null if it returns. 1.146 + } catch (Exception e) { 1.147 + Log.e(LOGTAG, "Generating report failed; responding with empty report.", e); 1.148 + report = new JSONObject(); 1.149 + } 1.150 + 1.151 + GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(EVENT_RESPONSE, report.toString())); 1.152 + } 1.153 + }); 1.154 + } catch (Exception e) { 1.155 + Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e); 1.156 + } 1.157 + } 1.158 +} 1.159 +