2014-07-12 01:42:15 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "MozMtpServer.h"
|
|
|
|
#include "MozMtpDatabase.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cutils/properties.h>
|
2014-08-20 00:41:35 +04:00
|
|
|
#include <private/android_filesystem_config.h>
|
2014-07-12 01:42:15 +04:00
|
|
|
|
2014-08-20 00:41:35 +04:00
|
|
|
#include "base/message_loop.h"
|
2014-08-29 00:13:06 +04:00
|
|
|
#include "DeviceStorage.h"
|
|
|
|
#include "mozilla/LazyIdleThread.h"
|
2014-07-12 01:42:15 +04:00
|
|
|
#include "mozilla/Scoped.h"
|
2014-08-29 00:13:06 +04:00
|
|
|
#include "mozilla/Services.h"
|
2014-08-20 00:41:35 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2014-08-29 00:13:06 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2014-07-12 01:42:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2014-08-20 00:41:35 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
#include "Volume.h"
|
2014-07-12 01:42:15 +04:00
|
|
|
|
2014-08-29 00:13:06 +04:00
|
|
|
#define DEFAULT_THREAD_TIMEOUT_MS 30000
|
|
|
|
|
2014-07-12 01:42:15 +04:00
|
|
|
using namespace android;
|
|
|
|
using namespace mozilla;
|
2014-08-20 00:41:35 +04:00
|
|
|
BEGIN_MTP_NAMESPACE
|
2014-07-12 01:42:15 +04:00
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
static const char* kMtpWatcherUpdate = "mtp-watcher-update";
|
|
|
|
|
2016-04-27 13:05:40 +03:00
|
|
|
class MtpWatcherUpdateRunnable final : public Runnable
|
2014-08-29 00:13:06 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-10-14 01:35:11 +03:00
|
|
|
MtpWatcherUpdateRunnable(MozMtpDatabase* aMozMtpDatabase,
|
|
|
|
RefCountedMtpServer* aMtpServer,
|
|
|
|
DeviceStorageFile* aFile,
|
|
|
|
const nsACString& aEventType)
|
2014-08-29 00:13:06 +04:00
|
|
|
: mMozMtpDatabase(aMozMtpDatabase),
|
|
|
|
mMtpServer(aMtpServer),
|
|
|
|
mFile(aFile),
|
|
|
|
mEventType(aEventType)
|
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-08-29 00:13:06 +04:00
|
|
|
{
|
2015-10-14 01:35:11 +03:00
|
|
|
// Runs on the MtpWatcherUpdate->mIOThread
|
2014-08-29 00:13:06 +04:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
mMozMtpDatabase->MtpWatcherUpdate(mMtpServer, mFile, mEventType);
|
2014-08-29 00:13:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MozMtpDatabase> mMozMtpDatabase;
|
|
|
|
RefPtr<RefCountedMtpServer> mMtpServer;
|
|
|
|
RefPtr<DeviceStorageFile> mFile;
|
2014-08-29 00:13:06 +04:00
|
|
|
nsCString mEventType;
|
|
|
|
};
|
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
// The MtpWatcherUpdate class listens for mtp-watcher-update events
|
|
|
|
// and tells the MtpServer about changes made in device storage.
|
|
|
|
class MtpWatcherUpdate final : public nsIObserver
|
2014-08-29 00:13:06 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
MtpWatcherUpdate(MozMtpServer* aMozMtpServer)
|
2014-08-29 00:13:06 +04:00
|
|
|
: mMozMtpServer(aMozMtpServer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mIOThread = new LazyIdleThread(
|
|
|
|
DEFAULT_THREAD_TIMEOUT_MS,
|
2015-10-14 01:35:11 +03:00
|
|
|
NS_LITERAL_CSTRING("MtpWatcherUpdate"));
|
2014-08-29 00:13:06 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2015-10-14 01:35:11 +03:00
|
|
|
obs->AddObserver(this, kMtpWatcherUpdate, false);
|
2014-08-29 00:13:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
if (strcmp(aTopic, kMtpWatcherUpdate)) {
|
|
|
|
// We're only interested in mtp-watcher-update events
|
2014-08-29 00:13:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 eventType(aData);
|
2014-10-08 02:12:00 +04:00
|
|
|
if (!eventType.EqualsLiteral("modified") && !eventType.EqualsLiteral("deleted")) {
|
|
|
|
// Bug 1074604: Needn't handle "created" event, once file operation
|
|
|
|
// finished, it would trigger "modified" event.
|
2014-08-29 00:13:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceStorageFile* file = static_cast<DeviceStorageFile*>(aSubject);
|
2015-10-14 01:35:11 +03:00
|
|
|
file->Dump(kMtpWatcherUpdate);
|
|
|
|
MTP_LOG("%s: file %s %s", kMtpWatcherUpdate,
|
2014-08-29 00:13:06 +04:00
|
|
|
NS_LossyConvertUTF16toASCII(file->mPath).get(),
|
|
|
|
eventType.get());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MozMtpDatabase> mozMtpDatabase = mMozMtpServer->GetMozMtpDatabase();
|
|
|
|
RefPtr<RefCountedMtpServer> mtpServer = mMozMtpServer->GetMtpServer();
|
2014-08-29 00:13:06 +04:00
|
|
|
|
|
|
|
// We're not supposed to perform I/O on the main thread, so punt the
|
|
|
|
// notification (which will write to /dev/mtp_usb) to an I/O Thread.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MtpWatcherUpdateRunnable> r =
|
2015-10-14 01:35:11 +03:00
|
|
|
new MtpWatcherUpdateRunnable(mozMtpDatabase, mtpServer, file, eventType);
|
2014-08-29 00:13:06 +04:00
|
|
|
mIOThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-10 15:44:02 +03:00
|
|
|
protected:
|
2015-10-14 01:35:11 +03:00
|
|
|
~MtpWatcherUpdate()
|
2015-03-10 15:44:02 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2015-10-14 01:35:11 +03:00
|
|
|
obs->RemoveObserver(this, kMtpWatcherUpdate);
|
2015-03-10 15:44:02 +03:00
|
|
|
}
|
|
|
|
|
2014-08-29 00:13:06 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MozMtpServer> mMozMtpServer;
|
2014-08-29 00:13:06 +04:00
|
|
|
nsCOMPtr<nsIThread> mIOThread;
|
|
|
|
};
|
2015-10-14 01:35:11 +03:00
|
|
|
NS_IMPL_ISUPPORTS(MtpWatcherUpdate, nsIObserver)
|
|
|
|
static StaticRefPtr<MtpWatcherUpdate> sMtpWatcherUpdate;
|
2014-08-29 00:13:06 +04:00
|
|
|
|
2016-04-27 13:05:40 +03:00
|
|
|
class AllocMtpWatcherUpdateRunnable final : public Runnable
|
2014-08-29 00:13:06 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-10-14 01:35:11 +03:00
|
|
|
AllocMtpWatcherUpdateRunnable(MozMtpServer* aMozMtpServer)
|
2014-08-29 00:13:06 +04:00
|
|
|
: mMozMtpServer(aMozMtpServer)
|
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-08-29 00:13:06 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
sMtpWatcherUpdate = new MtpWatcherUpdate(mMozMtpServer);
|
2014-08-29 00:13:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MozMtpServer> mMozMtpServer;
|
2014-08-29 00:13:06 +04:00
|
|
|
};
|
|
|
|
|
2016-04-27 13:05:40 +03:00
|
|
|
class FreeMtpWatcherUpdateRunnable final : public Runnable
|
2014-08-29 00:13:06 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-10-14 01:35:11 +03:00
|
|
|
FreeMtpWatcherUpdateRunnable(MozMtpServer* aMozMtpServer)
|
2014-08-29 00:13:06 +04:00
|
|
|
: mMozMtpServer(aMozMtpServer)
|
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-08-29 00:13:06 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
sMtpWatcherUpdate = nullptr;
|
2014-08-29 00:13:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MozMtpServer> mMozMtpServer;
|
2014-08-29 00:13:06 +04:00
|
|
|
};
|
|
|
|
|
2016-04-27 13:05:40 +03:00
|
|
|
class MtpServerRunnable : public Runnable
|
2014-07-12 01:42:15 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-08-20 00:41:35 +04:00
|
|
|
MtpServerRunnable(int aMtpUsbFd, MozMtpServer* aMozMtpServer)
|
|
|
|
: mMozMtpServer(aMozMtpServer),
|
|
|
|
mMtpUsbFd(aMtpUsbFd)
|
2014-08-20 00:41:35 +04:00
|
|
|
{
|
2014-08-20 00:41:35 +04:00
|
|
|
}
|
2014-08-20 10:49:36 +04:00
|
|
|
|
2014-08-20 00:41:35 +04:00
|
|
|
nsresult Run()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RefCountedMtpServer> server = mMozMtpServer->GetMtpServer();
|
2014-07-12 01:42:15 +04:00
|
|
|
|
2014-08-29 00:13:06 +04:00
|
|
|
DebugOnly<nsresult> rv =
|
2015-10-14 01:35:11 +03:00
|
|
|
NS_DispatchToMainThread(new AllocMtpWatcherUpdateRunnable(mMozMtpServer));
|
2014-08-29 00:13:06 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2014-07-12 01:42:15 +04:00
|
|
|
MTP_LOG("MozMtpServer started");
|
|
|
|
server->run();
|
|
|
|
MTP_LOG("MozMtpServer finished");
|
|
|
|
|
2014-09-27 03:21:58 +04:00
|
|
|
// server->run will have closed the file descriptor.
|
|
|
|
mMtpUsbFd.forget();
|
|
|
|
|
2015-10-14 01:35:11 +03:00
|
|
|
rv = NS_DispatchToMainThread(new FreeMtpWatcherUpdateRunnable(mMozMtpServer));
|
2014-08-29 00:13:06 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2014-07-12 01:42:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MozMtpServer> mMozMtpServer;
|
2014-08-20 00:41:35 +04:00
|
|
|
ScopedClose mMtpUsbFd; // We want to hold this open while the server runs
|
2014-07-12 01:42:15 +04:00
|
|
|
};
|
|
|
|
|
2014-08-20 00:41:35 +04:00
|
|
|
already_AddRefed<RefCountedMtpServer>
|
|
|
|
MozMtpServer::GetMtpServer()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RefCountedMtpServer> server = mMtpServer;
|
2014-08-20 00:41:35 +04:00
|
|
|
return server.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<MozMtpDatabase>
|
|
|
|
MozMtpServer::GetMozMtpDatabase()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MozMtpDatabase> db = mMozMtpDatabase;
|
2014-08-20 00:41:35 +04:00
|
|
|
return db.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-22 04:28:12 +04:00
|
|
|
bool
|
|
|
|
MozMtpServer::Init()
|
2014-07-12 01:42:15 +04:00
|
|
|
{
|
2014-08-20 00:41:35 +04:00
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
|
|
|
|
const char *mtpUsbFilename = "/dev/mtp_usb";
|
2014-10-22 04:28:12 +04:00
|
|
|
mMtpUsbFd = open(mtpUsbFilename, O_RDWR);
|
|
|
|
if (mMtpUsbFd.get() < 0) {
|
2015-01-27 08:54:31 +03:00
|
|
|
MTP_ERR("open of '%s' failed((%s))", mtpUsbFilename, strerror(errno));
|
2014-10-22 04:28:12 +04:00
|
|
|
return false;
|
2014-08-20 00:41:35 +04:00
|
|
|
}
|
2014-10-22 04:28:12 +04:00
|
|
|
MTP_LOG("Opened '%s' fd %d", mtpUsbFilename, mMtpUsbFd.get());
|
2014-08-20 00:41:35 +04:00
|
|
|
|
|
|
|
mMozMtpDatabase = new MozMtpDatabase();
|
2014-10-22 04:28:12 +04:00
|
|
|
mMtpServer = new RefCountedMtpServer(mMtpUsbFd.get(), // fd
|
2014-08-20 00:41:35 +04:00
|
|
|
mMozMtpDatabase.get(), // MtpDatabase
|
|
|
|
false, // ptp?
|
|
|
|
AID_MEDIA_RW, // file group
|
|
|
|
0664, // file permissions
|
|
|
|
0775); // dir permissions
|
2014-10-22 04:28:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-20 00:41:35 +04:00
|
|
|
|
2014-10-22 04:28:12 +04:00
|
|
|
void
|
|
|
|
MozMtpServer::Run()
|
|
|
|
{
|
2014-07-12 01:42:15 +04:00
|
|
|
nsresult rv = NS_NewNamedThread("MtpServer", getter_AddRefs(mServerThread));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mServerThread);
|
2014-10-22 04:28:12 +04:00
|
|
|
mServerThread->Dispatch(new MtpServerRunnable(mMtpUsbFd.forget(), this), NS_DISPATCH_NORMAL);
|
2014-07-12 01:42:15 +04:00
|
|
|
}
|
2014-08-20 00:41:35 +04:00
|
|
|
|
|
|
|
END_MTP_NAMESPACE
|