Update change log and spec for wrt-plugins-tizen_0.4.44
[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 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIError.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26
27 #include "plugin_config.h"
28
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         WebSettingManager *priv = new WebSettingManager();
82         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
83             delete priv;
84         }
85     }
86 }
87
88 void JSWebSettingManager::finalize(JSObjectRef object)
89 {
90     WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(object));
91     if (priv) {
92         JSObjectSetPrivate(object, NULL);
93         delete priv;
94     }
95 }
96
97 JSValueRef JSWebSettingManager::setUserAgentString(JSContextRef context,
98         JSObjectRef object,
99         JSObjectRef thisObject,
100         size_t argumentCount,
101         const JSValueRef arguments[],
102         JSValueRef* exception)
103 {
104     try {
105         // Private Object
106         WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
107         if (!priv) {
108             throw TypeMismatchException("Private object is NULL.");
109         }
110         ArgumentValidator validator(context, argumentCount, arguments);
111         MultiCallbackUserDataPtr callbacks(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
112
113         // userAgent
114         std::string userAgent = validator.toString(0);
115
116         // successCallback
117         JSObjectRef successCallbackObj = validator.toFunction(1, true);
118         if (successCallbackObj)
119         {
120             callbacks->setCallback("onsuccess", successCallbackObj);
121         }
122
123         // errorCallback
124         JSObjectRef errorCallbackObj = validator.toFunction(2, true);
125                 
126         if (errorCallbackObj)
127         {
128             callbacks->setCallback("onerror", errorCallbackObj);
129         }
130
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);                
136     } catch (...) {
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);                
140     }
141 }
142
143 JSValueRef JSWebSettingManager::removeAllCookies(JSContextRef context,
144         JSObjectRef object,
145         JSObjectRef thisObject,
146         size_t argumentCount,
147         const JSValueRef arguments[],
148         JSValueRef* exception)
149 {
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);
153
154     try {
155         // Private Object
156         WebSettingManager *priv = static_cast<WebSettingManager*>(JSObjectGetPrivate(thisObject));
157         if (!priv) {
158             throw TypeMismatchException("Private object is NULL.");
159         }
160
161         ArgumentValidator validator(context, argumentCount, arguments);
162         MultiCallbackUserDataPtr callbacks(new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
163
164         // successCallback
165         JSObjectRef successCallbackObj = validator.toFunction(0, true);
166         if (successCallbackObj)
167         {
168             callbacks->setCallback("onsuccess", successCallbackObj);
169         }
170
171         // errorCallback
172         JSObjectRef errorCallbackObj = validator.toFunction(1, true);
173         if (errorCallbackObj)
174         {
175             callbacks->setCallback("onerror", errorCallbackObj);
176         }
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);                
182     } catch (...) {
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);                
186     }
187 }
188
189
190 } // WebSetting
191 } // DeviceAPI