Bug 797345 - send a system message to notify that 'file transfer completed', r=qdot
authorEric Chou <echou@mozilla.com>
Wed, 03 Oct 2012 17:06:40 +0800
changeset 115512 6cb9dded5fed50f0e2856402fc42f56fb09bfe6e
parent 115511 5215c9e775538a2e9232d8e981c07b7c968608d7
child 115513 6134705b1a11fad73266562e0bbdc3cae743fe62
push id1708
push userakeybl@mozilla.com
push dateMon, 19 Nov 2012 21:10:21 +0000
treeherdermozilla-beta@27b14fe50103 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot
bugs797345
milestone18.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 797345 - send a system message to notify that 'file transfer completed', r=qdot
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothHfpManager.h
dom/bluetooth/BluetoothOppManager.cpp
dom/bluetooth/BluetoothOppManager.h
dom/bluetooth/BluetoothUtils.cpp
dom/bluetooth/BluetoothUtils.h
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -7,27 +7,25 @@
 #include "base/basictypes.h" 
 
 #include "BluetoothHfpManager.h"
 
 #include "BluetoothReplyRunnable.h"
 #include "BluetoothScoManager.h"
 #include "BluetoothService.h"
 #include "BluetoothServiceUuid.h"
+#include "BluetoothUtils.h"
 
 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
 #include "mozilla/Services.h"
 #include "mozilla/StaticPtr.h"
 #include "nsContentUtils.h"
 #include "nsIAudioManager.h"
 #include "nsIObserverService.h"
 #include "nsIRadioInterfaceLayer.h"
-#include "nsISystemMessagesInternal.h"
-#include "BluetoothUtils.h"
-
 #include "nsVariant.h"
 
 #include <unistd.h> /* usleep() */
 
 #define MOZSETTINGS_CHANGED_ID "mozsettings-changed"
 #define BLUETOOTH_SCO_STATUS_CHANGED "bluetooth-sco-status-changed"
 #define AUDIO_VOLUME_MASTER "audio.volume.master"
 
@@ -207,49 +205,16 @@ BluetoothHfpManager::Get()
     manager->Cleanup();
     return nullptr;
   }
 
   gBluetoothHfpManager = manager;
   return gBluetoothHfpManager;
 }
 
