Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_dom_bluetooth_bluetoothoppmanager_h__ |
michael@0 | 8 | #define mozilla_dom_bluetooth_bluetoothoppmanager_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include "BluetoothCommon.h" |
michael@0 | 11 | #include "BluetoothProfileManagerBase.h" |
michael@0 | 12 | #include "BluetoothSocketObserver.h" |
michael@0 | 13 | #include "DeviceStorage.h" |
michael@0 | 14 | #include "mozilla/dom/ipc/Blob.h" |
michael@0 | 15 | #include "mozilla/ipc/UnixSocket.h" |
michael@0 | 16 | #include "nsCOMArray.h" |
michael@0 | 17 | |
michael@0 | 18 | class nsIOutputStream; |
michael@0 | 19 | class nsIInputStream; |
michael@0 | 20 | class nsIVolumeMountLock; |
michael@0 | 21 | |
michael@0 | 22 | BEGIN_BLUETOOTH_NAMESPACE |
michael@0 | 23 | |
michael@0 | 24 | class BluetoothSocket; |
michael@0 | 25 | class ObexHeaderSet; |
michael@0 | 26 | class SendFileBatch; |
michael@0 | 27 | |
michael@0 | 28 | class BluetoothOppManager : public BluetoothSocketObserver |
michael@0 | 29 | , public BluetoothProfileManagerBase |
michael@0 | 30 | { |
michael@0 | 31 | public: |
michael@0 | 32 | BT_DECL_PROFILE_MGR_BASE |
michael@0 | 33 | virtual void GetName(nsACString& aName) |
michael@0 | 34 | { |
michael@0 | 35 | aName.AssignLiteral("OPP"); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | static const int MAX_PACKET_LENGTH = 0xFFFE; |
michael@0 | 39 | |
michael@0 | 40 | virtual ~BluetoothOppManager(); |
michael@0 | 41 | static BluetoothOppManager* Get(); |
michael@0 | 42 | void ClientDataHandler(mozilla::ipc::UnixSocketRawData* aMessage); |
michael@0 | 43 | void ServerDataHandler(mozilla::ipc::UnixSocketRawData* aMessage); |
michael@0 | 44 | |
michael@0 | 45 | bool Listen(); |
michael@0 | 46 | |
michael@0 | 47 | bool SendFile(const nsAString& aDeviceAddress, BlobParent* aActor); |
michael@0 | 48 | bool SendFile(const nsAString& aDeviceAddress, nsIDOMBlob* aBlob); |
michael@0 | 49 | bool StopSendingFile(); |
michael@0 | 50 | bool ConfirmReceivingFile(bool aConfirm); |
michael@0 | 51 | |
michael@0 | 52 | void SendConnectRequest(); |
michael@0 | 53 | void SendPutHeaderRequest(const nsAString& aFileName, int aFileSize); |
michael@0 | 54 | void SendPutRequest(uint8_t* aFileBody, int aFileBodyLength); |
michael@0 | 55 | void SendPutFinalRequest(); |
michael@0 | 56 | void SendDisconnectRequest(); |
michael@0 | 57 | |
michael@0 | 58 | void ExtractPacketHeaders(const ObexHeaderSet& aHeader); |
michael@0 | 59 | bool ExtractBlobHeaders(); |
michael@0 | 60 | void CheckPutFinal(uint32_t aNumRead); |
michael@0 | 61 | |
michael@0 | 62 | // The following functions are inherited from BluetoothSocketObserver |
michael@0 | 63 | void ReceiveSocketData( |
michael@0 | 64 | BluetoothSocket* aSocket, |
michael@0 | 65 | nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE; |
michael@0 | 66 | virtual void OnSocketConnectSuccess(BluetoothSocket* aSocket) MOZ_OVERRIDE; |
michael@0 | 67 | virtual void OnSocketConnectError(BluetoothSocket* aSocket) MOZ_OVERRIDE; |
michael@0 | 68 | virtual void OnSocketDisconnect(BluetoothSocket* aSocket) MOZ_OVERRIDE; |
michael@0 | 69 | |
michael@0 | 70 | private: |
michael@0 | 71 | BluetoothOppManager(); |
michael@0 | 72 | bool Init(); |
michael@0 | 73 | void HandleShutdown(); |
michael@0 | 74 | |
michael@0 | 75 | void StartFileTransfer(); |
michael@0 | 76 | void StartSendingNextFile(); |
michael@0 | 77 | void FileTransferComplete(); |
michael@0 | 78 | void UpdateProgress(); |
michael@0 | 79 | void ReceivingFileConfirmation(); |
michael@0 | 80 | bool CreateFile(); |
michael@0 | 81 | bool WriteToFile(const uint8_t* aData, int aDataLength); |
michael@0 | 82 | void DeleteReceivedFile(); |
michael@0 | 83 | void ReplyToConnect(); |
michael@0 | 84 | void ReplyToDisconnectOrAbort(); |
michael@0 | 85 | void ReplyToPut(bool aFinal, bool aContinue); |
michael@0 | 86 | void ReplyError(uint8_t aError); |
michael@0 | 87 | void AfterOppConnected(); |
michael@0 | 88 | void AfterFirstPut(); |
michael@0 | 89 | void AfterOppDisconnected(); |
michael@0 | 90 | void ValidateFileName(); |
michael@0 | 91 | bool IsReservedChar(char16_t c); |
michael@0 | 92 | void ClearQueue(); |
michael@0 | 93 | void RetrieveSentFileName(); |
michael@0 | 94 | void NotifyAboutFileChange(); |
michael@0 | 95 | bool AcquireSdcardMountLock(); |
michael@0 | 96 | void SendObexData(uint8_t* aData, uint8_t aOpcode, int aSize); |
michael@0 | 97 | void AppendBlobToSend(const nsAString& aDeviceAddress, nsIDOMBlob* aBlob); |
michael@0 | 98 | void DiscardBlobsToSend(); |
michael@0 | 99 | bool ProcessNextBatch(); |
michael@0 | 100 | void ConnectInternal(const nsAString& aDeviceAddress); |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Usually we won't get a full PUT packet in one operation, which means that |
michael@0 | 104 | * a packet may be devided into several parts and BluetoothOppManager should |
michael@0 | 105 | * be in charge of assembling. |
michael@0 | 106 | * |
michael@0 | 107 | * @return true if a packet has been fully received. |
michael@0 | 108 | * false if the received length exceeds/not reaches the expected |
michael@0 | 109 | * length. |
michael@0 | 110 | */ |
michael@0 | 111 | bool ComposePacket(uint8_t aOpCode, |
michael@0 | 112 | mozilla::ipc::UnixSocketRawData* aMessage); |
michael@0 | 113 | |
michael@0 | 114 | /** |
michael@0 | 115 | * OBEX session status. |
michael@0 | 116 | * Set when OBEX session is established. |
michael@0 | 117 | */ |
michael@0 | 118 | bool mConnected; |
michael@0 | 119 | nsString mConnectedDeviceAddress; |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Remote information |
michael@0 | 123 | */ |
michael@0 | 124 | uint8_t mRemoteObexVersion; |
michael@0 | 125 | uint8_t mRemoteConnectionFlags; |
michael@0 | 126 | int mRemoteMaxPacketLength; |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * For sending files, we decide our next action based on current command and |
michael@0 | 130 | * previous one. |
michael@0 | 131 | * For receiving files, we don't need previous command and it is set to 0 |
michael@0 | 132 | * as a default value. |
michael@0 | 133 | */ |
michael@0 | 134 | int mLastCommand; |
michael@0 | 135 | |
michael@0 | 136 | int mPacketLength; |
michael@0 | 137 | int mPutPacketReceivedLength; |
michael@0 | 138 | int mBodySegmentLength; |
michael@0 | 139 | int mUpdateProgressCounter; |
michael@0 | 140 | |
michael@0 | 141 | /** |
michael@0 | 142 | * When it is true and the target service on target device couldn't be found, |
michael@0 | 143 | * refreshing SDP records is necessary. |
michael@0 | 144 | */ |
michael@0 | 145 | bool mNeedsUpdatingSdpRecords; |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Set when StopSendingFile() is called. |
michael@0 | 149 | */ |
michael@0 | 150 | bool mAbortFlag; |
michael@0 | 151 | |
michael@0 | 152 | /** |
michael@0 | 153 | * Set when receiving the first PUT packet of a new file |
michael@0 | 154 | */ |
michael@0 | 155 | bool mNewFileFlag; |
michael@0 | 156 | |
michael@0 | 157 | /** |
michael@0 | 158 | * Set when receiving a PutFinal packet |
michael@0 | 159 | */ |
michael@0 | 160 | bool mPutFinalFlag; |
michael@0 | 161 | |
michael@0 | 162 | /** |
michael@0 | 163 | * Set when FileTransferComplete() is called |
michael@0 | 164 | */ |
michael@0 | 165 | bool mSendTransferCompleteFlag; |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Set when a transfer is successfully completed. |
michael@0 | 169 | */ |
michael@0 | 170 | bool mSuccessFlag; |
michael@0 | 171 | |
michael@0 | 172 | /** |
michael@0 | 173 | * True: Receive file (Server) |
michael@0 | 174 | * False: Send file (Client) |
michael@0 | 175 | */ |
michael@0 | 176 | bool mIsServer; |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * Set when receiving the first PUT packet and wait for |
michael@0 | 180 | * ConfirmReceivingFile() to be called. |
michael@0 | 181 | */ |
michael@0 | 182 | bool mWaitingForConfirmationFlag; |
michael@0 | 183 | |
michael@0 | 184 | nsString mFileName; |
michael@0 | 185 | nsString mContentType; |
michael@0 | 186 | uint32_t mFileLength; |
michael@0 | 187 | uint32_t mSentFileLength; |
michael@0 | 188 | bool mWaitingToSendPutFinal; |
michael@0 | 189 | |
michael@0 | 190 | nsAutoArrayPtr<uint8_t> mBodySegment; |
michael@0 | 191 | nsAutoArrayPtr<uint8_t> mReceivedDataBuffer; |
michael@0 | 192 | |
michael@0 | 193 | int mCurrentBlobIndex; |
michael@0 | 194 | nsCOMPtr<nsIDOMBlob> mBlob; |
michael@0 | 195 | nsTArray<SendFileBatch> mBatches; |
michael@0 | 196 | |
michael@0 | 197 | /** |
michael@0 | 198 | * A seperate member thread is required because our read calls can block |
michael@0 | 199 | * execution, which is not allowed to happen on the IOThread. |
michael@0 | 200 | */ |
michael@0 | 201 | nsCOMPtr<nsIThread> mReadFileThread; |
michael@0 | 202 | nsCOMPtr<nsIOutputStream> mOutputStream; |
michael@0 | 203 | nsCOMPtr<nsIInputStream> mInputStream; |
michael@0 | 204 | nsCOMPtr<nsIVolumeMountLock> mMountLock; |
michael@0 | 205 | nsRefPtr<DeviceStorageFile> mDsFile; |
michael@0 | 206 | |
michael@0 | 207 | // If a connection has been established, mSocket will be the socket |
michael@0 | 208 | // communicating with the remote socket. We maintain the invariant that if |
michael@0 | 209 | // mSocket is non-null, mRfcommSocket and mL2capSocket must be null (and vice |
michael@0 | 210 | // versa). |
michael@0 | 211 | nsRefPtr<BluetoothSocket> mSocket; |
michael@0 | 212 | |
michael@0 | 213 | // Server sockets. Once an inbound connection is established, it will hand |
michael@0 | 214 | // over the ownership to mSocket, and get a new server socket while Listen() |
michael@0 | 215 | // is called. |
michael@0 | 216 | nsRefPtr<BluetoothSocket> mRfcommSocket; |
michael@0 | 217 | nsRefPtr<BluetoothSocket> mL2capSocket; |
michael@0 | 218 | }; |
michael@0 | 219 | |
michael@0 | 220 | END_BLUETOOTH_NAMESPACE |
michael@0 | 221 | |
michael@0 | 222 | #endif |