add patch
[framework/osp/web.git] / src / controls / FWebCtrlWebStorageManager.cpp
1 //
2 // Open Service Platform
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  * @file                FWebCtrlWebStorageManager.cpp
20  * @brief               The file contains the definition of FWebCtrlWebStorageManager class.
21  *
22  * The file contains the definition of FWebCtrlWebStorageManager class.
23  */
24 #include <pthread.h>
25 #include <unique_ptr.h>
26 #include <FBaseColIList.h>
27 #include <FBaseSysLog.h>
28 #include <FWebCtrlWebStorageManager.h>
29 #include <FSec_AccessController.h>
30 #include "FWebCtrl_WebStorageManagerImpl.h"
31
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Security;
36
37
38 namespace Tizen { namespace Web { namespace Controls
39 {
40
41
42 WebStorageManager* WebStorageManager::__pInstance = null;
43
44
45 WebStorageManager::WebStorageManager(void)
46         : __pWebStorageManagerImpl(null)
47 {
48         std::unique_ptr<_WebStorageManagerImpl> pWebStorageManagerImpl(new (std::nothrow) _WebStorageManagerImpl());
49         SysTryReturnVoidResult(NID_WEB_CTRL, pWebStorageManagerImpl.get(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
50
51         __pWebStorageManagerImpl = pWebStorageManagerImpl.release();
52 }
53
54
55 WebStorageManager::~WebStorageManager(void)
56 {
57         delete __pWebStorageManagerImpl;
58         __pWebStorageManagerImpl = null;
59 }
60
61
62 void
63 WebStorageManager::InitWebStorageManager(void)
64 {
65         std::unique_ptr<WebStorageManager> pInstance(new (std::nothrow) WebStorageManager());
66         SysTryReturnVoidResult(NID_WEB_CTRL, pInstance.get(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
67
68         __pInstance = pInstance.release();
69         std::atexit(DestroyWebStorageManager);
70 }
71
72
73 void
74 WebStorageManager::DestroyWebStorageManager(void)
75 {
76         delete __pInstance;
77         __pInstance = null;
78 }
79
80
81 WebStorageManager*
82 WebStorageManager::GetInstance(void)
83 {
84         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
85         if (__pInstance== null)
86         {
87                 ClearLastResult();
88                 pthread_once(&onceBlock, InitWebStorageManager);
89                 result r = GetLastResult();
90                 if (IsFailed(r))
91                 {
92                         onceBlock = PTHREAD_ONCE_INIT;
93                 }
94         }
95
96         return __pInstance;
97 }
98
99
100 IList*
101 WebStorageManager::GetOriginListN(WebStorageType storageType) const
102 {
103         SysAssertf(__pWebStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
104
105         ClearLastResult();
106         result r = E_SUCCESS;
107
108         r = _AccessController::CheckUserPrivilege(_PRV_WEB_PRIVACY);
109         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, E_PRIVILEGE_DENIED, "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
110
111         IList* pList = __pWebStorageManagerImpl->GetOriginListN(storageType);
112         SysTryReturn(NID_WEB_CTRL, GetLastResult() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
113
114         return pList;
115 }
116
117
118 result
119 WebStorageManager::SetQuotaForOrigin(WebStorageType storageType, const String& origin, long quota)
120 {
121         SysAssertf(__pWebStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
122
123         result r = E_SUCCESS;
124
125         r = _AccessController::CheckUserPrivilege(_PRV_WEB_PRIVACY);
126         SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, E_PRIVILEGE_DENIED, "[%s] The application does not have the privilege to call this method.",  GetErrorMessage(E_PRIVILEGE_DENIED));
127
128         r = __pWebStorageManagerImpl->SetQuotaForOrigin(storageType, origin, quota);
129         SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
130
131         return E_SUCCESS;
132 }
133
134
135 long
136 WebStorageManager::GetQuotaForOrigin(WebStorageType storageType, const String& origin)
137 {
138         SysAssertf(__pWebStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
139
140         ClearLastResult();
141         result r = E_SUCCESS;
142         long quota = 0;
143
144         r = _AccessController::CheckUserPrivilege(_PRV_WEB_PRIVACY);
145         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, 0, E_PRIVILEGE_DENIED, "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
146
147         quota = __pWebStorageManagerImpl->GetQuotaForOrigin(storageType, origin);
148         SysTryReturn(NID_WEB_CTRL, GetLastResult() == E_SUCCESS, 0, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
149
150         return quota;
151 }
152
153
154 long
155 WebStorageManager::GetUsageForOrigin(WebStorageType storageType, const String& origin) const
156 {
157         SysAssertf(__pWebStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
158
159         ClearLastResult();
160         result r = E_SUCCESS;
161         long usage = 0;
162
163         r = _AccessController::CheckUserPrivilege(_PRV_WEB_PRIVACY);
164         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, 0, E_PRIVILEGE_DENIED, "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
165
166         usage = __pWebStorageManagerImpl->GetQuotaForOrigin(storageType, origin);
167         SysTryReturn(NID_WEB_CTRL, GetLastResult() == E_SUCCESS, 0, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
168
169         return usage;
170 }
171
172
173 result
174 WebStorageManager::Remove(WebStorageType storageType, const String& origin)
175 {
176         SysAssertf(__pWebStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
177
178         result r = E_SUCCESS;
179
180         r = _AccessController::CheckUserPrivilege(_PRV_WEB_PRIVACY);
181         SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, E_PRIVILEGE_DENIED, "[%s] The application does not have the privilege to call this method.",  GetErrorMessage(E_PRIVILEGE_DENIED));
182
183         r = __pWebStorageManagerImpl->Remove(storageType, origin);
184         SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
185
186         return E_SUCCESS;
187 }
188
189
190 result
191 WebStorageManager::RemoveAll(WebStorageType storageType)
192 {
193         SysAssertf(__pWebStorageManagerImpl != null, "Not yet constructed. Construct() should be called before use.");
194
195         result r = E_SUCCESS;
196
197         r = _AccessController::CheckUserPrivilege(_PRV_WEB_PRIVACY);
198         SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, E_PRIVILEGE_DENIED, "[%s] The application does not have the privilege to call this method.",  GetErrorMessage(E_PRIVILEGE_DENIED));
199
200         r = __pWebStorageManagerImpl->RemoveAll(storageType);
201         SysTryReturnResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
202
203         return E_SUCCESS;
204 }
205
206
207 }}} // Tizen::Web::Controls