2012-12-15 04:01:34 +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/. */
|
|
|
|
|
|
|
|
#include "nsVolumeMountLock.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIPowerManagerService.h"
|
|
|
|
#include "nsIVolume.h"
|
|
|
|
#include "nsIVolumeService.h"
|
|
|
|
#include "nsString.h"
|
2013-09-24 01:30:40 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-12-15 04:01:34 +04:00
|
|
|
|
2014-11-14 04:42:38 +03:00
|
|
|
#undef VOLUME_MANAGER_LOG_TAG
|
2012-12-15 04:01:34 +04:00
|
|
|
#define VOLUME_MANAGER_LOG_TAG "nsVolumeMountLock"
|
|
|
|
#include "VolumeManagerLog.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-01-07 16:16:07 +04:00
|
|
|
#include "mozilla/dom/power/PowerManagerService.h"
|
2012-12-15 04:01:34 +04:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::services;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace system {
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsVolumeMountLock, nsIVolumeMountLock,
|
|
|
|
nsIObserver, nsISupportsWeakReference)
|
2012-12-15 04:01:34 +04:00
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<nsVolumeMountLock>
|
2013-01-07 20:43:02 +04:00
|
|
|
nsVolumeMountLock::Create(const nsAString& aVolumeName)
|
2012-12-15 04:01:34 +04:00
|
|
|
{
|
|
|
|
DBG("nsVolumeMountLock::Create called");
|
|
|
|
|
|
|
|
nsRefPtr<nsVolumeMountLock> mountLock = new nsVolumeMountLock(aVolumeName);
|
|
|
|
nsresult rv = mountLock->Init();
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
return mountLock.forget();
|
|
|
|
}
|
|
|
|
|
2013-01-07 20:43:02 +04:00
|
|
|
nsVolumeMountLock::nsVolumeMountLock(const nsAString& aVolumeName)
|
2012-12-15 04:01:34 +04:00
|
|
|
: mVolumeName(aVolumeName),
|
|
|
|
mVolumeGeneration(-1),
|
|
|
|
mUnlocked(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//virtual
|
|
|
|
nsVolumeMountLock::~nsVolumeMountLock()
|
|
|
|
{
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsVolumeMountLock::Init()
|
|
|
|
{
|
|
|
|
LOG("nsVolumeMountLock created for '%s'",
|
|
|
|
NS_LossyConvertUTF16toASCII(mVolumeName).get());
|
|
|
|
|
|
|
|
// Add ourselves as an Observer. It's important that we use a weak
|
|
|
|
// reference here. If we used a strong reference, then that reference
|
|
|
|
// would prevent this object from being destructed.
|
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
obs->AddObserver(this, NS_VOLUME_STATE_CHANGED, true /*weak*/);
|
|
|
|
|
2014-09-15 22:51:25 +04:00
|
|
|
// Get the initial mountGeneration and grab a lock.
|
2012-12-15 04:01:34 +04:00
|
|
|
nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(vs, NS_ERROR_FAILURE);
|
|
|
|
|
2014-09-15 22:51:25 +04:00
|
|
|
nsCOMPtr<nsIVolume> vol;
|
|
|
|
nsresult rv = vs->GetVolumeByName(mVolumeName, getter_AddRefs(vol));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = vol->GetMountGeneration(&mVolumeGeneration);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-12-15 04:01:34 +04:00
|
|
|
|
2014-09-15 22:51:25 +04:00
|
|
|
return Lock(vol);
|
2012-12-15 04:01:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void unlock (); */
|
|
|
|
NS_IMETHODIMP nsVolumeMountLock::Unlock()
|
|
|
|
{
|
|
|
|
LOG("nsVolumeMountLock released for '%s'",
|
|
|
|
NS_LossyConvertUTF16toASCII(mVolumeName).get());
|
|
|
|
|
|
|
|
mUnlocked = true;
|
|
|
|
mWakeLock = nullptr;
|
|
|
|
|
|
|
|
// While we don't really need to remove weak observers, we do so anyways
|
|
|
|
// since it will reduce the number of times Observe gets called.
|
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
obs->RemoveObserver(this, NS_VOLUME_STATE_CHANGED);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
NS_IMETHODIMP nsVolumeMountLock::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
|
2012-12-15 04:01:34 +04:00
|
|
|
{
|
|
|
|
if (strcmp(aTopic, NS_VOLUME_STATE_CHANGED) != 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (mUnlocked) {
|
|
|
|
// We're not locked anymore, so we don't need to look at the notifications.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVolume> vol = do_QueryInterface(aSubject);
|
|
|
|
if (!vol) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsString volName;
|
|
|
|
vol->GetName(volName);
|
|
|
|
if (!volName.Equals(mVolumeName)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
int32_t state;
|
|
|
|
nsresult rv = vol->GetState(&state);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (state != nsIVolume::STATE_MOUNTED) {
|
|
|
|
mWakeLock = nullptr;
|
|
|
|
mVolumeGeneration = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t mountGeneration;
|
|
|
|
rv = vol->GetMountGeneration(&mountGeneration);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
DBG("nsVolumeMountLock::Observe mountGeneration = %d mVolumeGeneration = %d",
|
|
|
|
mountGeneration, mVolumeGeneration);
|
|
|
|
|
|
|
|
if (mVolumeGeneration == mountGeneration) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The generation changed, which means that any wakelock we may have
|
|
|
|
// been holding is now invalid. Grab a new wakelock for the new generation
|
|
|
|
// number.
|
|
|
|
|
|
|
|
mWakeLock = nullptr;
|
|
|
|
mVolumeGeneration = mountGeneration;
|
|
|
|
|
2014-09-15 22:51:25 +04:00
|
|
|
return Lock(vol);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsVolumeMountLock::Lock(nsIVolume* aVolume)
|
|
|
|
{
|
2014-01-07 16:16:07 +04:00
|
|
|
nsRefPtr<power::PowerManagerService> pmService =
|
|
|
|
power::PowerManagerService::GetInstance();
|
2012-12-15 04:01:34 +04:00
|
|
|
NS_ENSURE_TRUE(pmService, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsString mountLockName;
|
2014-09-15 22:51:25 +04:00
|
|
|
aVolume->GetMountLockName(mountLockName);
|
2014-01-07 16:16:07 +04:00
|
|
|
|
|
|
|
ErrorResult err;
|
|
|
|
mWakeLock = pmService->NewWakeLock(mountLockName, nullptr, err);
|
|
|
|
if (err.Failed()) {
|
|
|
|
return err.ErrorCode();
|
|
|
|
}
|
2012-12-15 04:01:34 +04:00
|
|
|
|
|
|
|
LOG("nsVolumeMountLock acquired for '%s' gen %d",
|
|
|
|
NS_LossyConvertUTF16toASCII(mVolumeName).get(), mVolumeGeneration);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace system
|
|
|
|
} // namespace mozilla
|