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 JSAccountService.cpp
20 * @author Jihwa Park (jh7979.park@samsung.com)
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/PrivateObject.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <API/Account/IAccountService.h>
29 #include <API/Account/AccountFactory.h>
30 #include <API/Account/IEventCreateAccount.h>
31 #include <API/Account/IEventDeleteAccount.h>
32 #include <API/Account/IEventFindAccounts.h>
33 #include <API/Account/IEventUpdateAccount.h>
36 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
37 #include <CommonsJavaScript/Utils.h>
38 #include <CommonsJavaScript/Validator.h>
39 #include <CommonsJavaScript/ScopedJSStringRef.h>
40 #include <CommonsJavaScript/SecurityExceptions.h>
43 #include "JSAccountManager.h"
44 #include "AccountConverter.h"
45 #include "JSAccountService.h"
46 #include "JSAccount.h"
47 #include "plugin_config.h"
48 #include "ResponseDispatcher.h"
50 using namespace TizenApis::Api::Account;
51 using namespace WrtDeviceApis::Commons;
52 using namespace WrtDeviceApis::CommonsJavaScript;
56 #define TIZEN_ACCOUNT_SERVICE_ATTRIBUTENAME "AccountService"
57 #define TIZEN_ACCOUNT_SERVICE_PROPERTY_TYPE "type"
58 #define TIZEN_ACCOUNT_SERVICE_PROPERTY_NAME "name"
62 * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
64 JSValueRef getFunctionOrNull(JSContextRef ctx,
67 if (Validator(ctx).isCallback(arg)) {
68 LogDebug("isCallback");
70 } else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) {
72 LogDebug("not Callback");
73 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
82 JSClassDefinition JSAccountService::m_classInfo = {
84 kJSClassAttributeNone,
85 TIZEN_ACCOUNT_SERVICE_ATTRIBUTENAME,
94 NULL, //DeleteProperty,
95 NULL, //GetPropertyNames,
96 NULL, //CallAsFunction,
97 NULL, //CallAsConstructor,
102 JSStaticValue JSAccountService::m_property[] = {
103 { TIZEN_ACCOUNT_SERVICE_PROPERTY_TYPE, JSAccountService::getPropertyType,
104 NULL, kJSPropertyAttributeReadOnly },
105 { TIZEN_ACCOUNT_SERVICE_PROPERTY_NAME, JSAccountService::getPropertyName,
106 NULL, kJSPropertyAttributeReadOnly },
110 JSStaticFunction JSAccountService::m_function[] = {
111 { "createAccount", createAccount, kJSPropertyAttributeNone },
112 { "addAccount", addAccount, kJSPropertyAttributeNone },
113 { "updateAccount", updateAccount, kJSPropertyAttributeNone },
114 { "deleteAccount", deleteAccount, kJSPropertyAttributeNone },
115 { "getAccounts", findAccounts, kJSPropertyAttributeNone },
120 JSClassRef JSAccountService::m_jsClassRef = JSClassCreate(JSAccountService::getClassInfo());
122 void JSAccountService::initialize(JSContextRef context,
126 AccountServicePrivObject *priv =
127 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(object));
129 //create default instance
130 LogWarning("create default instance");
131 IAccountServicePtr account =
132 AccountFactory::getInstance().createAccountObject();
133 priv = new AccountServicePrivObject(context, account);
134 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
138 //can be set by JSMakeObject inside getCalendars method
139 LogDebug("private object alrerady exists");
143 void JSAccountService::finalize(JSObjectRef object)
146 AccountServicePrivObject *priv =
147 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(object));
151 const JSClassRef JSAccountService::getClassRef()
154 m_jsClassRef = JSClassCreate(&m_classInfo);
159 const JSClassDefinition* JSAccountService::getClassInfo()
164 JSValueRef JSAccountService::updateAccount(JSContextRef context,
166 JSObjectRef thisObject,
167 size_t argumentCount,
168 const JSValueRef arguments[],
169 JSValueRef* exception)
173 AccountServicePrivObject *privateObject =
174 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(thisObject));
175 assert(privateObject);
177 // AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(privateObject->getContext(),ACCOUNT_FUNCTION_API_UPDATE_ACCOUNT);
181 IAccountServicePtr accountservice = getAccountService(context, thisObject, exception);
184 if (argumentCount != 1) {
185 LogError("Wrong number of parameters.");
186 return JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
191 if (JSValueIsUndefined(context, arguments[0]) ||JSValueIsNull(context, arguments[0])) {
192 return JSDOMExceptionFactory::InvalidValuesException.make(privateObject->getContext());}
195 JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
197 EventAccountPtr account = JSAccount::getIEvent(arg);
198 if (!JSAccount::validate(context, arg, exception)) {
199 LogError("account parameter contains errors");
201 return JSValueMakeNull(context);
204 AccountConverterFactory::ConverterType converter =AccountConverterFactory::getConverter(context);
205 //EventAccountPtr account = converter->toAccount(arguments[0]);
208 LogError("updateAccount: Failed to update account");
210 return JSDOMExceptionFactory::NotFoundException.make(privateObject->getContext());
213 IEventUpdateAccountPtr dplEvent(new IEventUpdateAccount());
214 dplEvent->setEvent(account);
215 dplEvent->setForSynchronousCall();
216 accountservice->updateAccount(dplEvent);
218 if (dplEvent->getResult()) {
219 return JSValueMakeNull(context);
221 LogError("Unknow error occured.");
222 return JSDOMExceptionFactory::UnknownException.make(context, exception);
225 Catch(InvalidArgumentException)
227 LogError("Invalid argument");
228 return JSDOMExceptionFactory::TypeMismatchException.make(context,
233 LogError("unknow error occured");
235 return JSValueMakeNull(context);
238 JSValueRef JSAccountService::deleteAccount(JSContextRef context,
240 JSObjectRef thisObject,
241 size_t argumentCount,
242 const JSValueRef arguments[],
243 JSValueRef* exception)
246 AccountServicePrivObject *privateObject =
247 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(thisObject));
248 assert(privateObject);
249 JSCallbackManagerPtr cbm(NULL);
251 // AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(privateObject->getContext(),ACCOUNT_FUNCTION_API_DELETE_ACCOUNT);
254 IAccountServicePtr accountservice = getAccountService(context, thisObject, exception);
255 if (argumentCount!=1) {
256 LogError("Wrong number of parameters.");
257 return JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
262 //if not able to convert to int then it won't be found
263 //TODO: Calendar has EventID function. I need to check this later
268 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
269 id = converter->toInt(converter->toString(arguments[0]));
273 LogError("invalid value of id parameter, account will not be found");
274 return JSDOMExceptionFactory::NotFoundException.make(privateObject->getContext());
276 //abstract API use event object as a filter, but only id
277 //attribute is revelant during delete operation.
278 EventAccountPtr account(new EventAccount());
280 IEventDeleteAccountPtr dplEvent(new IEventDeleteAccount());
281 dplEvent->setEvent(account);
283 dplEvent->setForSynchronousCall();
284 accountservice->deleteAccount(dplEvent);
286 if (dplEvent->getResult()) {
287 return JSValueMakeNull(context);
289 if (dplEvent->getExceptionCode() == ExceptionCodes::NotFoundException) {
290 return JSDOMExceptionFactory::NotFoundException.make(context);
292 return JSDOMExceptionFactory::UnknownException.make(context);
296 Catch(InvalidArgumentException)
298 LogError("Invalid argument");
299 return JSDOMExceptionFactory::TypeMismatchException.make(context,
304 LogError("unknow error occured");
307 return JSValueMakeNull(context);
310 JSValueRef JSAccountService::createAccount(JSContextRef context,
312 JSObjectRef thisObject,
313 size_t argumentCount,
314 const JSValueRef arguments[],
315 JSValueRef* exception)
319 AccountServicePrivObject *privateObject =
320 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(thisObject));
321 assert(privateObject);
325 // We need to use the global context for the event creation to invoke TZDate functions.
326 JSObjectRef jsObject = JSObjectMake(privateObject->getContext(),
327 JSAccount::getClassRef(), NULL);
328 if (jsObject == NULL) {
329 LogError("CreateEvent: failed to create object reference");
330 return JSDOMExceptionFactory::UnknownException.make(context, exception);
332 if (argumentCount >= 1 &&
333 !JSValueIsUndefined(context, arguments[0]) &&
334 !JSValueIsNull(context, arguments[0])) {
335 //set properties if declared
336 LogDebug("setting properties");
337 AccountConverterFactory::ConverterType converter =
338 AccountConverterFactory::getConverter(context);
339 EventAccountPtr event = converter->toAccount(arguments[0]);
342 JSAccount::setIEvent(event, privateObject->getContext(), jsObject);
345 //TODO: check if No default value for Account
348 EventAccountPtr account = JSAccount::getIEvent(jsObject);
349 event->setStartTime(std::time(NULL));
350 event->setEndTime(event->getStartTime());
351 event->setStatus(CalendarEvent::CONFIRMED_STATUS);
354 return static_cast<JSValueRef>(jsObject);
356 Catch(ConversionException)
358 LogError("conversion error");
359 return JSDOMExceptionFactory::InvalidValuesException.make(context,
364 LogError("error during execution");
366 return JSDOMExceptionFactory::UnknownException.make(context, exception);
370 JSValueRef JSAccountService::addAccount(JSContextRef context,
372 JSObjectRef thisObject,
373 size_t argumentCount,
374 const JSValueRef arguments[],
375 JSValueRef* exception)
379 AccountServicePrivObject *privateObject =
380 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(thisObject));
381 LogDebug("jcpark 1");
382 assert(privateObject);
383 //JSCallbackManagerPtr cbm(NULL);
384 LogDebug("jcpark 2");
385 //TODO: down here!! why?
386 //ACCOUNT_CHECK_ACCESS( privateObject->getContext(),ACCOUNT_FUNCTION_API_ADD_ACCOUNT);
390 IAccountServicePtr accountservice = getAccountService(context, thisObject, NULL);
392 if (argumentCount != 1) {
393 LogError("Wrong number of parameters.");
394 return JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
398 if (JSValueIsUndefined(context, arguments[0]) ||
399 JSValueIsNull(context, arguments[0])) {
400 return JSDOMExceptionFactory::InvalidValuesException.make(
401 privateObject->getContext());
402 return JSValueMakeNull(context);}
404 LogDebug("setting properties");
405 AccountConverterFactory::ConverterType converter =
406 AccountConverterFactory::getConverter(context);
407 EventAccountPtr account = converter->toAccount(arguments[0]);
410 LogError("Failed to get an event.");
411 return JSDOMExceptionFactory::UnknownException.make(privateObject->getContext());}
413 IEventAddAccountPtr dplEvent(new IEventAddAccount());
414 dplEvent->setEvent(account);
415 dplEvent->setForSynchronousCall();
416 accountservice->addAccount(dplEvent);
418 if (dplEvent->getResult()) {
419 return converter->toJSValueRef(dplEvent->getEvent()->getID());
421 return JSDOMExceptionFactory::UnknownException.make(context, exception);}
424 Catch(InvalidArgumentException)
426 LogError("Invalid argument");
427 return JSDOMExceptionFactory::TypeMismatchException.make(context,
432 LogError("Unexpected error during adding account");
433 return JSDOMExceptionFactory::UnknownException.make(privateObject->getContext());
435 return JSValueMakeNull(context);
437 JSValueRef JSAccountService::findAccounts(JSContextRef context,
439 JSObjectRef thisObject,
440 size_t argumentCount,
441 const JSValueRef arguments[],
442 JSValueRef* exception)
445 AccountServicePrivObject *privateObject =
446 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(thisObject));
447 assert(privateObject);
448 // AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(privateObject->getContext(),ACCOUNT_FUNCTION_API_FIND_ACCOUNTS);
449 JSCallbackManagerPtr cbm(NULL);
453 IAccountServicePtr account = getAccountService(context, thisObject, exception);
454 if (argumentCount < 1) {
455 LogError("No callback parameters");
456 return JSDOMExceptionFactory::TypeMismatchException.make(context,
462 1 ? getFunctionOrNull(context, arguments[1]) : NULL);
463 JSContextRef globalContext = privateObject->getContext();
466 cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
467 // ASYNC_CBM_ACCESS_STATUS_HANDLER(status, context, cbm);
469 Validator validator(context);
470 if (validator.isCallback(arguments[0])) {
471 cbm->setOnSuccess(arguments[0]);
472 } else if (JSValueIsNull(context,
474 JSValueIsUndefined(context, arguments[0])) {
475 cbm->callOnError(JSDOMExceptionFactory::InvalidValuesException.make(
477 return JSValueMakeNull(context);
479 return JSDOMExceptionFactory::TypeMismatchException.make(context,
483 /* EventFilterPtr filter(NULL);
484 if (argumentCount >= 3) {
485 LogDebug("setting some filters");
486 AccountConverterFactory::ConverterType converter =
487 AccountConverterFactory::getConverter(context);
490 if (!JSValueIsUndefined(context,
492 !JSValueIsNull(context, arguments[2])) {
493 filter = converter->toEventFilter(arguments[2]);
496 Catch(InvalidArgumentException)
498 LogError("Invalid argument");
503 IEventFindAccountsPtr dplEvent(new IEventFindAccounts());
504 dplEvent->setPrivateData(
505 DPL::StaticPointerCast<IEventPrivateData>(cbm));
506 dplEvent->setForAsynchronousCall(
507 &AccountResponseDispatcher::getInstance());
508 // dplEvent->setFilter(filter);
509 account->findAccounts(dplEvent);
511 return makePendingOperation(cbm->getContext(), dplEvent);
513 Catch(InvalidArgumentException)
515 return JSDOMExceptionFactory::TypeMismatchException.make(
518 Catch(ConversionException)
520 LogError("Invalid argument");
521 return JSDOMExceptionFactory::TypeMismatchException.make(
526 LogError("unknow error occured");
529 cbm->callOnError(JSDOMExceptionFactory::UnknownException.make(
530 privateObject->getContext()));
532 return JSValueMakeNull(context);
535 IAccountServicePtr JSAccountService::getAccountService(JSContextRef ctx,
536 const JSObjectRef object,
537 JSValueRef* exception)
539 AccountServicePrivObject *priv =
540 static_cast<AccountServicePrivObject*>(JSObjectGetPrivate(object));
542 return priv->getObject();
544 ThrowMsg(NullPointerException, "Private object is NULL.");
547 JSValueRef JSAccountService::getPropertyName(JSContextRef context,
549 JSStringRef propertyName,
550 JSValueRef* exception)
554 IAccountServicePtr accountservice = getAccountService(context, object, exception);
555 Converter converter(context);
556 return converter.toJSValueRef(accountservice->getName());
560 LogError("error during executing a function");
562 return JSValueMakeUndefined(context);
565 JSValueRef JSAccountService::getPropertyType(JSContextRef context,
567 JSStringRef propertyName,
568 JSValueRef* exception)
572 IAccountServicePtr accountservice = getAccountService(context, object, exception);
573 int accountserviceType = accountservice->getType();
574 Converter converter(context);
575 switch (accountserviceType) {
576 /* case IAccountService::TYPE_SIM:
577 return converter.toJSValueRef(
578 JSAccountManager::TYPE_SIM);
580 case IAccountService::TYPE_INTERNET:
581 return converter.toJSValueRef(JSAccountManager::TYPE_INTERNET);
584 LogWarning("invalid type of accountservice");
590 LogError("error during executing a function");
592 return JSValueMakeUndefined(context);