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.
19 #include <ArgumentValidator.h>
20 #include <GlobalContextManager.h>
22 #include <JSWebAPIError.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26 #include <SecurityExceptions.h>
28 #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 SLoggerI("JSWebSettingManager::initialize called...");
82 WebSettingManager *priv = WebSettingManager::getInstance();
83 JSObjectSetPrivate(object, static_cast<void*>(priv));
87 void JSWebSettingManager::finalize(JSObjectRef object)
89 SLoggerI("JSWebSettingManager::finalize called...");
90 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(object));
92 JSObjectSetPrivate(object, NULL);
96 JSValueRef JSWebSettingManager::setUserAgentString(JSContextRef context,
98 JSObjectRef thisObject,
100 const JSValueRef arguments[],
101 JSValueRef* exception)
105 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
107 throw TypeMismatchException("Private object is NULL.");
109 ArgumentValidator validator(context, argumentCount, arguments);
110 MultiCallbackUserData* callbacks = new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
113 std::string userAgent = validator.toString(0);
116 JSObjectRef successCallbackObj = validator.toFunction(1, true);
117 if (successCallbackObj)
119 callbacks->setCallback("onsuccess", successCallbackObj);
123 JSObjectRef errorCallbackObj = validator.toFunction(2, true);
125 if (errorCallbackObj)
127 callbacks->setCallback("onerror", errorCallbackObj);
130 priv->setUserAgentString(userAgent, callbacks);
131 return JSValueMakeUndefined(context);
132 } catch (const BasePlatformException &err) {
133 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
134 return JSWebAPIErrorFactory::postException(context, exception, err);
136 DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.setUserAgentString().");
137 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
138 return JSWebAPIErrorFactory::postException(context, exception, err);
142 JSValueRef JSWebSettingManager::removeAllCookies(JSContextRef context,
144 JSObjectRef thisObject,
145 size_t argumentCount,
146 const JSValueRef arguments[],
147 JSValueRef* exception)
149 //AceSecurityStatus status = WEB_SETTING_CHECK_ACCESS(WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES);
150 //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
154 WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
156 throw TypeMismatchException("Private object is NULL.");
159 ArgumentValidator validator(context, argumentCount, arguments);
160 MultiCallbackUserData* callbacks = new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
163 JSObjectRef successCallbackObj = validator.toFunction(0, true);
164 if (successCallbackObj)
166 callbacks->setCallback("onsuccess", successCallbackObj);
170 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
171 if (errorCallbackObj)
173 callbacks->setCallback("onerror", errorCallbackObj);
175 priv->removeAllCookies(callbacks);
176 return JSValueMakeUndefined(context);
177 } catch (const BasePlatformException &err) {
178 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
179 return JSWebAPIErrorFactory::postException(context, exception, err);
181 DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.removeAllCookies().");
182 SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
183 return JSWebAPIErrorFactory::postException(context, exception, err);