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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:42:38 -08:00
|
|
|
//static
|
|
|
|
void
|
|
|
|
VolumeManager::Dump(const char* aLabel)
|
|
|
|
{
|
|
|
|
if (!sVolumeManager) {
|
|
|
|
LOG("%s: sVolumeManager == null", aLabel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VolumeArray::size_type numVolumes = NumVolumes();
|
|
|
|
VolumeArray::index_type volIndex;
|
|
|
|
for (volIndex = 0; volIndex < numVolumes; volIndex++) {
|
|
|
|
RefPtr<Volume> vol = GetVolume(volIndex);
|
|
|
|
vol->Dump(aLabel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 08:43:02 -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 08:43:02 -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 08:43:02 -08:00
|
|
|
VolumeManager::FindVolumeByName(const nsCSubstring& aName)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
if (!sVolumeManager) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
TemporaryRef<Volume>
|
2013-01-07 08:43:02 -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;
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:42:38 -08:00
|
|
|
//static
|
|
|
|
void VolumeManager::InitConfig()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
|
|
|
|
// This function uses /system/etc/volume.cfg to add additional volumes
|
|
|
|
// to the Volume Manager.
|
|
|
|
//
|
|
|
|
// This is useful on devices like the Nexus 4, which have no physical sd card
|
|
|
|
// or dedicated partition.
|
|
|
|
//
|
|
|
|
// The format of the volume.cfg file is as follows:
|
|
|
|
// create volume-name mount-point
|
2014-12-21 18:33:33 -08:00
|
|
|
// configure volume-name preference preference-value
|
2014-11-13 17:42:38 -08:00
|
|
|
// Blank lines and lines starting with the hash character "#" will be ignored.
|
|
|
|
|
|
|
|
ScopedCloseFile fp;
|
|
|
|
int n = 0;
|
|
|
|
char line[255];
|
|
|
|
const char *filename = "/system/etc/volume.cfg";
|
|
|
|
if (!(fp = fopen(filename, "r"))) {
|
|
|
|
LOG("Unable to open volume configuration file '%s' - ignoring", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while(fgets(line, sizeof(line), fp)) {
|
|
|
|
const char *delim = " \t\n";
|
|
|
|
n++;
|
|
|
|
|
|
|
|
if (line[0] == '#')
|
|
|
|
continue;
|
2014-12-21 18:33:33 -08:00
|
|
|
|
|
|
|
nsCString commandline(line);
|
|
|
|
nsCWhitespaceTokenizer tokenizer(commandline);
|
|
|
|
if (!tokenizer.hasMoreTokens()) {
|
2014-11-13 17:42:38 -08:00
|
|
|
// Blank line - ignore
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-21 18:33:33 -08:00
|
|
|
|
|
|
|
nsCString command(tokenizer.nextToken());
|
|
|
|
if (command.EqualsLiteral("create")) {
|
|
|
|
if (!tokenizer.hasMoreTokens()) {
|
2014-11-13 17:42:38 -08:00
|
|
|
ERR("No vol_name in %s line %d", filename, n);
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-21 18:33:33 -08:00
|
|
|
nsCString volName(tokenizer.nextToken());
|
|
|
|
if (!tokenizer.hasMoreTokens()) {
|
|
|
|
ERR("No mount point for volume '%s'. %s line %d",
|
|
|
|
volName.get(), filename, n);
|
2014-11-13 17:42:38 -08:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-21 18:33:33 -08:00
|
|
|
nsCString mountPoint(tokenizer.nextToken());
|
2014-11-13 17:42:38 -08:00
|
|
|
RefPtr<Volume> vol = FindAddVolumeByName(volName);
|
|
|
|
vol->SetFakeVolume(mountPoint);
|
2014-12-21 18:33:33 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (command.EqualsLiteral("configure")) {
|
|
|
|
if (!tokenizer.hasMoreTokens()) {
|
|
|
|
ERR("No vol_name in %s line %d", filename, n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsCString volName(tokenizer.nextToken());
|
|
|
|
if (!tokenizer.hasMoreTokens()) {
|
|
|
|
ERR("No configuration name specified for volume '%s'. %s line %d",
|
|
|
|
volName.get(), filename, n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsCString configName(tokenizer.nextToken());
|
|
|
|
if (!tokenizer.hasMoreTokens()) {
|
|
|
|
ERR("No value for configuration name '%s'. %s line %d",
|
|
|
|
configName.get(), filename, n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsCString configValue(tokenizer.nextToken());
|
|
|
|
RefPtr<Volume> vol = FindVolumeByName(volName);
|
|
|
|
if (vol) {
|
|
|
|
vol->SetConfig(configName, configValue);
|
|
|
|
} else {
|
|
|
|
ERR("Invalid volume name '%s'.", volName.get());
|
|
|
|
}
|
|
|
|
continue;
|
2014-11-13 17:42:38 -08:00
|
|
|
}
|
2014-12-21 18:33:33 -08:00
|
|
|
ERR("Unrecognized command: '%s'", command.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VolumeManager::DefaultConfig()
|
|
|
|
{
|
|
|
|
|
|
|
|
VolumeManager::VolumeArray::size_type numVolumes = VolumeManager::NumVolumes();
|
|
|
|
if (numVolumes == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (numVolumes == 1) {
|
|
|
|
// This is to cover early shipping phones like the Buri,
|
|
|
|
// which had no internal storage, and only external sdcard.
|
|
|
|
//
|
|
|
|
// Phones line the nexus-4 which only have an internal
|
|
|
|
// storage area will need to have a volume.cfg file with
|
|
|
|
// removable set to false.
|
|
|
|
RefPtr<Volume> vol = VolumeManager::GetVolume(0);
|
|
|
|
vol->SetIsRemovable(true);
|
|
|
|
vol->SetIsHotSwappable(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VolumeManager::VolumeArray::index_type volIndex;
|
|
|
|
for (volIndex = 0; volIndex < numVolumes; volIndex++) {
|
|
|
|
RefPtr<Volume> vol = VolumeManager::GetVolume(volIndex);
|
|
|
|
if (!vol->Name().EqualsLiteral("sdcard")) {
|
|
|
|
vol->SetIsRemovable(true);
|
|
|
|
vol->SetIsHotSwappable(true);
|
2014-11-13 17:42:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-24 21:03:07 -07:00
|
|
|
class VolumeListCallback : public VolumeResponseCallback
|
|
|
|
{
|
2013-01-07 08:43:02 -08:00
|
|
|
virtual void ResponseReceived(const VolumeCommand* aCommand)
|
2012-05-24 21:03:07 -07:00
|
|
|
{
|
|
|
|
switch (ResponseCode()) {
|
2014-09-25 07:10:42 -07:00
|
|
|
case ::ResponseCode::VolumeListResult: {
|
2012-05-24 21:03:07 -07:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-09-25 07:10:42 -07:00
|
|
|
case ::ResponseCode::CommandOkay: {
|
2014-11-13 17:42:38 -08:00
|
|
|
// We've received the list of volumes. Now read the Volume.cfg
|
|
|
|
// file to perform customizations, and then tell everybody
|
|
|
|
// that we're ready for business.
|
2014-12-21 18:33:33 -08:00
|
|
|
VolumeManager::DefaultConfig();
|
2014-11-13 17:42:38 -08:00
|
|
|
VolumeManager::InitConfig();
|
|
|
|
VolumeManager::Dump("READY");
|
2012-05-24 21:03:07 -07:00
|
|
|
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 08:43:02 -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 08:43:02 -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 08:43:02 -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());
|
|
|
|
|
2014-09-25 07:10:42 -07:00
|
|
|
if (responseCode >= ::ResponseCode::UnsolicitedInformational) {
|
2012-11-08 11:35:02 -08:00
|
|
|
// 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 08:43:02 -08:00
|
|
|
VolumeCommand* cmd = mCommands.front();
|
2012-11-08 11:35:02 -08:00
|
|
|
cmd->HandleResponse(responseCode, responseLine);
|
2014-09-25 07:10:42 -07:00
|
|
|
if (responseCode >= ::ResponseCode::CommandOkay) {
|
2012-11-08 11:35:02 -08:00
|
|
|
// 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 08:43:02 -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());
|
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
sVolumeManager = nullptr;
|
2012-05-24 21:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* 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
|