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-07-12 01:42:15 +04:00
|
|
|
#include "mozilla/FileUtils.h"
|
|
|
|
#include "mozilla/Scoped.h"
|
2014-08-20 00:41:35 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsAutoPtr.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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
class MtpServerRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
nsRefPtr<RefCountedMtpServer> server = mMozMtpServer->GetMtpServer();
|
2014-07-12 01:42:15 +04:00
|
|
|
|
|
|
|
MTP_LOG("MozMtpServer started");
|
|
|
|
server->run();
|
|
|
|
MTP_LOG("MozMtpServer finished");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-08-20 00:41:35 +04:00
|
|
|
nsRefPtr<MozMtpServer> mMozMtpServer;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
nsRefPtr<RefCountedMtpServer> server = mMtpServer;
|
|
|
|
return server.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<MozMtpDatabase>
|
|
|
|
MozMtpServer::GetMozMtpDatabase()
|
|
|
|
{
|
|
|
|
nsRefPtr<MozMtpDatabase> db = mMozMtpDatabase;
|
|
|
|
return db.forget();
|
|
|
|
}
|
|
|
|
|
2014-07-12 01:42:15 +04:00
|
|
|
void
|
|
|
|
MozMtpServer::Run()
|
|
|
|
{
|
2014-08-20 00:41:35 +04:00
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
|
|
|
|
const char *mtpUsbFilename = "/dev/mtp_usb";
|
|
|
|
ScopedClose mtpUsbFd(open(mtpUsbFilename, O_RDWR));
|
|
|
|
if (mtpUsbFd.get() < 0) {
|
|
|
|
MTP_ERR("open of '%s' failed", mtpUsbFilename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MTP_LOG("Opened '%s' fd %d", mtpUsbFilename, mtpUsbFd.get());
|
|
|
|
|
|
|
|
mMozMtpDatabase = new MozMtpDatabase();
|
|
|
|
mMtpServer = new RefCountedMtpServer(mtpUsbFd.get(), // fd
|
|
|
|
mMozMtpDatabase.get(), // MtpDatabase
|
|
|
|
false, // ptp?
|
|
|
|
AID_MEDIA_RW, // file group
|
|
|
|
0664, // file permissions
|
|
|
|
0775); // dir permissions
|
|
|
|
|
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-08-20 00:41:35 +04:00
|
|
|
mServerThread->Dispatch(new MtpServerRunnable(mtpUsbFd.forget(), this), NS_DISPATCH_NORMAL);
|
2014-07-12 01:42:15 +04:00
|
|
|
}
|
2014-08-20 00:41:35 +04:00
|
|
|
|
|
|
|
END_MTP_NAMESPACE
|