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