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>
27 #include "plugin_config.h"
29 #include "JSWebSettingManager.h"
31 using namespace WrtDeviceApis::Commons;
32 using namespace DeviceAPI::Common;
35 namespace WebSetting {
37 JSClassDefinition JSWebSettingManager::m_classInfo = {
39 kJSClassAttributeNone,
43 m_function, //StaticFunctions
44 initialize, //Initialize
49 NULL, //DeleteProperty,
50 NULL, //GetPropertyNames,
51 NULL, //CallAsFunction,
52 NULL, //CallAsConstructor,
57 JSStaticFunction JSWebSettingManager::m_function[] = {
58 { WEB_SETTING_MANAGER_API_SET_USER_AGENT_STRING, setUserAgentString, kJSPropertyAttributeNone },
59 { WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES, removeAllCookies, kJSPropertyAttributeNone },
63 JSClassRef JSWebSettingManager::m_jsClassRef = JSClassCreate(JSWebSettingManager::getClassInfo());
65 const JSClassRef JSWebSettingManager::getClassRef()
68 m_jsClassRef = JSClassCreate(&m_classInfo);
73 const JSClassDefinition* JSWebSettingManager::getClassInfo()
78 void JSWebSettingManager::initialize(JSContextRef context, JSObjectRef object)
80 if (!JSObjectGetPrivate(object)) {
81 WebSettingManager *priv = new WebSettingManager();
82 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
88 void JSWebSettingManager::finalize(JSObjectRef object)
90 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(object));
92 JSObjectSetPrivate(object, NULL);
97 JSValueRef JSWebSettingManager::setUserAgentString(JSContextRef context,
99 JSObjectRef thisObject,
100 size_t argumentCount,
101 const JSValueRef arguments[],
102 JSValueRef* exception)
106 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
108 throw TypeMismatchException("Private object is NULL.");
110 ArgumentValidator validator(context, argumentCount, arguments);
111 MultiCallbackUserDataPtr callbacks(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
114 std::string userAgent = validator.toString(0);
117 JSObjectRef successCallbackObj = validator.toFunction(1, true);
118 if (successCallbackObj)
120 callbacks->setCallback("onsuccess", successCallbackObj);
124 JSObjectRef errorCallbackObj = validator.toFunction(2, true);
126 if (errorCallbackObj)
128 callbacks->setCallback("onerror", errorCallbackObj);
131 priv->setUserAgentString(userAgent, callbacks);
132 return JSValueMakeUndefined(context);
133 } catch (const BasePlatformException &err) {
134 LoggerE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
135 return JSWebAPIErrorFactory::postException(context, exception, err);
137 DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.setUserAgentString().");
138 LoggerE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
139 return JSWebAPIErrorFactory::postException(context, exception, err);
143 JSValueRef JSWebSettingManager::removeAllCookies(JSContextRef context,
145 JSObjectRef thisObject,
146 size_t argumentCount,
147 const JSValueRef arguments[],
148 JSValueRef* exception)
150 //TODO: Need to implement ACE check
151 //AceSecurityStatus status = WEB_SETTING_CHECK_ACCESS(WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES);
152 //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
156 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
158 throw TypeMismatchException("Private object is NULL.");
161 ArgumentValidator validator(context, argumentCount, arguments);
162 MultiCallbackUserDataPtr callbacks(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
165 JSObjectRef successCallbackObj = validator.toFunction(0, true);
166 if (successCallbackObj)
168 callbacks->setCallback("onsuccess", successCallbackObj);
172 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
173 if (errorCallbackObj)
175 callbacks->setCallback("onerror", errorCallbackObj);
177 priv->removeAllCookies(callbacks);
178 return JSValueMakeUndefined(context);
179 } catch (const BasePlatformException &err) {
180 LoggerE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
181 return JSWebAPIErrorFactory::postException(context, exception, err);
183 DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.removeAllCookies().");
184 LoggerE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
185 return JSWebAPIErrorFactory::postException(context, exception, err);