From 2d4e55c79df4107aeb9e972fda0930de2a38057f Mon Sep 17 00:00:00 2001
From: Pawel Andruszkiewicz
Date: Thu, 19 Mar 2015 09:23:50 +0100
Subject: [PATCH] [Bluetooth] Added privilege checks.
Change-Id: I519b72d516268d9fb4f37b439ae2ab7b4e7d57ef
---
src/bluetooth/bluetooth.gyp | 1 +
src/bluetooth/bluetooth_adapter.cc | 20 +++---
src/bluetooth/bluetooth_api.js | 64 +++++++------------
src/bluetooth/bluetooth_device.cc | 2 +-
src/bluetooth/bluetooth_health_application.cc | 2 +-
src/bluetooth/bluetooth_health_channel.cc | 4 +-
.../bluetooth_health_profile_handler.cc | 4 +-
src/bluetooth/bluetooth_instance.cc | 12 ----
src/bluetooth/bluetooth_privilege.cc | 31 +++++++++
src/bluetooth/bluetooth_privilege.h | 12 ++--
src/bluetooth/bluetooth_service_handler.cc | 2 +-
src/bluetooth/bluetooth_socket.cc | 6 +-
src/bluetooth/bluetooth_util.cc | 5 --
src/bluetooth/bluetooth_util.h | 2 -
14 files changed, 83 insertions(+), 84 deletions(-)
create mode 100644 src/bluetooth/bluetooth_privilege.cc
diff --git a/src/bluetooth/bluetooth.gyp b/src/bluetooth/bluetooth.gyp
index 4b6dea9d..e21e3607 100644
--- a/src/bluetooth/bluetooth.gyp
+++ b/src/bluetooth/bluetooth.gyp
@@ -24,6 +24,7 @@
'bluetooth_health_profile_handler.h',
'bluetooth_instance.cc',
'bluetooth_instance.h',
+ 'bluetooth_privilege.cc',
'bluetooth_privilege.h',
'bluetooth_service_handler.cc',
'bluetooth_service_handler.h',
diff --git a/src/bluetooth/bluetooth_adapter.cc b/src/bluetooth/bluetooth_adapter.cc
index acc06e17..52a55139 100644
--- a/src/bluetooth/bluetooth_adapter.cc
+++ b/src/bluetooth/bluetooth_adapter.cc
@@ -442,7 +442,7 @@ bool BluetoothAdapter::is_initialized() const {
void BluetoothAdapter::SetName(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothAdmin);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothAdmin, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
const auto& args = util::GetArguments(data);
@@ -497,7 +497,7 @@ void BluetoothAdapter::SetName(const picojson::value& data, picojson::object& ou
void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothAdmin);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothAdmin, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
const auto& args = util::GetArguments(data);
@@ -534,7 +534,7 @@ void BluetoothAdapter::SetPowered(const picojson::value& data, picojson::object&
void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothManager);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothManager, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
const auto& args = util::GetArguments(data);
@@ -607,7 +607,7 @@ void BluetoothAdapter::SetVisible(const picojson::value& data, picojson::object&
void BluetoothAdapter::DiscoverDevices(const picojson::value& /* data */, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothGap);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothGap, &out);
PlatformResult result = PlatformResult(ErrorCode::NO_ERROR);
@@ -641,7 +641,7 @@ void BluetoothAdapter::DiscoverDevices(const picojson::value& /* data */, picojs
void BluetoothAdapter::StopDiscovery(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothGap);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothGap, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
@@ -689,7 +689,7 @@ void BluetoothAdapter::StopDiscovery(const picojson::value& data, picojson::obje
void BluetoothAdapter::GetKnownDevices(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothGap);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothGap, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
@@ -738,7 +738,7 @@ void BluetoothAdapter::GetKnownDevices(const picojson::value& data, picojson::ob
void BluetoothAdapter::GetDevice(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothGap);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothGap, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
const auto& args = util::GetArguments(data);
@@ -837,7 +837,7 @@ class BondingHandler {
void BluetoothAdapter::CreateBonding(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothGap);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothGap, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
const auto& args = util::GetArguments(data);
@@ -943,7 +943,7 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj
{
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothGap);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothGap, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
const auto& args = util::GetArguments(data);
@@ -1043,7 +1043,7 @@ void BluetoothAdapter::DestroyBonding(const picojson::value& data, picojson::obj
void BluetoothAdapter::RegisterRFCOMMServiceByUUID(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothSpp);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothSpp, &out);
const auto callback_handle = util::GetAsyncCallbackHandle(data);
const auto& args = util::GetArguments(data);
diff --git a/src/bluetooth/bluetooth_api.js b/src/bluetooth/bluetooth_api.js
index bebae7e8..eb0c617b 100644
--- a/src/bluetooth/bluetooth_api.js
+++ b/src/bluetooth/bluetooth_api.js
@@ -154,26 +154,22 @@ var _PRIVILEGE_BLUETOOTH_GAP = 'http://tizen.org/privilege/bluetooth.gap';
BluetoothClass.prototype.hasService = function() {
console.log('Entered BluetoothClass.hasService()');
- var result = native.callSync('Bluetooth_checkPrivilege', {privilege : _PRIVILEGE_BLUETOOTH_GAP});
+ xwalk.utils.checkPrivilegeAccess(_PRIVILEGE_BLUETOOTH_GAP);
- if (native.isFailure(result)) {
- throw native.getErrorObject(result);
- } else {
- var args = AV.validateMethod(arguments, [
- {
- name : 'service',
- type : AV.Types.UNSIGNED_LONG
- }
- ]);
+ var args = AV.validateMethod(arguments, [
+ {
+ name : 'service',
+ type : AV.Types.UNSIGNED_LONG
+ }
+ ]);
- var size = this.services.length;
- for (var i = 0; i < size; i++) {
- if (this.services[i] === args.service) {
- return true;
- }
+ var size = this.services.length;
+ for (var i = 0; i < size; i++) {
+ if (this.services[i] === args.service) {
+ return true;
}
- return false;
}
+ return false;
};
// class BluetoothSocket ////////////////////////////////////////////////////
@@ -801,33 +797,25 @@ BluetoothHealthChannel.prototype.setListener = function() {
}
]);
- var result = native.callSync('Bluetooth_checkPrivilege', {privilege : _PRIVILEGE_BLUETOOTH_HEALTH});
+ xwalk.utils.checkPrivilegeAccess(_PRIVILEGE_BLUETOOTH_HEALTH);
- if (native.isFailure(result)) {
- throw native.getErrorObject(result);
- } else {
- if (T.isEmptyObject(_healthListeners)) {
- native.addListener('BluetoothHealthChannelChangeCallback',
- _BluetoothHealthChannelChangeCallback);
- }
- _healthListeners[this._id] = args.changeCallback;
+ if (T.isEmptyObject(_healthListeners)) {
+ native.addListener('BluetoothHealthChannelChangeCallback',
+ _BluetoothHealthChannelChangeCallback);
}
+ _healthListeners[this._id] = args.changeCallback;
};
BluetoothHealthChannel.prototype.unsetListener = function() {
console.log('Entered BluetoothHealthChannel.unsetListener ()');
- var result = native.callSync('Bluetooth_checkPrivilege', {privilege : _PRIVILEGE_BLUETOOTH_HEALTH});
+ xwalk.utils.checkPrivilegeAccess(_PRIVILEGE_BLUETOOTH_HEALTH);
- if (native.isFailure(result)) {
- throw native.getErrorObject(result);
- } else {
- delete _healthListeners[this._id];
+ delete _healthListeners[this._id];
- if (T.isEmptyObject(_healthListeners)) {
- native.removeListener('BluetoothHealthChannelChangeCallback',
- _BluetoothHealthChannelChangeCallback);
- }
+ if (T.isEmptyObject(_healthListeners)) {
+ native.removeListener('BluetoothHealthChannelChangeCallback',
+ _BluetoothHealthChannelChangeCallback);
}
};
@@ -1430,13 +1418,9 @@ var BluetoothManager = function() {
BluetoothManager.prototype.getDefaultAdapter = function() {
console.log('Entered BluetoothManager.getDefaultAdapter()');
- var result = native.callSync('Bluetooth_checkPrivilege', {privilege : _PRIVILEGE_BLUETOOTH_GAP});
+ xwalk.utils.checkPrivilegeAccess(_PRIVILEGE_BLUETOOTH_GAP);
- if (native.isFailure(result)) {
- throw native.getErrorObject(result);
- } else {
- return new BluetoothAdapter();
- }
+ return new BluetoothAdapter();
};
// exports ///////////////////////////////////////////////////////////////////
diff --git a/src/bluetooth/bluetooth_device.cc b/src/bluetooth/bluetooth_device.cc
index c3a2c7b0..4f392a23 100644
--- a/src/bluetooth/bluetooth_device.cc
+++ b/src/bluetooth/bluetooth_device.cc
@@ -100,7 +100,7 @@ void BluetoothDevice::ToJson(bt_adapter_device_discovery_info_s *info, picojson:
void BluetoothDevice::ConnectToServiceByUUID(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothSpp);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothSpp, &out);
const auto& args = util::GetArguments(data);
diff --git a/src/bluetooth/bluetooth_health_application.cc b/src/bluetooth/bluetooth_health_application.cc
index 35e9005d..fc8cc4c0 100644
--- a/src/bluetooth/bluetooth_health_application.cc
+++ b/src/bluetooth/bluetooth_health_application.cc
@@ -38,7 +38,7 @@ using namespace common;
void BluetoothHealthApplication::Unregister(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothHealth);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothHealth, &out);
const auto& args = util::GetArguments(data);
diff --git a/src/bluetooth/bluetooth_health_channel.cc b/src/bluetooth/bluetooth_health_channel.cc
index ae78e3f1..0a9a2366 100644
--- a/src/bluetooth/bluetooth_health_channel.cc
+++ b/src/bluetooth/bluetooth_health_channel.cc
@@ -43,7 +43,7 @@ const std::string kId = "_id";
void BluetoothHealthChannel::Close(const picojson::value& data , picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothHealth);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothHealth, &out);
const auto& args = util::GetArguments(data);
@@ -61,7 +61,7 @@ void BluetoothHealthChannel::Close(const picojson::value& data , picojson::objec
void BluetoothHealthChannel::SendData(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothHealth);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothHealth, &out);
const auto& args = util::GetArguments(data);
diff --git a/src/bluetooth/bluetooth_health_profile_handler.cc b/src/bluetooth/bluetooth_health_profile_handler.cc
index 0b7974fd..f0b4d0a0 100644
--- a/src/bluetooth/bluetooth_health_profile_handler.cc
+++ b/src/bluetooth/bluetooth_health_profile_handler.cc
@@ -229,7 +229,7 @@ void BluetoothHealthProfileHandler::OnDataReceived(unsigned int channel,
void BluetoothHealthProfileHandler::RegisterSinkApp(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothHealth);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothHealth, &out);
const auto& args = util::GetArguments(data);
const auto data_type = static_cast(FromJson(args, "dataType"));
@@ -290,7 +290,7 @@ void BluetoothHealthProfileHandler::RegisterSinkApp(const picojson::value& data,
void BluetoothHealthProfileHandler::ConnectToSource(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothHealth);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothHealth, &out);
const auto& args = util::GetArguments(data);
const auto& address = FromJson(args, "address");
diff --git a/src/bluetooth/bluetooth_instance.cc b/src/bluetooth/bluetooth_instance.cc
index 91d3937b..0327f179 100644
--- a/src/bluetooth/bluetooth_instance.cc
+++ b/src/bluetooth/bluetooth_instance.cc
@@ -32,15 +32,6 @@ BluetoothHealthProfileHandler* bluetooth_health_profile_handler = &BluetoothHeal
BluetoothServiceHandler bluetooth_service_handler;
BluetoothSocket bluetooth_socket;
-void CheckPrivilege(const picojson::value& data, picojson::object& out)
-{
- const auto& args = util::GetArguments(data);
- const auto& privilege = FromJson(args, "privilege");
- util::CheckAccess(privilege);
-
- tools::ReportSuccess(out);
-}
-
} // namespace
BluetoothInstance& BluetoothInstance::GetInstance()
@@ -128,9 +119,6 @@ BluetoothInstance::BluetoothInstance()
REGISTER_SYNC("BluetoothSocket_close",
std::bind(&BluetoothSocket::Close, &bluetooth_socket, _1, _2));
- // other
- REGISTER_SYNC("Bluetooth_checkPrivilege", CheckPrivilege);
-
#undef REGISTER_ASYNC
#undef REGISTER_SYNC
}
diff --git a/src/bluetooth/bluetooth_privilege.cc b/src/bluetooth/bluetooth_privilege.cc
new file mode 100644
index 00000000..4f433e90
--- /dev/null
+++ b/src/bluetooth/bluetooth_privilege.cc
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "bluetooth_privilege.h"
+
+namespace extension {
+namespace bluetooth {
+
+namespace Privilege {
+const std::string kBluetoothAdmin = "http://tizen.org/privilege/bluetooth.admin";
+const std::string kBluetoothManager = "http://tizen.org/privilege/bluetoothmanager";
+const std::string kBluetoothGap = "http://tizen.org/privilege/bluetooth.gap";
+const std::string kBluetoothSpp = "http://tizen.org/privilege/bluetooth.spp";
+const std::string kBluetoothHealth = "http://tizen.org/privilege/bluetooth.health";
+} // namespace Privilege
+
+} // namespace bluetooth
+} // namespace extension
diff --git a/src/bluetooth/bluetooth_privilege.h b/src/bluetooth/bluetooth_privilege.h
index 9c81b1ce..13a87d67 100644
--- a/src/bluetooth/bluetooth_privilege.h
+++ b/src/bluetooth/bluetooth_privilege.h
@@ -23,11 +23,13 @@ namespace extension {
namespace bluetooth {
namespace Privilege {
-const std::string kBluetoothAdmin = "http://tizen.org/privilege/bluetooth.admin";
-const std::string kBluetoothManager = "http://tizen.org/privilege/bluetoothmanager";
-const std::string kBluetoothGap = "http://tizen.org/privilege/bluetooth.gap";
-const std::string kBluetoothSpp = "http://tizen.org/privilege/bluetooth.spp";
-const std::string kBluetoothHealth = "http://tizen.org/privilege/bluetooth.health";
+
+extern const std::string kBluetoothAdmin;
+extern const std::string kBluetoothManager;
+extern const std::string kBluetoothGap;
+extern const std::string kBluetoothSpp;
+extern const std::string kBluetoothHealth;
+
} // namespace Privilege
} // namespace bluetooth
diff --git a/src/bluetooth/bluetooth_service_handler.cc b/src/bluetooth/bluetooth_service_handler.cc
index 501fd59c..369a40f2 100644
--- a/src/bluetooth/bluetooth_service_handler.cc
+++ b/src/bluetooth/bluetooth_service_handler.cc
@@ -32,7 +32,7 @@ using namespace common;
void BluetoothServiceHandler::Unregister(const picojson::value& data, picojson::object& out) {
LoggerD("Entered");
- util::CheckAccess(Privilege::kBluetoothSpp);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothSpp, &out);
const auto& args = util::GetArguments(data);
diff --git a/src/bluetooth/bluetooth_socket.cc b/src/bluetooth/bluetooth_socket.cc
index c07a310a..9b3af6ee 100644
--- a/src/bluetooth/bluetooth_socket.cc
+++ b/src/bluetooth/bluetooth_socket.cc
@@ -46,7 +46,7 @@ using namespace common::tools;
void BluetoothSocket::WriteData(const picojson::value& data, picojson::object& out) {
LoggerD("Enter");
- util::CheckAccess(Privilege::kBluetoothSpp);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothSpp, &out);
const auto& args = util::GetArguments(data);
@@ -72,7 +72,7 @@ void BluetoothSocket::WriteData(const picojson::value& data, picojson::object& o
void BluetoothSocket::ReadData(const picojson::value& data, picojson::object& out) {
LoggerD("Enter");
- util::CheckAccess(Privilege::kBluetoothSpp);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothSpp, &out);
const auto& args = util::GetArguments(data);
@@ -94,7 +94,7 @@ void BluetoothSocket::ReadData(const picojson::value& data, picojson::object& ou
void BluetoothSocket::Close(const picojson::value& data, picojson::object& out) {
LoggerD("Enter");
- util::CheckAccess(Privilege::kBluetoothSpp);
+ CHECK_PRIVILEGE_ACCESS(Privilege::kBluetoothSpp, &out);
const auto& args = util::GetArguments(data);
diff --git a/src/bluetooth/bluetooth_util.cc b/src/bluetooth/bluetooth_util.cc
index 94f5061f..70d5146f 100644
--- a/src/bluetooth/bluetooth_util.cc
+++ b/src/bluetooth/bluetooth_util.cc
@@ -26,11 +26,6 @@ const char* JSON_CALLBACK_ERROR = "error";
const char* JSON_DATA = "args";
} // namespace
-
-void CheckAccess(const std::string& privilege) {
- // TODO: check access to privilege, throw exception on failure
-}
-
void AsyncResponse(double callback_handle, const std::shared_ptr& response) {
common::TaskQueue::GetInstance().Async([callback_handle](const std::shared_ptr& response) {
SyncResponse(callback_handle, response);
diff --git a/src/bluetooth/bluetooth_util.h b/src/bluetooth/bluetooth_util.h
index 5ef31f6f..271be1f3 100644
--- a/src/bluetooth/bluetooth_util.h
+++ b/src/bluetooth/bluetooth_util.h
@@ -15,8 +15,6 @@ namespace extension {
namespace bluetooth {
namespace util {
-void CheckAccess(const std::string& privilege);
-
void AsyncResponse(double callback_handle, const std::shared_ptr& response);
void AsyncResponse(double callback_handle, const common::PlatformResult& result);
void SyncResponse(double callback_handle, const std::shared_ptr& response);
--
2.34.1