2012-08-01 08:53:04 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-17 06:53:45 +04:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-08-01 08:53:04 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2014-11-28 13:49:40 +03:00
|
|
|
#include "BluetoothUtils.h"
|
2012-11-19 11:10:38 +04:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
2013-08-02 14:32:57 +04:00
|
|
|
#include "BluetoothService.h"
|
2012-08-01 08:53:04 +04:00
|
|
|
#include "jsapi.h"
|
2012-10-01 11:44:48 +04:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2012-10-03 13:06:40 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsISystemMessagesInternal.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-10-03 13:06:40 +04:00
|
|
|
|
2012-11-19 11:10:38 +04:00
|
|
|
BEGIN_BLUETOOTH_NAMESPACE
|
2012-08-01 08:53:04 +04:00
|
|
|
|
2012-10-01 11:44:48 +04:00
|
|
|
bool
|
2012-11-19 11:10:38 +04:00
|
|
|
SetJsObject(JSContext* aContext,
|
2013-01-29 10:52:39 +04:00
|
|
|
const BluetoothValue& aValue,
|
2013-07-25 16:57:00 +04:00
|
|
|
JS::Handle<JSObject*> aObj)
|
2012-10-01 11:44:48 +04:00
|
|
|
{
|
2013-01-29 10:52:39 +04:00
|
|
|
MOZ_ASSERT(aContext && aObj);
|
2012-10-01 11:44:48 +04:00
|
|
|
|
2013-02-06 16:07:27 +04:00
|
|
|
if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("SetJsObject: Invalid parameter type");
|
2013-02-06 16:07:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-29 10:52:39 +04:00
|
|
|
|
2013-02-06 16:07:27 +04:00
|
|
|
const nsTArray<BluetoothNamedValue>& arr =
|
|
|
|
aValue.get_ArrayOfBluetoothNamedValue();
|
2013-01-29 10:52:39 +04:00
|
|
|
|
2013-02-06 16:07:27 +04:00
|
|
|
for (uint32_t i = 0; i < arr.Length(); i++) {
|
2013-07-25 16:57:00 +04:00
|
|
|
JS::Rooted<JS::Value> val(aContext);
|
2013-02-06 16:07:27 +04:00
|
|
|
const BluetoothValue& v = arr[i].value();
|
2013-01-29 10:52:39 +04:00
|
|
|
|
2013-02-06 16:07:27 +04:00
|
|
|
switch(v.type()) {
|
2013-07-25 16:57:00 +04:00
|
|
|
case BluetoothValue::TnsString: {
|
|
|
|
JSString* jsData = JS_NewUCStringCopyN(aContext,
|
2013-02-18 12:02:23 +04:00
|
|
|
v.get_nsString().BeginReading(),
|
|
|
|
v.get_nsString().Length());
|
2013-02-06 16:07:27 +04:00
|
|
|
NS_ENSURE_TRUE(jsData, false);
|
|
|
|
val = STRING_TO_JSVAL(jsData);
|
|
|
|
break;
|
2013-07-25 16:57:00 +04:00
|
|
|
}
|
2013-02-06 16:07:27 +04:00
|
|
|
case BluetoothValue::Tuint32_t:
|
|
|
|
val = INT_TO_JSVAL(v.get_uint32_t());
|
|
|
|
break;
|
|
|
|
case BluetoothValue::Tbool:
|
|
|
|
val = BOOLEAN_TO_JSVAL(v.get_bool());
|
|
|
|
break;
|
|
|
|
default:
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("SetJsObject: Parameter is not handled");
|
2013-02-06 16:07:27 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetProperty(aContext, aObj,
|
|
|
|
NS_ConvertUTF16toUTF8(arr[i].name()).get(),
|
2013-07-26 13:00:38 +04:00
|
|
|
val)) {
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("Failed to set property");
|
2013-02-06 16:07:27 +04:00
|
|
|
return false;
|
2013-01-29 10:52:39 +04:00
|
|
|
}
|
2012-10-01 11:44:48 +04:00
|
|
|
}
|
2013-01-29 10:52:39 +04:00
|
|
|
|
2012-10-01 11:44:48 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-03 13:06:40 +04:00
|
|
|
bool
|
2012-11-19 11:10:38 +04:00
|
|
|
BroadcastSystemMessage(const nsAString& aType,
|
2014-04-04 16:19:48 +04:00
|
|
|
const BluetoothValue& aData)
|
2012-10-03 13:06:40 +04:00
|
|
|
{
|
2013-05-22 20:05:27 +04:00
|
|
|
mozilla::AutoSafeJSContext cx;
|
2012-10-03 13:06:40 +04:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"Shouldn't get here when an exception is pending!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
|
|
|
|
do_GetService("@mozilla.org/system-message-internal;1");
|
2013-01-29 10:52:39 +04:00
|
|
|
NS_ENSURE_TRUE(systemMessenger, false);
|
2012-10-03 13:06:40 +04:00
|
|
|
|
2014-04-04 16:19:48 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
if (aData.type() == BluetoothValue::TnsString) {
|
|
|
|
JSString* jsData = JS_NewUCStringCopyN(cx,
|
|
|
|
aData.get_nsString().BeginReading(),
|
|
|
|
aData.get_nsString().Length());
|
|
|
|
value = STRING_TO_JSVAL(jsData);
|
|
|
|
} else if (aData.type() == BluetoothValue::TArrayOfBluetoothNamedValue) {
|
2015-01-24 18:38:08 +03:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
|
2014-04-04 16:19:48 +04:00
|
|
|
if (!obj) {
|
|
|
|
BT_WARNING("Failed to new JSObject for system message!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetJsObject(cx, aData, obj)) {
|
|
|
|
BT_WARNING("Failed to set properties of system message!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = JS::ObjectValue(*obj);
|
|
|
|
} else {
|
|
|
|
BT_WARNING("Not support the unknown BluetoothValue type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-09 21:39:36 +04:00
|
|
|
systemMessenger->BroadcastMessage(aType, value,
|
|
|
|
JS::UndefinedHandleValue);
|
2012-10-03 13:06:40 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-28 13:49:40 +03:00
|
|
|
bool
|
|
|
|
BroadcastSystemMessage(const nsAString& aType,
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& aData)
|
|
|
|
{
|
|
|
|
mozilla::AutoSafeJSContext cx;
|
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"Shouldn't get here when an exception is pending!");
|
|
|
|
|
2015-01-24 18:38:08 +03:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
|
2014-11-28 13:49:40 +03:00
|
|
|
if (!obj) {
|
|
|
|
BT_WARNING("Failed to new JSObject for system message!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetJsObject(cx, aData, obj)) {
|
|
|
|
BT_WARNING("Failed to set properties of system message!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
|
|
|
|
do_GetService("@mozilla.org/system-message-internal;1");
|
|
|
|
NS_ENSURE_TRUE(systemMessenger, false);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(cx, JS::ObjectValue(*obj));
|
|
|
|
systemMessenger->BroadcastMessage(aType, value,
|
|
|
|
JS::UndefinedHandleValue);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-19 11:10:38 +04:00
|
|
|
void
|
|
|
|
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
|
|
|
|
const BluetoothValue& aValue,
|
|
|
|
const nsAString& aErrorStr)
|
|
|
|
{
|
|
|
|
// Reply will be deleted by the runnable after running on main thread
|
|
|
|
BluetoothReply* reply;
|
|
|
|
if (!aErrorStr.IsEmpty()) {
|
|
|
|
nsString err(aErrorStr);
|
|
|
|
reply = new BluetoothReply(BluetoothReplyError(err));
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
|
|
|
|
reply = new BluetoothReply(BluetoothReplySuccess(aValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
aRunnable->SetReply(reply);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) {
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("Failed to dispatch to main thread!");
|
2012-11-19 11:10:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 14:32:57 +04:00
|
|
|
void
|
|
|
|
DispatchStatusChangedEvent(const nsAString& aType,
|
|
|
|
const nsAString& aAddress,
|
|
|
|
bool aStatus)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
InfallibleTArray<BluetoothNamedValue> data;
|
2014-04-11 07:52:44 +04:00
|
|
|
BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress));
|
|
|
|
BT_APPEND_NAMED_VALUE(data, "status", aStatus);
|
2013-08-02 14:32:57 +04:00
|
|
|
|
|
|
|
BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data);
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
|
|
bs->DistributeSignal(signal);
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:24:35 +04:00
|
|
|
END_BLUETOOTH_NAMESPACE
|