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 <SecurityExceptions.h>
21 #include <JSWebAPIError.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
28 #include "plugin_config.h"
30 #include "JSWebSettingManager.h"
32 using namespace WrtDeviceApis::Commons;
33 using namespace DeviceAPI::Common;
36 namespace WebSetting {
38 JSClassDefinition JSWebSettingManager::m_classInfo = {
40 kJSClassAttributeNone,
44 m_function, //StaticFunctions
45 initialize, //Initialize
50 NULL, //DeleteProperty,
51 NULL, //GetPropertyNames,
52 NULL, //CallAsFunction,
53 NULL, //CallAsConstructor,
58 JSStaticFunction JSWebSettingManager::m_function[] = {
59 { WEB_SETTING_MANAGER_API_SET_USER_AGENT_STRING, setUserAgentString, kJSPropertyAttributeNone },
60 { WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES, removeAllCookies, kJSPropertyAttributeNone },
64 JSClassRef JSWebSettingManager::m_jsClassRef = JSClassCreate(JSWebSettingManager::getClassInfo());
66 const JSClassRef JSWebSettingManager::getClassRef()
69 m_jsClassRef = JSClassCreate(&m_classInfo);
74 const JSClassDefinition* JSWebSettingManager::getClassInfo()
79 void JSWebSettingManager::initialize(JSContextRef context, JSObjectRef object)
81 SLoggerI("JSWebSettingManager::initialize: called once when the .so loaded for the web app");
82 if (!JSObjectGetPrivate(object)) {
83 WebSettingManager *priv = new WebSettingManager();
84 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
90 void JSWebSettingManager::finalize(JSObjectRef object)
92 SLoggerI("JSWebSettingManager::initialize: called once when the .so unloaded for the web app");
93 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(object));
95 JSObjectSetPrivate(object, NULL);
100 JSValueRef JSWebSettingManager::setUserAgentString(JSContextRef context,
102 JSObjectRef thisObject,
103 size_t argumentCount,
104 const JSValueRef arguments[],
105 JSValueRef* exception)
109 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
111 throw TypeMismatchException("Private object is NULL.");
113 ArgumentValidator validator(context, argumentCount, arguments);
114 MultiCallbackUserDataPtr callbacks(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
117 std::string userAgent = validator.toString(0);
120 JSObjectRef successCallbackObj = validator.toFunction(1, true);
121 if (successCallbackObj)
123 callbacks->setCallback("onsuccess", successCallbackObj);
127 JSObjectRef errorCallbackObj = validator.toFunction(2, true);
129 if (errorCallbackObj)
131 callbacks->setCallback("onerror", errorCallbackObj);
134 priv->setUserAgentString(userAgent, callbacks);
135 return JSValueMakeUndefined(context);
136 } catch (const BasePlatformException &err) {
137 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
138 return JSWebAPIErrorFactory::postException(context, exception, err);
140 DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.setUserAgentString().");
141 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
142 return JSWebAPIErrorFactory::postException(context, exception, err);
146 JSValueRef JSWebSettingManager::removeAllCookies(JSContextRef context,
148 JSObjectRef thisObject,
149 size_t argumentCount,
150 const JSValueRef arguments[],
151 JSValueRef* exception)
153 //AceSecurityStatus status = WEB_SETTING_CHECK_ACCESS(WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES);
154 //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
158 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
160 throw TypeMismatchException("Private object is NULL.");
163 ArgumentValidator validator(context, argumentCount, arguments);
164 MultiCallbackUserDataPtr callbacks(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
167 JSObjectRef successCallbackObj = validator.toFunction(0, true);
168 if (successCallbackObj)
170 callbacks->setCallback("onsuccess", successCallbackObj);
174 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
175 if (errorCallbackObj)
177 callbacks->setCallback("onerror", errorCallbackObj);
179 priv->removeAllCookies(callbacks);
180 return JSValueMakeUndefined(context);
181 } catch (const BasePlatformException &err) {
182 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
183 return JSWebAPIErrorFactory::postException(context, exception, err);
185 DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.removeAllCookies().");
186 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
187 return JSWebAPIErrorFactory::postException(context, exception, err);