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/Converter.h>
28 #include <CommonsJavaScript/JSUtils.h>
30 #include "AccountConverter.h"
31 #include "plugin_config.h"
33 #include <Tizen/Common/JSTizenException.h>
34 #include <Tizen/Common/JSTizenExceptionFactory.h>
35 #include <Tizen/Common/SecurityExceptions.h>
37 using namespace TizenApis::Api::Account;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace TizenApis::Commons;
48 #define ACCOUNT_ATTRIBUTE_NAME "Account"
50 #define ACCOUNT_ATTRIBUTE_ACCOUNT_ID "id"
51 #define ACCOUNT_ATTRIBUTE_DISPLAY_NAME "displayName"
52 #define ACCOUNT_ATTRIBUTE_ICON_PATH "icon"
53 #define ACCOUNT_ATTRIBUTE_ENABLED "enabled"
54 #define ACCOUNT_ATTRIBUTE_PROVIDER_ID "providerId"
55 #define ACCOUNT_ATTRIBUTE_CREDENTIAL_ID "credentialId"
56 #define ACCOUNT_ATTRIBUTE_SERVICES "services"
57 #define ACCOUNT_ATTRIBUTE_SETTINGS "settings"
61 JSClassDefinition JSAccount::m_classInfo = {
63 kJSClassAttributeNone,
64 ACCOUNT_ATTRIBUTE_NAME,
73 NULL, //deleteProperty,
74 NULL, //getPropertyNames,
75 NULL, //callAsFunction,
76 constructor, //callAsConstructor,
77 hasInstance, //hasInstance,
78 NULL, //convertToType,
81 JSStaticValue JSAccount::m_property[] = {
82 { ACCOUNT_ATTRIBUTE_ACCOUNT_ID, getProperty, setProperty, kJSPropertyAttributeNone },
83 { ACCOUNT_ATTRIBUTE_DISPLAY_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
84 { ACCOUNT_ATTRIBUTE_ICON_PATH, getProperty, setProperty, kJSPropertyAttributeNone },
85 { ACCOUNT_ATTRIBUTE_ENABLED, getProperty, setProperty, kJSPropertyAttributeNone },
86 { ACCOUNT_ATTRIBUTE_PROVIDER_ID, getProperty, setProperty, kJSPropertyAttributeNone },
87 { ACCOUNT_ATTRIBUTE_CREDENTIAL_ID, getProperty, setProperty, kJSPropertyAttributeNone },
88 { ACCOUNT_ATTRIBUTE_SERVICES, getServices, setServices, kJSPropertyAttributeNone },
89 { ACCOUNT_ATTRIBUTE_SETTINGS, getProperty, setProperty, kJSPropertyAttributeNone },
93 JSClassRef JSAccount::m_jsClassRef = JSClassCreate(
94 JSAccount::getClassInfo());
96 const JSClassDefinition* JSAccount::getClassInfo()
98 return &(m_classInfo);
101 JSClassRef JSAccount::getClassRef()
104 m_jsClassRef = JSClassCreate(&m_classInfo);
109 EventAccountPtr JSAccount::getIEvent(JSObjectRef object)
112 AccountPrivateObject *priv =
113 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
115 ThrowMsg(NullPointerException, "Private object is null");
117 EventAccountPtr result = priv->getObject();
119 ThrowMsg(NullPointerException, "Private object is null");
124 void JSAccount::setIEvent(const EventAccountPtr &event,
126 const JSObjectRef object)
131 AccountPrivateObject *priv =
132 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
134 priv = new AccountPrivateObject(ctx, event);
135 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
141 AccountLogError("Error during replacing account object");
146 bool JSAccount::validate(JSContextRef ctx,
147 const JSObjectRef object,
148 JSValueRef* exception)
151 AccountPrivateObject *priv =
152 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
156 EventAccountPtr account = priv->getObject();
160 return account->validate();
164 JSObjectRef JSAccount::createJSAccount(JSContextRef context, EventAccountPtr account)
167 AccountPrivateObject *priv = new AccountPrivateObject(context, account);
168 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
172 void JSAccount::initialize(JSContextRef context, JSObjectRef object)
176 AccountPrivateObject *privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
177 if (NULL == privateObject) {
178 AccountLogWarning("privateObject is NULL");
179 EventAccountPtr accountPtr(new EventAccount());
180 AccountPrivateObject *priv = new AccountPrivateObject(context, accountPtr);
181 if (!JSObjectSetPrivate(object, priv)) {
187 void JSAccount::finalize(JSObjectRef object)
190 AccountPrivateObject* priv =
191 static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
193 JSObjectSetPrivate(object, NULL);
197 JSValueRef JSAccount::getProperty(JSContextRef context,
199 JSStringRef propertyName,
200 JSValueRef* exception)
203 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
205 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
207 Throw(NullPointerException);
209 EventAccountPtr account = priv->getObject();
211 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ACCOUNT_ID)) {
212 return converter->toJSValueRef(account->getAccountId());
213 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
214 return converter->toJSValueRef(account->getDisplayName());
215 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
216 return converter->toJSValueRef(account->getIconPath());
217 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ENABLED)) {
218 return converter->toJSValueRef(account->getEnabled());
219 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_PROVIDER_ID)) {
220 return converter->toJSValueRef(account->getProviderId());
221 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_CREDENTIAL_ID)) {
222 return converter->toJSValueRef(account->getCredentailId());
223 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_SETTINGS)) {
224 std::string settingsString = account->getSettings();
225 LogDebug("settingsString:[" << settingsString << "]");
226 return converter->toJSONString(settingsString);
229 AccountLogError("invalid property");
232 return JSValueMakeUndefined(context);
235 bool JSAccount::setProperty(JSContextRef context,
237 JSStringRef propertyName,
239 JSValueRef* exception)
242 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
244 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
246 Throw(NullPointerException);
248 EventAccountPtr account = priv->getObject();
249 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
251 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
252 std::string displayname = converter->toString(value);
253 account->setDisplayName(displayname);
255 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
256 std::string iconPath = converter->toString(value);
257 account->setIconPath(iconPath);
259 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ENABLED)) {
260 bool enabled = converter->toBool(value);
261 account->setEnabled(enabled);
263 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_CREDENTIAL_ID)) {
264 std::string credentialId = converter->toString(value);
265 account->setCredentailId(credentialId);
267 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_SETTINGS)) {
268 JSStringRef jsonStringRef = JSValueCreateJSONString(context, value, 0, exception);
270 std::string settingString = converter->toString(jsonStringRef);
271 account->setSettings(settingString);
275 AccountLogWarning("trying to set incorrect value");
277 JSTizenExceptionFactory::postException(context, exception,
278 JSTizenException::TYPE_MISMATCH_ERROR, "Trying to set incorrect value");
282 JSValueRef JSAccount::getServices(JSContextRef context,
284 JSStringRef propertyName,
285 JSValueRef* exception)
289 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
291 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
293 Throw(NullPointerException);
295 EventAccountPtr account = priv->getObject();
297 AccountServicesArrayPtr services = account->getService();
299 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
300 if (NULL == jsResult) {
301 ThrowMsg(NullPointerException, "Could not create js array object");
303 for(unsigned int i = 0; i < services->size(); i++) {
304 if (!JSSetArrayElement(context, jsResult, i, JSAccountServices::createJSObject(context, services->at(i)))) {
305 ThrowMsg(UnknownException, "Could not insert value into js array");
309 LogDebug(">>> jsResult");
313 AccountLogWarning("trying to get incorrect value");
316 LogDebug(">>> undefined");
317 return JSValueMakeUndefined(context);
320 bool JSAccount::setServices(JSContextRef context,
322 JSStringRef propertyName,
324 JSValueRef* exception)
328 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
330 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
332 Throw(NullPointerException);
334 EventAccountPtr account = priv->getObject();
335 account->setService(converter->toAccountServiceArray(value));
338 AccountLogWarning("trying to set incorrect value");
341 JSTizenExceptionFactory::postException(context, exception,
342 JSTizenException::TYPE_MISMATCH_ERROR, "Trying to set incorrect value");
346 bool JSAccount::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
347 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
350 JSObjectRef JSAccount::constructor(JSContextRef context,
352 size_t argumentCount,
353 const JSValueRef arguments[],
354 JSValueRef* exception)
357 AccountPrivateObject* privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
358 if (!privateObject) {
359 AccountLogError(">>> NULL POINTER EXCEPTION !");
360 Throw(NullPointerException);
363 if (argumentCount > 2) {
364 AccountLogError("Wrong argument count:" << argumentCount);
365 JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count");
369 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
371 std::string accountServiceProviderId;
373 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
374 AccountLogError(">>> invalid value argument 0");
375 JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
378 accountServiceProviderId = converter->toString(arguments[0]);
379 LogDebug("accountServiceProviderId:[" << accountServiceProviderId << "]");
381 EventAccountPtr account = converter->toAccount(accountServiceProviderId);
383 if(argumentCount == 2){
384 JSValueRef displayNameRef = JSUtils::getJSProperty(context, arguments[1], "displayName");
385 if(displayNameRef != NULL){
386 std::string displayName = converter->toString(displayNameRef);
387 LogDebug("displayName:[" << displayName << "]");
388 account->setDisplayName(displayName);
391 JSValueRef iconPathRef = JSUtils::getJSProperty(context, arguments[1], "iconPath");
392 if(iconPathRef != NULL){
393 std::string iconPath = converter->toString(iconPathRef);
394 LogDebug("iconPath:[" << iconPath << "]");
395 account->setIconPath(iconPath);
400 return JSAccount::createJSAccount(context, account);
402 AccountLogError("UNKNOWN EXCEPTION");
403 JSTizenExceptionFactory::postException(context, exception,
404 JSTizenException::UNKNOWN_ERROR, "UNKNOWN_ERROR");
407 AccountLogWarning(">>> return NULL");