wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Messaging / JSMessagingServiceManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
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>
30
31 #include <JSWebAPIError.h>
32 #include <ArgumentValidator.h>
33
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"
41
42 using namespace std;
43 using namespace DPL;
44 using namespace DeviceAPI::Messaging;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47 using namespace DeviceAPI::Common;
48
49
50 namespace DeviceAPI {
51 namespace Messaging {
52
53     JSClassRef JSMessagingServiceManager::m_jsClassRef = NULL;
54
55     JSClassDefinition JSMessagingServiceManager::m_classInfo = {
56         0,
57         kJSClassAttributeNone,
58         "Messaging",
59         NULL,
60         m_property,
61         m_function,
62         initialize,
63         finalize,
64         NULL, //hasProperty,
65         NULL, //getProperty,
66         NULL, //setProperty,
67         NULL, //deleteProperty,
68         NULL, //getPropertyNames,
69         NULL,
70         NULL,
71         hasInstance,
72         NULL
73     };
74
75     JSStaticValue JSMessagingServiceManager::m_property[] =
76     {
77            { 0, 0, 0, 0 }
78     };
79
80     JSStaticFunction JSMessagingServiceManager::m_function[] = {
81             { "getMessageServices", JSMessagingServiceManager::getMessagingServices, kJSPropertyAttributeNone },
82             { 0, 0, 0 }
83     };
84
85     const JSClassRef JSMessagingServiceManager::getClassRef() {
86         if (!m_jsClassRef) {
87             m_jsClassRef = JSClassCreate(&m_classInfo);
88         }
89         return m_jsClassRef;
90     }
91
92     void JSMessagingServiceManager::initialize(JSContextRef context, JSObjectRef object) {
93         LoggerD("creation messaging instance");
94
95         JSMessagingServiceManagerPriv *priv = static_cast<JSMessagingServiceManagerPriv*>(JSObjectGetPrivate(object));
96         if (priv == NULL)
97         {
98             IMessagingServiceManagerPtr MessagingServiceManager(MessagingFactory::getInstance().getMessagingServiceManager());
99             priv = new JSMessagingServiceManagerPriv( context, MessagingServiceManager);
100
101             if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
102                 LoggerE("Object can't store private data.");
103                 delete priv;
104             }
105         }
106         else
107         {
108             LoggerD("already exist");
109             LoggerD("global context=" << priv->getContext());
110         }
111
112     }
113
114     void JSMessagingServiceManager::finalize(JSObjectRef object) {
115         LoggerD("enter");
116         JSMessagingServiceManagerPriv* priv = static_cast<JSMessagingServiceManagerPriv*>(JSObjectGetPrivate(object));
117         MessagingListener::getInstance().deregisterMessageReceivedEmitter();
118
119         if (priv) {
120             JSObjectSetPrivate(object, NULL);
121             delete priv;  // object will be deleted, but used to show that private object is deleted
122         }
123         LoggerD("Leave");
124
125     }
126
127     bool JSMessagingServiceManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
128         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
129     }
130
131     JSValueRef JSMessagingServiceManager::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
132     {
133         return JSValueMakeUndefined(context);
134     }
135
136 JSValueRef JSMessagingServiceManager::getMessagingServices(JSContextRef context,
137         JSObjectRef object,
138         JSObjectRef thisObject,
139         size_t argumentCount,
140         const JSValueRef arguments[],
141         JSValueRef* exception)
142 {
143     LoggerD("Entered");
144
145     JSMessagingServiceManagerPriv* priv = static_cast<JSMessagingServiceManagerPriv*>(JSObjectGetPrivate(thisObject));
146     if (!priv) {
147         LoggerE("Private object is NULL.");
148         DeviceAPI::Common::UnknownException err("Private object is NULL.");
149         return JSWebAPIError::throwException(context, exception, err);
150     }
151
152     JSContextRef gContext = priv->getContext();
153     ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
154
155     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext); //create callback manager
156     callbackManager->setObject(thisObject);
157
158     try {
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));
163
164         LoggerD("Messaging Service Type: " << messagingType);
165
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.
168
169         EventGetMessagingServicePtr event(new EventGetMessagingService()); //create event
170
171         IMessagingServiceManagerPtr messagingServiceManager(priv->getObject());
172
173         event->setPrivateData(StaticPointerCast<IEventPrivateData> (callbackManager)); //callback manager
174         event->setForAsynchronousCall(&MessagingController::getInstance());
175
176         event->setEventType(EventGetMessagingService::MESSAGING_SERVICE_MANAGER_EVENT_TYPE_ALL);
177
178         event->setMessagingServiceType(messagingType);
179
180         messagingServiceManager->getMessagingServiceManager(event); //call postEvent
181         MessageAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
182     }
183     catch(const BasePlatformException& err) {
184         LoggerE(err.getMessage().c_str());
185         return JSWebAPIError::throwException(context, exception, err);
186     }
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);
191     }
192
193     return JSValueMakeUndefined(context);
194 }
195
196
197 }
198 }
199