db6fdb609e585a843eb39ca06fd51e89d1bc45ca
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Account / JSAccountManager.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        JSAccountManager.cpp
20 * @author               Jihwa Park (jh7979.park@samsung.com)
21  * @author      Sangtai Kim
22  * @version     0.1
23  */
24
25 #include <dpl/log/log.h>
26
27 #include <string>
28 #include <API/Account/IAccountService.h>
29 #include <API/Account/IAccountManager.h>
30 #include <API/Account/AccountFactory.h>
31 #include <API/Account/IEventCreateAccount.h>
32 #include <API/Account/IEventDeleteAccount.h>
33 #include <API/Account/IEventFindAccounts.h>
34 #include <API/Account/IEventUpdateAccount.h>
35 #include <API/Account/IEventFindServiceTypes.h>
36
37 #include <CommonsJavaScript/PrivateObject.h>
38 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
39 #include <CommonsJavaScript/ScopedJSStringRef.h>
40 #include <CommonsJavaScript/Utils.h>
41 #include <CommonsJavaScript/Validator.h>
42 #include <CommonsJavaScript/Converter.h>
43
44 #include <Tizen/Common/JSTizenException.h>
45 #include <Tizen/Common/JSTizenExceptionFactory.h>
46 #include <Tizen/Common/SecurityExceptions.h>
47
48 #include "JSAccount.h"
49 #include "JSAccountManager.h"
50 #include "JSAccountService.h"
51 #include "JSAccountServiceType.h"
52 #include "AccountConverter.h"
53 #include "ResponseDispatcher.h"
54 #include "plugin_config.h"
55
56 using namespace TizenApis::Api::Account;
57 using namespace WrtDeviceApis::Commons;
58 using namespace WrtDeviceApis::CommonsJavaScript;
59 using namespace TizenApis::Commons;
60
61 namespace {
62 #define TIZEN_ACCOUNT_MANAGER_ATTRIBUTENAME  "account"
63
64 #define TIZEN_ACCOUNT_PROP_TYPE_NONE  "TYPE_NONE"
65 #define TIZEN_ACCOUNT_PROP_TYPE_INTERNET "TYPE_INTERNET"
66 }
67
68 namespace TizenApis {
69 namespace Tizen1_0 {
70 namespace Account{
71 JSClassDefinition JSAccountManager::m_classInfo = {
72     0,
73     kJSClassAttributeNone,
74     TIZEN_ACCOUNT_MANAGER_ATTRIBUTENAME,
75     0,
76     m_property,
77     m_function,
78     initialize,
79     finalize,
80     NULL, //HasProperty,
81     NULL, //GetProperty,
82     NULL, //SetProperty,
83     NULL, //DeleteProperty,
84     NULL, //GetPropertyNames,
85     NULL, //CallAsFunction,
86     NULL, //CallAsConstructor,
87     NULL, //HasInstance,
88     NULL  //ConvertToType
89 };
90
91 JSStaticValue JSAccountManager::m_property[] = {
92     //event properties - recurence
93     { TIZEN_ACCOUNT_PROP_TYPE_NONE, getTypeProperty,NULL, kJSPropertyAttributeReadOnly },
94     { TIZEN_ACCOUNT_PROP_TYPE_INTERNET, getTypeProperty,NULL, kJSPropertyAttributeReadOnly },
95     { 0, 0, 0, 0 }
96 };
97
98 JSStaticFunction JSAccountManager::m_function[] = {
99         { ACCOUNT_FUNCTION_API_GET_ACCOUNT_BY_ID,             getAccountById,            kJSPropertyAttributeNone },
100         { ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID,         getServiceTypeById,        kJSPropertyAttributeNone },
101         { ACCOUNT_FUNCTION_API_GET_PROVIDER_BY_ID,            getProviderById,           kJSPropertyAttributeNone },
102         { ACCOUNT_FUNCTION_API_FIND_ACCOUNTS_BY_SERVICETYPE,  findAccountsByServiceType, kJSPropertyAttributeNone },
103         { ACCOUNT_FUNCTION_API_FIND_ACCOUNTS_BY_TAGS,         findAccountsByTags,        kJSPropertyAttributeNone },
104         { ACCOUNT_FUNCTION_API_FIND_SERVICES_BY_TAGS,         findServicesByTags,        kJSPropertyAttributeNone },
105         { ACCOUNT_FUNCTION_API_FIND_PROVIDERS,                findProviders,             kJSPropertyAttributeNone },
106         { ACCOUNT_FUNCTION_API_FIND_SERVICETYPES,             findServiceTypes,          kJSPropertyAttributeNone },
107         { ACCOUNT_FUNCTION_API_ADD_ACCOUNT,                   addAccount,                kJSPropertyAttributeNone },
108         { ACCOUNT_FUNCTION_API_ADD_LISTENER_ACCOUNTS,         addAccountlistener,        kJSPropertyAttributeNone },
109         { ACCOUNT_FUNCTION_API_REMOVE_LISTENER_ACCOUNTS,      removeAccountlistener,     kJSPropertyAttributeNone },
110         { 0, 0, 0 }
111 };
112
113 JSClassRef JSAccountManager::m_jsClassRef = JSClassCreate(
114         JSAccountManager::getClassInfo());
115
116 void JSAccountManager::initialize(JSContextRef context,
117         JSObjectRef object)
118 {
119     LogDebug("entered");
120     AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(object));
121     if (NULL == privateObject) {
122         IAccountManagerPtr accountManager = AccountFactory::getInstance().createAccountManagerObject();
123         privateObject = new AccountManagerPrivObject(context, accountManager);
124         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
125             delete privateObject;
126         }
127     }
128 }
129
130 void JSAccountManager::finalize(JSObjectRef object)
131 {
132     AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(object));
133     delete privateObject;
134 }
135
136 /**
137  *      definition
138  * Account addAccount(AccountServiceProviderId providerId, optional AccountProperties? properties);
139  */
140 JSValueRef JSAccountManager::addAccount(JSContextRef context,
141         JSObjectRef object,
142         JSObjectRef thisObject,
143         size_t argumentCount,
144         const JSValueRef arguments[],
145         JSValueRef* exception)
146 {
147         LogDebug("<<<");
148         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
149         assert(privateObject);
150
151         JSContextRef globalContext = privateObject->getContext();
152         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_ADD_ACCOUNT);
153         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
154         Try{
155                 if (argumentCount > 3) {
156                         LogError("Wrong number of parameters.");
157                         return JSTizenExceptionFactory::postException(context, exception,
158                                                         JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
159                 }
160
161                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
162                         return JSTizenExceptionFactory::postException(context, exception,
163                                                                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
164                 }
165                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
166                 converter->setProvider(arguments[0]);
167                 if(argumentCount>1){
168                         //TODO implement for AccountProperties
169 //                      EventAccountPtr account = converter->toAccount(arguments[1]);
170                 }
171
172                 std::string accountServiceProviderId = converter->convertTostring(arguments[0]);
173                 EventAccountPtr account = converter->toAccount(accountServiceProviderId);
174
175
176                 if (!account) {
177                         LogError("Failed to get an event.");
178                         return JSTizenExceptionFactory::postException(context, exception,
179                                                                                                         JSTizenException::UNKNOWN_ERROR, "Failed to get an event");
180                 }
181
182                 IEventAddAccountPtr dplEvent(new IEventAddAccount());
183                 dplEvent->setEvent(account);
184                 dplEvent->setForSynchronousCall();
185                 privateObject->getObject()->addAccount(dplEvent);
186
187                 if (dplEvent->getResult()){
188                         LogDebug(">>>");
189                         return converter->toJSValueRefAccount(dplEvent->getEvent());
190                 }else{
191                         return JSTizenExceptionFactory::postException(context, exception,
192                                                                                                         JSTizenException::UNKNOWN_ERROR, "Unknown error");
193                 }
194         }       Catch(InvalidArgumentException){
195                 LogError("Invalid argument");
196                 return JSTizenExceptionFactory::postException(context, exception,
197                                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
198         }       Catch(Exception)        {
199                 LogError("Unexpected error during adding account");
200                 return JSTizenExceptionFactory::postException(context, exception,
201                                                                                                 JSTizenException::UNKNOWN_ERROR, "Unexpected error during adding account");
202         }
203
204         return JSValueMakeNull(context);
205 }
206
207
208 /**
209  * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
210  */
211 JSValueRef getFunctionOrNull(JSContextRef ctx,
212         JSValueRef arg)
213 {
214     if (Validator(ctx).isCallback(arg))
215         {
216                 LogDebug("isCallback");
217         return arg;
218     }else if(!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg))
219     {
220                 LogDebug("not Callback");
221         ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
222     }
223     return NULL;
224 }
225
226 JSValueRef JSAccountManager::addAccountlistener(JSContextRef context,
227         JSObjectRef object,
228         JSObjectRef thisObject,
229         size_t argumentCount,
230         const JSValueRef arguments[],
231         JSValueRef* exception)
232 {
233     LogDebug("entered");
234     return JSDOMExceptionFactory::UnknownException.make(context, exception);
235 }
236
237 JSValueRef JSAccountManager::removeAccountlistener(JSContextRef context,
238         JSObjectRef object,
239         JSObjectRef thisObject,
240         size_t argumentCount,
241         const JSValueRef arguments[],
242         JSValueRef* exception)
243 {
244     LogDebug("entered");
245     return JSDOMExceptionFactory::UnknownException.make(context, exception);
246 }
247
248 JSValueRef JSAccountManager::getAccountById(JSContextRef context,
249         JSObjectRef object,
250         JSObjectRef thisObject,
251         size_t argumentCount,
252         const JSValueRef arguments[],
253         JSValueRef* exception)
254 {
255         LogDebug("<<<");
256
257         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
258         assert(privateObject);
259
260         JSContextRef globalContext = privateObject->getContext();
261         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_ACCOUNT_BY_ID);
262         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
263         Try{
264                 if (argumentCount != 1) {
265                         LogError(">>> Wrong number of parameters.");
266                         return JSTizenExceptionFactory::postException(context, exception,
267                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong argument count:"+argumentCount);
268                 }
269
270                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
271                         return JSTizenExceptionFactory::postException(context, exception,
272                                                                                                         JSTizenException::INVALID_VALUES_ERROR, "Invlid value");
273                 }
274
275                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
276
277                 EventAccountPtr account(new EventAccount());
278                 std::string accountId = converter->convertTostring(arguments[0]);
279                 account->setAccountId(accountId);
280                 if (!account) {
281                         AccountLogError(">>> Failed to get an event.");
282                         return JSTizenExceptionFactory::postException(context, exception,
283                                                                                                                                 JSTizenException::UNKNOWN_ERROR, "UNKNOWN ERROR");
284                 }
285
286                 IEventGetAccountByIdPtr dplEvent(new IEventGetAccountById());
287                 dplEvent->setEvent(account);
288                 dplEvent->setForSynchronousCall();
289                 privateObject->getObject()->getAccountById(dplEvent);
290
291                 if (dplEvent->getResult()){
292                         LogDebug(">>>");
293                         return converter->toJSValueRefAccount(dplEvent->getEvent());
294                 }else{
295                         AccountLogError("Unexpected error");
296                         return JSDOMExceptionFactory::UnknownException.make(context, exception);
297                 }
298         }       Catch(InvalidArgumentException){
299                 AccountLogError("Invalid argument");
300                 return JSTizenExceptionFactory::postException(context, exception,
301                                                                                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
302         }Catch(Exception){
303                 AccountLogError("Unexpected error");
304                 return JSTizenExceptionFactory::postException(context, exception,
305                                                                                                                                                                 JSTizenException::UNKNOWN_ERROR, "Unexpected error");
306         }
307
308         AccountLogWarning(">>> return NULL");
309         return JSValueMakeNull(context);
310 }
311
312 JSValueRef JSAccountManager::getServiceTypeById(JSContextRef context,
313         JSObjectRef object,
314         JSObjectRef thisObject,
315         size_t argumentCount,
316         const JSValueRef arguments[],
317         JSValueRef* exception)
318 {
319         LogDebug("<<<");
320
321         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
322         assert(privateObject);
323
324         JSContextRef globalContext = privateObject->getContext();
325         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID);
326         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
327         Try{
328                 if (argumentCount != 1) {
329                         AccountLogError(">>> Wrong number of parameters.");
330                         return JSTizenExceptionFactory::postException(context, exception,
331                                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
332                 }
333
334                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
335                         AccountLogError(">>> invalid value argument 0");
336                         return JSTizenExceptionFactory::postException(context, exception,
337                                                                         JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
338                 }
339
340                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
341
342                 EventAccountPtr account(new EventAccount());
343                 std::string serviceTypeId = converter->convertTostring(arguments[0]);
344                 LogDebug("serviceTypeId:[" << serviceTypeId << "]");
345                 account->setServiceTypeId(serviceTypeId);
346                 if (!account) {
347                         AccountLogError(">>> Failed to get an event.");
348                         return JSTizenExceptionFactory::postException(context, exception,
349                                                                                                 JSTizenException::UNKNOWN_ERROR, "Failed to get an event.");
350                 }
351
352                 IEventGetServiceTypeByIdPtr dplEvent(new IEventGetServiceTypeById());
353                 dplEvent->setEvent(account);
354                 dplEvent->setForSynchronousCall();
355                 privateObject->getObject()->getServiceTypeById(dplEvent);
356
357                 if (dplEvent->getResult()){
358                         JSValueRef retVal = converter->toJSValueRefAccountServiceType(dplEvent->getAccountServiceTypeProperty());
359                         LogDebug(">>> RESULT");
360                         return retVal;
361                 }       else{
362                         AccountLogError(">>> Get result fail");
363                         return JSTizenExceptionFactory::postException(context, exception,
364                                                                                                                         JSTizenException::UNKNOWN_ERROR, "Get result fail");
365                 }
366         }       Catch(InvalidArgumentException) {
367                 AccountLogError("Invalid argument");
368                 return JSTizenExceptionFactory::postException(context, exception,
369                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
370         }       Catch(Exception)        {
371                 AccountLogError("Unexpected error during adding account");
372                 return JSTizenExceptionFactory::postException(context, exception,
373                                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
374         }
375
376         AccountLogWarning(">>> return NULL");
377         return JSValueMakeNull(context);
378 }
379
380 JSValueRef JSAccountManager::getProviderById(JSContextRef context,
381         JSObjectRef object,
382         JSObjectRef thisObject,
383         size_t argumentCount,
384         const JSValueRef arguments[],
385         JSValueRef* exception)
386 {
387         LogDebug("<<<");
388         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
389         assert(privateObject);
390
391         JSContextRef globalContext = privateObject->getContext();
392         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID);
393         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
394         Try{
395                 if (argumentCount != 1) {
396                         AccountLogError(">>> Wrong number of parameters.");
397                         return JSTizenExceptionFactory::postException(context, exception,
398              JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
399                 }
400
401                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
402                         AccountLogError(">>> invalid value argument 0");
403                         return JSTizenExceptionFactory::postException(context, exception,
404              JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
405                 }
406
407                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
408
409                 EventAccountPtr account(new EventAccount());
410                 std::string accountServiceProviderId = converter->convertTostring(arguments[0]);
411                 LogDebug("accountServiceProviderId:[" << accountServiceProviderId << "]");
412
413                 account->setProviderId(accountServiceProviderId);
414                 if (!account) {
415                         AccountLogError(">>> Failed to get an event.");
416                         return JSTizenExceptionFactory::postException(context, exception,
417              JSTizenException::UNKNOWN_ERROR, "Failed to get an event.");
418                 }
419
420                 IEventGetProviderByIdPtr dplEvent(new IEventGetProviderById());
421                 dplEvent->setEvent(account);
422                 dplEvent->setForSynchronousCall();
423                 privateObject->getObject()->getProviderById(dplEvent);
424
425                 if (dplEvent->getResult()){
426                         LogDebug(">>>");
427                         JSValueRef retVal = converter->toJSValueRefAccountServiceProvider(dplEvent->getAccountServiceProviderProperty());
428                         LogDebug(">>>");
429                         return retVal;
430                 }else{
431                         AccountLogError(">>> Get result fail");
432                         return JSTizenExceptionFactory::postException(context, exception,
433              JSTizenException::UNKNOWN_ERROR, "Get result fail");
434                 }
435         }Catch(InvalidArgumentException){
436                 AccountLogError("Invalid argument");
437                 return JSTizenExceptionFactory::postException(context, exception,
438            JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
439         }       Catch(Exception)        {
440                 AccountLogError("Unexpected error during adding account");
441                 return JSTizenExceptionFactory::postException(context, exception,
442            JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
443         }
444
445         return JSValueMakeNull(context);
446 }
447
448 JSValueRef JSAccountManager::findAccountsByServiceType(JSContextRef context,
449         JSObjectRef object,
450         JSObjectRef thisObject,
451         size_t argumentCount,
452         const JSValueRef arguments[],
453         JSValueRef* exception)
454 {
455     LogDebug("entered");
456     return JSDOMExceptionFactory::UnknownException.make(context, exception);
457 }
458
459 JSValueRef JSAccountManager::findAccountsByTags(JSContextRef context,
460         JSObjectRef object,
461         JSObjectRef thisObject,
462         size_t argumentCount,
463         const JSValueRef arguments[],
464         JSValueRef* exception)
465 {
466     LogDebug("entered");
467     return JSDOMExceptionFactory::UnknownException.make(context, exception);
468 }
469
470 JSValueRef JSAccountManager::findServicesByTags(JSContextRef context,
471         JSObjectRef object,
472         JSObjectRef thisObject,
473         size_t argumentCount,
474         const JSValueRef arguments[],
475         JSValueRef* exception)
476 {
477     LogDebug("entered");
478     return JSDOMExceptionFactory::UnknownException.make(context, exception);
479 }
480
481 JSValueRef JSAccountManager::findProviders(JSContextRef context,
482         JSObjectRef object,
483         JSObjectRef thisObject,
484         size_t argumentCount,
485         const JSValueRef arguments[],
486         JSValueRef* exception)
487 {
488     LogDebug("entered!!!!");
489         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
490     assert(privateObject);
491
492         JSContextRef globalContext = privateObject->getContext();
493         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_FIND_PROVIDERS);
494         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
495     Try
496     {
497                 if (argumentCount != 1) {
498                         LogError("Wrong number of parameters.");
499                         return JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
500                 }
501
502                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]))
503                 {
504                         return JSDOMExceptionFactory::InvalidValuesException.make(privateObject->getContext());
505                         return JSValueMakeNull(context);
506                 }
507
508                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
509
510                 EventAccountPtr account(new EventAccount());
511                 std::string value = converter->convertTostring(arguments[0]);
512                 account->setServiceTypeId(value);
513                 if (!account) {
514                         LogError("Failed to get an event.");
515                         return JSDOMExceptionFactory::UnknownException.make(privateObject->getContext());
516                 }
517
518                 IEventFindProvidersPtr dplEvent(new IEventFindProviders());
519                 dplEvent->setEvent(account);
520                 dplEvent->setForSynchronousCall();
521                 privateObject->getObject()->FindProviders(dplEvent);
522
523                 if (dplEvent->getResult())
524                         return converter->toJSValueRefAccount(dplEvent->getEvent());
525                 else
526                         return JSDOMExceptionFactory::UnknownException.make(context, exception);
527     }
528     Catch(InvalidArgumentException)
529     {
530         LogError("Invalid argument");
531         return JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
532     }
533     Catch(Exception)
534     {
535                 LogError("Unexpected error during adding account");
536                 return JSDOMExceptionFactory::UnknownException.make(privateObject->getContext());
537     }
538     return JSValueMakeNull(context);
539 }
540
541 /**
542  * AccountServiceType[]? findServiceTypes(DOMString? prefix)
543  */
544 JSValueRef JSAccountManager::findServiceTypes(JSContextRef context,
545         JSObjectRef object,
546         JSObjectRef thisObject,
547         size_t argumentCount,
548         const JSValueRef arguments[],
549         JSValueRef* exception)
550 {
551         LogDebug("<<<");
552
553         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
554         assert(privateObject);
555
556         JSContextRef globalContext = privateObject->getContext();
557         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID);
558         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
559         Try{
560                 IEventFindServiceTypesPtr eventPtr(new IEventFindServiceTypes());
561
562                 if (argumentCount == 1) {
563                         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
564                                 AccountLogError(">>> invalid value argument 0");
565                                 return JSTizenExceptionFactory::postException(context, exception,
566                                                                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
567                         }
568
569                         //TODO input argument
570                         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
571                         std::string prefix = converter->convertTostring(arguments[0]);
572                         LogDebug("prefix:[" << prefix << "]");
573                         eventPtr->setPrefix(prefix);
574                 }
575
576                 eventPtr->setForSynchronousCall();
577                 privateObject->getObject()->findServiceTypes(eventPtr);
578
579                 if (eventPtr->getResult()){
580                         AccountServiceTypePropertyArrayPtr properties = eventPtr->getAccountServiceTypeProperties();
581                         if(properties){
582                                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
583                                 if (NULL == jsResult) {
584                                         ThrowMsg(NullPointerException, "Could not create js array object");
585                                 }
586
587                                 for(unsigned int i = 0; i < properties->size(); i++) {
588                                         if (!JSSetArrayElement(context, jsResult, i, JSAccountServiceType::createJSObject(context, properties->at(i)))) {
589                                                 ThrowMsg(UnknownException, "Could not insert value into js array");
590                                         }
591                                 }// for
592
593                                 LogDebug(">>>");
594                                 return jsResult;
595                         }// if(properties)
596                 }else{
597                         AccountLogError(">>> Get result fail");
598                         return JSTizenExceptionFactory::postException(context, exception,
599                JSTizenException::UNKNOWN_ERROR, "Get result fail");
600                 }
601         }       Catch(InvalidArgumentException) {
602                 AccountLogError("Invalid argument");
603                 return JSTizenExceptionFactory::postException(context, exception,
604                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
605         }       Catch(Exception)        {
606                 AccountLogError("Unexpected error during adding account");
607                 return JSTizenExceptionFactory::postException(context, exception,
608                                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
609         }
610
611         AccountLogWarning(">>> return undefined");
612         return JSValueMakeUndefined(context);
613 }
614
615 const JSClassRef JSAccountManager::getClassRef()
616 {
617     if (!m_jsClassRef) {
618         m_jsClassRef = JSClassCreate(&m_classInfo);
619     }
620     return m_jsClassRef;
621 }
622
623 const JSClassDefinition* JSAccountManager::getClassInfo()
624 {
625     return &m_classInfo;
626 }
627
628 JSValueRef JSAccountManager::getTypeProperty(JSContextRef context,
629         JSObjectRef object,
630         JSStringRef propertyName,
631         JSValueRef* exception)
632 {
633     LogDebug("entered");
634     Try
635     {
636         Converter conveter(context);
637         std::string property = conveter.toString(propertyName);
638                 if (property == TIZEN_ACCOUNT_PROP_TYPE_NONE) {
639                         return conveter.toJSValueRef(JSAccountManager::TYPE_NONE);
640                 }else if(property == TIZEN_ACCOUNT_PROP_TYPE_INTERNET) {
641                         return conveter.toJSValueRef(JSAccountManager::TYPE_INTERNET);
642                 }
643     }
644     Catch(Exception)
645     {
646         LogWarning("conversion error");
647     }
648     return JSValueMakeUndefined(context);
649 }
650
651 }
652 }
653 }
654