2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-12-28 13:45:16 +04:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2012-01-20 07:25:00 +04:00
|
|
|
#include "mozilla/Hal.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "mozilla/HalWakeLock.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2019-01-08 23:56:58 +03:00
|
|
|
#include "mozilla/ModuleUtils.h"
|
2012-10-02 11:26:32 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2012-09-26 13:12:33 +04:00
|
|
|
#include "nsIObserverService.h"
|
2012-01-20 07:24:00 +04:00
|
|
|
#include "PowerManagerService.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "WakeLock.h"
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
// For _exit().
|
|
|
|
#ifdef XP_WIN
|
|
|
|
# include <process.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-20 07:24:00 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace power {
|
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
using namespace hal;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(PowerManagerService, nsIPowerManagerService)
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-07-26 23:33:45 +04:00
|
|
|
/* static */ StaticRefPtr<PowerManagerService> PowerManagerService::sSingleton;
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
/* static */ already_AddRefed<PowerManagerService>
|
2012-01-20 07:24:00 +04:00
|
|
|
PowerManagerService::GetInstance() {
|
2012-03-07 15:03:25 +04:00
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new PowerManagerService();
|
|
|
|
sSingleton->Init();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PowerManagerService> service = sSingleton.get();
|
2012-03-07 15:03:25 +04:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
void PowerManagerService::Init() { RegisterWakeLockObserver(this); }
|
2012-03-07 15:03:25 +04:00
|
|
|
|
|
|
|
PowerManagerService::~PowerManagerService() {
|
2012-10-02 11:26:32 +04:00
|
|
|
UnregisterWakeLockObserver(this);
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
void PowerManagerService::ComputeWakeLockState(
|
|
|
|
const WakeLockInformation &aWakeLockInfo, nsAString &aState) {
|
|
|
|
WakeLockState state = hal::ComputeWakeLockState(aWakeLockInfo.numLocks(),
|
|
|
|
aWakeLockInfo.numHidden());
|
2012-03-07 15:03:25 +04:00
|
|
|
switch (state) {
|
2012-10-02 11:26:32 +04:00
|
|
|
case WAKE_LOCK_STATE_UNLOCKED:
|
2012-03-07 15:03:25 +04:00
|
|
|
aState.AssignLiteral("unlocked");
|
|
|
|
break;
|
2012-10-02 11:26:32 +04:00
|
|
|
case WAKE_LOCK_STATE_HIDDEN:
|
2012-03-07 15:03:25 +04:00
|
|
|
aState.AssignLiteral("locked-background");
|
|
|
|
break;
|
2012-10-02 11:26:32 +04:00
|
|
|
case WAKE_LOCK_STATE_VISIBLE:
|
2012-03-07 15:03:25 +04:00
|
|
|
aState.AssignLiteral("locked-foreground");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
void PowerManagerService::Notify(const WakeLockInformation &aWakeLockInfo) {
|
2012-03-07 15:03:25 +04:00
|
|
|
nsAutoString state;
|
|
|
|
ComputeWakeLockState(aWakeLockInfo, state);
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
/**
|
|
|
|
* Copy the listeners list before we walk through the callbacks
|
|
|
|
* because the callbacks may install new listeners. We expect no
|
|
|
|
* more than one listener per window, so it shouldn't be too long.
|
|
|
|
*/
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(
|
|
|
|
mWakeLockListeners);
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < listeners.Length(); ++i) {
|
2012-03-07 15:03:25 +04:00
|
|
|
listeners[i]->Callback(aWakeLockInfo.topic(), state);
|
|
|
|
}
|
2012-01-20 07:24:00 +04:00
|
|
|
}
|
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener) {
|
|
|
|
if (mWakeLockListeners.Contains(aListener)) return NS_OK;
|
|
|
|
|
|
|
|
mWakeLockListeners.AppendElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::RemoveWakeLockListener(
|
|
|
|
nsIDOMMozWakeLockListener *aListener) {
|
|
|
|
mWakeLockListeners.RemoveElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::GetWakeLockState(const nsAString &aTopic,
|
|
|
|
nsAString &aState) {
|
2012-10-02 11:26:32 +04:00
|
|
|
WakeLockInformation info;
|
|
|
|
GetWakeLockInfo(aTopic, &info);
|
2012-03-07 15:03:25 +04:00
|
|
|
|
|
|
|
ComputeWakeLockState(info, aState);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:16:07 +04:00
|
|
|
already_AddRefed<WakeLock> PowerManagerService::NewWakeLock(
|
2016-01-30 20:05:36 +03:00
|
|
|
const nsAString &aTopic, nsPIDOMWindowInner *aWindow,
|
2014-01-07 16:16:07 +04:00
|
|
|
mozilla::ErrorResult &aRv) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WakeLock> wakelock = new WakeLock();
|
2014-01-07 16:16:07 +04:00
|
|
|
aRv = wakelock->Init(aTopic, aWindow);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wakelock.forget();
|
|
|
|
}
|
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::NewWakeLock(const nsAString &aTopic,
|
2016-01-30 20:05:36 +03:00
|
|
|
mozIDOMWindow *aWindow,
|
2018-09-28 08:05:07 +03:00
|
|
|
nsIWakeLock **aWakeLock) {
|
2014-01-07 16:16:07 +04:00
|
|
|
mozilla::ErrorResult rv;
|
2016-01-30 20:05:36 +03:00
|
|
|
RefPtr<WakeLock> wakelock =
|
|
|
|
NewWakeLock(aTopic, nsPIDOMWindowInner::From(aWindow), rv);
|
2014-01-07 16:16:07 +04:00
|
|
|
if (rv.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-01-07 16:16:07 +04:00
|
|
|
}
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2018-09-28 08:05:07 +03:00
|
|
|
wakelock.forget(aWakeLock);
|
2012-03-07 15:03:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-15 00:41:30 +04:00
|
|
|
already_AddRefed<WakeLock> PowerManagerService::NewWakeLockOnBehalfOfProcess(
|
|
|
|
const nsAString &aTopic, ContentParent *aContentParent) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WakeLock> wakelock = new WakeLock();
|
2013-02-15 00:41:30 +04:00
|
|
|
nsresult rv = wakelock->Init(aTopic, aContentParent);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return wakelock.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace power
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2019-01-08 23:56:58 +03:00
|
|
|
|
|
|
|
NS_DEFINE_NAMED_CID(NS_POWERMANAGERSERVICE_CID);
|
|
|
|
|
|
|
|
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(
|
|
|
|
nsIPowerManagerService,
|
|
|
|
mozilla::dom::power::PowerManagerService::GetInstance)
|
|
|
|
|
|
|
|
static const mozilla::Module::CIDEntry kPowerManagerCIDs[] = {
|
|
|
|
// clang-format off
|
2019-01-17 02:05:11 +03:00
|
|
|
{ &kNS_POWERMANAGERSERVICE_CID, false, nullptr, nsIPowerManagerServiceConstructor, mozilla::Module::ALLOW_IN_GPU_AND_SOCKET_PROCESS },
|
2019-01-08 23:56:58 +03:00
|
|
|
{ nullptr }
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module::ContractIDEntry kPowerManagerContracts[] = {
|
|
|
|
// clang-format off
|
2019-01-17 02:05:11 +03:00
|
|
|
{ POWERMANAGERSERVICE_CONTRACTID, &kNS_POWERMANAGERSERVICE_CID, mozilla::Module::ALLOW_IN_GPU_AND_SOCKET_PROCESS },
|
2019-01-08 23:56:58 +03:00
|
|
|
{ nullptr }
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
|
|
|
|
// We mark the power module as being available in the GPU process because the
|
|
|
|
// appshell depends on the power manager service.
|
|
|
|
static const mozilla::Module kPowerManagerModule = {
|
|
|
|
mozilla::Module::kVersion,
|
|
|
|
kPowerManagerCIDs,
|
|
|
|
kPowerManagerContracts,
|
|
|
|
nullptr,
|
2019-01-16 11:50:07 +03:00
|
|
|
nullptr,
|
2019-01-08 23:56:58 +03:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2019-01-17 02:05:11 +03:00
|
|
|
mozilla::Module::ALLOW_IN_GPU_AND_SOCKET_PROCESS};
|
2019-01-08 23:56:58 +03:00
|
|
|
|
|
|
|
NSMODULE_DEFN(nsPowerManagerModule) = &kPowerManagerModule;
|