Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "BluetoothSocket.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "BluetoothSocketObserver.h" |
michael@0 | 10 | #include "BluetoothUnixSocketConnector.h" |
michael@0 | 11 | #include "nsThreadUtils.h" |
michael@0 | 12 | |
michael@0 | 13 | using namespace mozilla::ipc; |
michael@0 | 14 | USING_BLUETOOTH_NAMESPACE |
michael@0 | 15 | |
michael@0 | 16 | BluetoothSocket::BluetoothSocket(BluetoothSocketObserver* aObserver, |
michael@0 | 17 | BluetoothSocketType aType, |
michael@0 | 18 | bool aAuth, |
michael@0 | 19 | bool aEncrypt) |
michael@0 | 20 | : mObserver(aObserver) |
michael@0 | 21 | , mType(aType) |
michael@0 | 22 | , mAuth(aAuth) |
michael@0 | 23 | , mEncrypt(aEncrypt) |
michael@0 | 24 | { |
michael@0 | 25 | MOZ_ASSERT(aObserver); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | bool |
michael@0 | 29 | BluetoothSocket::Connect(const nsACString& aDeviceAddress, int aChannel) |
michael@0 | 30 | { |
michael@0 | 31 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 32 | MOZ_ASSERT(!aDeviceAddress.IsEmpty()); |
michael@0 | 33 | |
michael@0 | 34 | nsAutoPtr<BluetoothUnixSocketConnector> c( |
michael@0 | 35 | new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt)); |
michael@0 | 36 | |
michael@0 | 37 | if (!ConnectSocket(c.forget(), aDeviceAddress.BeginReading())) { |
michael@0 | 38 | nsAutoString addr; |
michael@0 | 39 | GetAddress(addr); |
michael@0 | 40 | BT_LOGD("%s failed. Current connected device address: %s", |
michael@0 | 41 | __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get()); |
michael@0 | 42 | return false; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | bool |
michael@0 | 49 | BluetoothSocket::Listen(int aChannel) |
michael@0 | 50 | { |
michael@0 | 51 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 52 | |
michael@0 | 53 | nsAutoPtr<BluetoothUnixSocketConnector> c( |
michael@0 | 54 | new BluetoothUnixSocketConnector(mType, aChannel, mAuth, mEncrypt)); |
michael@0 | 55 | |
michael@0 | 56 | if (!ListenSocket(c.forget())) { |
michael@0 | 57 | nsAutoString addr; |
michael@0 | 58 | GetAddress(addr); |
michael@0 | 59 | BT_LOGD("%s failed. Current connected device address: %s", |
michael@0 | 60 | __FUNCTION__, NS_ConvertUTF16toUTF8(addr).get()); |
michael@0 | 61 | return false; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | return true; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | void |
michael@0 | 68 | BluetoothSocket::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) |
michael@0 | 69 | { |
michael@0 | 70 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 71 | MOZ_ASSERT(mObserver); |
michael@0 | 72 | mObserver->ReceiveSocketData(this, aMessage); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | void |
michael@0 | 76 | BluetoothSocket::OnConnectSuccess() |
michael@0 | 77 | { |
michael@0 | 78 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 79 | MOZ_ASSERT(mObserver); |
michael@0 | 80 | mObserver->OnSocketConnectSuccess(this); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | void |
michael@0 | 84 | BluetoothSocket::OnConnectError() |
michael@0 | 85 | { |
michael@0 | 86 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 87 | MOZ_ASSERT(mObserver); |
michael@0 | 88 | mObserver->OnSocketConnectError(this); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | void |
michael@0 | 92 | BluetoothSocket::OnDisconnect() |
michael@0 | 93 | { |
michael@0 | 94 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 95 | MOZ_ASSERT(mObserver); |
michael@0 | 96 | mObserver->OnSocketDisconnect(this); |
michael@0 | 97 | } |
michael@0 | 98 |