From cb3959d2324af0da44140bf2c50c146c1d41fc16 Mon Sep 17 00:00:00 2001 From: "bryner%brianryner.com" Date: Wed, 12 Jul 2006 16:54:18 +0000 Subject: [PATCH] Stop collection and clean up when the metrics.upload.enable pref is set to false (bug 344366) r=marria --- extensions/metrics/src/nsMetricsService.cpp | 77 +++++++++++++++++-- extensions/metrics/src/nsMetricsService.h | 9 +++ .../metrics/src/nsUICommandCollector.cpp | 6 +- 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/extensions/metrics/src/nsMetricsService.cpp b/extensions/metrics/src/nsMetricsService.cpp index f9612391979..c2aeef7a6e7 100644 --- a/extensions/metrics/src/nsMetricsService.cpp +++ b/extensions/metrics/src/nsMetricsService.cpp @@ -55,7 +55,7 @@ #include "nsIObserverService.h" #include "nsIUploadChannel.h" #include "nsIPrefService.h" -#include "nsIPrefBranch.h" +#include "nsIPrefBranch2.h" #include "nsIObserver.h" #include "nsILocalFile.h" #include "nsIPropertyBag.h" @@ -117,6 +117,8 @@ PRLogModuleInfo *gMetricsLog; static const char kQuitApplicationTopic[] = "quit-application"; static const char kUploadTimePref[] = "metrics.upload.next-time"; static const char kPingTimePref[] = "metrics.upload.next-ping"; +static const char kEventCountPref[] = "metrics.event-count"; +static const char kEnablePref[] = "metrics.upload.enable"; const PRUint32 nsMetricsService::kMaxRetries = 3; @@ -1029,6 +1031,15 @@ nsMetricsService::Observe(nsISupports *subject, const char *topic, } } } + } else if (strcmp(topic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0) { + // The only pref we care about changing is the .enable pref + if (NS_ConvertUTF8toUTF16(kEnablePref).Equals(nsDependentString(data))) { + if (CollectionEnabled()) { + StartCollection(); + } else { + StopCollection(); + } + } } return NS_OK; @@ -1036,9 +1047,18 @@ nsMetricsService::Observe(nsISupports *subject, const char *topic, nsresult nsMetricsService::ProfileStartup() +{ + nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); + NS_ENSURE_STATE(prefs); + prefs->AddObserver(kEnablePref, this, PR_FALSE); + + return CollectionEnabled() ? StartCollection() : StopCollection(); +} + +nsresult +nsMetricsService::StartCollection() { // Initialize configuration by reading our old config file if one exists. - NS_ENSURE_STATE(mConfig.Init()); nsCOMPtr file; GetConfigFile(getter_AddRefs(file)); @@ -1072,6 +1092,35 @@ nsMetricsService::ProfileStartup() return NS_OK; } +nsresult +nsMetricsService::StopCollection() +{ + // Clear out prefs and files associated with metrics collection + MS_LOG(("Clearing metrics state")); + FlushClearPref(kUploadTimePref); + FlushClearPref(kPingTimePref); + FlushClearPref(kEventCountPref); + + nsCOMPtr configFile; + GetConfigFile(getter_AddRefs(configFile)); + if (configFile) { + configFile->Remove(PR_FALSE); + } + + nsCOMPtr dataFile; + GetDataFile(&dataFile); + if (dataFile) { + dataFile->Remove(PR_FALSE); + } + + // Clear our current config and make sure all collectors are disabled + mConfig.Reset(); + EnableCollectors(); + CreateRoot(); // clear any unflushed events + + return NS_OK; +} + NS_IMETHODIMP nsMetricsService::Notify(nsITimer *timer) { @@ -1139,6 +1188,8 @@ nsMetricsService::Init() mMD5Context = MD5_NewContext(); NS_ENSURE_TRUE(mMD5Context, NS_ERROR_FAILURE); + NS_ENSURE_STATE(mConfig.Init()); + // Create an XML document to serve as the owner document for elements. mDocument = do_CreateInstance("@mozilla.org/xml/xml-document;1"); NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE); @@ -1223,15 +1274,19 @@ nsMetricsService::UploadData() // TODO: Prepare a data stream for upload that is prefixed with a PROFILE // event. + if (!CollectionEnabled()) { + MS_LOG(("Upload disabled")); + return NS_ERROR_ABORT; + } + PRBool enable = PR_FALSE; nsCString spec; nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); if (prefs) { - prefs->GetBoolPref("metrics.upload.enable", &enable); prefs->GetCharPref("metrics.upload.uri", getter_Copies(spec)); } - if (!enable || spec.IsEmpty()) { - MS_LOG(("Upload disabled or URI not set")); + if (spec.IsEmpty()) { + MS_LOG(("Upload URI not set")); return NS_ERROR_ABORT; } @@ -1530,7 +1585,7 @@ nsMetricsService::HashBytes(const PRUint8 *bytes, PRUint32 length, PRBool nsMetricsService::PersistEventCount() { - return NS_SUCCEEDED(FlushIntPref("metrics.event-count", mEventCount)); + return NS_SUCCEEDED(FlushIntPref(kEventCountPref, mEventCount)); } /* static */ PRUint32 @@ -1633,6 +1688,16 @@ nsMetricsService::FlushClearPref(const char *prefName) return NS_OK; } +/* static */ PRBool +nsMetricsService::CollectionEnabled() +{ + nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); + NS_ENSURE_TRUE(prefs, PR_FALSE); + + PRBool enabled = PR_FALSE; + prefs->GetBoolPref(kEnablePref, &enabled); + return enabled; +} /* static */ nsresult nsMetricsUtils::NewPropertyBag(nsIWritablePropertyBag2 **result) diff --git a/extensions/metrics/src/nsMetricsService.h b/extensions/metrics/src/nsMetricsService.h index d75b1ab7d40..965f66463ff 100644 --- a/extensions/metrics/src/nsMetricsService.h +++ b/extensions/metrics/src/nsMetricsService.h @@ -152,6 +152,12 @@ private: // Post-profile-initialization startup code nsresult ProfileStartup(); + // Reads the config, starts a new session, and turns on collectors + nsresult StartCollection(); + + // Stops collectors and removes all metrics-related prefs and files + nsresult StopCollection(); + // Starts and stops collectors based on the current configuration void EnableCollectors(); @@ -236,6 +242,9 @@ private: static nsresult FlushCharPref(const char *prefName, const char *prefValue); static nsresult FlushClearPref(const char *prefName); + // Returns true if the pref to enable collection is set to true + static PRBool CollectionEnabled(); + private: class BadCertListener; diff --git a/extensions/metrics/src/nsUICommandCollector.cpp b/extensions/metrics/src/nsUICommandCollector.cpp index ea738168e7d..7f63b82b995 100644 --- a/extensions/metrics/src/nsUICommandCollector.cpp +++ b/extensions/metrics/src/nsUICommandCollector.cpp @@ -81,7 +81,8 @@ const nsIDOMWindow* key, PRUint32 windowID, void* userArg) nsresult rv = windowTarget->AddEventListener(NS_LITERAL_STRING("command"), listener, PR_TRUE); if (NS_FAILED(rv)) { - MS_LOG(("Warning: Adding event listener failed")); + MS_LOG(("Warning: Adding event listener failed, window %p (id %d)", + key, windowID)); } return PL_DHASH_NEXT; } @@ -107,7 +108,8 @@ const nsIDOMWindow* key, PRUint32 windowID, void* userArg) nsresult rv = windowTarget->RemoveEventListener(NS_LITERAL_STRING("command"), listener, PR_TRUE); if (NS_FAILED(rv)) { - MS_LOG(("Warning: Removing event listener failed")); + MS_LOG(("Warning: Removing event listener failed, window %p (id %d)", + key, windowID)); } return PL_DHASH_NEXT; }