Bug 822135 - Disable Profiler when there's a Private Browsing Window. r=jdm
authorBenoit Girard <b56girard@gmail.com>
Mon, 17 Dec 2012 18:25:50 -0500
changeset 118600 f5ec1efd39ee839a506fd3d76636f73feb231495
parent 118599 2bfc7fb79f781cfb2b59b6ed451c49372b0896af
child 118601 14d1d368bcd289fa23462772e3c79a4485306ead
push id24170
push userryanvm@gmail.com
push dateSat, 12 Jan 2013 20:26:58 +0000
treeherdermozilla-central@d8599591d07c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs822135
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 822135 - Disable Profiler when there's a Private Browsing Window. r=jdm
services/sync/SyncComponents.manifest
tools/profiler/TableTicker.cpp
tools/profiler/nsProfiler.cpp
tools/profiler/nsProfiler.h
tools/profiler/nsProfilerFactory.cpp
tools/profiler/sps_sampler.h
--- a/tools/profiler/TableTicker.cpp
+++ b/tools/profiler/TableTicker.cpp
@@ -1235,8 +1235,23 @@ void mozilla_sampler_print_location()
   ThreadProfile threadProfile(1000, stack);
   doSampleStackTrace(stack, threadProfile, NULL);
 
   threadProfile.flush();
 
   printf_stderr("Backtrace:\n");
   threadProfile.IterateTags(print_callback);
 }
+
+void mozilla_sampler_lock()
+{
+  mozilla_sampler_stop();
+  nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
+  if (os)
+    os->NotifyObservers(nullptr, "profiler-locked", nullptr);
+}
+
+void mozilla_sampler_unlock()
+{
+  nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
+  if (os)
+    os->NotifyObservers(nullptr, "profiler-unlocked", nullptr);
+}
--- a/tools/profiler/nsProfiler.cpp
+++ b/tools/profiler/nsProfiler.cpp
@@ -6,34 +6,82 @@
 #include <string>
 #include <sstream>
 #ifdef MOZ_INSTRUMENT_EVENT_LOOP
 #include "EventTracer.h"
 #endif
 #include "sampler.h"
 #include "nsProfiler.h"
 #include "nsMemory.h"
+#include "nsString.h"
+#include "mozilla/Services.h"
+#include "nsIObserverService.h"
+#include "nsIInterfaceRequestor.h"
+#include "nsILoadContext.h"
+#include "nsIWebNavigation.h"
+#include "nsIInterfaceRequestorUtils.h"
 #include "shared-libraries.h"
-#include "nsString.h"
 #include "jsapi.h"
 
 using std::string;
 
 NS_IMPL_ISUPPORTS1(nsProfiler, nsIProfiler)
 
-
 nsProfiler::nsProfiler()
+  : mLockedForPrivateBrowsing(false)
 {
 }
 
+nsProfiler::~nsProfiler()
+{
+  nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+  if (observerService) {
+    observerService->RemoveObserver(this, "chrome-document-global-created");
+    observerService->RemoveObserver(this, "last-pb-context-exited");
+  }
+}
+
+nsresult
+nsProfiler::Init() {
+  nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+  if (observerService) {
+    observerService->AddObserver(this, "chrome-document-global-created", false);
+    observerService->AddObserver(this, "last-pb-context-exited", false);
+  }
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsProfiler::Observe(nsISupports *aSubject,
+                    const char *aTopic,
+                    const PRUnichar *aData)
+{
+  if (strcmp(aTopic, "chrome-document-global-created") == 0) {
+    nsCOMPtr<nsIInterfaceRequestor> requestor = do_QueryInterface(aSubject);
+    nsCOMPtr<nsIWebNavigation> parentWebNav = do_GetInterface(requestor);
+    nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(parentWebNav);
+    if (loadContext && loadContext->UsePrivateBrowsing() && !mLockedForPrivateBrowsing) {
+      mLockedForPrivateBrowsing = true;
+      mozilla_sampler_lock();
+    }
+  } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
+    mLockedForPrivateBrowsing = false;
+    mozilla_sampler_unlock();
+  }
+  return NS_OK;
+}
 
 NS_IMETHODIMP
 nsProfiler::StartProfiler(uint32_t aEntries, uint32_t aInterval,
                           const char** aFeatures, uint32_t aFeatureCount)
 {
+  if (mLockedForPrivateBrowsing) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+
   SAMPLER_START(aEntries, aInterval, aFeatures, aFeatureCount);
 #ifdef MOZ_INSTRUMENT_EVENT_LOOP
   mozilla::InitEventTracing();
 #endif
   return NS_OK;
 }
 
 NS_IMETHODIMP
