Bug 1072576 - Register storages with MTP Server before starting it. r=echou

This commit is contained in:
Dave Hylands 2014-10-21 17:28:12 -07:00
Родитель 8b040fd4c3
Коммит 4af2b23a95
6 изменённых файлов: 62 добавлений и 18 удалений

Просмотреть файл

@ -80,10 +80,13 @@ USING_MTP_NAMESPACE
#define USE_DEBUG 0
#undef LOG
#undef LOGW
#undef ERR
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "AutoMounter", ## args)
#define LOGW(args...) __android_log_print(ANDROID_LOG_WARN, "AutoMounter", ## args)
#define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, "AutoMounter", ## args)
#undef DBG
#if USE_DEBUG
#define DBG(args...) __android_log_print(ANDROID_LOG_DEBUG, "AutoMounter" , ## args)
#else
@ -270,7 +273,7 @@ public:
void ConfigureUsbFunction(const char* aUsbFunc);
void StartMtpServer();
bool StartMtpServer();
void StopMtpServer();
void StartUmsSharing();
@ -582,16 +585,33 @@ SetUsbFunction(const char* aUsbFunc)
property_set(SYS_USB_CONFIG, newSysUsbConfig);
}
void
bool
AutoMounter::StartMtpServer()
{
if (sMozMtpServer) {
// Mtp Server is already running - nothing to do
return;
return true;
}
LOG("Starting MtpServer");
// For debugging, Change the #if 0 to #if 1, and then attach gdb during
// the 5 second interval below. Otherwise, configuring MTP will cause adb
// (and thus gdb) to get bounced.
#if 0
LOG("Sleeping");
PRTime now = PR_Now();
PRTime stopTime = now + 5000000;
while (PR_Now() < stopTime) {
LOG("Sleeping...");
sleep(1);
}
LOG("Sleep done");
#endif
sMozMtpServer = new MozMtpServer();
sMozMtpServer->Run();
if (!sMozMtpServer->Init()) {
return false;
}
VolumeArray::index_type volIndex;
VolumeArray::size_type numVolumes = VolumeManager::NumVolumes();
@ -600,6 +620,9 @@ AutoMounter::StartMtpServer()
nsRefPtr<MozMtpStorage> storage = new MozMtpStorage(vol, sMozMtpServer);
mMozMtpStorage.AppendElement(storage);
}
sMozMtpServer->Run();
return true;
}
void
@ -736,8 +759,13 @@ AutoMounter::UpdateState()
// and start the MTP server. This particular codepath will not
// normally be taken, but it could happen if you stop and restart
// b2g while sys.usb.config is set to enable mtp.
StartMtpServer();
SetState(STATE_MTP_STARTED);
if (StartMtpServer()) {
SetState(STATE_MTP_STARTED);
} else {
// Unable to start MTP. Go back to UMS.
SetUsbFunction(USB_FUNC_UMS);
SetState(STATE_UMS_CONFIGURING);
}
} else {
// We need to configure USB to use mtp. Wait for it to be configured
// before we start the MTP server.
@ -763,8 +791,13 @@ AutoMounter::UpdateState()
if (mtpEnabled && mtpConfigured) {
// The USB layer has been configured. Now we can go ahead and start
// the MTP server.
StartMtpServer();
SetState(STATE_MTP_STARTED);
if (StartMtpServer()) {
SetState(STATE_MTP_STARTED);
} else {
// Unable to start MTP. Go back to UMS.
SetUsbFunction(USB_FUNC_UMS);
SetState(STATE_UMS_CONFIGURING);
}
break;
}
if (rndisConfigured) {

Просмотреть файл

@ -24,6 +24,7 @@
#include "mozilla/dom/SettingChangeNotificationBinding.h"
#undef LOG
#undef ERR
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "AutoMounterSetting" , ## args)
#define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, "AutoMounterSetting" , ## args)

Просмотреть файл

@ -225,33 +225,38 @@ MozMtpServer::GetMozMtpDatabase()
return db.forget();
}
void
MozMtpServer::Run()
bool
MozMtpServer::Init()
{
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
const char *mtpUsbFilename = "/dev/mtp_usb";
ScopedClose mtpUsbFd(open(mtpUsbFilename, O_RDWR));
if (mtpUsbFd.get() < 0) {
mMtpUsbFd = open(mtpUsbFilename, O_RDWR);
if (mMtpUsbFd.get() < 0) {
MTP_ERR("open of '%s' failed", mtpUsbFilename);
return;
return false;
}
MTP_LOG("Opened '%s' fd %d", mtpUsbFilename, mtpUsbFd.get());
MTP_LOG("Opened '%s' fd %d", mtpUsbFilename, mMtpUsbFd.get());
mMozMtpDatabase = new MozMtpDatabase();
mMtpServer = new RefCountedMtpServer(mtpUsbFd.get(), // fd
mMtpServer = new RefCountedMtpServer(mMtpUsbFd.get(), // fd
mMozMtpDatabase.get(), // MtpDatabase
false, // ptp?
AID_MEDIA_RW, // file group
0664, // file permissions
0775); // dir permissions
return true;
}
void
MozMtpServer::Run()
{
nsresult rv = NS_NewNamedThread("MtpServer", getter_AddRefs(mServerThread));
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
MOZ_ASSERT(mServerThread);
mServerThread->Dispatch(new MtpServerRunnable(mtpUsbFd.forget(), this), NS_DISPATCH_NORMAL);
mServerThread->Dispatch(new MtpServerRunnable(mMtpUsbFd.forget(), this), NS_DISPATCH_NORMAL);
}
END_MTP_NAMESPACE

Просмотреть файл

@ -40,10 +40,9 @@ class MozMtpServer
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MozMtpServer)
bool Init();
void Run();
// void UpdateStorage(android::MtpStorageID id, Volume *vol);
already_AddRefed<RefCountedMtpServer> GetMtpServer();
already_AddRefed<MozMtpDatabase> GetMozMtpDatabase();
@ -51,6 +50,7 @@ private:
nsRefPtr<RefCountedMtpServer> mMtpServer;
nsRefPtr<MozMtpDatabase> mMozMtpDatabase;
nsCOMPtr<nsIThread> mServerThread;
ScopedClose mMtpUsbFd;
};
END_MTP_NAMESPACE

Просмотреть файл

@ -13,10 +13,13 @@
#define USE_DEBUG 0
#undef LOG
#undef LOGW
#undef ERR
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "OpenFileFinder", ## args)
#define LOGW(args...) __android_log_print(ANDROID_LOG_WARN, "OpenFileFinder", ## args)
#define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, "OpenFileFinder", ## args)
#undef DBG
#if USE_DEBUG
#define DBG(args...) __android_log_print(ANDROID_LOG_DEBUG, "OpenFileFinder" , ## args)
#else

Просмотреть файл

@ -12,9 +12,11 @@
#endif
#undef LOG
#undef ERR
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, VOLUME_MANAGER_LOG_TAG, ## args)
#define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, VOLUME_MANAGER_LOG_TAG, ## args)
#undef DBG
#if USE_DEBUG
#define DBG(args...) __android_log_print(ANDROID_LOG_DEBUG, VOLUME_MANAGER_LOG_TAG, ## args)
#else