Update change log and spec for wrt-plugins-tizen_0.4.49
[framework/web/wrt-plugins-tizen.git] / src / WebSetting / JSWebSettingManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19 #include <ArgumentValidator.h>
20 #include <GlobalContextManager.h>
21 #include <JSUtil.h>
22 #include <JSWebAPIError.h>
23 #include <Logger.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26 #include <SecurityExceptions.h>
27
28 #include "plugin_config.h"
29 #include "JSWebSettingManager.h"
30
31 using namespace WrtDeviceApis::Commons;
32 using namespace DeviceAPI::Common;
33
34 namespace DeviceAPI {
35 namespace WebSetting {
36
37 JSClassDefinition JSWebSettingManager::m_classInfo = {
38     0,
39     kJSClassAttributeNone,
40     "WebSettingManager",
41     NULL, //ParentClass
42     NULL, //StaticValues
43     m_function, //StaticFunctions
44     initialize, //Initialize
45     finalize, //Finalize
46     NULL, //HasProperty,
47     NULL, //GetProperty,
48     NULL, //SetProperty,
49     NULL, //DeleteProperty,
50     NULL, //GetPropertyNames,
51     NULL, //CallAsFunction,
52     NULL, //CallAsConstructor,
53     NULL, //HasInstance,
54     NULL //ConvertToType
55 };
56
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 },
60     { 0, 0, 0 }
61 };
62
63 JSClassRef JSWebSettingManager::m_jsClassRef = JSClassCreate(JSWebSettingManager::getClassInfo());
64
65 const JSClassRef JSWebSettingManager::getClassRef()
66 {
67     if (!m_jsClassRef) {
68         m_jsClassRef = JSClassCreate(&m_classInfo);
69     }
70     return m_jsClassRef;
71 }
72
73 const JSClassDefinition* JSWebSettingManager::getClassInfo()
74 {
75     return &m_classInfo;
76 }
77
78 void JSWebSettingManager::initialize(JSContextRef context, JSObjectRef object)
79 {
80     if (!JSObjectGetPrivate(object)) {
81                 SLoggerI("JSWebSettingManager::initialize called...");
82         WebSettingManager *priv = WebSettingManager::getInstance();
83                 JSObjectSetPrivate(object, static_cast<void*>(priv));
84     }
85 }
86
87 void JSWebSettingManager::finalize(JSObjectRef object)
88 {
89         SLoggerI("JSWebSettingManager::finalize called...");
90     WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(object));
91     if (priv) {
92         JSObjectSetPrivate(object, NULL);
93     }
94 }
95
96 JSValueRef JSWebSettingManager::setUserAgentString(JSContextRef context,
97         JSObjectRef object,
98         JSObjectRef thisObject,
99         size_t argumentCount,
100         const JSValueRef arguments[],
101         JSValueRef* exception)
102 {
103     try {
104         // Private Object
105         WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
106         if (!priv) {
107             throw TypeMismatchException("Private object is NULL.");
108         }
109         ArgumentValidator validator(context, argumentCount, arguments);
110         MultiCallbackUserData* callbacks = new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
111
112         // userAgent
113         std::string userAgent = validator.toString(0);
114
115         // successCallback
116         JSObjectRef successCallbackObj = validator.toFunction(1, true);
117         if (successCallbackObj)
118         {
119             callbacks->setCallback("onsuccess", successCallbackObj);
120         }
121
122         // errorCallback
123         JSObjectRef errorCallbackObj = validator.toFunction(2, true);
124                 
125         if (errorCallbackObj)
126         {
127             callbacks->setCallback("onerror", errorCallbackObj);
128         }
129
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);                
135     } catch (...) {
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);                
139     }
140 }
141
142 JSValueRef JSWebSettingManager::removeAllCookies(JSContextRef context,
143         JSObjectRef object,
144         JSObjectRef thisObject,
145         size_t argumentCount,
146         const JSValueRef arguments[],
147         JSValueRef* exception)
148 {
149     //AceSecurityStatus status = WEB_SETTING_CHECK_ACCESS(WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES);
150     //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
151
152     try {
153         // Private Object
154         WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
155         if (!priv) {
156             throw TypeMismatchException("Private object is NULL.");
157         }
158
159         ArgumentValidator validator(context, argumentCount, arguments);
160         MultiCallbackUserData* callbacks = new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
161
162         // successCallback
163         JSObjectRef successCallbackObj = validator.toFunction(0, true);
164         if (successCallbackObj)
165         {
166             callbacks->setCallback("onsuccess", successCallbackObj);
167         }
168
169         // errorCallback
170         JSObjectRef errorCallbackObj = validator.toFunction(1, true);
171         if (errorCallbackObj)
172         {
173             callbacks->setCallback("onerror", errorCallbackObj);
174         }
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);                
180     } catch (...) {
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);                
184     }
185 }
186
187
188 } // WebSetting
189 } // DeviceAPI