Beta merge 2
[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 #include <API/Account/IEventFindProviders.h>
37 #include <API/Account/IEventGetServiceByName.h>
38 #include <API/Account/IEventGetServiceById.h>
39
40 #include <CommonsJavaScript/PrivateObject.h>
41 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
42 #include <CommonsJavaScript/ScopedJSStringRef.h>
43 #include <CommonsJavaScript/Utils.h>
44 #include <CommonsJavaScript/JSUtils.h>
45 #include <CommonsJavaScript/Validator.h>
46 #include <CommonsJavaScript/Converter.h>
47
48
49 #include <Tizen/Common/JSTizenException.h>
50 #include <Tizen/Common/JSTizenExceptionFactory.h>
51 #include <Tizen/Common/SecurityExceptions.h>
52
53 #include "JSAccount.h"
54 #include "JSAccountManager.h"
55 #include "JSAccountServiceType.h"
56 #include "JSAccountServices.h"
57 #include "JSAccountServiceProvider.h"
58
59 #include "AccountConverter.h"
60 #include "ResponseDispatcher.h"
61 #include "plugin_config.h"
62
63 using namespace TizenApis::Api::Account;
64 using namespace WrtDeviceApis::Commons;
65 using namespace WrtDeviceApis::CommonsJavaScript;
66 using namespace TizenApis::Commons;
67
68 namespace {
69 #define TIZEN_ACCOUNT_MANAGER_ATTRIBUTENAME  "account"
70
71 #define TIZEN_ACCOUNT_PROP_TYPE_NONE  "TYPE_NONE"
72 #define TIZEN_ACCOUNT_PROP_TYPE_INTERNET "TYPE_INTERNET"
73 }
74
75 namespace TizenApis {
76 namespace Tizen1_0 {
77 namespace Account{
78 JSClassDefinition JSAccountManager::m_classInfo = {
79     0,
80     kJSClassAttributeNone,
81     TIZEN_ACCOUNT_MANAGER_ATTRIBUTENAME,
82     0,
83     m_property,
84     m_function,
85     initialize,
86     finalize,
87     NULL, //HasProperty,
88     NULL, //GetProperty,
89     NULL, //SetProperty,
90     NULL, //DeleteProperty,
91     NULL, //GetPropertyNames,
92     NULL, //CallAsFunction,
93     NULL, //CallAsConstructor,
94     NULL, //HasInstance,
95     NULL  //ConvertToType
96 };
97
98 JSStaticValue JSAccountManager::m_property[] = {
99     //event properties - recurence
100     { TIZEN_ACCOUNT_PROP_TYPE_NONE, getTypeProperty,NULL, kJSPropertyAttributeReadOnly },
101     { TIZEN_ACCOUNT_PROP_TYPE_INTERNET, getTypeProperty,NULL, kJSPropertyAttributeReadOnly },
102     { 0, 0, 0, 0 }
103 };
104
105 JSStaticFunction JSAccountManager::m_function[] = {
106         { ACCOUNT_FUNCTION_API_GET_ACCOUNT_BY_ID,             getAccountById,            kJSPropertyAttributeNone },
107         { ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID,         getServiceTypeById,        kJSPropertyAttributeNone },
108         { ACCOUNT_FUNCTION_API_GET_PROVIDER_BY_ID,            getProviderById,           kJSPropertyAttributeNone },
109 //      { ACCOUNT_FUNCTION_API_FIND_ACCOUNTS_BY_SERVICETYPE,  findAccountsByServiceType, kJSPropertyAttributeNone },
110 //      { ACCOUNT_FUNCTION_API_FIND_ACCOUNTS_BY_TAGS,         findAccountsByTags,        kJSPropertyAttributeNone },
111         { ACCOUNT_FUNCTION_API_FIND_ACCOUNTS,                 findAccounts,              kJSPropertyAttributeNone },
112 //      { ACCOUNT_FUNCTION_API_FIND_SERVICES_BY_TAGS,         findServicesByTags,        kJSPropertyAttributeNone },
113         { ACCOUNT_FUNCTION_API_FIND_SERVICES,                 findServices,              kJSPropertyAttributeNone },
114         { ACCOUNT_FUNCTION_API_FIND_PROVIDERS,                findProviders,             kJSPropertyAttributeNone },
115         { ACCOUNT_FUNCTION_API_FIND_SERVICETYPES,             findServiceTypes,          kJSPropertyAttributeNone },
116         { ACCOUNT_FUNCTION_API_ADD_ACCOUNT,                   addAccount,                kJSPropertyAttributeNone },
117         { ACCOUNT_FUNCTION_API_ADD_LISTENER_ACCOUNTS,         addAccountlistener,        kJSPropertyAttributeNone },
118         { ACCOUNT_FUNCTION_API_REMOVE_LISTENER_ACCOUNTS,      removeAccountlistener,     kJSPropertyAttributeNone },
119         { ACCOUNT_FUNCTION_API_GET_SERVICE_BY_ID,             getServiceById,            kJSPropertyAttributeNone },
120         { ACCOUNT_FUNCTION_API_GET_SERVICE_BY_NAME,           getServiceByName,          kJSPropertyAttributeNone },
121         { 0, 0, 0 }
122 };
123
124 JSClassRef JSAccountManager::m_jsClassRef = JSClassCreate(
125         JSAccountManager::getClassInfo());
126
127 void JSAccountManager::initialize(JSContextRef context,
128         JSObjectRef object)
129 {
130     LogDebug("entered");
131     AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(object));
132     if (NULL == privateObject) {
133         IAccountManagerPtr accountManager = AccountFactory::getInstance().createAccountManagerObject();
134         privateObject = new AccountManagerPrivObject(context, accountManager);
135         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject))) {
136             delete privateObject;
137         }
138     }
139 }
140
141 void JSAccountManager::finalize(JSObjectRef object)
142 {
143     AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(object));
144     delete privateObject;
145 }
146
147 /**
148  *      definition
149  * void addAccount(Account account)
150  */
151 JSValueRef JSAccountManager::addAccount(JSContextRef context,
152         JSObjectRef object,
153         JSObjectRef thisObject,
154         size_t argumentCount,
155         const JSValueRef arguments[],
156         JSValueRef* exception)
157 {
158         LogDebug("<<<");
159         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
160         assert(privateObject);
161
162         JSContextRef globalContext = privateObject->getContext();
163         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_ADD_ACCOUNT);
164         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165         Try{
166                 if (argumentCount != 1) {
167                         AccountLogError("Wrong number of parameters.");
168                         return JSTizenExceptionFactory::postException(context, exception,
169                                                         JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
170                 }
171
172                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])){
173                         return JSTizenExceptionFactory::postException(context, exception,
174                                                                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
175                 }
176                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
177                 EventAccountPtr account = converter->toAccount(arguments[0]);
178
179                 if (!account) {
180                         AccountLogError("Failed to get an event.");
181                         return JSTizenExceptionFactory::postException(context, exception,
182                                                                                                         JSTizenException::UNKNOWN_ERROR, "Failed to get an event");
183                 }
184
185                 IEventAddAccountPtr dplEvent(new IEventAddAccount());
186                 dplEvent->setEvent(account);
187                 dplEvent->setForSynchronousCall();
188                 privateObject->getObject()->addAccount(dplEvent);
189
190                 if (dplEvent->getResult()){
191                         LogDebug(">>>");
192                         return converter->toJSValueRefAccount(dplEvent->getEvent());
193                 }else{
194                         return JSTizenExceptionFactory::postException(context, exception,
195                                                                                                         JSTizenException::UNKNOWN_ERROR, "Unknown error");
196                 }
197         }       Catch(InvalidArgumentException){
198                 LogError("Invalid argument");
199                 return JSTizenExceptionFactory::postException(context, exception,
200                                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
201         }       Catch(Exception)        {
202                 LogError("Unexpected error during adding account");
203                 return JSTizenExceptionFactory::postException(context, exception,
204                                                                                                 JSTizenException::UNKNOWN_ERROR, "Unexpected error during adding account");
205         }
206
207         return JSValueMakeNull(context);
208 }
209
210
211 /**
212  * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
213  */
214 JSValueRef getFunctionOrNull(JSContextRef ctx,
215         JSValueRef arg)
216 {
217     if (Validator(ctx).isCallback(arg))
218         {
219                 LogDebug("isCallback");
220         return arg;
221     }else if(!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg))
222     {
223                 LogDebug("not Callback");
224         ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
225     }
226     return NULL;
227 }
228
229 JSValueRef JSAccountManager::addAccountlistener(JSContextRef context,
230         JSObjectRef object,
231         JSObjectRef thisObject,
232         size_t argumentCount,
233         const JSValueRef arguments[],
234         JSValueRef* exception)
235 {
236     LogDebug("entered");
237     return JSDOMExceptionFactory::UnknownException.make(context, exception);
238 }
239
240 JSValueRef JSAccountManager::removeAccountlistener(JSContextRef context,
241         JSObjectRef object,
242         JSObjectRef thisObject,
243         size_t argumentCount,
244         const JSValueRef arguments[],
245         JSValueRef* exception)
246 {
247     LogDebug("entered");
248     return JSDOMExceptionFactory::UnknownException.make(context, exception);
249 }
250
251 JSValueRef JSAccountManager::getAccountById(JSContextRef context,
252         JSObjectRef object,
253         JSObjectRef thisObject,
254         size_t argumentCount,
255         const JSValueRef arguments[],
256         JSValueRef* exception)
257 {
258         LogDebug("<<<");
259
260         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
261         assert(privateObject);
262
263         JSContextRef globalContext = privateObject->getContext();
264         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_ACCOUNT_BY_ID);
265         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
266         Try{
267                 if (argumentCount != 1) {
268                         LogError(">>> Wrong number of parameters.");
269                         return JSTizenExceptionFactory::postException(context, exception,
270                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong argument count:"+argumentCount);
271                 }
272
273                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
274                         return JSTizenExceptionFactory::postException(context, exception,
275                                                                                                         JSTizenException::INVALID_VALUES_ERROR, "Invlid value");
276                 }
277
278                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
279
280                 EventAccountPtr account(new EventAccount());
281                 std::string accountId = converter->convertTostring(arguments[0]);
282                 account->setAccountId(accountId);
283                 if (!account) {
284                         AccountLogError(">>> Failed to get an event.");
285                         return JSTizenExceptionFactory::postException(context, exception,
286                                                                                                                                 JSTizenException::UNKNOWN_ERROR, "UNKNOWN ERROR");
287                 }
288
289                 IEventGetAccountByIdPtr dplEvent(new IEventGetAccountById());
290                 dplEvent->setEvent(account);
291                 dplEvent->setForSynchronousCall();
292                 privateObject->getObject()->getAccountById(dplEvent);
293
294                 if (dplEvent->getResult()){
295                         LogDebug(">>>");
296                         return converter->toJSValueRefAccount(dplEvent->getEvent());
297                 }else{
298                         AccountLogError("Unexpected error");
299                         return JSDOMExceptionFactory::UnknownException.make(context, exception);
300                 }
301         }       Catch(InvalidArgumentException){
302                 AccountLogError("Invalid argument");
303                 return JSTizenExceptionFactory::postException(context, exception,
304                                                                                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
305         }Catch(Exception){
306                 AccountLogError("Unexpected error");
307                 return JSTizenExceptionFactory::postException(context, exception,
308                                                                                                                                                                 JSTizenException::UNKNOWN_ERROR, "Unexpected error");
309         }
310
311         AccountLogWarning(">>> return NULL");
312         return JSValueMakeNull(context);
313 }
314
315 JSValueRef JSAccountManager::getServiceTypeById(JSContextRef context,
316         JSObjectRef object,
317         JSObjectRef thisObject,
318         size_t argumentCount,
319         const JSValueRef arguments[],
320         JSValueRef* exception)
321 {
322         LogDebug("<<<");
323
324         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
325         assert(privateObject);
326
327         JSContextRef globalContext = privateObject->getContext();
328         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID);
329         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
330         Try{
331                 if (argumentCount != 1) {
332                         AccountLogError(">>> Wrong number of parameters.");
333                         return JSTizenExceptionFactory::postException(context, exception,
334                                                                                                 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
335                 }
336
337                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
338                         AccountLogError(">>> invalid value argument 0");
339                         return JSTizenExceptionFactory::postException(context, exception,
340                                                                         JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
341                 }
342
343                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
344
345                 EventAccountPtr account(new EventAccount());
346                 std::string serviceTypeId = converter->convertTostring(arguments[0]);
347                 LogDebug("serviceTypeId:[" << serviceTypeId << "]");
348                 account->setServiceTypeId(serviceTypeId);
349                 if (!account) {
350                         AccountLogError(">>> Failed to get an event.");
351                         return JSTizenExceptionFactory::postException(context, exception,
352                                                                                                 JSTizenException::UNKNOWN_ERROR, "Failed to get an event.");
353                 }
354
355                 IEventGetServiceTypeByIdPtr dplEvent(new IEventGetServiceTypeById());
356                 dplEvent->setEvent(account);
357                 dplEvent->setForSynchronousCall();
358                 privateObject->getObject()->getServiceTypeById(dplEvent);
359
360                 if (dplEvent->getResult()){
361                         JSValueRef retVal = converter->toJSValueRefAccountServiceType(dplEvent->getAccountServiceTypeProperty());
362                         LogDebug(">>> RESULT");
363                         return retVal;
364                 }       else{
365                         AccountLogError(">>> Get result fail");
366                         return JSTizenExceptionFactory::postException(context, exception,
367                                                                                                                         JSTizenException::UNKNOWN_ERROR, "Get result fail");
368                 }
369         }       Catch(InvalidArgumentException) {
370                 AccountLogError("Invalid argument");
371                 return JSTizenExceptionFactory::postException(context, exception,
372                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
373         }       Catch(Exception)        {
374                 AccountLogError("Unexpected error during adding account");
375                 return JSTizenExceptionFactory::postException(context, exception,
376                                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
377         }
378
379         AccountLogWarning(">>> return NULL");
380         return JSValueMakeNull(context);
381 }
382
383 JSValueRef JSAccountManager::getProviderById(JSContextRef context,
384         JSObjectRef object,
385         JSObjectRef thisObject,
386         size_t argumentCount,
387         const JSValueRef arguments[],
388         JSValueRef* exception)
389 {
390         LogDebug("<<<");
391         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
392         assert(privateObject);
393
394         JSContextRef globalContext = privateObject->getContext();
395         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID);
396         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
397         Try{
398                 if (argumentCount != 1) {
399                         AccountLogError(">>> Wrong number of parameters.");
400                         return JSTizenExceptionFactory::postException(context, exception,
401              JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
402                 }
403
404                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
405                         AccountLogError(">>> invalid value argument 0");
406                         return JSTizenExceptionFactory::postException(context, exception,
407              JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
408                 }
409
410                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
411
412                 EventAccountPtr account(new EventAccount());
413                 std::string accountServiceProviderId = converter->convertTostring(arguments[0]);
414                 LogDebug("accountServiceProviderId:[" << accountServiceProviderId << "]");
415
416                 account->setProviderId(accountServiceProviderId);
417                 if (!account) {
418                         AccountLogError(">>> Failed to get an event.");
419                         return JSTizenExceptionFactory::postException(context, exception,
420              JSTizenException::UNKNOWN_ERROR, "Failed to get an event.");
421                 }
422
423                 IEventGetProviderByIdPtr dplEvent(new IEventGetProviderById());
424                 dplEvent->setEvent(account);
425                 dplEvent->setForSynchronousCall();
426                 privateObject->getObject()->getProviderById(dplEvent);
427
428                 if (dplEvent->getResult()){
429                         JSValueRef retVal = converter->toJSValueRefAccountServiceProvider(dplEvent->getAccountServiceProviderProperty());
430                         LogDebug(">>>");
431                         return retVal;
432                 }else{
433                         AccountLogError(">>> Get result fail");
434                         return JSTizenExceptionFactory::postException(context, exception,
435              JSTizenException::UNKNOWN_ERROR, "Get result fail");
436                 }
437         }Catch(InvalidArgumentException){
438                 AccountLogError("Invalid argument");
439                 return JSTizenExceptionFactory::postException(context, exception,
440            JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
441         }       Catch(Exception)        {
442                 AccountLogError("Unexpected error during adding account");
443                 return JSTizenExceptionFactory::postException(context, exception,
444            JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
445         }
446
447         return JSValueMakeNull(context);
448 }
449
450 JSValueRef JSAccountManager::findAccountsByServiceType(JSContextRef context,
451         JSObjectRef object,
452         JSObjectRef thisObject,
453         size_t argumentCount,
454         const JSValueRef arguments[],
455         JSValueRef* exception)
456 {
457     LogDebug("entered");
458     return JSDOMExceptionFactory::UnknownException.make(context, exception);
459 }
460
461 JSValueRef JSAccountManager::findAccounts(JSContextRef context,
462         JSObjectRef object,
463         JSObjectRef thisObject,
464         size_t argumentCount,
465         const JSValueRef arguments[],
466         JSValueRef* exception)
467 {
468         LogDebug("<<<");
469         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
470         assert(privateObject);
471
472         JSContextRef globalContext = privateObject->getContext();
473         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_FIND_ACCOUNTS);
474         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
475
476         if (argumentCount > 2) {
477                 AccountLogError(">>> Wrong number of parameters.");
478                 return JSTizenExceptionFactory::postException(context, exception,
479                  JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
480         }
481         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
482
483         IEventFindAccountsPtr eventFindAccountPtr(new IEventFindAccounts());
484
485         if(argumentCount == 1){
486                 Try{
487                         //arguments[0] is filter
488                         AccountServiceFilterPropertyPtr filterPropertyPtr = converter->toAccountServiceFilterProperty(arguments[0]);
489                         eventFindAccountPtr->setFilterProperty(filterPropertyPtr);
490                 }Catch(Exception){
491                         AccountLogWarning("Excpetion occur");
492                 }
493         }
494
495         if(argumentCount == 2){
496                 //arguments[1] is enable
497                 Try{
498                         bool enable = converter->toBool(arguments[1]);
499                         eventFindAccountPtr->setEnable(enable);
500                 }Catch(Exception){
501                         AccountLogError(">>> Wrong number of parameters.");
502                         return JSTizenExceptionFactory::postException(context, exception,
503                          JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
504                 }
505         }
506
507         eventFindAccountPtr->setForSynchronousCall();
508         privateObject->getObject()->findAccounts(eventFindAccountPtr);
509
510         if (eventFindAccountPtr->getResult()){
511                 EventAccountListPtr accountList = eventFindAccountPtr->getAccountLists();
512                 if(accountList){
513                         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
514                         if(jsResult == NULL){
515                                 ThrowMsg(NullPointerException, "Could not create js array object");
516                         }
517
518                         for(unsigned int i = 0; i < accountList->size(); i++) {
519                                 if (!JSSetArrayElement(context, jsResult, i, JSAccount::createJSAccount(context, accountList->at(i)))) {
520                                                 ThrowMsg(UnknownException, "Could not insert value into js array");
521                                         }
522                         }
523                         LogDebug(">>> jsResult");
524                         return jsResult;
525                 }
526         }else{
527                 AccountLogError(">>> Get result fail");
528                 return JSTizenExceptionFactory::postException(context, exception,
529                  JSTizenException::UNKNOWN_ERROR, "Get result fail");
530         }
531
532         AccountLogWarning(">>> return undefined");
533         return JSValueMakeNull(context);
534 }
535
536 JSValueRef JSAccountManager::findServices(JSContextRef context,
537         JSObjectRef object,
538         JSObjectRef thisObject,
539         size_t argumentCount,
540         const JSValueRef arguments[],
541         JSValueRef* exception)
542 {
543         LogDebug("<<<");
544         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
545         assert(privateObject);
546
547         JSContextRef globalContext = privateObject->getContext();
548         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_FIND_SERVICES);
549         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
550
551         if (argumentCount > 2) {
552                 AccountLogError(">>> Wrong number of parameters.");
553                 return JSTizenExceptionFactory::postException(context, exception,
554                  JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
555         }
556         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
557
558         IEventFindServicesPtr eventFindServicesPtr(new IEventFindServices());
559
560         if(argumentCount == 1){
561                 Try{
562                         //arguments[0] is filter
563                         AccountServiceFilterPropertyPtr filterPropertyPtr = converter->toAccountServiceFilterProperty(arguments[0]);
564                         eventFindServicesPtr->setFilterProperty(filterPropertyPtr);
565                 }Catch(Exception){
566                         AccountLogWarning("Excpetion occur");
567                 }
568         }
569
570         if(argumentCount == 2){
571                 //arguments[1] is enable
572                 Try{
573                         bool enable = converter->toBool(arguments[1]);
574                         eventFindServicesPtr->setEnable(enable);
575                 }Catch(Exception){
576                         AccountLogError(">>> Wrong number of parameters.");
577                         return JSTizenExceptionFactory::postException(context, exception,
578                          JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
579                 }
580         }
581
582         eventFindServicesPtr->setForSynchronousCall();
583         privateObject->getObject()->findServices(eventFindServicesPtr);
584
585         if (eventFindServicesPtr->getResult()){
586                 AccountServicesArrayPtr serviceList = eventFindServicesPtr->getAccountServiceList();
587                 if(serviceList){
588                         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
589                         if(jsResult == NULL){
590                                 ThrowMsg(NullPointerException, "Could not create js array object");
591                         }
592
593                         for(unsigned int i = 0; i < serviceList->size(); i++) {
594                                 if (!JSSetArrayElement(context, jsResult, i, JSAccountServices::createJSObject(context, serviceList->at(i)))) {
595                                                 ThrowMsg(UnknownException, "Could not insert value into js array");
596                                         }
597                         }
598                         LogDebug(">>> jsResult");
599                         return jsResult;
600                 }
601         }else{
602                 AccountLogError(">>> Get result fail");
603                 return JSTizenExceptionFactory::postException(context, exception,
604                  JSTizenException::UNKNOWN_ERROR, "Get result fail");
605         }
606
607         AccountLogWarning(">>> return Null");
608         return JSValueMakeNull(context);
609 }
610
611 JSValueRef JSAccountManager::findProviders(JSContextRef context,
612         JSObjectRef object,
613         JSObjectRef thisObject,
614         size_t argumentCount,
615         const JSValueRef arguments[],
616         JSValueRef* exception)
617 {
618         LogDebug("<<<");
619
620         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
621         assert(privateObject);
622
623         JSContextRef globalContext = privateObject->getContext();
624         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_FIND_SERVICETYPES);
625         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
626
627         if (argumentCount > 1) {
628                 AccountLogError(">>> Wrong number of parameters.");
629                 return JSTizenExceptionFactory::postException(context, exception,
630                  JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
631         }
632
633         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
634
635         Try{
636                 IEventFindProvidersPtr eventFindProvidersPtr(new IEventFindProviders());
637
638                 if (!JSValueIsNull(context, arguments[0])){
639                         if(JSValueIsString(context, arguments[0])){
640                                 std::string value = converter->convertTostring(arguments[0]);
641                                 eventFindProvidersPtr->setServiceTypeId(value);
642                         }else{
643                                 AccountLogError(">>> invalid value argument 0");
644                                 return JSTizenExceptionFactory::postException(context, exception,
645                                                                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
646                         }
647                 }
648                 eventFindProvidersPtr->setForSynchronousCall();
649                 privateObject->getObject()->FindProviders(eventFindProvidersPtr);
650
651                 if (eventFindProvidersPtr->getResult()){
652                         AccountServiceProviderPropertyArrayPtr providerPropertiesPtr = eventFindProvidersPtr->getAccountServiceProviderProperties();
653                         if(providerPropertiesPtr){
654                                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
655                                 if(jsResult == NULL){
656                                         ThrowMsg(NullPointerException, "Could not create js array object");
657                                 }
658
659                                 for(unsigned int i = 0; i < providerPropertiesPtr->size(); i++) {
660                                         if (!JSSetArrayElement(context, jsResult, i, JSAccountServiceProvider::createJSObject(context, providerPropertiesPtr->at(i)))) {
661                                                         ThrowMsg(UnknownException, "Could not insert value into js array");
662                                                 }
663                                 }
664                                 LogDebug(">>> jsResult");
665                                 return jsResult;
666                         }
667                 }else{
668                         AccountLogError(">>> Get result fail");
669                         return JSTizenExceptionFactory::postException(context, exception,
670                          JSTizenException::UNKNOWN_ERROR, "Get result fail");
671                 }
672         }       Catch(InvalidArgumentException) {
673                 AccountLogError("Invalid argument");
674                 return JSTizenExceptionFactory::postException(context, exception,
675                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
676         }       Catch(Exception)        {
677                 AccountLogError("Unexpected error during adding account");
678                 return JSTizenExceptionFactory::postException(context, exception,
679             JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
680         }
681
682         return JSValueMakeNull(context);
683 }
684
685 /**
686  * AccountServiceType[]? findServiceTypes(DOMString? prefix)
687  */
688 JSValueRef JSAccountManager::findServiceTypes(JSContextRef context,
689         JSObjectRef object,
690         JSObjectRef thisObject,
691         size_t argumentCount,
692         const JSValueRef arguments[],
693         JSValueRef* exception)
694 {
695         LogDebug("<<<");
696
697         AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
698         assert(privateObject);
699
700         JSContextRef globalContext = privateObject->getContext();
701         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICETYPE_BY_ID);
702         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
703         Try{
704                 IEventFindServiceTypesPtr eventPtr(new IEventFindServiceTypes());
705
706                 if (argumentCount == 1) {
707                         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
708                                 AccountLogError(">>> invalid value argument 0");
709                                 return JSTizenExceptionFactory::postException(context, exception,
710                                                                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
711                         }
712
713                         //TODO input argument
714                         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
715                         std::string prefix = converter->convertTostring(arguments[0]);
716                         LogDebug("prefix:[" << prefix << "]");
717                         eventPtr->setPrefix(prefix);
718                 }
719
720                 eventPtr->setForSynchronousCall();
721                 privateObject->getObject()->findServiceTypes(eventPtr);
722
723                 if (eventPtr->getResult()){
724                         AccountServiceTypePropertyArrayPtr properties = eventPtr->getAccountServiceTypeProperties();
725                         if(properties){
726                                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
727                                 if (NULL == jsResult) {
728                                         ThrowMsg(NullPointerException, "Could not create js array object");
729                                 }
730
731                                 for(unsigned int i = 0; i < properties->size(); i++) {
732                                         if (!JSSetArrayElement(context, jsResult, i, JSAccountServiceType::createJSObject(context, properties->at(i)))) {
733                                                 ThrowMsg(UnknownException, "Could not insert value into js array");
734                                         }
735                                 }// for
736
737                                 LogDebug(">>>");
738                                 return jsResult;
739                         }// if(properties)
740                 }else{
741                         AccountLogError(">>> Get result fail");
742                         return JSTizenExceptionFactory::postException(context, exception,
743                JSTizenException::UNKNOWN_ERROR, "Get result fail");
744                 }
745         }       Catch(InvalidArgumentException) {
746                 AccountLogError("Invalid argument");
747                 return JSTizenExceptionFactory::postException(context, exception,
748                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
749         }       Catch(Exception)        {
750                 AccountLogError("Unexpected error during adding account");
751                 return JSTizenExceptionFactory::postException(context, exception,
752                                                                                                                                                         JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
753         }
754
755         AccountLogWarning(">>> return null");
756         return JSValueMakeNull(context);
757 }
758
759 const JSClassRef JSAccountManager::getClassRef()
760 {
761     if (!m_jsClassRef) {
762         m_jsClassRef = JSClassCreate(&m_classInfo);
763     }
764     return m_jsClassRef;
765 }
766
767 const JSClassDefinition* JSAccountManager::getClassInfo()
768 {
769     return &m_classInfo;
770 }
771
772 JSValueRef JSAccountManager::getTypeProperty(JSContextRef context,
773         JSObjectRef object,
774         JSStringRef propertyName,
775         JSValueRef* exception)
776 {
777     LogDebug("entered");
778     Try
779     {
780         Converter conveter(context);
781         std::string property = conveter.toString(propertyName);
782                 if (property == TIZEN_ACCOUNT_PROP_TYPE_NONE) {
783                         return conveter.toJSValueRef(JSAccountManager::TYPE_NONE);
784                 }else if(property == TIZEN_ACCOUNT_PROP_TYPE_INTERNET) {
785                         return conveter.toJSValueRef(JSAccountManager::TYPE_INTERNET);
786                 }
787     }
788     Catch(Exception)
789     {
790         LogWarning("conversion error");
791     }
792     return JSValueMakeUndefined(context);
793 }
794
795 JSValueRef JSAccountManager::getServiceById(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
796                 const JSValueRef arguments[], JSValueRef* exception) {
797         Try{
798                 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
799                         assert(privateObject);
800
801                         JSContextRef globalContext = privateObject->getContext();
802                         AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICE_BY_ID);
803                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
804
805                         if (argumentCount!=1) {
806                                 AccountLogError("Wrong number of parameters.");
807                                 return JSTizenExceptionFactory::postException(context, exception,
808                                                                 JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
809                         }
810
811                         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
812                                 return JSTizenExceptionFactory::postException(context, exception,
813                                                                                         JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
814                         }
815
816                         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
817                         std::string serviceId = converter->convertTostring(arguments[0]);
818                         LogDebug("<<< serviceId:[" << serviceId << "]");
819
820                         IEventGetServiceByIdPtr eventGetServiceByIdPtr(new IEventGetServiceById());
821                         eventGetServiceByIdPtr->setServiceId(serviceId);
822                         eventGetServiceByIdPtr->setForSynchronousCall();
823                         privateObject->getObject()->getServiceById(eventGetServiceByIdPtr);
824
825                         if (eventGetServiceByIdPtr->getResult()){
826                                 LogDebug(">>> ");
827                                 return JSAccountServices::createJSObject(context, eventGetServiceByIdPtr->getAccountService());
828                         }else{
829                                 AccountLogError(">>> Get result fail");
830                                 return JSTizenExceptionFactory::postException(context, exception,
831                                  JSTizenException::UNKNOWN_ERROR, "Get result fail");
832                         }
833                 }Catch(Exception){
834                         AccountLogError(">>> UNKNOWN EXCEPTION");
835                         return JSTizenExceptionFactory::postException(context, exception,
836                                                                                                         JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
837                 }
838
839                 LogDebug(">>> return null");
840                 return JSValueMakeNull(context);
841 }
842
843 JSValueRef JSAccountManager::getServiceByName(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
844                 const JSValueRef arguments[], JSValueRef* exception) {
845         Try{
846                 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
847                 assert(privateObject);
848
849                 JSContextRef globalContext = privateObject->getContext();
850                 AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_API_GET_SERVICE_BY_NAME);
851                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
852
853                 if (argumentCount!=1) {
854                         AccountLogError("Wrong number of parameters.");
855                         return JSTizenExceptionFactory::postException(context, exception,
856                                                         JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
857                 }
858
859                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
860                         return JSTizenExceptionFactory::postException(context, exception,
861                                                                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
862                 }
863
864                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
865                 std::string serviceName = converter->convertTostring(arguments[0]);
866                 LogDebug("<<< serviceName:[" << serviceName << "]");
867
868                 IEventGetServiceByNamePtr eventGetServicesByNamePtr(new IEventGetServiceByName());
869                 eventGetServicesByNamePtr->setServiceName(serviceName);
870                 eventGetServicesByNamePtr->setForSynchronousCall();
871                 privateObject->getObject()->getServiceByName(eventGetServicesByNamePtr);
872
873                 if (eventGetServicesByNamePtr->getResult()){
874                         LogDebug(">>> ");
875                         return JSAccountServices::createJSObject(context, eventGetServicesByNamePtr->getAccountService());
876                 }else{
877                         AccountLogError(">>> Get result fail");
878                         return JSTizenExceptionFactory::postException(context, exception,
879                          JSTizenException::UNKNOWN_ERROR, "Get result fail");
880                 }
881         }Catch(Exception){
882                 AccountLogError(">>> UNKNOWN EXCEPTION");
883                 return JSTizenExceptionFactory::postException(context, exception,
884                                                                                                 JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
885         }
886
887         LogDebug(">>> return null");
888         return JSValueMakeNull(context);
889 }
890
891
892 }
893 }
894 }
895