2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include "MessagingFactory.h"
20 #include "IMessagingServiceManager.h"
21 #include <Commons/FunctionDeclaration.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/Validator.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include <CommonsJavaScript/Utils.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include <SecurityExceptions.h>
31 #include <JSWebAPIError.h>
32 #include <ArgumentValidator.h>
34 #include "JSMessagingServiceManager.h"
35 #include "MessagingController.h"
36 #include "MessagingListener.h"
37 #include "ConverterMessage.h"
38 #include "plugin_config.h"
39 #include "MessageAsyncCallbackManager.h"
40 #include "MessagingErrorMsg.h"
44 using namespace DeviceAPI::Messaging;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47 using namespace DeviceAPI::Common;
53 JSClassRef JSMessagingServiceManager::m_jsClassRef = NULL;
55 JSClassDefinition JSMessagingServiceManager::m_classInfo = {
57 kJSClassAttributeNone,
67 NULL, //deleteProperty,
68 NULL, //getPropertyNames,
75 JSStaticValue JSMessagingServiceManager::m_property[] =
80 JSStaticFunction JSMessagingServiceManager::m_function[] = {
81 { "getMessageServices", JSMessagingServiceManager::getMessagingServices, kJSPropertyAttributeNone },
85 const JSClassRef JSMessagingServiceManager::getClassRef() {
87 m_jsClassRef = JSClassCreate(&m_classInfo);
92 void JSMessagingServiceManager::initialize(JSContextRef context, JSObjectRef object) {
93 LoggerD("creation messaging instance");
95 JSMessagingServiceManagerPriv *priv = static_cast<JSMessagingServiceManagerPriv*>(JSObjectGetPrivate(object));
98 IMessagingServiceManagerPtr MessagingServiceManager(MessagingFactory::getInstance().getMessagingServiceManager());
99 priv = new JSMessagingServiceManagerPriv( context, MessagingServiceManager);
101 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
102 LoggerE("Object can't store private data.");
108 LoggerD("already exist");
109 LoggerD("global context=" << priv->getContext());
114 void JSMessagingServiceManager::finalize(JSObjectRef object) {
116 JSMessagingServiceManagerPriv* priv = static_cast<JSMessagingServiceManagerPriv*>(JSObjectGetPrivate(object));
117 MessagingListener::getInstance().deregisterMessageReceivedEmitter();
120 JSObjectSetPrivate(object, NULL);
121 delete priv; // object will be deleted, but used to show that private object is deleted
127 bool JSMessagingServiceManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
128 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
131 JSValueRef JSMessagingServiceManager::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
133 return JSValueMakeUndefined(context);
136 JSValueRef JSMessagingServiceManager::getMessagingServices(JSContextRef context,
138 JSObjectRef thisObject,
139 size_t argumentCount,
140 const JSValueRef arguments[],
141 JSValueRef* exception)
145 JSMessagingServiceManagerPriv* priv = static_cast<JSMessagingServiceManagerPriv*>(JSObjectGetPrivate(thisObject));
147 LoggerE("Private object is NULL.");
148 DeviceAPI::Common::UnknownException err("Private object is NULL.");
149 return JSWebAPIError::throwException(context, exception, err);
152 JSContextRef gContext = priv->getContext();
153 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
155 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext); //create callback manager
156 callbackManager->setObject(thisObject);
159 ArgumentValidator validator(context, argumentCount, arguments);
160 int messagingType = converter->toMessageType(validator.toString(0));
161 callbackManager->setOnSuccess(validator.toFunction(1));
162 callbackManager->setOnError(validator.toFunction(2, true));
164 LoggerD("Messaging Service Type: " << messagingType);
166 // JSCallbackManager contains function pointers of successCallback and errorCallback.
167 // The function pointers is be using to return results to Javascriptcore. So JSCallbackManager should be executed by main thread.
169 EventGetMessagingServicePtr event(new EventGetMessagingService()); //create event
171 IMessagingServiceManagerPtr messagingServiceManager(priv->getObject());
173 event->setPrivateData(StaticPointerCast<IEventPrivateData> (callbackManager)); //callback manager
174 event->setForAsynchronousCall(&MessagingController::getInstance());
176 event->setEventType(EventGetMessagingService::MESSAGING_SERVICE_MANAGER_EVENT_TYPE_ALL);
178 event->setMessagingServiceType(messagingType);
180 messagingServiceManager->getMessagingServiceManager(event); //call postEvent
181 MessageAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
183 catch(const BasePlatformException& err) {
184 LoggerE(err.getMessage().c_str());
185 return JSWebAPIError::throwException(context, exception, err);
187 catch (const WrtDeviceApis::Commons::Exception& exc) {
188 LoggerE(exc.GetMessage().c_str());
189 DeviceAPI::Common::UnknownException err(exc.GetMessage().c_str());
190 return JSWebAPIError::throwException(context, exception, err);
193 return JSValueMakeUndefined(context);