2012-01-19 19:24:00 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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-19 19:24:00 -08:00
|
|
|
|
2012-12-28 01:45:16 -08:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2012-01-19 19:25:00 -08:00
|
|
|
#include "mozilla/Hal.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "mozilla/HalWakeLock.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-10-02 00:26:32 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-09-26 02:12:33 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2012-09-26 02:12:33 -07:00
|
|
|
#include "nsIObserverService.h"
|
2012-01-19 19:24:00 -08:00
|
|
|
#include "PowerManagerService.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "WakeLock.h"
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
// For _exit().
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-09-04 05:14:54 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
extern "C" char* PrintJSStack();
|
|
|
|
static void LogFunctionAndJSStack(const char* funcname) {
|
|
|
|
char *jsstack = PrintJSStack();
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "PowerManagerService", \
|
|
|
|
"Call to %s. The JS stack is:\n%s\n",
|
|
|
|
funcname,
|
|
|
|
jsstack ? jsstack : "<no JS stack>");
|
|
|
|
}
|
|
|
|
// bug 839452
|
|
|
|
#define LOG_FUNCTION_AND_JS_STACK() \
|
|
|
|
LogFunctionAndJSStack(__PRETTY_FUNCTION__);
|
|
|
|
#else
|
|
|
|
#define LOG_FUNCTION_AND_JS_STACK()
|
|
|
|
#endif
|
|
|
|
|
2012-01-19 19:24:00 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace power {
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(PowerManagerService, nsIPowerManagerService)
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2012-07-26 12:33:45 -07:00
|
|
|
/* static */ StaticRefPtr<PowerManagerService> PowerManagerService::sSingleton;
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
/* static */ already_AddRefed<PowerManagerService>
|
2012-01-19 19:24:00 -08:00
|
|
|
PowerManagerService::GetInstance()
|
|
|
|
{
|
2012-03-07 03:03:25 -08:00
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new PowerManagerService();
|
|
|
|
sSingleton->Init();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
nsRefPtr<PowerManagerService> service = sSingleton.get();
|
2012-03-07 03:03:25 -08:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PowerManagerService::Init()
|
|
|
|
{
|
2012-10-02 00:26:32 -07:00
|
|
|
RegisterWakeLockObserver(this);
|
|
|
|
|
|
|
|
// NB: default to *enabling* the watchdog even when the pref is
|
|
|
|
// absent, in case the profile might be damaged and we need to
|
|
|
|
// restart to repair it.
|
|
|
|
mWatchdogTimeoutSecs =
|
2014-04-24 09:48:00 -07:00
|
|
|
Preferences::GetInt("shutdown.watchdog.timeoutSecs", 10);
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
PowerManagerService::~PowerManagerService()
|
|
|
|
{
|
2012-10-02 00:26:32 -07:00
|
|
|
UnregisterWakeLockObserver(this);
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-02 00:26:32 -07:00
|
|
|
PowerManagerService::ComputeWakeLockState(const WakeLockInformation& aWakeLockInfo,
|
2012-03-07 03:03:25 -08:00
|
|
|
nsAString &aState)
|
|
|
|
{
|
2012-10-02 00:26:32 -07:00
|
|
|
WakeLockState state = hal::ComputeWakeLockState(aWakeLockInfo.numLocks(),
|
|
|
|
aWakeLockInfo.numHidden());
|
2012-03-07 03:03:25 -08:00
|
|
|
switch (state) {
|
2012-10-02 00:26:32 -07:00
|
|
|
case WAKE_LOCK_STATE_UNLOCKED:
|
2012-03-07 03:03:25 -08:00
|
|
|
aState.AssignLiteral("unlocked");
|
|
|
|
break;
|
2012-10-02 00:26:32 -07:00
|
|
|
case WAKE_LOCK_STATE_HIDDEN:
|
2012-03-07 03:03:25 -08:00
|
|
|
aState.AssignLiteral("locked-background");
|
|
|
|
break;
|
2012-10-02 00:26:32 -07:00
|
|
|
case WAKE_LOCK_STATE_VISIBLE:
|
2012-03-07 03:03:25 -08:00
|
|
|
aState.AssignLiteral("locked-foreground");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2012-03-07 03:03:25 -08:00
|
|
|
void
|
2012-10-02 00:26:32 -07:00
|
|
|
PowerManagerService::Notify(const WakeLockInformation& aWakeLockInfo)
|
2012-03-07 03:03:25 -08:00
|
|
|
{
|
|
|
|
nsAutoString state;
|
|
|
|
ComputeWakeLockState(aWakeLockInfo, state);
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2012-03-07 03:03:25 -08: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.
|
|
|
|
*/
|
|
|
|
nsAutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mWakeLockListeners);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < listeners.Length(); ++i) {
|
2012-03-07 03:03:25 -08:00
|
|
|
listeners[i]->Callback(aWakeLockInfo.topic(), state);
|
|
|
|
}
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
2012-09-26 02:12:33 -07:00
|
|
|
void
|
|
|
|
PowerManagerService::SyncProfile()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obsServ = services::GetObserverService();
|
|
|
|
if (obsServ) {
|
|
|
|
NS_NAMED_LITERAL_STRING(context, "shutdown-persist");
|
|
|
|
obsServ->NotifyObservers(nullptr, "profile-change-net-teardown", context.get());
|
|
|
|
obsServ->NotifyObservers(nullptr, "profile-change-teardown", context.get());
|
|
|
|
obsServ->NotifyObservers(nullptr, "profile-before-change", context.get());
|
2013-03-01 12:11:21 -08:00
|
|
|
obsServ->NotifyObservers(nullptr, "profile-before-change2", context.get());
|
2012-09-26 02:12:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 19:24:00 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::Reboot()
|
|
|
|
{
|
2013-09-04 05:14:54 -07:00
|
|
|
LOG_FUNCTION_AND_JS_STACK() // bug 839452
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_Reboot, mWatchdogTimeoutSecs);
|
2012-09-26 02:12:33 -07:00
|
|
|
// To synchronize any unsaved user data before rebooting.
|
|
|
|
SyncProfile();
|
2012-01-19 19:25:00 -08:00
|
|
|
hal::Reboot();
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("hal::Reboot() shouldn't return");
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::PowerOff()
|
|
|
|
{
|
2013-09-04 05:14:54 -07:00
|
|
|
LOG_FUNCTION_AND_JS_STACK() // bug 839452
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_PowerOff, mWatchdogTimeoutSecs);
|
2012-09-26 02:12:33 -07:00
|
|
|
// To synchronize any unsaved user data before powering off.
|
|
|
|
SyncProfile();
|
2012-01-19 19:25:00 -08:00
|
|
|
hal::PowerOff();
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("hal::PowerOff() shouldn't return");
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::Restart()
|
|
|
|
{
|
2013-09-04 05:14:54 -07:00
|
|
|
LOG_FUNCTION_AND_JS_STACK() // bug 839452
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
// FIXME/bug 796826 this implementation is currently gonk-specific,
|
|
|
|
// because it relies on the Gonk to initialize the Gecko processes to
|
|
|
|
// restart B2G. It's better to do it here to have a real "restart".
|
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_Restart, mWatchdogTimeoutSecs);
|
2012-12-28 01:45:16 -08:00
|
|
|
// Ensure all content processes are dead before we continue
|
|
|
|
// restarting. This code is used to restart to apply updates, and
|
|
|
|
// if we don't join all the subprocesses, race conditions can cause
|
|
|
|
// them to see an inconsistent view of the application directory.
|
|
|
|
ContentParent::JoinAllSubprocesses();
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
// To synchronize any unsaved user data before restarting.
|
|
|
|
SyncProfile();
|
2012-12-12 15:41:07 -08:00
|
|
|
#ifdef XP_UNIX
|
|
|
|
sync();
|
|
|
|
#endif
|
2012-10-02 00:26:32 -07:00
|
|
|
_exit(0);
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("_exit() shouldn't return");
|
2012-10-02 00:26:32 -07:00
|
|
|
}
|
|
|
|
|
2012-03-07 03:03:25 -08: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 00:26:32 -07:00
|
|
|
WakeLockInformation info;
|
|
|
|
GetWakeLockInfo(aTopic, &info);
|
2012-03-07 03:03:25 -08:00
|
|
|
|
|
|
|
ComputeWakeLockState(info, aState);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-07 04:16:07 -08:00
|
|
|
already_AddRefed<WakeLock>
|
|
|
|
PowerManagerService::NewWakeLock(const nsAString& aTopic,
|
|
|
|
nsIDOMWindow* aWindow,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<WakeLock> wakelock = new WakeLock();
|
|
|
|
aRv = wakelock->Init(aTopic, aWindow);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wakelock.forget();
|
|
|
|
}
|
|
|
|
|
2012-03-07 03:03:25 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::NewWakeLock(const nsAString &aTopic,
|
|
|
|
nsIDOMWindow *aWindow,
|
2014-01-07 04:16:07 -08:00
|
|
|
nsISupports **aWakeLock)
|
2012-03-07 03:03:25 -08:00
|
|
|
{
|
2014-01-07 04:16:07 -08:00
|
|
|
mozilla::ErrorResult rv;
|
|
|
|
nsRefPtr<WakeLock> wakelock = NewWakeLock(aTopic, aWindow, rv);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
return rv.ErrorCode();
|
|
|
|
}
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2014-01-07 04:16:07 -08:00
|
|
|
nsCOMPtr<nsIDOMEventListener> eventListener = wakelock.get();
|
|
|
|
eventListener.forget(aWakeLock);
|
2012-03-07 03:03:25 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-07 04:16:07 -08:00
|
|
|
already_AddRefed<WakeLock>
|
2013-02-14 12:41:30 -08:00
|
|
|
PowerManagerService::NewWakeLockOnBehalfOfProcess(const nsAString& aTopic,
|
|
|
|
ContentParent* aContentParent)
|
|
|
|
{
|
|
|
|
nsRefPtr<WakeLock> wakelock = new WakeLock();
|
|
|
|
nsresult rv = wakelock->Init(aTopic, aContentParent);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return wakelock.forget();
|
|
|
|
}
|
|
|
|
|
2012-01-19 19:24:00 -08:00
|
|
|
} // power
|
|
|
|
} // dom
|
|
|
|
} // mozilla
|