2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include "SystemSettingFactory.h"
25 #include "ISystemSetting.h"
26 #include "EventSetWallpaper.h"
27 #include "EventSetRingtone.h"
28 #include "EventSetSetting.h"
29 #include "EventGetSetting.h"
30 #include "SystemSettingFactory.h"
32 #include <JSTizenExceptionFactory.h>
33 #include <JSTizenException.h>
34 #include <SecurityExceptions.h>
35 #include <TimeTracer.h>
36 #include "JSSystemSetting.h"
37 #include "SystemSettingResponseDispatcher.h"
39 #include "ConverterSystemSetting.h"
40 #include "SystemSettingAsyncCallbackManager.h"
43 //#include <EventGetNodeData.h>
44 #include <FilesystemUtils.h>
45 #include "plugin_config.h"
50 using namespace WrtDeviceApis::CommonsJavaScript;
51 using namespace WrtDeviceApis::Commons;
52 using namespace DeviceAPI::Common;
55 namespace SystemSetting {
57 JSClassDefinition JSSystemSetting::m_classInfo = {
59 kJSClassAttributeNone,
77 JSStaticFunction JSSystemSetting::m_function[] = {
78 // { "setWallpaper", JSSystemSetting::setWallpaper, kJSPropertyAttributeNone },
79 // { "setRingtone", JSSystemSetting::setRingtone, kJSPropertyAttributeNone },
80 { "setProperty", JSSystemSetting::setProperty, kJSPropertyAttributeNone },
81 { "getProperty", JSSystemSetting::getProperty, kJSPropertyAttributeNone },
85 const JSClassRef JSSystemSetting::getClassRef()
88 m_jsClassRef = JSClassCreate(&m_classInfo);
93 const JSClassDefinition* JSSystemSetting::getClassInfo()
98 JSClassRef JSSystemSetting::m_jsClassRef = JSClassCreate(JSSystemSetting::getClassInfo());
100 void JSSystemSetting::initialize(JSContextRef context, JSObjectRef object)
102 JSSystemSettingPriv* priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(object));
103 ISystemSettingPtr SystemSettings(SystemSettingFactory::getInstance().getSystemSetting());
107 LoggerD("JSSystemSetting::initialize already");
112 priv = new JSSystemSettingPriv(context, SystemSettings);
113 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
114 LoggerE("Object can't store private data.");
117 } Catch (UnsupportedException) {
118 LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
119 } Catch (UnknownException) {
120 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
121 } Catch (PlatformException) {
122 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
123 } Catch (WrtDeviceApis::Commons::Exception) {
124 LoggerE("Exception: " << _rethrown_exception.GetMessage());
127 LoggerD("JSSystemSetting::initialize ");
131 void JSSystemSetting::finalize(JSObjectRef object)
133 JSSystemSettingPriv* priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(object));
135 JSObjectSetPrivate(object, NULL);
136 LoggerD("Deleting gallery");
140 JSValueRef JSSystemSetting::setProperty(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
141 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
145 SettingType settingType;
146 std::string settingValue;
148 JSSystemSettingPriv *priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(thisObject));
149 ConverterSystemSettingFactory::ConverterType converter = ConverterSystemSettingFactory::getConverter(context);
150 Validator check(context, exception);
153 LoggerE("private object is null");
154 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
156 if (argumentCount < 3) {
157 LoggerD("Argument is too few");
158 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
160 if (!check.isCallback(arguments[2])) {
161 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
164 AceSecurityStatus status = SYSTEMSETTING_CHECK_ACCESS(SYSTEMSETTING_FUNCTION_API_SET_PROPERTY);
165 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
167 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
168 onSuccessForCbm = arguments[2];
169 if (argumentCount > 3) {
170 if (check.isCallback(arguments[3])) {
171 onErrorForCbm = arguments[3];
172 } else if (!JSValueIsNull(context, arguments[3])) {
173 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
177 JSCallbackManagerPtr callbackManager(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
178 callbackManager->setObject(thisObject);
180 std::string strSettingType;
182 strSettingType = converter->toString(arguments[0]);
183 TIME_TRACER_ITEM_BEGIN(strSettingType.c_str(), 0);
185 settingType = converter->toSettingType(arguments[0]);
186 settingValue = converter->toString(arguments[1]);
188 DeviceAPI::Filesystem::IPathPtr src = DeviceAPI::Filesystem::Utils::fromVirtualPath(priv->getContext(), settingValue);
190 settingValue = src->getFullPath();
192 settingValue = ""; //set empty string.
194 LoggerD("real path = " << settingValue);
196 Catch(WrtDeviceApis::Commons::Exception) {
197 LoggerD("settingValue is not Virtual Path");
200 LoggerD("settingValue = " << settingValue);
202 ISystemSettingPtr SystemSettings(priv->getObject());
203 EventSetSettingPtr dplEvent(new EventSetSetting());
204 dplEvent->setSettingType(settingType);
205 dplEvent->setSettingValue(settingValue);
206 dplEvent->setPrivateData(StaticPointerCast<IEventPrivateData>(callbackManager));
207 dplEvent->setForAsynchronousCall(&SystemSettingResponseDispatcher::getInstance());
209 SystemSettings->setProperty(dplEvent);
210 SystemSettingAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, priv->getContext());
213 Catch(WrtDeviceApis::Commons::ConversionException) {
214 LoggerD("Exception");
215 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
218 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
219 LoggerD("Exception");
220 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
223 Catch(WrtDeviceApis::Commons::PlatformException) {
224 LoggerD("Exception");
225 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "platform error");
228 Catch(WrtDeviceApis::Commons::Exception) {
229 LoggerD("Exception");
230 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
234 TIME_TRACER_ITEM_END(strSettingType.c_str(), 0);
235 return JSValueMakeUndefined(context);
238 JSValueRef JSSystemSetting::getProperty(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
239 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
242 JSSystemSettingPriv *priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(thisObject));
244 ConverterSystemSettingFactory::ConverterType converter = ConverterSystemSettingFactory::getConverter(context);
246 Validator check(context, exception);
249 LoggerE("private object is null");
250 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
253 if (argumentCount < 2) {
254 LoggerD("Argument is too few");
255 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
257 if (!check.isCallback(arguments[1])) {
258 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
262 AceSecurityStatus status = SYSTEMSETTING_CHECK_ACCESS(SYSTEMSETTING_FUNCTION_API_GET_PROPERTY);
263 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
266 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
267 onSuccessForCbm = arguments[1];
268 if (argumentCount > 2) {
269 if (check.isCallback(arguments[2])) {
270 onErrorForCbm = arguments[2];
271 } else if (!JSValueIsNull(context, arguments[2])) {
272 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
276 JSCallbackManagerPtr callbackManager(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
277 callbackManager->setObject(thisObject);
279 std::string strGettingType;
281 strGettingType = converter->toString(arguments[0]);
282 TIME_TRACER_ITEM_BEGIN(strGettingType.c_str(), 0);
283 SettingType settingType = converter->toSettingType(arguments[0]);
285 ISystemSettingPtr SystemSettings(priv->getObject());
286 EventGetSettingPtr dplEvent(new EventGetSetting());
287 dplEvent->setSettingType(settingType);
288 dplEvent->setPrivateData(StaticPointerCast<IEventPrivateData>(callbackManager));
289 dplEvent->setForAsynchronousCall(&SystemSettingResponseDispatcher::getInstance());
291 SystemSettings->getProperty(dplEvent);
292 SystemSettingAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, priv->getContext());
296 Catch(WrtDeviceApis::Commons::ConversionException) {
297 LoggerD("Exception");
298 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
301 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
302 LoggerD("Exception");
303 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
306 Catch(WrtDeviceApis::Commons::PlatformException) {
307 LoggerD("Exception");
308 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "platform error");
311 Catch(WrtDeviceApis::Commons::Exception) {
312 LoggerD("Exception");
313 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
317 TIME_TRACER_ITEM_BEGIN(strGettingType.c_str(), 0);
318 return JSValueMakeUndefined(context);