@@ -121,18 +169,16 @@ GetSharedLibraryInfoString()
 
 NS_IMETHODIMP
 nsProfiler::GetSharedLibraryInformation(nsAString& aOutString)
 {
   aOutString.Assign(NS_ConvertUTF8toUTF16(GetSharedLibraryInfoString().c_str()));
   return NS_OK;
 }
 
-
-
 NS_IMETHODIMP nsProfiler::GetProfileData(JSContext* aCx, jsval* aResult)
 {
   JSObject *obj = SAMPLER_GET_PROFILE_DATA(aCx);
   if (!obj)
     return NS_ERROR_FAILURE;
 
   *aResult = OBJECT_TO_JSVAL(obj);
   return NS_OK;
--- a/tools/profiler/nsProfiler.h
+++ b/tools/profiler/nsProfiler.h
@@ -2,22 +2,28 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef _NSPROFILER_H_
 #define _NSPROFILER_H_
 
 #include "nsIProfiler.h"
+#include "nsIObserver.h"
 #include "mozilla/Attributes.h"
 
-class nsProfiler MOZ_FINAL : public nsIProfiler
+class nsProfiler MOZ_FINAL : public nsIProfiler, public nsIObserver
 {
 public:
     nsProfiler();
+    ~nsProfiler();
 
     NS_DECL_ISUPPORTS
+    NS_DECL_NSIOBSERVER
+    NS_DECL_NSIPROFILER
 
-    NS_DECL_NSIPROFILER
+    nsresult Init();
+private:
+    bool mLockedForPrivateBrowsing;
 };
 
 #endif /* _NSPROFILER_H_ */
 
--- a/tools/profiler/nsProfilerFactory.cpp
+++ b/tools/profiler/nsProfilerFactory.cpp
@@ -3,17 +3,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/ModuleUtils.h"
 #include "nsCOMPtr.h"
 #include "nsProfiler.h"
 #include "nsProfilerCIID.h"
 
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsProfiler)
+NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsProfiler, Init)
 
 NS_DEFINE_NAMED_CID(NS_PROFILER_CID);
 
 static const mozilla::Module::CIDEntry kProfilerCIDs[] = {
     { &kNS_PROFILER_CID, false, NULL, nsProfilerConstructor },
     { NULL }
 };
 
--- a/tools/profiler/sps_sampler.h
+++ b/tools/profiler/sps_sampler.h
@@ -173,18 +173,23 @@ void mozilla_sampler_responsiveness(Time
 void mozilla_sampler_frame_number(int frameNumber);
 const double* mozilla_sampler_get_responsiveness();
 void mozilla_sampler_save();
 char* mozilla_sampler_get_profile();
 JSObject *mozilla_sampler_get_profile_data(JSContext *aCx);
 const char** mozilla_sampler_get_features();
 void mozilla_sampler_init();
 void mozilla_sampler_shutdown();
-
 void mozilla_sampler_print_location();
+// Lock the profiler. When locked the profiler is (1) stopped,
+// (2) profile data is cleared, (3) profiler-locked is fired.
+// This is used to lock down the profiler during private browsing
+void mozilla_sampler_lock();
+// Unlock the profiler, leaving it stopped and fires profiler-unlocked.
+void mozilla_sampler_unlock();
 
 namespace mozilla {
 
 class NS_STACK_CLASS SamplerStackFrameRAII {
 public:
   // we only copy the strings at save time, so to take multiple parameters we'd need to copy them then.
   SamplerStackFrameRAII(const char *aInfo, uint32_t line) {
     mHandle = mozilla_sampler_call_enter(aInfo, this, false, line);