Update change log and spec for wrt-plugins-tizen_0.4.51
[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 {
36 namespace WebSetting
37 {
38
39 JSClassDefinition JSWebSettingManager::m_classInfo =
40 {
41     0,
42     kJSClassAttributeNone,
43     "WebSettingManager",
44     NULL, //ParentClass
45     NULL, //StaticValues
46     m_function, //StaticFunctions
47     initialize, //Initialize
48     finalize, //Finalize
49     NULL, //HasProperty,
50     NULL, //GetProperty,
51     NULL, //SetProperty,
52     NULL, //DeleteProperty,
53     NULL, //GetPropertyNames,
54     NULL, //CallAsFunction,
55     NULL, //CallAsConstructor,
56     NULL, //HasInstance,
57     NULL //ConvertToType
58 };
59
60 JSStaticFunction JSWebSettingManager::m_function[] =
61 {
62     { WEB_SETTING_MANAGER_API_SET_USER_AGENT_STRING, setUserAgentString, kJSPropertyAttributeNone },
63     { WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES, removeAllCookies, kJSPropertyAttributeNone },
64     { 0, 0, 0 }
65 };
66
67 JSClassRef JSWebSettingManager::m_jsClassRef = JSClassCreate(JSWebSettingManager::getClassInfo());
68
69 const JSClassRef JSWebSettingManager::getClassRef()
70 {
71     if (!m_jsClassRef)
72     {
73         m_jsClassRef = JSClassCreate(&m_classInfo);
74     }
75     return m_jsClassRef;
76 }
77
78 const JSClassDefinition* JSWebSettingManager::getClassInfo()
79 {
80     return &m_classInfo;
81 }
82
83 void JSWebSettingManager::initialize(JSContextRef context, JSObjectRef object)
84 {
85     if (!JSObjectGetPrivate(object))
86     {
87         SLoggerI("JSWebSettingManager::initialize called...");
88         WebSettingManager *priv = WebSettingManager::getInstance();
89         JSObjectSetPrivate(object, static_cast<void*>(priv));
90     }
91 }
92
93 void JSWebSettingManager::finalize(JSObjectRef object)
94 {
95     SLoggerI("JSWebSettingManager::finalize called...");
96     WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(object));
97     if (priv)
98     {
99         JSObjectSetPrivate(object, NULL);
100     }
101 }
102
103 JSValueRef JSWebSettingManager::setUserAgentString(JSContextRef context,
104         JSObjectRef object,
105         JSObjectRef thisObject,
106         size_t argumentCount,
107         const JSValueRef arguments[],
108         JSValueRef* exception)
109 {
110     try
111     {
112         // Private Object
113         WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
114         if (!priv)
115         {
116             throw TypeMismatchException("Private object is NULL.");
117         }
118         ArgumentValidator validator(context, argumentCount, arguments);
119         MultiCallbackUserData* callbacks = new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
120
121         // userAgent
122         std::string userAgent = validator.toString(0);
123
124         // successCallback
125         JSObjectRef successCallbackObj = validator.toFunction(1, true);
126         if (successCallbackObj)
127         {
128             callbacks->setCallback("onsuccess", successCallbackObj);
129         }
130
131         // errorCallback
132         JSObjectRef errorCallbackObj = validator.toFunction(2, true);
133
134         if (errorCallbackObj)
135         {
136             callbacks->setCallback("onerror", errorCallbackObj);
137         }
138
139         priv->setUserAgentString(userAgent, callbacks);
140
141         return JSValueMakeUndefined(context);
142     }
143     catch (const BasePlatformException &err)
144     {
145         SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
146         return JSWebAPIErrorFactory::postException(context, exception, err);
147     }
148     catch (...)
149     {
150         DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.setUserAgentString().");
151         SLoggerE(err.getName().c_str() << ":" <<  err.getMessage().c_str());
152         return JSWebAPIErrorFactory::postException(context, exception, err);
153     }
154 }
155
156 JSValueRef JSWebSettingManager::removeAllCookies(JSContextRef context,
157         JSObjectRef object,
158         JSObjectRef thisObject,
159         size_t argumentCount,
160         const JSValueRef arguments[],
161         JSValueRef* exception)
162 {
163     AceSecurityStatus status = WEB_SETTING_CHECK_ACCESS(WEB_SETTING_MANAGER_API_REMOVE_ALL_COOKIES);
164     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165     SLoggerI("removeAllCookies::TIZEN_SYNC_ACCESS_HANDLER succeeded.");
166
167     try
168     {
169         // Private Object
170         WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
171         if (!priv)
172         {
173             throw TypeMismatchException("Private object is NULL.");
174         }
175
176         ArgumentValidator validator(context, argumentCount, arguments);
177         MultiCallbackUserData* callbacks = new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
178
179         // successCallback
180         JSObjectRef successCallbackObj = validator.toFunction(0, true);
181         if (successCallbackObj)
182         {
183             callbacks->setCallback("onsuccess", successCallbackObj);
184         }
185
186         // errorCallback
187         JSObjectRef errorCallbackObj = validator.toFunction(1, true);
188         if (errorCallbackObj)
189         {
190             callbacks->setCallback("onerror", errorCallbackObj);
191         }
192         priv->removeAllCookies(callbacks);
193         return JSValueMakeUndefined(context);
194     }
195     catch (const BasePlatformException &err)
196     {
197         SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
198         return JSWebAPIErrorFactory::postException(context, exception, err);
199     }
200     catch (...)
201     {
202         DeviceAPI::Common::UnknownException err("Unknown Error in WebSettingManager.removeAllCookies().");
203         SLoggerE(err.getName().c_str() << ":" << err.getMessage().c_str());
204         return JSWebAPIErrorFactory::postException(context, exception, err);
205     }
206 }
207
208
209 } // WebSetting
210 } // DeviceAPI