2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
37 JSClassDefinition JSSecureStorageManager::m_classInfo =
40 kJSClassAttributeNone,
41 "SecureStorageManager",
59 JSStaticFunction JSSecureStorageManager::m_function[] =
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 },
70 const JSClassRef JSSecureStorageManager::getClassRef()
73 m_jsClassRef = JSClassCreate(&m_classInfo);
78 const JSClassDefinition* JSSecureStorageManager::getClassInfo()
83 JSClassRef JSSecureStorageManager::m_jsClassRef = JSClassCreate(JSSecureStorageManager::getClassInfo());
85 void JSSecureStorageManager::initialize(JSContextRef context, JSObjectRef object)
87 LoggerD("JSSecureStorageManager::initialize ");
88 JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(object));
92 SecureStorageManagerPtr secureStorage(new SecureStorageManager());
93 priv = new JSSecureStorageManagerPriv( context, secureStorage);
94 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
96 LoggerE("Object can't store private data.");
102 LoggerD("JSSecureStorageManager::already exist ");
106 void JSSecureStorageManager::finalize(JSObjectRef object)
108 JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(object));
110 LoggerD("JSSecureStorageManager::Finalrize");
114 JSObjectSetPrivate(object, NULL);
119 bool JSSecureStorageManager::hasInstance(JSContextRef context, JSObjectRef constructor,
120 JSValueRef possibleInstance, JSValueRef* exception)
122 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
126 JSValueRef JSSecureStorageManager::listKeys(JSContextRef context, JSObjectRef object,
127 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
128 JSValueRef* exception)
130 JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));
136 LoggerE("priv null");
137 Throw(WrtDeviceApis::Commons::ConversionException);
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);
145 SecureStorageManagerPtr secureStorage(priv->getObject());
146 Converter converter(priv->getContext());
147 std::vector<std::string> keys = secureStorage->listKeys();
148 return converter.toJSValueRef(keys);
150 catch (const WrtDeviceApis::Commons::Exception& ex)
152 LoggerD("Error message" << ex.GetMessage());
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:
169 return JSTizenExceptionFactory::postException(context, exception,
170 JSTizenException::UNKNOWN_ERROR, "Unkown error");
173 return JSValueMakeUndefined(context);
178 JSValueRef JSSecureStorageManager::set(JSContextRef context, JSObjectRef object,
179 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
180 JSValueRef* exception)
182 JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));
183 JSValueRef reserveArguments[2];
191 LoggerE("priv null");
193 Throw(WrtDeviceApis::Commons::ConversionException);
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);
201 for (index = 0; index < 2; index++)
203 if (index < argumentCount)
204 reserveArguments[index] = arguments[index];
206 reserveArguments[index] = JSValueMakeUndefined(context);
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);
216 catch (const WrtDeviceApis::Commons::Exception& ex)
218 LoggerD("Error message" << ex.GetMessage());
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:
235 return JSTizenExceptionFactory::postException(context, exception,
236 JSTizenException::UNKNOWN_ERROR, "Unkown error");
239 return JSValueMakeUndefined(context);
242 JSValueRef JSSecureStorageManager::get(JSContextRef context, JSObjectRef object,
243 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
244 JSValueRef* exception)
246 JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));
247 JSValueRef reserveArguments;
253 LoggerE("priv null");
254 Throw(WrtDeviceApis::Commons::ConversionException);
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);
261 if (argumentCount > 0)
262 reserveArguments = arguments[0];
264 reserveArguments = JSValueMakeUndefined(context);
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);
272 catch (const WrtDeviceApis::Commons::Exception& ex)
274 LoggerD("Error message" << ex.GetMessage());
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:
291 return JSTizenExceptionFactory::postException(context, exception,
292 JSTizenException::UNKNOWN_ERROR, "Unkown error");
295 return JSValueMakeUndefined(context);
299 JSValueRef JSSecureStorageManager::remove(JSContextRef context, JSObjectRef object,
300 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
301 JSValueRef* exception)
303 JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));
304 JSValueRef reserveArguments;
310 LoggerE("priv null");
311 Throw(WrtDeviceApis::Commons::ConversionException);
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);
318 if (argumentCount > 0)
319 reserveArguments = arguments[0];
321 reserveArguments = JSValueMakeUndefined(context);
323 SecureStorageManagerPtr secureStorage(priv->getObject());
324 Converter converter(priv->getContext());
325 std::string key = converter.toString(reserveArguments);
326 secureStorage->remove(key);
328 catch (const WrtDeviceApis::Commons::Exception& ex)
330 LoggerD("Error message" << ex.GetMessage());
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:
347 return JSTizenExceptionFactory::postException(context, exception,
348 JSTizenException::UNKNOWN_ERROR, "Unkown error");
351 return JSValueMakeUndefined(context);
355 JSValueRef JSSecureStorageManager::removeAll(JSContextRef context, JSObjectRef object,
356 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
357 JSValueRef* exception)
359 JSSecureStorageManagerPriv* priv = static_cast<JSSecureStorageManagerPriv*>(JSObjectGetPrivate(thisObject));
365 LoggerE("priv null");
366 Throw(WrtDeviceApis::Commons::ConversionException);
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);
373 SecureStorageManagerPtr secureStorage(priv->getObject());
374 Converter converter(priv->getContext());
375 secureStorage->removeAll();
378 catch (const WrtDeviceApis::Commons::Exception& ex)
380 LoggerD("Error message" << ex.GetMessage());
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:
397 return JSTizenExceptionFactory::postException(context, exception,
398 JSTizenException::UNKNOWN_ERROR, "Unkown error");
401 return JSValueMakeUndefined(context);