-bool
-BluetoothHfpManager::BroadcastSystemMessage(const nsAString& aType,
-                                            const InfallibleTArray<BluetoothNamedValue>& aData)
-{
-  JSContext* cx = nsContentUtils::GetSafeJSContext();
-  NS_ASSERTION(!::JS_IsExceptionPending(cx),
-               "Shouldn't get here when an exception is pending!");
-
-  JSAutoRequest jsar(cx);
-  JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
-  if (!obj) {
-    NS_WARNING("Failed to new JSObject for system message!");
-    return false;
-  }
-
-  if (!SetJsObject(cx, obj, aData)) {
-    NS_WARNING("Failed to set properties of system message!");
-    return false;
-  }
-
-  nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
-    do_GetService("@mozilla.org/system-message-internal;1");
-
-  if (!systemMessenger) {
-    NS_WARNING("Failed to get SystemMessenger service!");
-    return false;
-  }
-
-  systemMessenger->BroadcastMessage(aType, OBJECT_TO_JSVAL(obj));
-
-  return true;
-}
-
 void
 BluetoothHfpManager::NotifySettings(const bool aConnected)
 {
   nsString type, name;
   BluetoothValue v;
   InfallibleTArray<BluetoothNamedValue> parameters;
   type.AssignLiteral("bluetooth-hfp-status-changed");
 
--- a/dom/bluetooth/BluetoothHfpManager.h
+++ b/dom/bluetooth/BluetoothHfpManager.h
@@ -10,17 +10,16 @@
 #include "BluetoothCommon.h"
 #include "BluetoothRilListener.h"
 #include "mozilla/ipc/UnixSocket.h"
 #include "nsIObserver.h"
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 class BluetoothReplyRunnable;
-class BluetoothNamedValue;
 
 class BluetoothHfpManager : public mozilla::ipc::UnixSocketConsumer
                           , public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
@@ -36,18 +35,16 @@ public:
   bool Listen();
 
 private:
   BluetoothHfpManager();
   bool Init();
   void Cleanup();
   nsresult HandleVolumeChanged(const nsAString& aData);
   nsresult HandleShutdown();
-  bool BroadcastSystemMessage(const nsAString& aType,
-                              const InfallibleTArray<BluetoothNamedValue>& aData);
   void NotifyDialer(const nsAString& aCommand);
   void NotifySettings(const bool aConnected);
 
   int mCurrentVgs;
   int mCurrentCallIndex;
   int mCurrentCallState;
   nsAutoPtr<BluetoothRilListener> mListener;
   nsString mDevicePath;
--- a/dom/bluetooth/BluetoothOppManager.cpp
+++ b/dom/bluetooth/BluetoothOppManager.cpp
@@ -5,27 +5,30 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "base/basictypes.h"
 #include "BluetoothOppManager.h"
 
 #include "BluetoothReplyRunnable.h"
 #include "BluetoothService.h"
 #include "BluetoothServiceUuid.h"
+#include "BluetoothUtils.h"
 #include "ObexBase.h"
 
+#include "mozilla/dom/bluetooth/BluetoothTypes.h"
 #include "mozilla/RefPtr.h"
 #include "nsIInputStream.h"
 
 USING_BLUETOOTH_NAMESPACE
 using namespace mozilla::ipc;
 
 static mozilla::RefPtr<BluetoothOppManager> sInstance;
 static nsCOMPtr<nsIInputStream> stream = nullptr;
-static uint64_t sSentFileSize = 0;
+static uint32_t sSentFileSize = 0;
+static nsString sFileName;
 
 class ReadFileTask : public nsRunnable
 {
 public:
   ReadFileTask(nsIDOMBlob* aBlob) : mBlob(aBlob)
   {
     MOZ_ASSERT(NS_IsMainThread());
   }
@@ -202,23 +205,22 @@ BluetoothOppManager::ReceiveSocketData(U
 
       if (mBlob) {
         /*
          * Before sending content, we have to send a header including
          * information such as file name, file length and content type.
          */
         nsresult rv;
         nsCOMPtr<nsIDOMFile> file = do_QueryInterface(mBlob);
-        nsString fileName;
         if (file) {
-          rv = file->GetName(fileName);
+          rv = file->GetName(sFileName);
         }
 
-        if (!file || fileName.IsEmpty()) {
-          fileName.AssignLiteral("Unknown");
+        if (!file || sFileName.IsEmpty()) {
+          sFileName.AssignLiteral("Unknown");
         }
 
         uint64_t fileSize;
         rv = mBlob->GetSize(&fileSize);
         if (NS_FAILED(rv)) {
           NS_WARNING("Can't get file size");
           return;
         }
@@ -226,17 +228,17 @@ BluetoothOppManager::ReceiveSocketData(U
         if (NS_FAILED(NS_NewThread(getter_AddRefs(mReadFileThread)))) {
           NS_WARNING("Can't create thread");
           SendDisconnectRequest();
           return;
         }
 
         sSentFileSize = 0;
         mAbortFlag = false;
-        sInstance->SendPutHeaderRequest(fileName, fileSize);
+        sInstance->SendPutHeaderRequest(sFileName, fileSize);
       }
     }
   } else if (mLastCommand == ObexRequestCode::Disconnect) {
     if (responseCode != ObexResponseCode::Success) {
       // FIXME: Needs error handling here
       NS_WARNING("[OPP] Disconnect failed");
     } else {
       mConnected = false;
@@ -258,23 +260,25 @@ BluetoothOppManager::ReceiveSocketData(U
         }
       }
     }
   } else if (mLastCommand == ObexRequestCode::PutFinal) {
     if (responseCode != ObexResponseCode::Success) {
       // FIXME: Needs error handling here
       NS_WARNING("[OPP] PutFinal failed");
     } else {
+      FileTransferComplete(true, false, sFileName, sSentFileSize);
       SendDisconnectRequest();
     }
   } else if (mLastCommand == ObexRequestCode::Abort) {
     if (responseCode != ObexResponseCode::Success) {
       NS_WARNING("[OPP] Abort failed");
     }
 
+    FileTransferComplete(false, false, sFileName, sSentFileSize);
     SendDisconnectRequest();
   }
 }
 
 void
 BluetoothOppManager::SendConnectRequest()
 {
   if (mConnected) return;
@@ -392,8 +396,41 @@ BluetoothOppManager::SendAbortRequest()
 
   SetObexPacketInfo(req, ObexRequestCode::Abort, index);
   mLastCommand = ObexRequestCode::Abort;
 
   UnixSocketRawData* s = new UnixSocketRawData(index);
   memcpy(s->mData, req, s->mSize);
   SendSocketData(s);
 }
+
+void
+BluetoothOppManager::FileTransferComplete(bool aSuccess,
+                                          bool aReceived,
+                                          const nsString& aFileName,
+                                          uint32_t aFileLength)
+{
+  nsString type, name;
+  BluetoothValue v;
+  InfallibleTArray<BluetoothNamedValue> parameters;
+  type.AssignLiteral("bluetooth-opp-transfer-complete");
+
+  name.AssignLiteral("success");
+  v = aSuccess;
+  parameters.AppendElement(BluetoothNamedValue(name, v));
+
+  name.AssignLiteral("received");
+  v = aReceived;
+  parameters.AppendElement(BluetoothNamedValue(name, v));
+
+  name.AssignLiteral("filename");
+  v = aFileName;
+  parameters.AppendElement(BluetoothNamedValue(name, v));
+
+  name.AssignLiteral("filelength");
+  v = aFileLength;
+  parameters.AppendElement(BluetoothNamedValue(name, v));
+
+  if (!BroadcastSystemMessage(type, parameters)) {
+    NS_WARNING("Failed to broadcast [bluetooth-opp-transfer-complete]");
+    return;
+  }
+}
--- a/dom/bluetooth/BluetoothOppManager.h
+++ b/dom/bluetooth/BluetoothOppManager.h
@@ -56,16 +56,19 @@ public:
   void SendPutHeaderRequest(const nsAString& aFileName, int aFileSize);
   void SendPutRequest(uint8_t* aFileBody, int aFileBodyLength,
                       bool aFinal);
   void SendDisconnectRequest();
   void SendAbortRequest();
 
 private:
   BluetoothOppManager();
+  void UpdateProgress(uint32_t aTotalBytes, uint32_t aSentBytes);
+  void FileTransferComplete(bool aSuccess, bool aReceived,
+                            const nsString& aFileName, uint32_t aFileLength);
 
   bool mConnected;
   int mConnectionId;
   int mLastCommand;
   uint8_t mRemoteObexVersion;
   uint8_t mRemoteConnectionFlags;
   int mRemoteMaxPacketLength;
   bool mAbortFlag;
--- a/dom/bluetooth/BluetoothUtils.cpp
+++ b/dom/bluetooth/BluetoothUtils.cpp
@@ -1,24 +1,27 @@
 /* -*- 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 "base/basictypes.h"
 
+#include "BluetoothDevice.h"
 #include "BluetoothUtils.h"
+#include "jsapi.h"
+#include "mozilla/Scoped.h"
+#include "mozilla/dom/bluetooth/BluetoothTypes.h"
 #include "nsContentUtils.h"
-#include "BluetoothDevice.h"
-#include "jsapi.h"
+#include "nsISystemMessagesInternal.h"
 #include "nsTArray.h"
 #include "nsString.h"
-#include "mozilla/Scoped.h"
-#include "mozilla/dom/bluetooth/BluetoothTypes.h"
+
+USING_BLUETOOTH_NAMESPACE
 
 nsresult
 mozilla::dom::bluetooth::StringArrayToJSArray(JSContext* aCx, JSObject* aGlobal,
                                               const nsTArray<nsString>& aSourceArray,
                                               JSObject** aResultArray)
 {
   NS_ASSERTION(aCx, "Null context!");
   NS_ASSERTION(aGlobal, "Null global!");
@@ -161,8 +164,42 @@ mozilla::dom::bluetooth::GetAddressFromO
   MOZ_ASSERT(addressHead + BLUETOOTH_ADDRESS_LENGTH == address.Length());
 
   address.Cut(0, addressHead);
   address.ReplaceChar('_', ':');
 
   return address;
 }
 
+bool
+mozilla::dom::bluetooth::BroadcastSystemMessage(
+  const nsAString& aType,
+  const InfallibleTArray<BluetoothNamedValue>& aData)
+{
+  JSContext* cx = nsContentUtils::GetSafeJSContext();
+  NS_ASSERTION(!::JS_IsExceptionPending(cx),
+      "Shouldn't get here when an exception is pending!");
+
+  JSAutoRequest jsar(cx);
+  JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
+  if (!obj) {
+    NS_WARNING("Failed to new JSObject for system message!");
+    return false;
+  }
+
+  if (!SetJsObject(cx, obj, aData)) {
+    NS_WARNING("Failed to set properties of system message!");
+    return false;
+  }
+
+  nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
+    do_GetService("@mozilla.org/system-message-internal;1");
+
+  if (!systemMessenger) {
+    NS_WARNING("Failed to get SystemMessenger service!");
+    return false;
+  }
+
+  systemMessenger->BroadcastMessage(aType, OBJECT_TO_JSVAL(obj));
+
+  return true;
+}
+
--- a/dom/bluetooth/BluetoothUtils.h
+++ b/dom/bluetooth/BluetoothUtils.h
@@ -34,11 +34,15 @@ SetJsObject(JSContext* aContext,
 
 nsString
 GetObjectPathFromAddress(const nsAString& aAdapterPath,
                          const nsAString& aDeviceAddress);
 
 nsString
 GetAddressFromObjectPath(const nsAString& aObjectPath);
 
+bool
+BroadcastSystemMessage(const nsAString& aType,
+                       const InfallibleTArray<BluetoothNamedValue>& aData);
+
 END_BLUETOOTH_NAMESPACE
 
 #endif