Bug 869259 - Get a mount lock to prevent the sdcard from being shared with the PC, r=echou
authorShawn Huang <shuang@mozilla.com>
Fri, 07 Jun 2013 15:10:24 +0800
changeset 145751 740e7a18cd39046590b52b762bd5d08d84c206e3
parent 145750 244f3242f7de0b1bdfdac39f763b79046a49de9d
child 145752 2fee4d3f494166b4e0668e46418e3696304c77ae
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechou
bugs869259
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 869259 - Get a mount lock to prevent the sdcard from being shared with the PC, r=echou
dom/bluetooth/BluetoothOppManager.cpp
dom/bluetooth/BluetoothOppManager.h
--- a/dom/bluetooth/BluetoothOppManager.cpp
+++ b/dom/bluetooth/BluetoothOppManager.cpp
@@ -22,16 +22,17 @@
 #include "nsCExternalHandlerService.h"
 #include "nsIObserver.h"
 #include "nsIObserverService.h"
 #include "nsIDOMFile.h"
 #include "nsIFile.h"
 #include "nsIInputStream.h"
 #include "nsIMIMEService.h"
 #include "nsIOutputStream.h"
+#include "nsIVolumeService.h"
 #include "nsNetUtil.h"
 
 #define TARGET_SUBDIR "Download/Bluetooth/"
 
 USING_BLUETOOTH_NAMESPACE
 using namespace mozilla;
 using namespace mozilla::ipc;
 
@@ -446,16 +447,25 @@ void
 BluetoothOppManager::AfterOppConnected()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   mConnected = true;
   mAbortFlag = false;
   mWaitingForConfirmationFlag = true;
   AfterFirstPut();
+  // Get a mount lock to prevent the sdcard from being shared with
+  // the PC while we're doing a OPP file transfer. After OPP transcation
+  // were done, the mount lock will be freed.
+  if (!AcquireSdcardMountLock()) {
+    // If we fail to get a mount lock, abort this transaction
+    // Directly sending disconnect-request is better than abort-request
+    NS_WARNING("BluetoothOPPManager couldn't get a mount lock!");
+    Disconnect();
+  }
 }
 
 void
 BluetoothOppManager::AfterOppDisconnected()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   mConnected = false;
@@ -478,16 +488,21 @@ BluetoothOppManager::AfterOppDisconnecte
     mOutputStream->Close();
     mOutputStream = nullptr;
   }
 
   if (mReadFileThread) {
     mReadFileThread->Shutdown();
     mReadFileThread = nullptr;
   }
+  // Release the Mount lock if file transfer completed
+  if (mMountLock) {
+    // The mount lock will be implicitly unlocked
+    mMountLock = nullptr;
+  }
 }
 
 void
 BluetoothOppManager::DeleteReceivedFile()
 {
   if (mOutputStream) {
     mOutputStream->Close();
     mOutputStream = nullptr;
@@ -1488,8 +1503,20 @@ BluetoothOppManager::OnUpdateSdpRecords(
     DispatchBluetoothReply(mRunnable, BluetoothValue(),
                            NS_LITERAL_STRING(ERR_SERVICE_CHANNEL_NOT_FOUND));
     mRunnable = nullptr;
     mSocket = nullptr;
     Listen();
   }
 }
 
+bool
+BluetoothOppManager::AcquireSdcardMountLock()
+{
+  nsCOMPtr<nsIVolumeService> volumeSrv =
+    do_GetService(NS_VOLUMESERVICE_CONTRACTID);
+  NS_ENSURE_TRUE(volumeSrv, false);
+  nsresult rv;
+  rv = volumeSrv->CreateMountLock(NS_LITERAL_STRING("sdcard"),
+                                  getter_AddRefs(mMountLock));
+  NS_ENSURE_SUCCESS(rv, false);
+  return true;
+}
--- a/dom/bluetooth/BluetoothOppManager.h
+++ b/dom/bluetooth/BluetoothOppManager.h
@@ -12,16 +12,17 @@
 #include "BluetoothSocketObserver.h"
 #include "DeviceStorage.h"
 #include "mozilla/dom/ipc/Blob.h"
 #include "mozilla/ipc/UnixSocket.h"
 #include "nsCOMArray.h"
 
 class nsIOutputStream;
 class nsIInputStream;
+class nsIVolumeMountLock;
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 class BluetoothReplyRunnable;
 class BluetoothSocket;
 class ObexHeaderSet;
 
 class BluetoothOppManager : public BluetoothSocketObserver
@@ -106,17 +107,17 @@ private:
   void AfterOppConnected();
   void AfterFirstPut();
   void AfterOppDisconnected();
   void ValidateFileName();
   bool IsReservedChar(PRUnichar c);
   void ClearQueue();
   void RetrieveSentFileName();
   void NotifyAboutFileChange();
-
+  bool AcquireSdcardMountLock();
   /**
    * OBEX session status.
    * Set when OBEX session is established.
    */
   bool mConnected;
   nsString mConnectedDeviceAddress;
 
   /**
@@ -192,17 +193,17 @@ private:
   /**
    * A seperate member thread is required because our read calls can block
    * execution, which is not allowed to happen on the IOThread.
    * 
    */
   nsCOMPtr<nsIThread> mReadFileThread;
   nsCOMPtr<nsIOutputStream> mOutputStream;
   nsCOMPtr<nsIInputStream> mInputStream;
-
+  nsCOMPtr<nsIVolumeMountLock> mMountLock;
   nsRefPtr<BluetoothReplyRunnable> mRunnable;
   nsRefPtr<DeviceStorageFile> mDsFile;
 
   // If a connection has been established, mSocket will be the socket
   // communicating with the remote socket. We maintain the invariant that if
   // mSocket is non-null, mRfcommSocket and mL2capSocket must be null (and vice
   // versa).
   nsRefPtr<BluetoothSocket> mSocket;