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.
24 #include "JSAccount.h"
25 #include "JSFeatureArray.h"
26 #include "JSAccountServices.h"
27 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include "AccountConverter.h"
30 #include "plugin_config.h"
32 #include <Tizen/Common/JSTizenException.h>
33 #include <Tizen/Common/JSTizenExceptionFactory.h>
34 #include <Tizen/Common/SecurityExceptions.h>
36 using namespace TizenApis::Api::Account;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Commons;
47 #define ACCOUNT_ATTRIBUTE_NAME "Account"
49 #define ACCOUNT_ATTRIBUTE_ACCOUNT_ID "id"
50 #define ACCOUNT_ATTRIBUTE_DISPLAY_NAME "displayName"
51 #define ACCOUNT_ATTRIBUTE_ICON_PATH "icon"
52 #define ACCOUNT_ATTRIBUTE_ENABLED "enabled"
53 #define ACCOUNT_ATTRIBUTE_PROVIDER_ID "providerId"
54 #define ACCOUNT_ATTRIBUTE_CREDENTIAL_ID "credentialId"
55 #define ACCOUNT_ATTRIBUTE_SERVICES "services"
56 #define ACCOUNT_ATTRIBUTE_SETTINGS "settings"
58 #define ACCOUNT_FUNCTION_GET_SERVICE_BY_ID "getServiceById"
59 #define ACCOUNT_FUNCTION_GET_SERVICE_BY_NAME "getServiceByName"
63 JSClassDefinition JSAccount::m_classInfo = {
65 kJSClassAttributeNone,
66 ACCOUNT_ATTRIBUTE_NAME,
69 m_function, //m_function,
75 NULL, //deleteProperty,
76 NULL, //getPropertyNames,
77 NULL, //callAsFunction,
78 NULL, //callAsConstructor,
79 hasInstance, //hasInstance,
80 NULL, //convertToType,
83 JSStaticValue JSAccount::m_property[] = {
84 { ACCOUNT_ATTRIBUTE_ACCOUNT_ID, getProperty, setProperty, kJSPropertyAttributeNone },
85 { ACCOUNT_ATTRIBUTE_DISPLAY_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
86 { ACCOUNT_ATTRIBUTE_ICON_PATH, getProperty, setProperty, kJSPropertyAttributeNone },
87 { ACCOUNT_ATTRIBUTE_ENABLED, getProperty, setProperty, kJSPropertyAttributeNone },
88 { ACCOUNT_ATTRIBUTE_PROVIDER_ID, getProperty, setProperty, kJSPropertyAttributeNone },
89 { ACCOUNT_ATTRIBUTE_CREDENTIAL_ID, getProperty, setProperty, kJSPropertyAttributeNone },
90 { ACCOUNT_ATTRIBUTE_SERVICES, getServices, setServices, kJSPropertyAttributeNone },
91 { ACCOUNT_ATTRIBUTE_SETTINGS, getProperty, setProperty, kJSPropertyAttributeNone },
95 JSStaticFunction JSAccount::m_function[] = {
96 { ACCOUNT_FUNCTION_GET_SERVICE_BY_ID, JSAccount::getServiceById, kJSPropertyAttributeNone },
97 { ACCOUNT_FUNCTION_GET_SERVICE_BY_NAME, JSAccount::getServiceByName, kJSPropertyAttributeNone },
103 JSClassRef JSAccount::m_jsClassRef = JSClassCreate(
104 JSAccount::getClassInfo());
106 const JSClassDefinition* JSAccount::getClassInfo()
108 return &(m_classInfo);
111 JSClassRef JSAccount::getClassRef()
114 m_jsClassRef = JSClassCreate(&m_classInfo);
119 EventAccountPtr JSAccount::getIEvent(JSObjectRef object)
122 AccountPrivateObject *priv =
123 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
125 ThrowMsg(NullPointerException, "Private object is null");
127 EventAccountPtr result = priv->getObject();
129 ThrowMsg(NullPointerException, "Private object is null");
134 void JSAccount::setIEvent(const EventAccountPtr &event,
136 const JSObjectRef object)
141 AccountPrivateObject *priv =
142 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
144 priv = new AccountPrivateObject(ctx, event);
145 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
151 LogError("Error during replacing account object");
156 bool JSAccount::validate(JSContextRef ctx,
157 const JSObjectRef object,
158 JSValueRef* exception)
161 AccountPrivateObject *priv =
162 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
166 EventAccountPtr account = priv->getObject();
170 return account->validate();
174 JSObjectRef JSAccount::createJSAccount(JSContextRef context, EventAccountPtr account)
177 AccountPrivateObject *priv = new AccountPrivateObject(context, account);
178 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
182 void JSAccount::initialize(JSContextRef context,
187 AccountPrivateObject *privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
188 if (NULL == privateObject) {
189 LogDebug("privateObject is NULL");
193 void JSAccount::finalize(JSObjectRef object)
196 AccountPrivateObject* priv =
197 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
199 JSObjectSetPrivate(object, NULL);
203 JSValueRef JSAccount::getProperty(JSContextRef context,
205 JSStringRef propertyName,
206 JSValueRef* exception)
209 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
212 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
214 Throw(NullPointerException);
216 EventAccountPtr account = priv->getObject();
218 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ACCOUNT_ID)) {
219 return converter->toJSValueRef(account->getAccountId());
220 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
221 return converter->toJSValueRef(account->getDisplayName());
222 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
223 return converter->toJSValueRef(account->getIconPath());
224 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ENABLED)) {
225 return converter->toJSValueRef(account->getEnabled());
226 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_PROVIDER_ID)) {
227 return converter->toJSValueRef(account->getProviderName());
228 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_CREDENTIAL_ID)) {
229 return converter->toJSValueRefLong(account->getCredentailId());
230 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_SETTINGS)) {
231 return converter->toJSValueRef(account->getSettings());
236 LogError("invalid property");
238 return JSValueMakeUndefined(context);
241 bool JSAccount::setProperty(JSContextRef context,
243 JSStringRef propertyName,
245 JSValueRef* exception)
248 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
251 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
253 Throw(NullPointerException);
255 EventAccountPtr account = priv->getObject();
257 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
258 std::string displayname = converter->toString(value);
259 account->setDisplayName(displayname);
261 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
262 std::string iconPath = converter->toString(value);
263 account->setIconPath(iconPath);
269 LogWarning("trying to set incorrect value");
271 JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
275 JSValueRef JSAccount::getServices(JSContextRef context,
277 JSStringRef propertyName,
278 JSValueRef* exception)
282 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
284 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
286 Throw(NullPointerException);
288 EventAccountPtr account = priv->getObject();
290 AccountServicesArrayPtr services = account->getService();
292 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
293 if (NULL == jsResult) {
294 ThrowMsg(NullPointerException, "Could not create js array object");
296 for(unsigned int i = 0; i < services->size(); i++) {
297 if (!JSSetArrayElement(context, jsResult, i, JSAccountServices::createJSObject(context, services->at(i)))) {
298 ThrowMsg(UnknownException, "Could not insert value into js array");
302 LogDebug(">>> jsResult");
306 LogWarning("trying to get incorrect value");
309 LogDebug(">>> undefined");
310 return JSValueMakeUndefined(context);
313 bool JSAccount::setServices(JSContextRef context,
315 JSStringRef propertyName,
317 JSValueRef* exception)
321 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
324 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
326 Throw(NullPointerException);
328 EventAccountPtr account = priv->getObject();
329 account->setService(converter->toAccountService(value));
334 LogWarning("trying to set incorrect value");
336 JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
340 JSValueRef JSAccount::getServiceById(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
341 const JSValueRef arguments[], JSValueRef* exception) {
343 AccountPrivateObject* privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(thisObject));
344 assert(privateObject);
346 JSContextRef globalContext = privateObject->getContext();
347 AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_GET_SERVICE_BY_ID);
348 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
350 if (argumentCount!=1) {
351 AccountLogError("Wrong number of parameters.");
352 return JSTizenExceptionFactory::postException(context, exception,
353 JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
356 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
357 return JSTizenExceptionFactory::postException(context, exception,
358 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
361 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
362 std::string serviceId = converter->convertTostring(arguments[0]);
363 LogDebug("<<< serviceId:[" << serviceId << "]");
365 EventAccountPtr account = privateObject->getObject();
366 AccountServicesArrayPtr services = account->getService();
368 for(unsigned int i = 0; i < services->size(); i++) {
369 std::string tmpServiceId = services->at(i)->getId();
370 LogDebug("tmpServiceId:[" << tmpServiceId << "]");
371 if(tmpServiceId.compare(serviceId)==0){
372 LogDebug(">>> return JSAccoutServices");
373 return JSAccountServices::createJSObject(context, services->at(i));
377 AccountLogWarning("not supported service name:[" << serviceId << "]");
379 AccountLogWarning("services is NULL");
382 LogDebug(">>> UNKNOWN EXCEPTION");
383 return JSTizenExceptionFactory::postException(context, exception,
384 JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
387 LogDebug(">>> return null");
388 return JSValueMakeNull(context);
391 JSValueRef JSAccount::getServiceByName(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
392 const JSValueRef arguments[], JSValueRef* exception) {
394 AccountPrivateObject* privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(thisObject));
395 assert(privateObject);
397 JSContextRef globalContext = privateObject->getContext();
398 AceSecurityStatus status = ACCOUNT_CHECK_ACCESS(globalContext, ACCOUNT_FUNCTION_GET_SERVICE_BY_NAME);
399 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
401 if (argumentCount!=1) {
402 AccountLogError("Wrong number of parameters.");
403 return JSTizenExceptionFactory::postException(context, exception,
404 JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count:"+argumentCount);
407 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
408 return JSTizenExceptionFactory::postException(context, exception,
409 JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
412 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(globalContext);
413 std::string serviceName = converter->convertTostring(arguments[0]);
414 LogDebug("<<< serviceName:[" << serviceName << "]");
416 EventAccountPtr account = privateObject->getObject();
417 AccountServicesArrayPtr services = account->getService();
419 for(unsigned int i = 0; i < services->size(); i++) {
420 std::string tmpServiceName = services->at(i)->getName();
421 LogDebug("tmpServiceName:[" << tmpServiceName << "]");
422 if(tmpServiceName.compare(serviceName)==0){
423 LogDebug(">>> return JSAccoutServices");
424 return JSAccountServices::createJSObject(context, services->at(i));
428 AccountLogWarning("not supported service name:[" << serviceName << "]");
430 AccountLogWarning("services is NULL");
433 LogDebug(">>> UNKNOWN EXCEPTION");
434 return JSTizenExceptionFactory::postException(context, exception,
435 JSTizenException::UNKNOWN_ERROR, "UNKNOWN EXCEPTION");
438 LogDebug(">>> return null");
439 return JSValueMakeNull(context);
442 bool JSAccount::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
443 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());