Bug 1278716 - Remove wakelocks from geo. r?jdm draft
authorMichelangelo De Simone <mdesimone@mozilla.com>
Tue, 04 Oct 2016 15:12:16 -0700
changeset 420880 03601a025adb92c4e1b260ba1312da0ad32688e6
parent 420789 b6ec2f0daaa5a5a1ce3ea7ec07f119f413010a53
child 532930 340180488d8a3673f585a3696b75ae438a5b7817
push id31332
push usermdesimone@mozilla.com
push dateTue, 04 Oct 2016 22:23:41 +0000
reviewersjdm
bugs1278716
milestone52.0a1
Bug 1278716 - Remove wakelocks from geo. r?jdm MozReview-Commit-ID: 2Uu1W34r3g8
dom/geolocation/nsGeolocation.cpp
dom/geolocation/nsGeolocation.h
--- a/dom/geolocation/nsGeolocation.cpp
+++ b/dom/geolocation/nsGeolocation.cpp
@@ -13,30 +13,25 @@
 
 #include "nsGeolocation.h"
 #include "nsDOMClassInfoID.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsContentUtils.h"
 #include "nsContentPermissionHelper.h"
 #include "nsIDocument.h"
-#include "nsIDOMEvent.h"
 #include "nsIObserverService.h"
 #include "nsPIDOMWindow.h"
 #include "nsThreadUtils.h"
-#include "mozilla/HalWakeLock.h"
-#include "mozilla/Hal.h"
 #include "mozilla/Services.h"
 #include "mozilla/Unused.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/ClearOnShutdown.h"
-#include "mozilla/dom/Event.h"
 #include "mozilla/WeakPtr.h"
 #include "mozilla/dom/PermissionMessageUtils.h"
-#include "mozilla/dom/WakeLock.h"
 
 class nsIPrincipal;
 
 #ifdef MOZ_WIDGET_ANDROID
 #include "AndroidLocationProvider.h"
 #endif
 
 #ifdef MOZ_WIDGET_GONK
@@ -58,17 +53,16 @@ class nsIPrincipal;
 
 // Some limit to the number of get or watch geolocation requests
 // that a window can make.
 #define MAX_GEO_REQUESTS_PER_WINDOW  1500
 
 using mozilla::Unused;          // <snicker>
 using namespace mozilla;
 using namespace mozilla::dom;
