2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file JSAccountManager.cpp
20 * @author Jihwa Park (jh7979.park@samsung.com)
25 #include <dpl/log/log.h>
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>
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>
49 #include <Tizen/Common/JSTizenException.h>
50 #include <Tizen/Common/JSTizenExceptionFactory.h>
51 #include <Tizen/Common/SecurityExceptions.h>
53 #include "JSAccount.h"
54 #include "JSAccountManager.h"
55 #include "JSAccountServiceType.h"
56 #include "JSAccountServices.h"
57 #include "JSAccountServiceProvider.h"
59 #include "AccountConverter.h"
60 #include "ResponseDispatcher.h"
61 #include "plugin_config.h"
63 using namespace TizenApis::Api::Account;
64 using namespace WrtDeviceApis::Commons;
65 using namespace WrtDeviceApis::CommonsJavaScript;
66 using namespace TizenApis::Commons;
69 #define TIZEN_ACCOUNT_MANAGER_ATTRIBUTENAME "account"
71 #define TIZEN_ACCOUNT_PROP_TYPE_NONE "TYPE_NONE"
72 #define TIZEN_ACCOUNT_PROP_TYPE_INTERNET "TYPE_INTERNET"
78 JSClassDefinition JSAccountManager::m_classInfo = {
80 kJSClassAttributeNone,
81 TIZEN_ACCOUNT_MANAGER_ATTRIBUTENAME,
90 NULL, //DeleteProperty,
91 NULL, //GetPropertyNames,
92 NULL, //CallAsFunction,
93 NULL, //CallAsConstructor,
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 },
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 },
124 JSClassRef JSAccountManager::m_jsClassRef = JSClassCreate(
125 JSAccountManager::getClassInfo());
127 void JSAccountManager::initialize(JSContextRef context,
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;
141 void JSAccountManager::finalize(JSObjectRef object)
143 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(object));
144 delete privateObject;
149 * void addAccount(Account account)
151 JSValueRef JSAccountManager::addAccount(JSContextRef context,
153 JSObjectRef thisObject,
154 size_t argumentCount,
155 const JSValueRef arguments[],
156 JSValueRef* exception)
159 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
160 assert(privateObject);
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);
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);
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");
176 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
177 EventAccountPtr account = converter->toAccount(arguments[0]);
180 AccountLogError("Failed to get an event.");
181 return JSTizenExceptionFactory::postException(context, exception,
182 JSTizenException::UNKNOWN_ERROR, "Failed to get an event");
185 IEventAddAccountPtr dplEvent(new IEventAddAccount());
186 dplEvent->setEvent(account);
187 dplEvent->setForSynchronousCall();
188 privateObject->getObject()->addAccount(dplEvent);
190 if (dplEvent->getResult()){
192 return converter->toJSValueRefAccount(dplEvent->getEvent());
194 return JSTizenExceptionFactory::postException(context, exception,
195 JSTizenException::UNKNOWN_ERROR, "Unknown error");
197 } Catch(InvalidArgumentException){
198 LogError("Invalid argument");
199 return JSTizenExceptionFactory::postException(context, exception,
200 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
202 LogError("Unexpected error during adding account");
203 return JSTizenExceptionFactory::postException(context, exception,
204 JSTizenException::UNKNOWN_ERROR, "Unexpected error during adding account");
207 return JSValueMakeNull(context);
212 * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
214 JSValueRef getFunctionOrNull(JSContextRef ctx,
217 if (Validator(ctx).isCallback(arg))
219 LogDebug("isCallback");
221 }else if(!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg))
223 LogDebug("not Callback");
224 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
229 JSValueRef JSAccountManager::addAccountlistener(JSContextRef context,
231 JSObjectRef thisObject,
232 size_t argumentCount,
233 const JSValueRef arguments[],
234 JSValueRef* exception)
237 return JSDOMExceptionFactory::UnknownException.make(context, exception);
240 JSValueRef JSAccountManager::removeAccountlistener(JSContextRef context,
242 JSObjectRef thisObject,
243 size_t argumentCount,
244 const JSValueRef arguments[],
245 JSValueRef* exception)
248 return JSDOMExceptionFactory::UnknownException.make(context, exception);
251 JSValueRef JSAccountManager::getAccountById(JSContextRef context,
253 JSObjectRef thisObject,
254 size_t argumentCount,
255 const JSValueRef arguments[],
256 JSValueRef* exception)
260 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
261 assert(privateObject);
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);
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);
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");
278 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
280 EventAccountPtr account(new EventAccount());
281 std::string accountId = converter->convertTostring(arguments[0]);
282 account->setAccountId(accountId);
284 AccountLogError(">>> Failed to get an event.");
285 return JSTizenExceptionFactory::postException(context, exception,
286 JSTizenException::UNKNOWN_ERROR, "UNKNOWN ERROR");
289 IEventGetAccountByIdPtr dplEvent(new IEventGetAccountById());
290 dplEvent->setEvent(account);
291 dplEvent->setForSynchronousCall();
292 privateObject->getObject()->getAccountById(dplEvent);
294 if (dplEvent->getResult()){
296 return converter->toJSValueRefAccount(dplEvent->getEvent());
298 AccountLogError("Unexpected error");
299 return JSDOMExceptionFactory::UnknownException.make(context, exception);
301 } Catch(InvalidArgumentException){
302 AccountLogError("Invalid argument");
303 return JSTizenExceptionFactory::postException(context, exception,
304 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
306 AccountLogError("Unexpected error");
307 return JSTizenExceptionFactory::postException(context, exception,
308 JSTizenException::UNKNOWN_ERROR, "Unexpected error");
311 AccountLogWarning(">>> return NULL");
312 return JSValueMakeNull(context);
315 JSValueRef JSAccountManager::getServiceTypeById(JSContextRef context,
317 JSObjectRef thisObject,
318 size_t argumentCount,
319 const JSValueRef arguments[],
320 JSValueRef* exception)
324 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
325 assert(privateObject);
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);
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.");
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");
343 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
345 EventAccountPtr account(new EventAccount());
346 std::string serviceTypeId = converter->convertTostring(arguments[0]);
347 LogDebug("serviceTypeId:[" << serviceTypeId << "]");
348 account->setServiceTypeId(serviceTypeId);
350 AccountLogError(">>> Failed to get an event.");
351 return JSTizenExceptionFactory::postException(context, exception,
352 JSTizenException::UNKNOWN_ERROR, "Failed to get an event.");
355 IEventGetServiceTypeByIdPtr dplEvent(new IEventGetServiceTypeById());
356 dplEvent->setEvent(account);
357 dplEvent->setForSynchronousCall();
358 privateObject->getObject()->getServiceTypeById(dplEvent);
360 if (dplEvent->getResult()){
361 JSValueRef retVal = converter->toJSValueRefAccountServiceType(dplEvent->getAccountServiceTypeProperty());
362 LogDebug(">>> RESULT");
365 AccountLogError(">>> Get result fail");
366 return JSTizenExceptionFactory::postException(context, exception,
367 JSTizenException::UNKNOWN_ERROR, "Get result fail");
369 } Catch(InvalidArgumentException) {
370 AccountLogError("Invalid argument");
371 return JSTizenExceptionFactory::postException(context, exception,
372 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
374 AccountLogError("Unexpected error during adding account");
375 return JSTizenExceptionFactory::postException(context, exception,
376 JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
379 AccountLogWarning(">>> return NULL");
380 return JSValueMakeNull(context);
383 JSValueRef JSAccountManager::getProviderById(JSContextRef context,
385 JSObjectRef thisObject,
386 size_t argumentCount,
387 const JSValueRef arguments[],
388 JSValueRef* exception)
391 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
392 assert(privateObject);
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);
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.");
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");
410 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
412 EventAccountPtr account(new EventAccount());
413 std::string accountServiceProviderId = converter->convertTostring(arguments[0]);
414 LogDebug("accountServiceProviderId:[" << accountServiceProviderId << "]");
416 account->setProviderId(accountServiceProviderId);
418 AccountLogError(">>> Failed to get an event.");
419 return JSTizenExceptionFactory::postException(context, exception,
420 JSTizenException::UNKNOWN_ERROR, "Failed to get an event.");
423 IEventGetProviderByIdPtr dplEvent(new IEventGetProviderById());
424 dplEvent->setEvent(account);
425 dplEvent->setForSynchronousCall();
426 privateObject->getObject()->getProviderById(dplEvent);
428 if (dplEvent->getResult()){
429 JSValueRef retVal = converter->toJSValueRefAccountServiceProvider(dplEvent->getAccountServiceProviderProperty());
433 AccountLogError(">>> Get result fail");
434 return JSTizenExceptionFactory::postException(context, exception,
435 JSTizenException::UNKNOWN_ERROR, "Get result fail");
437 }Catch(InvalidArgumentException){
438 AccountLogError("Invalid argument");
439 return JSTizenExceptionFactory::postException(context, exception,
440 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
442 AccountLogError("Unexpected error during adding account");
443 return JSTizenExceptionFactory::postException(context, exception,
444 JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
447 return JSValueMakeNull(context);
450 JSValueRef JSAccountManager::findAccountsByServiceType(JSContextRef context,
452 JSObjectRef thisObject,
453 size_t argumentCount,
454 const JSValueRef arguments[],
455 JSValueRef* exception)
458 return JSDOMExceptionFactory::UnknownException.make(context, exception);
461 JSValueRef JSAccountManager::findAccounts(JSContextRef context,
463 JSObjectRef thisObject,
464 size_t argumentCount,
465 const JSValueRef arguments[],
466 JSValueRef* exception)
469 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
470 assert(privateObject);
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);
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.");
481 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
483 IEventFindAccountsPtr eventFindAccountPtr(new IEventFindAccounts());
485 if(argumentCount == 1){
487 //arguments[0] is filter
488 AccountServiceFilterPropertyPtr filterPropertyPtr = converter->toAccountServiceFilterProperty(arguments[0]);
489 eventFindAccountPtr->setFilterProperty(filterPropertyPtr);
491 AccountLogWarning("Excpetion occur");
495 if(argumentCount == 2){
496 //arguments[1] is enable
498 bool enable = converter->toBool(arguments[1]);
499 eventFindAccountPtr->setEnable(enable);
501 AccountLogError(">>> Wrong number of parameters.");
502 return JSTizenExceptionFactory::postException(context, exception,
503 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
507 eventFindAccountPtr->setForSynchronousCall();
508 privateObject->getObject()->findAccounts(eventFindAccountPtr);
510 if (eventFindAccountPtr->getResult()){
511 EventAccountListPtr accountList = eventFindAccountPtr->getAccountLists();
513 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
514 if(jsResult == NULL){
515 ThrowMsg(NullPointerException, "Could not create js array object");
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");
523 LogDebug(">>> jsResult");
527 AccountLogError(">>> Get result fail");
528 return JSTizenExceptionFactory::postException(context, exception,
529 JSTizenException::UNKNOWN_ERROR, "Get result fail");
532 AccountLogWarning(">>> return undefined");
533 return JSValueMakeNull(context);
536 JSValueRef JSAccountManager::findServices(JSContextRef context,
538 JSObjectRef thisObject,
539 size_t argumentCount,
540 const JSValueRef arguments[],
541 JSValueRef* exception)
544 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
545 assert(privateObject);
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);
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.");
556 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
558 IEventFindServicesPtr eventFindServicesPtr(new IEventFindServices());
560 if(argumentCount == 1){
562 //arguments[0] is filter
563 AccountServiceFilterPropertyPtr filterPropertyPtr = converter->toAccountServiceFilterProperty(arguments[0]);
564 eventFindServicesPtr->setFilterProperty(filterPropertyPtr);
566 AccountLogWarning("Excpetion occur");
570 if(argumentCount == 2){
571 //arguments[1] is enable
573 bool enable = converter->toBool(arguments[1]);
574 eventFindServicesPtr->setEnable(enable);
576 AccountLogError(">>> Wrong number of parameters.");
577 return JSTizenExceptionFactory::postException(context, exception,
578 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong number of parameters.");
582 eventFindServicesPtr->setForSynchronousCall();
583 privateObject->getObject()->findServices(eventFindServicesPtr);
585 if (eventFindServicesPtr->getResult()){
586 AccountServicesArrayPtr serviceList = eventFindServicesPtr->getAccountServiceList();
588 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
589 if(jsResult == NULL){
590 ThrowMsg(NullPointerException, "Could not create js array object");
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");
598 LogDebug(">>> jsResult");
602 AccountLogError(">>> Get result fail");
603 return JSTizenExceptionFactory::postException(context, exception,
604 JSTizenException::UNKNOWN_ERROR, "Get result fail");
607 AccountLogWarning(">>> return Null");
608 return JSValueMakeNull(context);
611 JSValueRef JSAccountManager::findProviders(JSContextRef context,
613 JSObjectRef thisObject,
614 size_t argumentCount,
615 const JSValueRef arguments[],
616 JSValueRef* exception)
620 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
621 assert(privateObject);
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);
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.");
633 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
636 IEventFindProvidersPtr eventFindProvidersPtr(new IEventFindProviders());
638 if (!JSValueIsNull(context, arguments[0])){
639 if(JSValueIsString(context, arguments[0])){
640 std::string value = converter->convertTostring(arguments[0]);
641 eventFindProvidersPtr->setServiceTypeId(value);
643 AccountLogError(">>> invalid value argument 0");
644 return JSTizenExceptionFactory::postException(context, exception,
645 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
648 eventFindProvidersPtr->setForSynchronousCall();
649 privateObject->getObject()->FindProviders(eventFindProvidersPtr);
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");
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");
664 LogDebug(">>> jsResult");
668 AccountLogError(">>> Get result fail");
669 return JSTizenExceptionFactory::postException(context, exception,
670 JSTizenException::UNKNOWN_ERROR, "Get result fail");
672 } Catch(InvalidArgumentException) {
673 AccountLogError("Invalid argument");
674 return JSTizenExceptionFactory::postException(context, exception,
675 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
677 AccountLogError("Unexpected error during adding account");
678 return JSTizenExceptionFactory::postException(context, exception,
679 JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
682 return JSValueMakeNull(context);
686 * AccountServiceType[]? findServiceTypes(DOMString? prefix)
688 JSValueRef JSAccountManager::findServiceTypes(JSContextRef context,
690 JSObjectRef thisObject,
691 size_t argumentCount,
692 const JSValueRef arguments[],
693 JSValueRef* exception)
697 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
698 assert(privateObject);
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);
704 IEventFindServiceTypesPtr eventPtr(new IEventFindServiceTypes());
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");
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);
720 eventPtr->setForSynchronousCall();
721 privateObject->getObject()->findServiceTypes(eventPtr);
723 if (eventPtr->getResult()){
724 AccountServiceTypePropertyArrayPtr properties = eventPtr->getAccountServiceTypeProperties();
726 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
727 if (NULL == jsResult) {
728 ThrowMsg(NullPointerException, "Could not create js array object");
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");
741 AccountLogError(">>> Get result fail");
742 return JSTizenExceptionFactory::postException(context, exception,
743 JSTizenException::UNKNOWN_ERROR, "Get result fail");
745 } Catch(InvalidArgumentException) {
746 AccountLogError("Invalid argument");
747 return JSTizenExceptionFactory::postException(context, exception,
748 JSTizenException::TYPE_MISMATCH_ERROR, "Invalid argument");
750 AccountLogError("Unexpected error during adding account");
751 return JSTizenExceptionFactory::postException(context, exception,
752 JSTizenException::TYPE_MISMATCH_ERROR, "Unexpected error during adding account");
755 AccountLogWarning(">>> return null");
756 return JSValueMakeNull(context);
759 const JSClassRef JSAccountManager::getClassRef()
762 m_jsClassRef = JSClassCreate(&m_classInfo);
767 const JSClassDefinition* JSAccountManager::getClassInfo()
772 JSValueRef JSAccountManager::getTypeProperty(JSContextRef context,
774 JSStringRef propertyName,
775 JSValueRef* exception)
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);
790 LogWarning("conversion error");
792 return JSValueMakeUndefined(context);
795 JSValueRef JSAccountManager::getServiceById(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
796 const JSValueRef arguments[], JSValueRef* exception) {
798 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
799 assert(privateObject);
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);
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);
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");
816 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
817 std::string serviceId = converter->convertTostring(arguments[0]);
818 LogDebug("<<< serviceId:[" << serviceId << "]");
820 IEventGetServiceByIdPtr eventGetServiceByIdPtr(new IEventGetServiceById());
821 eventGetServiceByIdPtr->setServiceId(serviceId);
822 eventGetServiceByIdPtr->setForSynchronousCall();
823 privateObject->getObject()->getServiceById(eventGetServiceByIdPtr);
825 if (eventGetServiceByIdPtr->getResult()){
827 return JSAccountServices::createJSObject(context, eventGetServiceByIdPtr->getAccountService());
829 AccountLogError(">>> Get result fail");
830 return JSTizenExceptionFactory::postException(context, exception,
831 JSTizenException::UNKNOWN_ERROR, "Get result fail");
834 AccountLogError(">>> UNKNOWN EXCEPTION");
835 return JSTizenExceptionFactory::postException(context, exception,
836 JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
839 LogDebug(">>> return null");
840 return JSValueMakeNull(context);
843 JSValueRef JSAccountManager::getServiceByName(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
844 const JSValueRef arguments[], JSValueRef* exception) {
846 AccountManagerPrivObject *privateObject = static_cast<AccountManagerPrivObject*>(JSObjectGetPrivate(thisObject));
847 assert(privateObject);
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);
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);
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");
864 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
865 std::string serviceName = converter->convertTostring(arguments[0]);
866 LogDebug("<<< serviceName:[" << serviceName << "]");
868 IEventGetServiceByNamePtr eventGetServicesByNamePtr(new IEventGetServiceByName());
869 eventGetServicesByNamePtr->setServiceName(serviceName);
870 eventGetServicesByNamePtr->setForSynchronousCall();
871 privateObject->getObject()->getServiceByName(eventGetServicesByNamePtr);
873 if (eventGetServicesByNamePtr->getResult()){
875 return JSAccountServices::createJSObject(context, eventGetServicesByNamePtr->getAccountService());
877 AccountLogError(">>> Get result fail");
878 return JSTizenExceptionFactory::postException(context, exception,
879 JSTizenException::UNKNOWN_ERROR, "Get result fail");
882 AccountLogError(">>> UNKNOWN EXCEPTION");
883 return JSTizenExceptionFactory::postException(context, exception,
884 JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
887 LogDebug(">>> return null");
888 return JSValueMakeNull(context);