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.
26 #include <dpl/log/log.h>
27 #include <CommonsJavaScript/PrivateObject.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <Commons/WrtAccess/WrtAccess.h>
30 #include <API/Filter/FilterTypes.h>
31 #include <API/Filter/AnyType.h>
32 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
34 #include "JSFeature.h"
35 #include "plugin_config.h"
36 #include <WidgetDB/WidgetDBMgr.h>
37 #include <WidgetDB/IWidgetDB.h>
38 #include <PluginManager/PluginManagerFactory.h>
42 #define PLUGIN_NAME "tizen"
43 #define FUNCTION_LIST_AVAILABLE_FEATURES "listAvailableFeatures"
44 #define FUNCTION_LIST_ACTIVATED_FEATURES "listActivatedFeatures"
50 using namespace WrtDeviceApis;
51 using namespace WrtDeviceApis::Commons;
52 using namespace TizenApis::Api::Tizen;
53 using namespace WrtDeviceApis::PluginManager::Api;
55 JSClassDefinition JSTizen::m_classInfo =
58 kJSClassAttributeNone,
68 NULL, //deleteProperty,
76 JSStaticValue JSTizen::m_properties[] = {
80 JSStaticFunction JSTizen::m_functions[] = {
81 { FUNCTION_LIST_AVAILABLE_FEATURES, listAvailableFeatures, kJSPropertyAttributeNone},
82 { FUNCTION_LIST_ACTIVATED_FEATURES, listActivatedFeatures, kJSPropertyAttributeNone},
86 const JSClassDefinition* JSTizen::getClassInfo()
91 JSClassRef JSTizen::m_classRef = JSClassCreate(JSTizen::getClassInfo());
93 void JSTizen::initialize(JSContextRef context, JSObjectRef object)
96 TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
97 JSObjectSetPrivate(object, NULL);
102 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
103 PluginOnDemandPrivPtr privObject(
104 new PluginOnDemandPriv(
105 PluginManagerFactory::getInstance().getPluginManager(
111 priv = new TizenPrivate(context, privObject);
112 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
113 JSObjectSetPrivate(object, NULL);
118 void JSTizen::finalize(JSObjectRef object)
121 TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
122 JSObjectSetPrivate(object, NULL);
126 JSClassRef JSTizen::getClassRef()
129 m_classRef = JSClassCreate(&m_classInfo);
134 //bool JSTizen::deleteProperty(JSContextRef context, JSObjectRef object,
135 // JSStringRef propertyName, JSValueRef* exception)
140 void JSTizen::getPropertyNames(JSContextRef context, JSObjectRef object,
141 JSPropertyNameAccumulatorRef propertyNames)
146 static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
147 if (!priv || !priv->getObject()) {
148 LogError("No private obejct");
151 PluginOnDemandPrivPtr privObj = priv->getObject();
152 privObj->getPluginManager()->addPropertiesToList(propertyNames);
155 Catch(Commons::Exception)
157 LogError("Error occured");
161 bool JSTizen::hasInstance(JSContextRef context, JSObjectRef constructor,
162 JSValueRef possibleInstance, JSValueRef* exception)
167 JSValueRef JSTizen::convertToType(JSContextRef context, JSObjectRef object,
168 JSType type, JSValueRef* exception)
170 return JSValueMakeUndefined(context);
173 bool JSTizen::hasProperty(JSContextRef context,
174 JSObjectRef object, JSStringRef propertyName)
180 TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
182 if (!priv || !priv->getObject()) {
183 LogError("No private obejct");
187 return priv->getObject()->getPluginManager()->hasChild(
188 CommonsJavaScript::Converter(context).toString(propertyName));
190 Catch(WrtDeviceApis::Commons::Exception)
192 LogError("Error occured");
195 //let JSCore to handle this property
199 JSValueRef JSTizen::getProperty(JSContextRef context, JSObjectRef object,
200 JSStringRef propertyName, JSValueRef* exception)
207 static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
208 if (!priv || !priv->getObject()) {
209 LogError("No private obejct");
213 return priv->getObject()->getPluginManager()->getProperty(
214 CommonsJavaScript::Converter(context).toString(propertyName));
216 Catch(WrtDeviceApis::Commons::Exception)
218 LogError("Error occured");
221 /* During normal operation flow should not reach here,
222 but wrt may fail with loading plugin. */
223 return JSValueMakeUndefined(context);
226 bool JSTizen::setProperty(JSContextRef context, JSObjectRef object,
227 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
233 static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
234 if (!priv || !priv->getObject()) {
235 LogError("No private obejct");
238 priv->getObject()->getPluginManager()->setProperty(
239 CommonsJavaScript::Converter(context).toString(propertyName),
242 Catch(WrtDeviceApis::Commons::Exception)
244 LogError("Error occured");
247 //this code should not be reached. setProperty is not called when
248 //hasProperty returns false. Anyway, if we didn't set value then we
253 JSValueRef JSTizen::listAvailableFeatures(JSContextRef context, JSObjectRef object,
254 JSObjectRef thisObject, size_t argumentCount,
255 const JSValueRef arguments[], JSValueRef* exception)
259 TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(thisObject));
263 * Current Tizen spec assures that tizen is always available
264 AccessStatus status = DEVICEAPIS_CHECK_ACCESS(
265 DEVICEAPIS_FUNCTION_API_LIST_AVAIL_FEATURES);
267 SYNC_ACCESS_STATUS_HANDLER(status, context, exception);
273 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "No private object");
275 PluginOnDemandPrivPtr privObj = priv->getObject();
277 catch(const WrtDeviceApis::Commons::Exception &ex)
279 return WrtDeviceApis::CommonsJavaScript::JSDOMExceptionFactory::UnknownException.make(context, exception);
281 JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
282 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
283 WidgetDB::Api::IWidgetDBPtr widgetDB =
284 WidgetDB::Api::getWidgetDB(widgetId);
285 WidgetDB::Api::Features features = widgetDB->getRegisteredFeatures();
286 for (size_t i=0; i<features.size(); ++i) {
287 JSFeaturePrivateObject *priv = new JSFeaturePrivateObject(context, features[i]);
288 JSObjectRef tempObj = JSObjectMake(context, JSFeature::getClassRef(), priv);
290 if (!JSSetArrayElement(context, result, i, tempObj)) {
301 JSValueRef JSTizen::listActivatedFeatures(JSContextRef context, JSObjectRef object,
302 JSObjectRef thisObject, size_t argumentCount,
303 const JSValueRef arguments[], JSValueRef* exception)
307 TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(thisObject));
311 * Current Tizen spec assures that tizen is always available
312 AccessStatus status = DEVICEAPIS_CHECK_ACCESS(
313 DEVICEAPIS_FUNCTION_API_LIST_ACTIV_FEATURES);
315 SYNC_ACCESS_STATUS_HANDLER(status, context, exception);*/
319 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "No private object");
321 PluginOnDemandPrivPtr privObj = priv->getObject();
323 catch(const WrtDeviceApis::Commons::Exception &ex)
325 return WrtDeviceApis::CommonsJavaScript::JSDOMExceptionFactory::UnknownException.make(context, exception);
327 JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
328 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
329 WidgetDB::Api::IWidgetDBPtr widgetDB =
330 WidgetDB::Api::getWidgetDB(widgetId);
331 WidgetDB::Api::Features features = widgetDB->getWidgetFeatures();
332 for (size_t i=0; i<features.size(); ++i) {
333 JSFeaturePrivateObject *priv = new JSFeaturePrivateObject(context, features[i]);
334 JSObjectRef tempObj = JSObjectMake(context, JSFeature::getClassRef(), priv);
336 if (!JSSetArrayElement(context, result, i, tempObj)) {