7b4e570a82e227903482b998c9f77def22cb0c18
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Account / JSAccount.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 /*
19  * @file        JSAccount.cpp
20  *
21  * @version     0.1
22  */
23
24 #include "JSAccount.h"
25 #include "JSFeatureArray.h"
26 #include "JSAccountServices.h"
27 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include "AccountConverter.h"
30 #include "plugin_config.h"
31
32 #include <Tizen/Common/JSTizenException.h>
33 #include <Tizen/Common/JSTizenExceptionFactory.h>
34 #include <Tizen/Common/SecurityExceptions.h>
35
36 using namespace TizenApis::Api::Account;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Commons;
40
41 namespace TizenApis {
42 namespace Tizen1_0 {
43 namespace Account{
44
45 namespace {
46
47 #define ACCOUNT_ATTRIBUTE_NAME  "Account"
48
49 #define ACCOUNT_ATTRIBUTE_ACCOUNT_ID  "id"
50 #define ACCOUNT_ATTRIBUTE_DISPLAY_NAME  "displayName"
51 #define ACCOUNT_ATTRIBUTE_ICON_PATH  "icon"
52 #define ACCOUNT_ATTRIBUTE_ENABLED  "enabled"
53 #define ACCOUNT_ATTRIBUTE_PROVIDER_ID  "providerId"
54 #define ACCOUNT_ATTRIBUTE_CREDENTIAL_ID  "credentialId"
55 #define ACCOUNT_ATTRIBUTE_SERVICES  "services"
56 #define ACCOUNT_ATTRIBUTE_SETTINGS  "settings"
57
58 #define ACCOUNT_FUNCTION_GET_SERVICE_BY_ID    "getServiceById"
59 #define ACCOUNT_FUNCTION_GET_SERVICE_BY_NAME  "getServiceByName"
60
61 }
62
63 JSClassDefinition JSAccount::m_classInfo = {
64     0,
65     kJSClassAttributeNone,
66     ACCOUNT_ATTRIBUTE_NAME,
67     0,
68     m_property,
69     m_function, //m_function,
70     initialize,
71     finalize,
72     NULL, //hasProperty,
73     NULL, //getProperty,
74     NULL, //setProperty,
75     NULL, //deleteProperty,
76     NULL, //getPropertyNames,
77     NULL, //callAsFunction,
78     NULL, //callAsConstructor,
79     hasInstance, //hasInstance,
80     NULL, //convertToType,
81 };
82
83 JSStaticValue JSAccount::m_property[] = {
84     { ACCOUNT_ATTRIBUTE_ACCOUNT_ID, getProperty, setProperty, kJSPropertyAttributeNone },
85     { ACCOUNT_ATTRIBUTE_DISPLAY_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
86     { ACCOUNT_ATTRIBUTE_ICON_PATH, getProperty, setProperty, kJSPropertyAttributeNone },
87     { ACCOUNT_ATTRIBUTE_ENABLED, getProperty, setProperty, kJSPropertyAttributeNone },
88     { ACCOUNT_ATTRIBUTE_PROVIDER_ID, getProperty, setProperty, kJSPropertyAttributeNone },
89     { ACCOUNT_ATTRIBUTE_CREDENTIAL_ID, getProperty, setProperty, kJSPropertyAttributeNone },
90     { ACCOUNT_ATTRIBUTE_SERVICES, getServices, setServices, kJSPropertyAttributeNone },
91     { ACCOUNT_ATTRIBUTE_SETTINGS, getProperty, setProperty, kJSPropertyAttributeNone },
92     { 0, 0, 0, 0 }
93 };
94
95 JSStaticFunction JSAccount::m_function[] = {
96         { ACCOUNT_FUNCTION_GET_SERVICE_BY_ID,   JSAccount::getServiceById,   kJSPropertyAttributeNone },
97         { ACCOUNT_FUNCTION_GET_SERVICE_BY_NAME, JSAccount::getServiceByName, kJSPropertyAttributeNone },
98         { 0, 0, 0 }
99 };
100
101
102
103 JSClassRef JSAccount::m_jsClassRef = JSClassCreate(
104         JSAccount::getClassInfo());
105
106 const JSClassDefinition* JSAccount::getClassInfo()
107 {
108     return &(m_classInfo);
109 }
110
111 JSClassRef JSAccount::getClassRef()
112 {
113     if (!m_jsClassRef) {
114         m_jsClassRef = JSClassCreate(&m_classInfo);
115     }
116     return m_jsClassRef;
117 }
118
119 EventAccountPtr JSAccount::getIEvent(JSObjectRef object)
120 {
121     LogDebug("entered");
122     AccountPrivateObject *priv =
123         static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
124     if (!priv) {
125         ThrowMsg(NullPointerException, "Private object is null");
126     }
127     EventAccountPtr result = priv->getObject();
128     if (!result) {
129         ThrowMsg(NullPointerException, "Private object is null");
130     }
131     return result;
132 }
133
134 void JSAccount::setIEvent(const EventAccountPtr &event,
135         JSContextRef ctx,
136         const JSObjectRef object)
137 {
138     LogDebug("entered");
139     Try
140     {
141         AccountPrivateObject *priv =
142             static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
143         delete priv;
144         priv = new AccountPrivateObject(ctx, event);
145         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
146             delete priv;
147         }
148     }
149     Catch(Exception)
150     {
151         LogError("Error during replacing account object");
152     }
153 }
154
155
156 bool JSAccount::validate(JSContextRef ctx,
157         const JSObjectRef object,
158         JSValueRef* exception)
159 {
160     LogDebug("entered");
161     AccountPrivateObject *priv =
162         static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
163     if (priv == NULL) {
164         return false;
165     }
166     EventAccountPtr account = priv->getObject();
167     if (!account) {
168         return false;
169     }
170     return account->validate();
171 }
172
173
174 JSObjectRef JSAccount::createJSAccount(JSContextRef context, EventAccountPtr account)
175 {
176         LogDebug("<<< ");
177         AccountPrivateObject *priv = new AccountPrivateObject(context, account);
178         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
179         return jsValueRef;
180 }
181
182 void JSAccount::initialize(JSContextRef context,
183         JSObjectRef object)
184 {
185         LogDebug("<<< ");
186
187         AccountPrivateObject *privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
188         if (NULL == privateObject) {
189                 LogDebug("privateObject is NULL");
190         }
191 }
192
193 void JSAccount::finalize(JSObjectRef object)
194 {
195     LogDebug("enter");
196     AccountPrivateObject* priv =
197         static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
198     delete priv;
199     JSObjectSetPrivate(object, NULL);
200 }
201
202
203 JSValueRef JSAccount::getProperty(JSContextRef context,
204         JSObjectRef object,
205         JSStringRef propertyName,
206         JSValueRef* exception)
207 {
208     LogDebug("<<<");
209     AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
210     Try
211     {
212         AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
213         if (!priv) {
214             Throw(NullPointerException);
215         }
216         EventAccountPtr account = priv->getObject();
217
218         if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ACCOUNT_ID)) {
219             return converter->toJSValueRef(account->getAccountId());
220                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
221                         return converter->toJSValueRef(account->getDisplayName());
222                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
223                         return converter->toJSValueRef(account->getIconPath());
224                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ENABLED)) {
225                         return converter->toJSValueRef(account->getEnabled());
226                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_PROVIDER_ID)) {
227                         return converter->toJSValueRef(account->getProviderName());
228                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_CREDENTIAL_ID)) {
229                         return converter->toJSValueRefLong(account->getCredentailId());
230                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_SETTINGS)) {
231                         return converter->toJSValueRef(account->getSettings());
232                 }
233     }
234     Catch(Exception)
235     {
236         LogError("invalid property");
237     }
238     return JSValueMakeUndefined(context);
239 }
240
241 bool JSAccount::setProperty(JSContextRef context,
242         JSObjectRef object,
243         JSStringRef propertyName,
244         JSValueRef value,
245         JSValueRef* exception)
246 {
247     LogDebug("entered");
248     AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
249     Try
250     {
251         AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
252         if (!priv) {
253             Throw(NullPointerException);
254         }
255         EventAccountPtr account = priv->getObject();
256
257         if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
258             std::string displayname = converter->toString(value);
259             account->setDisplayName(displayname);
260             return true;
261         } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
262             std::string iconPath = converter->toString(value);
263             account->setIconPath(iconPath);
264             return true;
265         }
266     }
267     Catch(Exception)
268     {
269         LogWarning("trying to set incorrect value");
270     }
271     JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
272     return false;
273 }
274
275 JSValueRef JSAccount::getServices(JSContextRef context,
276         JSObjectRef object,
277         JSStringRef propertyName,
278         JSValueRef* exception)
279 {
280         LogDebug("<<<");
281
282         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
283         Try{
284                 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
285                 if (!priv) {
286                         Throw(NullPointerException);
287                 }
288                 EventAccountPtr account = priv->getObject();
289
290                 AccountServicesArrayPtr services = account->getService();
291                 if (services) {
292                         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
293                         if (NULL == jsResult) {
294                                 ThrowMsg(NullPointerException, "Could not create js array object");
295                         }
296                         for(unsigned int i = 0; i < services->size(); i++) {
297                                 if (!JSSetArrayElement(context, jsResult, i, JSAccountServices::createJSObject(context, services->at(i)))) {
298                                         ThrowMsg(UnknownException, "Could not insert value into js array");
299                                 }
300                         }
301
302                         LogDebug(">>> jsResult");
303                         return jsResult;
304                 }
305         }       Catch(Exception)        {
306                 LogWarning("trying to get incorrect value");
307         }
308
309         LogDebug(">>> undefined");
310         return JSValueMakeUndefined(context);
311 }
312
313 bool JSAccount::setServices(JSContextRef context,
314         JSObjectRef object,
315         JSStringRef propertyName,
316         JSValueRef value,
317         JSValueRef* exception)
318 {
319     LogDebug("entered");
320
321     AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
322     Try
323     {
324         AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
325         if (!priv) {
326             Throw(NullPointerException);
327         }
328         EventAccountPtr account = priv->getObject();
329         account->setService(converter->toAccountService(value));
330         return true;
331     }
332     Catch(Exception)
333     {
334         LogWarning("trying to set incorrect value");
335     }
336     JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
337     return false;
338 }
339
340 JSValueRef JSAccount::getServiceById(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
341                 const JSValueRef arguments[], JSValueRef* exception) {
342         Try{
343                         AccountPrivateObject* privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(thisObject));
344                         assert(privateObject);
345
346                         JSContextRef globalContext = privateObject->getContext();
347                         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_GET_SERVICE_BY_ID);
348                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
349
350                         if (argumentCount!=1) {
351                                 AccountLogError("Wrong number of parameters.");
352                                 return JSTizenExceptionFactory::postException(context, exception,
353                                                                 JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
354                         }
355
356                         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
357                                 return JSTizenExceptionFactory::postException(context, exception,
358                                                                                         JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
359                         }
360
361                         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
362                         std::string serviceId = converter->convertTostring(arguments[0]);
363                         LogDebug("<<< serviceId:[" << serviceId << "]");
364
365                         EventAccountPtr account = privateObject->getObject();
366                         AccountServicesArrayPtr services = account->getService();
367                         if (services) {
368                                 for(unsigned int i = 0; i < services->size(); i++) {
369                                         std::string tmpServiceId = services->at(i)->getId();
370                                         LogDebug("tmpServiceId:[" << tmpServiceId << "]");
371                                         if(tmpServiceId.compare(serviceId)==0){
372                                                 LogDebug(">>> return JSAccoutServices");
373                                                 return JSAccountServices::createJSObject(context, services->at(i));
374                                         }
375                                 }
376
377                                 AccountLogWarning("not supported service name:[" << serviceId << "]");
378                         }else{
379                                 AccountLogWarning("services is NULL");
380                         }
381                 }Catch(Exception){
382                         LogDebug(">>> UNKNOWN EXCEPTION");
383                         return JSTizenExceptionFactory::postException(context, exception,
384                                                                                                         JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
385                 }
386
387                 LogDebug(">>> return null");
388                 return JSValueMakeNull(context);
389 }
390
391 JSValueRef JSAccount::getServiceByName(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
392                 const JSValueRef arguments[], JSValueRef* exception) {
393         Try{
394                 AccountPrivateObject* privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(thisObject));
395                 assert(privateObject);
396
397                 JSContextRef globalContext = privateObject->getContext();
398                 AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_GET_SERVICE_BY_NAME);
399                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
400
401                 if (argumentCount!=1) {
402                         AccountLogError("Wrong number of parameters.");
403                         return JSTizenExceptionFactory::postException(context, exception,
404                                                         JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
405                 }
406
407                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
408                         return JSTizenExceptionFactory::postException(context, exception,
409                                                                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
410                 }
411
412                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
413                 std::string serviceName = converter->convertTostring(arguments[0]);
414                 LogDebug("<<< serviceName:[" << serviceName << "]");
415
416                 EventAccountPtr account = privateObject->getObject();
417                 AccountServicesArrayPtr services = account->getService();
418                 if (services) {
419                         for(unsigned int i = 0; i < services->size(); i++) {
420                                 std::string tmpServiceName = services->at(i)->getName();
421                                 LogDebug("tmpServiceName:[" << tmpServiceName << "]");
422                                 if(tmpServiceName.compare(serviceName)==0){
423                                         LogDebug(">>> return JSAccoutServices");
424                                         return JSAccountServices::createJSObject(context, services->at(i));
425                                 }
426                         }
427
428                         AccountLogWarning("not supported service name:[" << serviceName << "]");
429                 }else{
430                         AccountLogWarning("services is NULL");
431                 }
432         }Catch(Exception){
433                 LogDebug(">>> UNKNOWN EXCEPTION");
434                 return JSTizenExceptionFactory::postException(context, exception,
435                                                                                                 JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
436         }
437
438         LogDebug(">>> return null");
439         return JSValueMakeNull(context);
440 }
441
442 bool JSAccount::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
443         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
444 }
445
446 }
447 }
448 }