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