[Bluetooth] Added privilege checks.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Thu, 19 Mar 2015 08:23:50 +0000 (09:23 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 25 Mar 2015 13:53:57 +0000 (22:53 +0900)
Change-Id: I519b72d516268d9fb4f37b439ae2ab7b4e7d57ef

14 files changed:
src/bluetooth/bluetooth.gyp
src/bluetooth/bluetooth_adapter.cc
src/bluetooth/bluetooth_api.js
src/bluetooth/bluetooth_device.cc
src/bluetooth/bluetooth_health_application.cc
src/bluetooth/bluetooth_health_channel.cc
src/bluetooth/bluetooth_health_profile_handler.cc
src/bluetooth/bluetooth_instance.cc
src/bluetooth/bluetooth_privilege.cc [new file with mode: 0644]
src/bluetooth/bluetooth_privilege.h
src/bluetooth/bluetooth_service_handler.cc
src/bluetooth/bluetooth_socket.cc
src/bluetooth/bluetooth_util.cc
src/bluetooth/bluetooth_util.h

index 4b6dea9d63167d526a3f529faa29caa61d165e51..e21e36077e9dcb3df5eb7e7c946b34a3e6c75036 100644 (file)
@@ -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',
index acc06e173bc5749b25b7d4417b37014ea373949f..52a5513954bc82eed1b634752312a820aec0e5d0 100644 (file)
@@ -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);
index bebae7e8c7be4b1a2512c838dc08dbd97816a51f..eb0c617bb7ce75b514595a58cebbfef83384365b 100644 (file)
@@ -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 ///////////////////////////////////////////////////////////////////
index c3a2c7b02ad3e7d78f609c1ed7afd437913aa10e..4f392a2311367f3aaaae7895d79d794168cdde43 100644 (file)
@@ -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);
 
index 35e9005d3cbfdd5a0bb4d095ec1f61aa3d0cde90..fc8cc4c0667c1958f142e804e7ace2fd8775ef39 100644 (file)
@@ -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);
 
index ae78e3f14ecf866b188ba1016f28b17c3cf9d026..0a9a236669740eaf4968ee633c232e695642d7e9 100644 (file)
@@ -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);
 
index 0b7974fda5fd808dd996fe601724d4a7e21f0297..f0b4d0a026557d5037176e9b0bf92505537b28f0 100644 (file)
@@ -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<short>(FromJson<double>(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<std::string>(args, "address");
index 91d3937b28f014847fa28ea5496467afb29868c3..0327f179e88bd1d6a32e845f0b33f27a2867c583 100644 (file)
@@ -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<std::string>(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 (file)
index 0000000..4f433e9
--- /dev/null
@@ -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
index 9c81b1cefc310bd729b82b09ed23bd939ca767f3..13a87d67c75add3d4c08e635ebf63b5d86a710d5 100644 (file)
@@ -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
index 501fd59cff607e9939b2b6169374e1c63023c3a5..369a40f27df2e989ae745b939a88f2737e10676f 100644 (file)
@@ -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);
 
index c07a310a213608be40110cccc3b16c16af2472d2..9b3af6eefcce5f5cfcf7183d9f71c18b9faee26e 100644 (file)
@@ -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);
 
index 94f5061fa147ce5b5a98674c77f9a0ae083401d3..70d5146fbbd367407889902e087b27ac0ff8ea82 100644 (file)
@@ -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<picojson::value>& response) {
   common::TaskQueue::GetInstance().Async<picojson::value>([callback_handle](const std::shared_ptr<picojson::value>& response) {
     SyncResponse(callback_handle, response);
index 5ef31f6f59e54db32b78da0431525a876aa31496..271be1f383e3af55375d672c759df288333df2f2 100644 (file)
@@ -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<picojson::value>& response);
 void AsyncResponse(double callback_handle, const common::PlatformResult& result);
 void SyncResponse(double callback_handle, const std::shared_ptr<picojson::value>& response);