-using namespace mozilla::hal;
 
 class nsGeolocationRequest final
  : public nsIContentPermissionRequest
  , public nsIGeolocationUpdate
  , public SupportsWeakPtr<nsGeolocationRequest>
 {
  public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@@ -482,20 +476,16 @@ nsGeolocationRequest::Allow(JS::HandleVa
   nsresult rv = gs->StartDevice(GetPrincipal());
 
   if (NS_FAILED(rv)) {
     // Location provider error
     NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
     return NS_OK;
   }
 
-  if (mLocator->ContainsRequest(this)) {
-    return NS_OK;
-  }
-
   if (mIsWatchPositionRequest || !canUseCache) {
     // let the locator know we're pending
     // we will now be owned by the locator
     mLocator->NotifyAllowedRequest(this);
   }
 
   SetTimeoutTimer();
 
@@ -828,19 +818,19 @@ nsGeolocationService::GetCachedPosition(
 
 nsresult
 nsGeolocationService::StartDevice(nsIPrincipal *aPrincipal)
 {
   if (!sGeoEnabled) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
-  // we do not want to keep the geolocation devices online
-  // indefinitely.  Close them down after a reasonable period of
-  // inactivivity
+  // We do not want to keep the geolocation devices online
+  // indefinitely.
+  // Close them down after a reasonable period of inactivivity.
   SetDisconnectTimer();
 
   if (XRE_IsContentProcess()) {
     ContentChild* cpc = ContentChild::GetSingleton();
     cpc->SendAddGeolocationListener(IPC::Principal(aPrincipal),
                                     HighAccuracyRequested());
     return NS_OK;
   }
@@ -867,25 +857,16 @@ nsGeolocationService::StartDevice(nsIPri
   obs->NotifyObservers(mProvider,
                        "geolocation-device-events",
                        u"starting");
 
   return NS_OK;
 }
 
 void
-nsGeolocationService::StopDisconnectTimer()
-{
-  if (mDisconnectTimer) {
-    mDisconnectTimer->Cancel();
-    mDisconnectTimer = nullptr;
-  }
-}
-
-void
 nsGeolocationService::SetDisconnectTimer()
 {
   if (!mDisconnectTimer) {
     mDisconnectTimer = do_CreateInstance("@mozilla.org/timer;1");
   } else {
     mDisconnectTimer->Cancel();
   }
 
@@ -922,17 +903,20 @@ nsGeolocationService::UpdateAccuracy(boo
 
   mProvider->SetHighAccuracy(!mHigherAccuracy && highRequired);
   mHigherAccuracy = highRequired;
 }
 
 void
 nsGeolocationService::StopDevice()
 {
-  StopDisconnectTimer();
+  if (mDisconnectTimer) {
+    mDisconnectTimer->Cancel();
+    mDisconnectTimer = nullptr;
+  }
 
   if (XRE_IsContentProcess()) {
     ContentChild* cpc = ContentChild::GetSingleton();
     cpc->SendRemoveGeolocationListener();
 
     return; // bail early
   }
 
@@ -1032,24 +1016,16 @@ Geolocation::Init(nsPIDOMWindowInner* aC
     // Grab the principal of the document
     nsCOMPtr<nsIDocument> doc = aContentDom->GetDoc();
     if (!doc) {
       return NS_ERROR_FAILURE;
     }
 
     mPrincipal = doc->NodePrincipal();
 
-    if (Preferences::GetBool("dom.wakelock.enabled") &&
-        XRE_IsContentProcess()) {
-      doc->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
-                                  /* listener */ this,
-                                  /* use capture */ true,
-                                  /* wants untrusted */ false);
-    }
-
     nsCOMPtr<nsIURI> uri;
     nsresult rv = mPrincipal->GetURI(getter_AddRefs(uri));
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (uri) {
       bool isHttp;
       rv = uri->SchemeIs("http", &isHttp);
       NS_ENSURE_SUCCESS(rv, rv);
@@ -1073,95 +1049,23 @@ Geolocation::Init(nsPIDOMWindowInner* aC
   mService = nsGeolocationService::GetGeolocationService();
   if (mService) {
     mService->AddLocator(this);
   }
 
   return NS_OK;
 }
 
-bool
-Geolocation::ContainsRequest(nsGeolocationRequest* aRequest)
-{
-  if (aRequest->IsWatch() && mWatchingCallbacks.Contains(aRequest)) {
-	return true;
-  }
-
-  if (mPendingCallbacks.Contains(aRequest)) {
-      return true;
-  }
-
-  return false;
-}
-
-
-NS_IMETHODIMP
-Geolocation::HandleEvent(nsIDOMEvent* aEvent)
-{
-
-  nsAutoString type;
-  aEvent->GetType(type);
-  if (!type.EqualsLiteral("visibilitychange")) {
-    return NS_OK;
-  }
-
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(aEvent->InternalDOMEvent()->GetTarget());
-  MOZ_ASSERT(doc);
-
-  if (doc->Hidden()) {
-    WakeLockInformation info;
-    GetWakeLockInfo(NS_LITERAL_STRING("gps"), &info);
-
-    MOZ_ASSERT(XRE_IsContentProcess());
-    ContentChild* cpc = ContentChild::GetSingleton();
-
-    if (!info.lockingProcesses().Contains(cpc->GetID())) {
-      cpc->SendRemoveGeolocationListener();
-      mService->StopDisconnectTimer();
-    }
-
-    return NS_OK;
-  }
-
-  mService->SetDisconnectTimer();
-
-  // We will unconditionally allow all the requests in the callbacks
-  // because if a request is put into either of these two callbacks,
-  // it means that it has been allowed before.
-  // That's why when we resume them, we unconditionally allow them again.
-  for (uint32_t i = 0, length = mWatchingCallbacks.Length(); i < length; ++i) {
-    mWatchingCallbacks[i]->Allow(JS::UndefinedHandleValue);
-  }
-
-  for (uint32_t i = 0, length = mPendingCallbacks.Length(); i < length; ++i) {
-    mPendingCallbacks[i]->Allow(JS::UndefinedHandleValue);
-  }
-
-  return NS_OK;
-}
-
 void
 Geolocation::Shutdown()
 {
   // Release all callbacks
   mPendingCallbacks.Clear();
   mWatchingCallbacks.Clear();
 
-  if (Preferences::GetBool("dom.wakelock.enabled") &&
-      XRE_IsContentProcess()) {
-    if (nsCOMPtr<nsPIDOMWindowInner> window = do_QueryReferent(mOwner)) {
-      nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
-      if (doc) {
-        doc->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
-                                       this,
-                                       /* aUseCapture = */ true);
-      }
-    }
-  }
-
   if (mService) {
     mService->RemoveLocator(this);
     mService->UpdateAccuracy();
   }
 
   mService = nullptr;
   mPrincipal = nullptr;
 }
--- a/dom/geolocation/nsGeolocation.h
+++ b/dom/geolocation/nsGeolocation.h
@@ -16,17 +16,16 @@
 #include "nsITimer.h"
 #include "nsIObserver.h"
 #include "nsWrapperCache.h"
 
 #include "nsWeakPtr.h"
 #include "nsCycleCollectionParticipant.h"
 
 #include "nsGeoPosition.h"
-#include "nsIDOMEventListener.h"
 #include "nsIDOMGeoGeolocation.h"
 #include "nsIDOMGeoPosition.h"
 #include "nsIDOMGeoPositionError.h"
 #include "nsIDOMGeoPositionCallback.h"
 #include "nsIDOMGeoPositionErrorCallback.h"
 #include "mozilla/dom/GeolocationBinding.h"
 #include "mozilla/dom/PositionErrorBinding.h"
 #include "mozilla/dom/CallbackObject.h"
@@ -82,17 +81,16 @@ public:
   // Find and startup a geolocation device (gps, nmea, etc.)
   nsresult StartDevice(nsIPrincipal* aPrincipal);
 
   // Stop the started geolocation device (gps, nmea, etc.)
   void     StopDevice();
 
   // create, or reinitalize the callback timer
   void     SetDisconnectTimer();
-  void     StopDisconnectTimer();
 
   // Update the accuracy and notify the provider if changed
   void     UpdateAccuracy(bool aForceHigh = false);
   bool     HighAccuracyRequested();
 
 private:
 
   ~nsGeolocationService();
@@ -120,48 +118,42 @@ private:
 namespace mozilla {
 namespace dom {
 
 /**
  * Can return a geolocation info
  */
 class Geolocation final : public nsIDOMGeoGeolocation,
                           public nsIGeolocationUpdate,
-                          public nsWrapperCache,
-                          public nsIDOMEventListener
+                          public nsWrapperCache
 {
 public:
 
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Geolocation, nsIDOMGeoGeolocation)
 
   NS_DECL_NSIGEOLOCATIONUPDATE
   NS_DECL_NSIDOMGEOGEOLOCATION
 
-  NS_DECL_NSIDOMEVENTLISTENER
-
   Geolocation();
 
   nsresult Init(nsPIDOMWindowInner* aContentDom = nullptr);
 
   nsPIDOMWindowInner* GetParentObject() const;
   virtual JSObject* WrapObject(JSContext *aCtx, JS::Handle<JSObject*> aGivenProto) override;
 
   int32_t WatchPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
   void GetCurrentPosition(PositionCallback& aCallback, PositionErrorCallback* aErrorCallback, const PositionOptions& aOptions, ErrorResult& aRv);
 
   // Returns true if any of the callbacks are repeating
   bool HasActiveCallbacks();
 
   // Register an allowed request
   void NotifyAllowedRequest(nsGeolocationRequest* aRequest);
 
-  // Check if callbacks arrays already contain this request
-  bool ContainsRequest(nsGeolocationRequest* aRequest);
-
   // Remove request from all callbacks arrays
   void RemoveRequest(nsGeolocationRequest* request);
 
   // Check if there is already ClearWatch called for current
   // request & clear if yes
   bool ClearPendingRequest(nsGeolocationRequest* aRequest);
 
   // Shutting down.