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.
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <API/Filesystem/StorageProperties.h>
25 #include "JSStorage.h"
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace Api::Filesystem;
36 const char* STR_STORAGE_LABEL = "label";
37 const char* STR_STORAGE_TYPE = "type";
38 const char* STR_STORAGE_STATE = "state";
39 const char* STR_TYPE_INTERNAL = "TYPE_INTERNAL";
40 const char* STR_TYPE_EXTERNAL = "TYPE_EXTERNAL";
41 const char* STR_STATE_MOUNTED = "STATE_MOUNTED";
42 const char* STR_STATE_REMOVED = "STATE_REMOVED";
43 const char* STR_STATE_UNMOUNTABLE = "STATE_UNMOUNTABLE";
46 JSClassDefinition JSStorage::m_classInfo = {
48 kJSClassAttributeNone,
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
66 JSStaticValue JSStorage::m_property[] = {
67 { STR_STORAGE_LABEL, getProperty, NULL, kJSPropertyAttributeReadOnly },
68 { STR_STORAGE_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
69 { STR_STORAGE_STATE, getProperty, NULL, kJSPropertyAttributeReadOnly },
70 { STR_TYPE_INTERNAL, getProperty, NULL, kJSPropertyAttributeReadOnly },
71 { STR_TYPE_EXTERNAL, getProperty, NULL, kJSPropertyAttributeReadOnly },
72 { STR_STATE_MOUNTED, getProperty, NULL, kJSPropertyAttributeReadOnly },
73 { STR_STATE_REMOVED, getProperty, NULL, kJSPropertyAttributeReadOnly },
74 { STR_STATE_UNMOUNTABLE, getProperty, NULL, kJSPropertyAttributeReadOnly },
78 const JSClassRef JSStorage::getClassRef()
81 m_jsClassRef = JSClassCreate(&m_classInfo);
86 const JSClassDefinition* JSStorage::getClassInfo()
91 JSClassRef JSStorage::m_jsClassRef = JSClassCreate(
92 JSStorage::getClassInfo());
94 JSObjectRef JSStorage::createJSObject(JSContextRef context,
95 const StorageProperties &storages)
97 std::auto_ptr<StorageProperties> storageProps(new StorageProperties());
99 storageProps->setLabel(storages.getLabel());
100 storageProps->setType(storages.getType());
101 storageProps->setState(storages.getState());
103 JSStoragePriv *priv = new JSStoragePriv(context, storageProps.get());
104 storageProps.release();
106 ThrowMsg(Commons::NullPointerException, "Can not new an object");
108 return JSObjectMake(context, getClassRef(), priv);
111 void JSStorage::initialize(JSContextRef context,
116 void JSStorage::finalize(JSObjectRef object)
118 JSStoragePriv* priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
119 JSObjectSetPrivate(object, NULL);
123 JSValueRef JSStorage::getProperty(JSContextRef context,
125 JSStringRef propertyName,
126 JSValueRef* exception)
128 JSStoragePriv *priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
129 assert(priv && "Private object not set.");
132 StorageProperties *storages = priv->getObject();
133 Converter convert(context);
135 if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_LABEL)) {
136 return convert.toJSValueRef(storages->getLabel());
137 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_TYPE)) {
138 return convert.toJSValueRef(storages->getType());
139 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_STATE)) {
140 return convert.toJSValueRef(storages->getState());
141 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_TYPE_INTERNAL)) {
142 return convert.toJSValueRef(TYPE_INTERNAL);
143 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_TYPE_EXTERNAL)) {
144 return convert.toJSValueRef(TYPE_EXTERNAL);
145 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_MOUNTED)) {
146 return convert.toJSValueRef(STATE_MOUNTED);
147 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_REMOVED)) {
148 return convert.toJSValueRef(STATE_REMOVED);
149 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_UNMOUNTABLE)) {
150 return convert.toJSValueRef(STATE_UNMOUNTABLE);
152 } Catch(Commons::Exception) {
153 LogWarning("trying to get incorrect value");
155 return JSValueMakeUndefined(context);
158 bool JSStorage::hasInstance(JSContextRef context,
159 JSObjectRef constructor,
160 JSValueRef possibleInstance,
161 JSValueRef* exception)
163 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());