2012-07-16 09:38:18 -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/. */
|
|
|
|
|
|
|
|
#include "nsVolume.h"
|
2012-12-14 16:01:34 -08:00
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "nsIPowerManagerService.h"
|
2012-07-16 09:38:18 -07:00
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsIVolume.h"
|
2012-12-14 16:01:34 -08:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2012-07-16 09:38:18 -07:00
|
|
|
#include "nsVolumeStat.h"
|
2012-12-14 16:01:34 -08:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-08-03 16:48:58 -07:00
|
|
|
#include "Volume.h"
|
2012-07-16 09:38:18 -07:00
|
|
|
|
2012-12-14 16:01:34 -08:00
|
|
|
#define VOLUME_MANAGER_LOG_TAG "nsVolume"
|
|
|
|
#include "VolumeManagerLog.h"
|
|
|
|
|
2012-07-16 09:38:18 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace system {
|
|
|
|
|
|
|
|
const char *
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_VolumeStateStr(int32_t aState)
|
2012-07-16 09:38:18 -07:00
|
|
|
{
|
|
|
|
switch (aState) {
|
|
|
|
case nsIVolume::STATE_INIT: return "Init";
|
|
|
|
case nsIVolume::STATE_NOMEDIA: return "NoMedia";
|
|
|
|
case nsIVolume::STATE_IDLE: return "Idle";
|
|
|
|
case nsIVolume::STATE_PENDING: return "Pending";
|
|
|
|
case nsIVolume::STATE_CHECKING: return "Checking";
|
|
|
|
case nsIVolume::STATE_MOUNTED: return "Mounted";
|
|
|
|
case nsIVolume::STATE_UNMOUNTING: return "Unmounting";
|
|
|
|
case nsIVolume::STATE_FORMATTING: return "Formatting";
|
|
|
|
case nsIVolume::STATE_SHARED: return "Shared";
|
|
|
|
case nsIVolume::STATE_SHAREDMNT: return "Shared-Mounted";
|
|
|
|
}
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:43:02 -08:00
|
|
|
// While nsVolumes can only be used on the main thread, in the
|
|
|
|
// UpdateVolumeRunnable constructor (which is called from IOThread) we
|
|
|
|
// allocate an nsVolume which is then passed to MainThread. Since we
|
|
|
|
// have a situation where we allocate on one thread and free on another
|
|
|
|
// we use a thread safe AddRef implementation.
|
2012-07-16 09:38:18 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsVolume, nsIVolume)
|
|
|
|
|
2013-01-07 08:43:02 -08:00
|
|
|
nsVolume::nsVolume(const Volume* aVolume)
|
2012-08-03 16:48:58 -07:00
|
|
|
: mName(NS_ConvertUTF8toUTF16(aVolume->Name())),
|
|
|
|
mMountPoint(NS_ConvertUTF8toUTF16(aVolume->MountPoint())),
|
2012-12-14 16:01:34 -08:00
|
|
|
mState(aVolume->State()),
|
|
|
|
mMountGeneration(aVolume->MountGeneration()),
|
|
|
|
mMountLocked(aVolume->IsMountLocked())
|
2012-08-03 16:48:58 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-02 12:52:17 -07:00
|
|
|
bool nsVolume::Equals(nsIVolume* aVolume)
|
|
|
|
{
|
|
|
|
nsString volName;
|
|
|
|
aVolume->GetName(volName);
|
|
|
|
if (!mName.Equals(volName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString volMountPoint;
|
|
|
|
aVolume->GetMountPoint(volMountPoint);
|
|
|
|
if (!mMountPoint.Equals(volMountPoint)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t volState;
|
|
|
|
aVolume->GetState(&volState);
|
|
|
|
if (mState != volState){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t volMountGeneration;
|
|
|
|
aVolume->GetMountGeneration(&volMountGeneration);
|
|
|
|
if (mMountGeneration != volMountGeneration) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool volIsMountLocked;
|
|
|
|
aVolume->GetIsMountLocked(&volIsMountLocked);
|
|
|
|
if (mMountLocked != volIsMountLocked) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsVolume::GetIsMountLocked(bool *aIsMountLocked)
|
|
|
|
{
|
|
|
|
*aIsMountLocked = mMountLocked;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:43:02 -08:00
|
|
|
NS_IMETHODIMP nsVolume::GetName(nsAString& aName)
|
2012-07-16 09:38:18 -07:00
|
|
|
{
|
|
|
|
aName = mName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:43:02 -08:00
|
|
|
NS_IMETHODIMP nsVolume::GetMountGeneration(int32_t* aMountGeneration)
|
2012-12-14 16:01:34 -08:00
|
|
|
{
|
|
|
|
*aMountGeneration = mMountGeneration;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:43:02 -08:00
|
|
|
NS_IMETHODIMP nsVolume::GetMountLockName(nsAString& aMountLockName)
|
2012-12-14 16:01:34 -08:00
|
|
|
{
|
|
|
|
aMountLockName = NS_LITERAL_STRING("volume-") + Name();
|
|
|
|
aMountLockName.AppendPrintf("-%d", mMountGeneration);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:43:02 -08:00
|
|
|
NS_IMETHODIMP nsVolume::GetMountPoint(nsAString& aMountPoint)
|
2012-07-16 09:38:18 -07:00
|
|
|
{
|
|
|
|
aMountPoint = mMountPoint;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-07 08:43:02 -08:00
|
|
|
NS_IMETHODIMP nsVolume::GetState(int32_t* aState)
|
2012-07-16 09:38:18 -07:00
|
|
|
{
|
|
|
|
*aState = mState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:14:07 -07:00
|
|
|
NS_IMETHODIMP nsVolume::GetStats(nsIVolumeStat **aResult)
|
2012-07-16 09:38:18 -07:00
|
|
|
{
|
|
|
|
if (mState != STATE_MOUNTED) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aResult = new nsVolumeStat(mMountPoint));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-14 16:01:34 -08:00
|
|
|
void
|
|
|
|
nsVolume::LogState() const
|
|
|
|
{
|
|
|
|
if (mState == nsIVolume::STATE_MOUNTED) {
|
|
|
|
LOG("nsVolume: %s state %s @ '%s' gen %d locked %d",
|
2013-01-28 14:34:30 -08:00
|
|
|
NameStr().get(), StateStr(), MountPointStr().get(),
|
2012-12-14 16:01:34 -08:00
|
|
|
MountGeneration(), (int)IsMountLocked());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-28 14:34:30 -08:00
|
|
|
LOG("nsVolume: %s state %s", NameStr().get(), StateStr());
|
2012-12-14 16:01:34 -08:00
|
|
|
}
|
|
|
|
|
2013-04-02 12:52:17 -07:00
|
|
|
void nsVolume::Set(nsIVolume* aVolume)
|
2012-12-14 16:01:34 -08:00
|
|
|
{
|
2013-01-07 08:43:02 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-04-02 12:52:17 -07:00
|
|
|
aVolume->GetName(mName);
|
|
|
|
aVolume->GetMountPoint(mMountPoint);
|
|
|
|
aVolume->GetState(&mState);
|
|
|
|
|
|
|
|
int32_t volMountGeneration;
|
|
|
|
aVolume->GetMountGeneration(&volMountGeneration);
|
2012-12-14 16:01:34 -08:00
|
|
|
|
|
|
|
if (mState != nsIVolume::STATE_MOUNTED) {
|
|
|
|
// Since we're not in the mounted state, we need to
|
|
|
|
// forgot whatever mount generation we may have had.
|
|
|
|
mMountGeneration = -1;
|
|
|
|
return;
|
|
|
|
}
|
2013-04-02 12:52:17 -07:00
|
|
|
if (mMountGeneration == volMountGeneration) {
|
2012-12-14 16:01:34 -08:00
|
|
|
// No change in mount generation, nothing else to do
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-02 12:52:17 -07:00
|
|
|
mMountGeneration = volMountGeneration;
|
|
|
|
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
// Child processes just track the state, not maintain it.
|
|
|
|
aVolume->GetIsMountLocked(&mMountLocked);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-14 16:01:34 -08:00
|
|
|
|
|
|
|
// Notify the Volume on IOThread whether the volume is locked or not.
|
|
|
|
nsCOMPtr<nsIPowerManagerService> pmService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
|
|
|
if (!pmService) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsString mountLockName;
|
|
|
|
GetMountLockName(mountLockName);
|
|
|
|
nsString mountLockState;
|
|
|
|
pmService->GetWakeLockState(mountLockName, mountLockState);
|
|
|
|
UpdateMountLock(mountLockState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-07 08:43:02 -08:00
|
|
|
nsVolume::UpdateMountLock(const nsAString& aMountLockState)
|
2012-12-14 16:01:34 -08:00
|
|
|
{
|
2013-04-02 12:52:17 -07:00
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
2013-01-07 08:43:02 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-14 16:01:34 -08:00
|
|
|
// There are 3 states, unlocked, locked-background, and locked-foreground
|
|
|
|
// I figured it was easier to use negtive logic and compare for unlocked.
|
|
|
|
UpdateMountLock(!aMountLockState.EqualsLiteral("unlocked"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsVolume::UpdateMountLock(bool aMountLocked)
|
|
|
|
{
|
2013-04-02 12:52:17 -07:00
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
2013-01-07 08:43:02 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-14 16:01:34 -08:00
|
|
|
if (aMountLocked == mMountLocked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The locked/unlocked state changed. Tell IOThread about it.
|
|
|
|
mMountLocked = aMountLocked;
|
|
|
|
LogState();
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableFunction(Volume::UpdateMountLock,
|
|
|
|
NS_LossyConvertUTF16toASCII(Name()),
|
|
|
|
MountGeneration(), aMountLocked));
|
|
|
|
}
|
|
|
|
|
2012-07-16 09:38:18 -07:00
|
|
|
} // system
|
|
|
|
} // mozilla
|