[mediacontroller] New implementation for sendCommand(), 52/211252/9
authorMichal Michalski <m.michalski2@partner.samsung.com>
Wed, 31 Jul 2019 11:09:35 +0000 (13:09 +0200)
committerMichal Michalski <m.michalski2@partner.samsung.com>
Tue, 6 Aug 2019 08:31:30 +0000 (10:31 +0200)
http://suprem.sec.samsung.net/jira/browse/TWDAPI-226

+ sendCommand() accepts both object and tizen.Bundle as data argument.
+ Command listener can now return both RequestReply object, tizen.Bundle and object.

[Verification] Tested with custom javascript test cases.

Signed-off-by: Michal Michalski <m.michalski2@partner.samsung.com>
Change-Id: I357e5b398724bdfada48684fb5bbc8f11be52f20

src/mediacontroller/mediacontroller_api.js
src/mediacontroller/mediacontroller_client.cc
src/mediacontroller/mediacontroller_instance.cc
src/mediacontroller/mediacontroller_server.cc

index 0eec9e5..8dda839 100755 (executable)
@@ -114,49 +114,40 @@ ListenerManager.prototype.removeServerInfoListener = function(watchId) {
     }
 };
 
-var ServerCommandListener = new ListenerManager(
-    native_,
-    '_ServerCommandListener',
-    function(msg, listener) {
-        var data = undefined;
-        data = listener(msg.clientName, msg.command, msg.data);
+var ServerCommandListener = new ListenerManager(native_, '_ServerCommandListener', function(msg, listener) {
+  // passing raw msg.data object instead of tizen.Bundle
+  // due to backward compatibility issue.
+  var reply = listener(msg.clientName, msg.command, msg.data);
 
-        if (type_.isUndefined(data)) {
-            data = null;
-        }
+  if (!(reply instanceof RequestReply)) {
+    reply = new RequestReply(xwalk.utils.type.isNullOrUndefined(reply) ? null : reply, 0);
+  }
 
-        var bundle;
-        if (data instanceof tizen.Bundle) {
-            bundle = data;
-        } else {
-            bundle = new tizen.Bundle(data);
-        }
+  var nativeData = {
+    clientName: msg.clientName,
+    requestId: msg.requestId,
+    reply: reply
+  };
 
-        var nativeData = {
-            clientName: msg.clientName,
-            requestId: msg.requestId,
-            data: bundle
-        };
+  var result = native_.callSync('MediaControllerServer_replyCommand', nativeData);
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+});
 
-        var result = native_.callSync('MediaControllerServer_replyCommand', nativeData);
-        if (native_.isFailure(result)) {
-            throw native_.getErrorObject(result);
-        }
-    }
-);
+var ReplyCommandListener = new ListenerManager(native_, '_ReplyCommandListener', function(msg, listener, watchId) {
+  if (this.requestIdToListenerId[watchId] === msg.requestId) {
 
-var ReplyCommandListener = new ListenerManager(native_, '_ReplyCommandListener', function(
-    msg,
-    listener,
-    watchId
-) {
-    if (this.requestIdToListenerId[watchId] === msg.requestId) {
-        listener(msg);
-        this.removeListener(watchId);
-        delete this.requestIdToListenerId[watchId];
-        return true;
-    }
-    return false;
+    // this listener is a wrapper around user callback which is neccessary
+    // due to difference between sendCommand() and new API methods.
+    listener(msg);
+
+    this.removeListener(watchId);
+    delete this.requestIdToListenerId[watchId];
+
+    return true;
+  }
+  return false;
 });
 
 var ServerPlaybackInfoListener = new ListenerManager(
@@ -1037,15 +1028,7 @@ MediaControllerServer.prototype.getAllPlaylists = function(
 function RequestReply(data, code) {
   xwalk.utils.validator.isConstructorCall(this, RequestReply);
   this.code = xwalk.utils.converter.toLong(code);
-  if (xwalk.utils.type.isUndefined(data)) {
-    this.data = null;
-  }
-  else if (!(data instanceof tizen.Bundle)) {
-    this.data = new tizen.Bundle(data);
-  }
-  else {
-    this.data = data;
-  }
+  this.data = xwalk.utils.type.isNullOrUndefined(data) ? null : new tizen.Bundle(data);
 }
 
 MediaControllerServer.prototype.setSearchRequestListener = function(listener) {
@@ -1075,14 +1058,13 @@ MediaControllerServer.prototype.setSearchRequestListener = function(listener) {
 
       var reply = args.listener(msg.clientName, request);
       if (type_.isUndefined(reply)) {
-        reply = null;
+        reply = new RequestReply(null, 0);
       }
 
       var nativeData = {
         clientName: msg.clientName,
         requestId: msg.requestId,
-        reply: reply,
-        replyType: "RequestReply"
+        reply: reply
       };
 
       var result = native_.callSync('MediaControllerServer_replyCommand', nativeData);
@@ -1442,48 +1424,34 @@ MediaControllerServerInfo.prototype.sendRepeatState = function() {
     native_.call('MediaControllerServerInfo_sendRepeatState', data, callback);
 };
 
-MediaControllerServerInfo.prototype.sendCommand = function(
-    command,
-    data,
-    successCallback,
-    errorCallback
-) {
-    var args = validator_.validateArgs(arguments, [
-        { name: 'command', type: types_.STRING },
-        { name: 'data', type: types_.DICTIONARY },
-        { name: 'successCallback', type: types_.FUNCTION },
-        { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
-    ]);
-
-    var callback = function(result) {
-        if (native_.isFailure(result)) {
-            native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
-            return;
-        }
-        native_.callIfPossible(
-            args.successCallback,
-            native_.getResultObject(result).data
-        );
-    };
+MediaControllerServerInfo.prototype.sendCommand = function(command, data, successCallback, errorCallback) {
+  var args = validator_.validateArgs(arguments, [
+    {name: 'command', type: types_.STRING},
+    {name: 'data', type: types_.DICTIONARY, nullable: true},
+    {name: 'successCallback', type: types_.FUNCTION},
+    {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
+  ]);
 
-    var bundle;
-    if (args.data instanceof tizen.Bundle) {
-        bundle = args.data;
-    } else {
-        bundle = new tizen.Bundle(args.data);
+  var callback = function(result) {
+    if (native_.isFailure(result)) {
+      native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+      return;
     }
+    native_.callIfPossible(args.successCallback, native_.getResultObject(result).data,
+                           native_.getResultObject(result).code);
+  };
 
-    var nativeData = {
-        command: args.command,
-        data: bundle,
-        name: this.name,
-        listenerId: ReplyCommandListener.listenerName
-    };
+  var nativeData = {
+    command: args.command,
+    data: args.data === null ? null : new tizen.Bundle(args.data),
+    name: this.name,
+    listenerId: ReplyCommandListener.listenerName
+  };
 
-    var replyListenerId = ReplyCommandListener.addListener(callback);
-    var result = native_.callSync('MediaControllerServerInfo_sendCommand', nativeData);
+  var replyListenerId = ReplyCommandListener.addListener(callback);
+  var result = native_.callSync('MediaControllerServerInfo_sendCommand', nativeData);
 
-    ReplyCommandListener.requestIdToListenerId[replyListenerId] = result.requestId;
+  ReplyCommandListener.requestIdToListenerId[replyListenerId] = result.requestId;
 };
 
 MediaControllerServerInfo.prototype.sendSearchRequest = function(request, successCallback, errorCallback) {
index 7d6c20c..71e29c5 100644 (file)
@@ -627,22 +627,22 @@ PlatformResult MediaControllerClient::SendCommand(const std::string& server_name
                                                   const picojson::value& data,
                                                   const JsonCallback& reply_cb, char** request_id) {
   ScopeLogger();
-  bundle* bundle = nullptr;
-  int ret;
+  bundle* bundle_data = nullptr;
   SCOPE_EXIT {
-    bundle_free(bundle);
+    bundle_free(bundle_data);
   };
 
-  if (!data.is<picojson::null>()) {
-    PlatformResult result = common::JsonToBundle(data, &bundle);
-    if (!result) {
-      LoggerE("JsonToBundle() failed.");
-      return result;
-    }
+  PlatformResult result = common::JsonToBundle(data, &bundle_data);
+  if (!result) {
+    LoggerE("JsonToBundle() failed.");
+    return result;
   }
 
-  ret =
-      mc_client_send_custom_cmd(handle_, server_name.c_str(), command.c_str(), bundle, request_id);
+  if (nullptr == bundle_data) {
+    LoggerD("bundle is null");
+  }
+  int ret = mc_client_send_custom_cmd(handle_, server_name.c_str(),
+                                      command.c_str(), bundle_data, request_id);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
         ErrorCode::UNKNOWN_ERR, "Error sending custom command",
@@ -650,7 +650,6 @@ PlatformResult MediaControllerClient::SendCommand(const std::string& server_name
   }
 
   command_reply_callback_ = reply_cb;
-
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
index e39eab0..fd9158b 100644 (file)
@@ -498,25 +498,19 @@ void MediaControllerInstance::MediaControllerServerReplyCommand(const picojson::
   CHECK_EXIST(args, "clientName", out);
   CHECK_EXIST(args, "requestId", out);
   CHECK_EXIST(args, "reply", out);
-  CHECK_EXIST(args, "replyType", out);
 
-  int code = 0;
-  picojson::value data;
+  auto client_name = args.get("clientName").get<std::string>();
+  auto request_id = args.get("requestId").get<std::string>();
 
-  if (args.get("replyType").get<std::string>() == "RequestReply") {
-    CHECK_EXIST(args.get("reply"), "code", out);
-    CHECK_EXIST(args.get("reply"), "data", out);
-    code = static_cast<int>(args.get("reply").get("code").get<double>());
-    data = args.get("reply").get<picojson::object>().at("data");
-  }
-  else {
-    data = args.get("reply");
-  }
+  auto reply = args.get("reply");
+  CHECK_EXIST(reply, "code", out);
+  CHECK_EXIST(reply, "data", out);
 
-  auto result = server_->CommandReply(args.get("clientName").get<std::string>(),
-                                      args.get("requestId").get<std::string>(),
-                                      code, data);
+  LoggerD("reply json: %s", reply.serialize().c_str());
+  int code = static_cast<int>(reply.get("code").get<double>());
+  auto data = reply.get("data");
 
+  auto result = server_->CommandReply(client_name, request_id, code, data);
   if (!result) {
     LogAndReportError(result, &out);
     return;
index 266f8d7..acdbf19 100644 (file)
@@ -394,22 +394,19 @@ PlatformResult MediaControllerServer::CommandReply(const std::string& client_nam
                                                    int code,
                                                    const picojson::value& data) {
   ScopeLogger();
-
-  int ret;
   bundle* bundle = nullptr;
   SCOPE_EXIT {
     bundle_free(bundle);
   };
 
-  if (!data.is<picojson::null>()) {
-    PlatformResult result = common::JsonToBundle(data, &bundle);
-    if (!result) {
-      LoggerE("JsonToBundle() failed.");
-      return result;
-    }
+  PlatformResult result = common::JsonToBundle(data, &bundle);
+  if (!result) {
+    LoggerE("JsonToBundle() failed.");
+    return result;
   }
 
-  ret = mc_server_send_cmd_reply(handle_, client_name.c_str(), request_id.c_str(), code, bundle);
+  int ret = mc_server_send_cmd_reply(handle_, client_name.c_str(), request_id.c_str(),
+                                     code, bundle);
   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
     return LogAndCreateResult(
         ErrorCode::UNKNOWN_ERR, "Error sending command reply",