Update change log and spec for wrt-plugins-tizen_0.4.12
[framework/web/wrt-plugins-tizen.git] / src / SecureStorage / JSSecureStorageManager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17 #include <CommonsJavaScript/Converter.h>
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <SecurityExceptions.h>
23 #include <JSTizenExceptionFactory.h>
24 #include <JSTizenException.h>
25 #include "JSSecureStorageManager.h"
26 #include "plugin_config.h"
27
28 using namespace std;
29 using namespace DPL;
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
33
34 namespace TizenApis {
35 namespace Tizen1_0 {
36
37 JSClassDefinition JSSecureStorageManager::m_classInfo =
38 {
39         0,
40         kJSClassAttributeNone,
41         "SecureStorageManager",
42         NULL,
43         NULL,
44         m_function,
45         initialize,
46         finalize,
47         NULL, 
48         NULL, 
49         NULL, 
50         NULL, 
51         NULL, 
52         NULL,
53         NULL,
54         hasInstance,
55         NULL
56 };
57
58         
59 JSStaticFunction JSSecureStorageManager::m_function[] =
60 {
61         { "remove", JSSecureStorageManager::remove, kJSPropertyAttributeNone },
62         { "set", JSSecureStorageManager::set, kJSPropertyAttributeNone },
63         { "get", JSSecureStorageManager::get, kJSPropertyAttributeNone },       
64         { "listKeys", JSSecureStorageManager::listKeys, kJSPropertyAttributeNone },     
65         { "removeAll", JSSecureStorageManager::removeAll, kJSPropertyAttributeNone },           
66         { 0, 0, 0 }
67 };
68
69
70 const JSClassRef JSSecureStorageManager::getClassRef() 
71 {
72         if (!m_jsClassRef) {
73                 m_jsClassRef = JSClassCreate(&m_classInfo);
74         }
75         return m_jsClassRef;
76 }
77
78 const JSClassDefinition* JSSecureStorageManager::getClassInfo() 
79 {
80         return &m_classInfo;
81 }
82
83 JSClassRef JSSecureStorageManager::m_jsClassRef = JSClassCreate(JSSecureStorageManager::getClassInfo());
84
85 void JSSecureStorageManager::initialize(JSContextRef context, JSObjectRef object) 
86 {
87         LogDebug("JSSecureStorageManager::initialize ");
88         JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(object));
89
90         if (priv == NULL)
91         {
92                 SecureStorageManagerPtr secureStorage(new SecureStorageManager());
93                 priv = new JSSecureStorageManagerPriv( context, secureStorage);
94                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
95                 {
96                         LogError("Object can't store private data.");
97                         delete priv;
98                 }
99         }
100         else
101         {
102                 LogDebug("JSSecureStorageManager::already exist ");             
103         }
104 }
105
106 void JSSecureStorageManager::finalize(JSObjectRef object) 
107 {
108         JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(object));
109
110         LogDebug("JSSecureStorageManager::Finalrize");
111
112         if (priv != NULL) 
113         {
114                 JSObjectSetPrivate(object, NULL);
115                 delete priv;
116         }
117 }
118
119 bool JSSecureStorageManager::hasInstance(JSContextRef context, JSObjectRef constructor,
120                 JSValueRef possibleInstance, JSValueRef* exception) 
121 {
122         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
123 }
124
125
126 JSValueRef JSSecureStorageManager::listKeys(JSContextRef context, JSObjectRef object,
127         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
128         JSValueRef* exception)
129 {
130         JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));    
131
132         try 
133         {
134                 if (priv == NULL)
135                 {
136                         LogError("priv null");
137                         Throw(WrtDeviceApis::Commons::ConversionException);
138                 }
139
140
141                 // to do : need to update TizenPolicy.xml
142                 // AceSecurityStatus status = SECURE_STORAGE_CHECK_ACCESS(SECURE_STORAGE_FUNC_READ);
143                 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
144                 
145                 SecureStorageManagerPtr secureStorage(priv->getObject());
146                 Converter converter(priv->getContext());
147                 std::vector<std::string> keys = secureStorage->listKeys();
148                 return converter.toJSValueRef(keys);    
149         }
150         catch (const WrtDeviceApis::Commons::Exception& ex) 
151         {
152                 LogDebug("Error message" << ex.GetMessage());
153                 switch(ex.getCode())
154                 {
155                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
156                                 return JSTizenExceptionFactory::postException(context, exception, 
157                                         JSTizenException::NOT_FOUND_ERROR, "not found error"); 
158                         case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
159                                 return JSTizenExceptionFactory::postException(context, exception, 
160                                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error"); 
161                         case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
162                                 return JSTizenExceptionFactory::postException(context, exception, 
163                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");  
164                         case WrtDeviceApis::Commons::ExceptionCodes::SecurityException:
165                                 return JSTizenExceptionFactory::postException(context, exception, 
166                                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
167                         case WrtDeviceApis::Commons::ExceptionCodes::Exception:
168                         default:
169                                 return JSTizenExceptionFactory::postException(context, exception, 
170                                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
171                 }
172         }
173         return JSValueMakeUndefined(context);
174
175 }
176
177
178 JSValueRef JSSecureStorageManager::set(JSContextRef context, JSObjectRef object,
179         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
180         JSValueRef* exception)
181 {
182         JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));    
183         JSValueRef reserveArguments[2];
184         size_t index = 0;
185
186
187         try 
188         {
189                 if (priv == NULL)
190                 {
191                         LogError("priv null");
192
193                         Throw(WrtDeviceApis::Commons::ConversionException);
194                 }
195
196                 // to do : need to update TizenPolicy.xml
197                 // AceSecurityStatus status = SECURE_STORAGE_CHECK_ACCESS(SECURE_STORAGE_FUNC_WRITE);
198                 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
199                 
200
201                 for (index = 0; index < 2; index++)
202                 {
203                         if (index < argumentCount)
204                                 reserveArguments[index] = arguments[index];
205                         else 
206                                 reserveArguments[index] = JSValueMakeUndefined(context);
207                 }
208
209                 SecureStorageManagerPtr secureStorage(priv->getObject());
210                 Converter converter(priv->getContext());
211                 std::string key = converter.toString(reserveArguments[0]);
212                 std::string value = converter.toString(reserveArguments[1]);
213                 secureStorage->set(key, value);
214                 
215         }
216         catch (const WrtDeviceApis::Commons::Exception& ex) 
217         {
218                 LogDebug("Error message" << ex.GetMessage());
219                 switch(ex.getCode())
220                 {
221                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
222                                 return JSTizenExceptionFactory::postException(context, exception, 
223                                         JSTizenException::NOT_FOUND_ERROR, "not found error"); 
224                         case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
225                                 return JSTizenExceptionFactory::postException(context, exception, 
226                                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error"); 
227                         case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
228                                 return JSTizenExceptionFactory::postException(context, exception, 
229                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");  
230                         case WrtDeviceApis::Commons::ExceptionCodes::SecurityException:
231                                 return JSTizenExceptionFactory::postException(context, exception, 
232                                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
233                         case WrtDeviceApis::Commons::ExceptionCodes::Exception:
234                         default:
235                                 return JSTizenExceptionFactory::postException(context, exception, 
236                                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
237                 }
238         }
239         return JSValueMakeUndefined(context);
240 }
241
242 JSValueRef JSSecureStorageManager::get(JSContextRef context, JSObjectRef object,
243         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
244         JSValueRef* exception)
245 {
246         JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));    
247         JSValueRef reserveArguments;
248
249         try 
250         {
251                 if (priv == NULL)
252                 {
253                         LogError("priv null");
254                         Throw(WrtDeviceApis::Commons::ConversionException);
255                 }
256
257                 // to do : need to update TizenPolicy.xml
258                 // AceSecurityStatus status = SECURE_STORAGE_CHECK_ACCESS(SECURE_STORAGE_FUNC_READ);
259                 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
260
261                 if (argumentCount > 0) 
262                         reserveArguments = arguments[0];
263                 else 
264                         reserveArguments = JSValueMakeUndefined(context);
265                 
266                 SecureStorageManagerPtr secureStorage(priv->getObject());
267                 Converter converter(priv->getContext());
268                 std::string key = converter.toString(reserveArguments);
269                 std::string value = secureStorage->get(key);
270                 return converter.toJSValueRef(value);
271         }
272         catch (const WrtDeviceApis::Commons::Exception& ex) 
273         {
274                 LogDebug("Error message" << ex.GetMessage());
275                 switch(ex.getCode())
276                 {
277                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
278                                 return JSTizenExceptionFactory::postException(context, exception, 
279                                         JSTizenException::NOT_FOUND_ERROR, "not found error"); 
280                         case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
281                                 return JSTizenExceptionFactory::postException(context, exception, 
282                                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error"); 
283                         case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
284                                 return JSTizenExceptionFactory::postException(context, exception, 
285                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");  
286                         case WrtDeviceApis::Commons::ExceptionCodes::SecurityException:
287                                 return JSTizenExceptionFactory::postException(context, exception, 
288                                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
289                         case WrtDeviceApis::Commons::ExceptionCodes::Exception:
290                         default:
291                                 return JSTizenExceptionFactory::postException(context, exception, 
292                                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
293                 }
294         }
295         return JSValueMakeUndefined(context);
296
297 }
298
299 JSValueRef JSSecureStorageManager::remove(JSContextRef context, JSObjectRef object,
300         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
301         JSValueRef* exception)
302 {
303         JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));    
304         JSValueRef reserveArguments;
305
306         try 
307         {
308                 if (priv == NULL)
309                 {
310                         LogError("priv null");
311                         Throw(WrtDeviceApis::Commons::ConversionException);
312                 }
313
314                 // to do : need to update TizenPolicy.xml
315                 // AceSecurityStatus status = SECURE_STORAGE_CHECK_ACCESS(SECURE_STORAGE_FUNC_WRITE);
316                 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
317
318                 if (argumentCount > 0) 
319                         reserveArguments = arguments[0];
320                 else 
321                         reserveArguments = JSValueMakeUndefined(context);
322                 
323                 SecureStorageManagerPtr secureStorage(priv->getObject());
324                 Converter converter(priv->getContext());
325                 std::string key = converter.toString(reserveArguments);
326                 secureStorage->remove(key);
327         }
328         catch (const WrtDeviceApis::Commons::Exception& ex) 
329         {
330                 LogDebug("Error message" << ex.GetMessage());
331                 switch(ex.getCode())
332                 {
333                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
334                                 return JSTizenExceptionFactory::postException(context, exception, 
335                                         JSTizenException::NOT_FOUND_ERROR, "not found error"); 
336                         case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
337                                 return JSTizenExceptionFactory::postException(context, exception, 
338                                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error"); 
339                         case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
340                                 return JSTizenExceptionFactory::postException(context, exception, 
341                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");  
342                         case WrtDeviceApis::Commons::ExceptionCodes::SecurityException:
343                                 return JSTizenExceptionFactory::postException(context, exception, 
344                                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
345                         case WrtDeviceApis::Commons::ExceptionCodes::Exception:
346                         default:
347                                 return JSTizenExceptionFactory::postException(context, exception, 
348                                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
349                 }
350         }       
351         return JSValueMakeUndefined(context);
352
353 }
354
355 JSValueRef JSSecureStorageManager::removeAll(JSContextRef context, JSObjectRef object,
356         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
357         JSValueRef* exception)
358 {
359         JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));    
360
361         try 
362         {
363                 if (priv == NULL)
364                 {
365                         LogError("priv null");
366                         Throw(WrtDeviceApis::Commons::ConversionException);
367                 }
368
369                 // to do : need to update TizenPolicy.xml
370                 // AceSecurityStatus status = SECURE_STORAGE_CHECK_ACCESS(SECURE_STORAGE_FUNC_WRITE);
371                 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
372
373                 SecureStorageManagerPtr secureStorage(priv->getObject());
374                 Converter converter(priv->getContext());
375                 secureStorage->removeAll();
376
377         }
378         catch (const WrtDeviceApis::Commons::Exception& ex) 
379         {
380                 LogDebug("Error message" << ex.GetMessage());
381                 switch(ex.getCode())
382                 {
383                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
384                                 return JSTizenExceptionFactory::postException(context, exception, 
385                                         JSTizenException::NOT_FOUND_ERROR, "not found error"); 
386                         case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
387                                 return JSTizenExceptionFactory::postException(context, exception, 
388                                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error"); 
389                         case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
390                                 return JSTizenExceptionFactory::postException(context, exception, 
391                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");  
392                         case WrtDeviceApis::Commons::ExceptionCodes::SecurityException:
393                                 return JSTizenExceptionFactory::postException(context, exception, 
394                                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
395                         case WrtDeviceApis::Commons::ExceptionCodes::Exception:
396                         default:
397                                 return JSTizenExceptionFactory::postException(context, exception, 
398                                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
399                 }
400         }
401         return JSValueMakeUndefined(context);
402
403 }
404
405 }
406 }
407