diff --git a/dom/bluetooth/BluetoothOppManager.cpp b/dom/bluetooth/BluetoothOppManager.cpp index 1aa9f127efb..1fd4315bc42 100644 --- a/dom/bluetooth/BluetoothOppManager.cpp +++ b/dom/bluetooth/BluetoothOppManager.cpp @@ -298,6 +298,8 @@ BluetoothOppManager::ConfirmReceivingFile(bool aConfirm) if (aConfirm) { StartFileTransfer(mConnectedDeviceAddress, true, sFileName, sFileLength, sContentType); + } else { + DeleteReceivedFile(); } if (mPutFinal || !aConfirm) { @@ -348,6 +350,28 @@ BluetoothOppManager::AfterOppDisconnected() mConnectedDeviceAddress.AssignLiteral("00:00:00:00:00:00"); } +void +BluetoothOppManager::DeleteReceivedFile() +{ + nsString path; + path.AssignLiteral(TARGET_FOLDER); + path += sFileName; + + nsCOMPtr f; + nsresult rv = NS_NewLocalFile(path + sFileName, false, getter_AddRefs(f)); + if (NS_FAILED(rv)) { + NS_WARNING("Couldn't find received file, nothing to delete."); + return; + } + + if (mOutputStream) { + mOutputStream->Close(); + mOutputStream = nullptr; + } + + f->Remove(false); +} + // Virtual function of class SocketConsumer void BluetoothOppManager::ReceiveSocketData(UnixSocketRawData* aMessage) @@ -521,10 +545,9 @@ BluetoothOppManager::ReceiveSocketData(UnixSocketRawData* aMessage) pktHeaders.GetContentType(sContentType); pktHeaders.GetLength(&sFileLength); - path += sFileName; - nsCOMPtr f; - nsresult rv = NS_NewLocalFile(path, false, getter_AddRefs(f)); + nsresult rv; + rv = NS_NewLocalFile(path + sFileName, false, getter_AddRefs(f)); if (NS_FAILED(rv)) { NS_WARNING("Couldn't new a local file"); } @@ -534,6 +557,13 @@ BluetoothOppManager::ReceiveSocketData(UnixSocketRawData* aMessage) NS_WARNING("Couldn't create the file"); } + /* + * The function CreateUnique() may create a file with a different file + * name from the original sFileName. Therefore we have to retrieve + * the file name again. + */ + f->GetLeafName(sFileName); + NS_NewLocalFileOutputStream(getter_AddRefs(mOutputStream), f); if (!mOutputStream) { NS_WARNING("Couldn't new an output stream"); @@ -607,6 +637,7 @@ BluetoothOppManager::ReceiveSocketData(UnixSocketRawData* aMessage) mReceiving = false; FileTransferComplete(mConnectedDeviceAddress, false, true, sFileName, sSentFileLength, sContentType); + DeleteReceivedFile(); } else if (mPutFinal) { mReceiving = false; FileTransferComplete(mConnectedDeviceAddress, true, true, diff --git a/dom/bluetooth/BluetoothOppManager.h b/dom/bluetooth/BluetoothOppManager.h index 779b617cfcf..c03a9580e76 100644 --- a/dom/bluetooth/BluetoothOppManager.h +++ b/dom/bluetooth/BluetoothOppManager.h @@ -83,6 +83,7 @@ private: const nsString& aFileName, uint32_t aFileLength, const nsString& aContentType); + void DeleteReceivedFile(); void ReplyToConnect(); void ReplyToDisconnect(); void ReplyToPut(bool aFinal, bool aContinue);