2012-05-24 21:03:07 -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-07-16 09:38:18 -07:00
|
|
|
#include "VolumeManager.h"
|
2012-05-24 21:03:07 -07:00
|
|
|
|
|
|
|
#include "Volume.h"
|
|
|
|
#include "VolumeCommand.h"
|
|
|
|
#include "VolumeManagerLog.h"
|
2012-07-16 09:38:18 -07:00
|
|
|
#include "VolumeServiceTest.h"
|
|
|
|
|
|
|
|
#include "nsWhitespaceTokenizer.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "mozilla/Scoped.h"
|
2012-09-20 12:15:22 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-05-24 21:03:07 -07:00
|
|
|
|
2012-07-16 09:38:18 -07:00
|
|
|
#include <android/log.h>
|
|
|
|
#include <cutils/sockets.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
2012-05-24 21:03:07 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace system {
|
|
|
|
|
2012-09-20 12:15:22 -07:00
|
|
|
static StaticRefPtr<VolumeManager> sVolumeManager;
|
2012-05-24 21:03:07 -07:00
|
|
|
|
2012-08-09 20:40:05 -07:00
|
|
|
VolumeManager::STATE VolumeManager::mState = VolumeManager::UNINITIALIZED;
|
|
|
|
VolumeManager::StateObserverList VolumeManager::mStateObserverList;
|
|
|
|
|
2012-05-24 21:03:07 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
VolumeManager::VolumeManager()
|
2012-11-08 11:35:02 -08:00
|
|
|
: LineWatcher('\0', kRcvBufSize),
|
|
|
|
mSocket(-1),
|
|
|
|
mCommandPending(false)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
DBG("VolumeManager constructor called");
|
|
|
|
}
|
|
|
|
|
|
|
|
VolumeManager::~VolumeManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-16 09:38:18 -07:00
|
|
|
//static
|
|
|
|
size_t
|
|
|
|
VolumeManager::NumVolumes()
|
|
|
|
{
|
|
|
|
if (!sVolumeManager) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return sVolumeManager->mVolumeArray.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
TemporaryRef<Volume>
|
|
|
|
VolumeManager::GetVolume(size_t aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIndex < NumVolumes());
|
|
|
|
return sVolumeManager->mVolumeArray[aIndex];
|
|
|
|
}
|
|
|
|
|
2012-05-24 21:03:07 -07:00
|
|
|
//static
|
|
|
|
VolumeManager::STATE
|
|
|
|
VolumeManager::State()
|
|
|
|
{
|
2012-08-09 20:40:05 -07:00
|
|
|
return mState;
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
const char *
|
2012-08-09 20:40:05 -07:00
|
|
|
VolumeManager::StateStr(VolumeManager::STATE aState)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
2012-08-09 20:40:05 -07:00
|
|
|
switch (aState) {
|
2012-05-24 21:03:07 -07:00
|
|
|
case UNINITIALIZED: return "Uninitialized";
|
|
|
|
case STARTING: return "Starting";
|
|
|
|
case VOLUMES_READY: return "Volumes Ready";
|
|
|
|
}
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//static
|
|
|
|
void
|
|
|
|
VolumeManager::SetState(STATE aNewState)
|
|
|
|
{
|
2012-08-09 20:40:05 -07:00
|
|
|
if (mState != aNewState) {
|
|
|
|
LOG("changing state from '%s' to '%s'",
|
|
|
|
StateStr(mState), StateStr(aNewState));
|
|
|
|
mState = aNewState;
|
|
|
|
mStateObserverList.Broadcast(StateChangedEvent());
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
void
|
2013-01-07 22:59:32 -08:00
|
|
|
VolumeManager::RegisterStateObserver(StateObserver *aObserver)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
2012-08-09 20:40:05 -07:00
|
|
|
mStateObserverList.AddObserver(aObserver);
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
2013-01-07 22:59:32 -08:00
|
|
|
void VolumeManager::UnregisterStateObserver(StateObserver *aObserver)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
2012-08-09 20:40:05 -07:00
|
|
|
mStateObserverList.RemoveObserver(aObserver);
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
TemporaryRef<Volume>
|
2013-01-07 22:59:32 -08:00
|
|
|
VolumeManager::FindVolumeByName(const nsCSubstring &aName)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
if (!sVolumeManager) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-07-16 09:38:18 -07:00
|
|
|
VolumeArray::size_type numVolumes = NumVolumes();
|
|
|
|
VolumeArray::index_type volIndex;
|
|
|
|
for (volIndex = 0; volIndex < numVolumes; volIndex++) {
|
|
|
|
RefPtr<Volume> vol = GetVolume(volIndex);
|
|
|
|
if (vol->Name().Equals(aName)) {
|
|
|
|
return vol;
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
TemporaryRef<Volume>
|
2013-01-07 22:59:32 -08:00
|
|
|
VolumeManager::FindAddVolumeByName(const nsCSubstring &aName)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
RefPtr<Volume> vol = FindVolumeByName(aName);
|
|
|
|
if (vol) {
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
// No volume found, create and add a new one.
|
|
|
|
vol = new Volume(aName);
|
2012-07-16 09:38:18 -07:00
|
|
|
sVolumeManager->mVolumeArray.AppendElement(vol);
|
2012-05-24 21:03:07 -07:00
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
class VolumeListCallback : public VolumeResponseCallback
|
|
|
|
{
|
2013-01-07 22:59:32 -08:00
|
|
|
virtual void ResponseReceived(const VolumeCommand *aCommand)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
switch (ResponseCode()) {
|
|
|
|
case ResponseCode::VolumeListResult: {
|
|
|
|
// Each line will look something like:
|
|
|
|
//
|
|
|
|
// sdcard /mnt/sdcard 1
|
|
|
|
//
|
|
|
|
// So for each volume that we get back, we update any volumes that
|
|
|
|
// we have of the same name, or add new ones if they don't exist.
|
|
|
|
nsCWhitespaceTokenizer tokenizer(ResponseStr());
|
|
|
|
nsDependentCSubstring volName(tokenizer.nextToken());
|
|
|
|
RefPtr<Volume> vol = VolumeManager::FindAddVolumeByName(volName);
|
2012-07-16 09:38:18 -07:00
|
|
|
vol->HandleVoldResponse(ResponseCode(), tokenizer);
|
2012-05-24 21:03:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ResponseCode::CommandOkay: {
|
|
|
|
// We've received the list of volumes. Tell anybody who
|
|
|
|
// is listening that we're open for business.
|
|
|
|
VolumeManager::SetState(VolumeManager::VOLUMES_READY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
VolumeManager::OpenSocket()
|
|
|
|
{
|
|
|
|
SetState(STARTING);
|
|
|
|
if ((mSocket.rwget() = socket_local_client("vold",
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED,
|
|
|
|
SOCK_STREAM)) < 0) {
|
|
|
|
ERR("Error connecting to vold: (%s) - will retry", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// add FD_CLOEXEC flag
|
|
|
|
int flags = fcntl(mSocket.get(), F_GETFD);
|
|
|
|
if (flags == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
if (fcntl(mSocket.get(), F_SETFD, flags) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// set non-blocking
|
|
|
|
if (fcntl(mSocket.get(), F_SETFL, O_NONBLOCK) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!MessageLoopForIO::current()->
|
|
|
|
WatchFileDescriptor(mSocket.get(),
|
|
|
|
true,
|
|
|
|
MessageLoopForIO::WATCH_READ,
|
|
|
|
&mReadWatcher,
|
|
|
|
this)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Connected to vold");
|
|
|
|
PostCommand(new VolumeListCommand(new VolumeListCallback));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
void
|
2013-01-07 22:59:32 -08:00
|
|
|
VolumeManager::PostCommand(VolumeCommand *aCommand)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
if (!sVolumeManager) {
|
|
|
|
ERR("VolumeManager not initialized. Dropping command '%s'", aCommand->Data());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aCommand->SetPending(true);
|
|
|
|
|
|
|
|
DBG("Sending command '%s'", aCommand->Data());
|
|
|
|
// vold can only process one command at a time, so add our command
|
|
|
|
// to the end of the command queue.
|
|
|
|
sVolumeManager->mCommands.push(aCommand);
|
|
|
|
if (!sVolumeManager->mCommandPending) {
|
|
|
|
// There aren't any commands currently being processed, so go
|
|
|
|
// ahead and kick this one off.
|
|
|
|
sVolumeManager->mCommandPending = true;
|
|
|
|
sVolumeManager->WriteCommandData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* The WriteCommandData initiates sending of a command to vold. Since
|
|
|
|
* we're running on the IOThread and not allowed to block, WriteCommandData
|
|
|
|
* will write as much data as it can, and if not all of the data can be
|
|
|
|
* written then it will setup a file descriptor watcher and
|
|
|
|
* OnFileCanWriteWithoutBlocking will call WriteCommandData to write out
|
|
|
|
* more of the command data.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
VolumeManager::WriteCommandData()
|
|
|
|
{
|
|
|
|
if (mCommands.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:59:32 -08:00
|
|
|
VolumeCommand *cmd = mCommands.front();
|
2012-05-24 21:03:07 -07:00
|
|
|
if (cmd->BytesRemaining() == 0) {
|
|
|
|
// All bytes have been written. We're waiting for a response.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// There are more bytes left to write. Try to write them all.
|
|
|
|
ssize_t bytesWritten = write(mSocket.get(), cmd->Data(), cmd->BytesRemaining());
|
|
|
|
if (bytesWritten < 0) {
|
|
|
|
ERR("Failed to write %d bytes to vold socket", cmd->BytesRemaining());
|
|
|
|
Restart();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DBG("Wrote %ld bytes (of %d)", bytesWritten, cmd->BytesRemaining());
|
|
|
|
cmd->ConsumeBytes(bytesWritten);
|
|
|
|
if (cmd->BytesRemaining() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We were unable to write all of the command bytes. Setup a watcher
|
|
|
|
// so we'll get called again when we can write without blocking.
|
|
|
|
if (!MessageLoopForIO::current()->
|
|
|
|
WatchFileDescriptor(mSocket.get(),
|
|
|
|
false, // one-shot
|
|
|
|
MessageLoopForIO::WATCH_WRITE,
|
|
|
|
&mWriteWatcher,
|
|
|
|
this)) {
|
|
|
|
ERR("Failed to setup write watcher for vold socket");
|
|
|
|
Restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-08 11:35:02 -08:00
|
|
|
VolumeManager::OnLineRead(int aFd, nsDependentCSubstring& aMessage)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFd == mSocket.get());
|
2013-01-07 22:59:32 -08:00
|
|
|
char *endPtr;
|
2012-11-08 11:35:02 -08:00
|
|
|
int responseCode = strtol(aMessage.Data(), &endPtr, 10);
|
|
|
|
if (*endPtr == ' ') {
|
|
|
|
endPtr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now fish out the rest of the line after the response code
|
|
|
|
nsDependentCString responseLine(endPtr, aMessage.Length() - (endPtr - aMessage.Data()));
|
|
|
|
DBG("Rcvd: %d '%s'", responseCode, responseLine.Data());
|
|
|
|
|
|
|
|
if (responseCode >= ResponseCode::UnsolicitedInformational) {
|
|
|
|
// These are unsolicited broadcasts. We intercept these and process
|
|
|
|
// them ourselves
|
|
|
|
HandleBroadcast(responseCode, responseLine);
|
|
|
|
} else {
|
|
|
|
// Everything else is considered to be part of the command response.
|
|
|
|
if (mCommands.size() > 0) {
|
2013-01-07 22:59:32 -08:00
|
|
|
VolumeCommand *cmd = mCommands.front();
|
2012-11-08 11:35:02 -08:00
|
|
|
cmd->HandleResponse(responseCode, responseLine);
|
|
|
|
if (responseCode >= ResponseCode::CommandOkay) {
|
|
|
|
// That's a terminating response. We can remove the command.
|
|
|
|
mCommands.pop();
|
|
|
|
mCommandPending = false;
|
|
|
|
// Start the next command, if there is one.
|
|
|
|
WriteCommandData();
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
2012-11-08 11:35:02 -08:00
|
|
|
} else {
|
|
|
|
ERR("Response with no command");
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VolumeManager::OnFileCanWriteWithoutBlocking(int aFd)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFd == mSocket.get());
|
|
|
|
WriteCommandData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-07 22:59:32 -08:00
|
|
|
VolumeManager::HandleBroadcast(int aResponseCode, nsCString &aResponseLine)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
// Format of the line is something like:
|
|
|
|
//
|
|
|
|
// Volume sdcard /mnt/sdcard state changed from 7 (Shared-Unmounted) to 1 (Idle-Unmounted)
|
|
|
|
//
|
|
|
|
// So we parse out the volume name and the state after the string " to "
|
|
|
|
nsCWhitespaceTokenizer tokenizer(aResponseLine);
|
|
|
|
tokenizer.nextToken(); // The word "Volume"
|
|
|
|
nsDependentCSubstring volName(tokenizer.nextToken());
|
|
|
|
|
|
|
|
RefPtr<Volume> vol = FindVolumeByName(volName);
|
|
|
|
if (!vol) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-16 09:38:18 -07:00
|
|
|
vol->HandleVoldResponse(aResponseCode, tokenizer);
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VolumeManager::Restart()
|
|
|
|
{
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
|
|
|
|
while (!mCommands.empty()) {
|
|
|
|
mCommands.pop();
|
|
|
|
}
|
|
|
|
mCommandPending = false;
|
|
|
|
mSocket.dispose();
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
void
|
|
|
|
VolumeManager::Start()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
|
|
|
|
if (!sVolumeManager) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-09 20:40:05 -07:00
|
|
|
SetState(STARTING);
|
2012-05-24 21:03:07 -07:00
|
|
|
if (!sVolumeManager->OpenSocket()) {
|
|
|
|
// Socket open failed, try again in a second.
|
|
|
|
MessageLoopForIO::current()->
|
|
|
|
PostDelayedTask(FROM_HERE,
|
|
|
|
NewRunnableFunction(VolumeManager::Start),
|
|
|
|
1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 11:35:02 -08:00
|
|
|
void
|
|
|
|
VolumeManager::OnError()
|
|
|
|
{
|
|
|
|
Restart();
|
|
|
|
}
|
|
|
|
|
2012-05-24 21:03:07 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitVolumeManagerIOThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
MOZ_ASSERT(!sVolumeManager);
|
|
|
|
|
|
|
|
sVolumeManager = new VolumeManager();
|
|
|
|
VolumeManager::Start();
|
2012-07-16 09:38:18 -07:00
|
|
|
|
|
|
|
InitVolumeServiceTestIOThread();
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ShutdownVolumeManagerIOThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
|
|
|
|
sVolumeManager = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Public API
|
|
|
|
*
|
|
|
|
* Since the VolumeManager runs in IO Thread context, we need to switch
|
|
|
|
* to IOThread context before we can do anything.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
InitVolumeManager()
|
|
|
|
{
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableFunction(InitVolumeManagerIOThread));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShutdownVolumeManager()
|
|
|
|
{
|
2012-07-16 09:38:18 -07:00
|
|
|
ShutdownVolumeServiceTest();
|
|
|
|
|
2012-05-24 21:03:07 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableFunction(ShutdownVolumeManagerIOThread));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // system
|
|
|
|
} // mozilla
|