2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "JSMediaKeyManager.h"
19 #include "MediaKeyManager.h"
20 #include "plugin_config.h"
22 #include <SecurityExceptions.h>
24 #include <JSWebAPIError.h>
25 #include <ArgumentValidator.h>
26 #include <GlobalContextManager.h>
27 #include <MultiCallbackUserData.h>
28 #include <PlatformException.h>
30 using namespace WrtDeviceApis::Commons;
31 using namespace DeviceAPI::Common;
36 JSClassDefinition JSMediaKeyManager::m_classInfo = {
38 kJSClassAttributeNone,
42 m_function, //StaticFunctions
43 initialize, //Initialize
48 NULL, //DeleteProperty,
49 NULL, //GetPropertyNames,
50 NULL, //CallAsFunction,
51 NULL, //CallAsConstructor,
56 JSStaticFunction JSMediaKeyManager::m_function[] = {
57 { MEDIA_KEY_MANAGER_API_SET_MEDIA_KEY_EVENT_LISTENER, setMediaKeyEventListener, kJSPropertyAttributeNone },
58 { MEDIA_KEY_MANAGER_API_UNSET_MEDIA_KEY_EVENT_LISTENER, unsetMediaKeyEventListener, kJSPropertyAttributeNone },
62 JSClassRef JSMediaKeyManager::m_jsClassRef = JSClassCreate(JSMediaKeyManager::getClassInfo());
64 const JSClassRef JSMediaKeyManager::getClassRef()
67 m_jsClassRef = JSClassCreate(&m_classInfo);
72 const JSClassDefinition* JSMediaKeyManager::getClassInfo()
77 void JSMediaKeyManager::initialize(JSContextRef context, JSObjectRef object)
82 void JSMediaKeyManager::finalize(JSObjectRef object)
87 JSValueRef JSMediaKeyManager::setMediaKeyEventListener(JSContextRef context,
89 JSObjectRef thisObject,
91 const JSValueRef arguments[],
92 JSValueRef* exception)
98 MediaKeyManager *priv = MediaKeyManager::getInstance();
100 ArgumentValidator validator(context, argumentCount, arguments);
103 JSObjectRef callbackObj = validator.toCallbackObject(0, false, "onpressed","onreleased",NULL);
105 JSValueRef onpressed = JSUtil::getProperty(context, callbackObj, "onpressed");
106 JSValueRef onreleased = JSUtil::getProperty(context, callbackObj, "onreleased");
108 JSObjectRef pressedCallback = NULL;
109 if(!JSValueIsUndefined(context, onpressed)){
110 pressedCallback = JSUtil::JSValueToObject(context, onpressed);
113 JSObjectRef releasedCallback = NULL;
114 if(!JSValueIsUndefined(context, onreleased)){
115 releasedCallback = JSUtil::JSValueToObject(context, onreleased);
118 MediaKeyEventListenerPtr callback(new MediaKeyEventCallback(
119 GlobalContextManager::getInstance()->getGlobalContext(context),
123 priv -> setMediaKeyEventListener(callback);
125 return JSValueMakeUndefined(context);
126 } catch (const BasePlatformException &err) {
127 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
128 return JSWebAPIErrorFactory::postException(context, exception, err);
130 DeviceAPI::Common::UnknownException err("Unknown Error in MediaKeyManager.setMediaKeyEventListener().");
131 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
132 return JSWebAPIErrorFactory::postException(context, exception, err);
136 JSValueRef JSMediaKeyManager::unsetMediaKeyEventListener(JSContextRef context,
138 JSObjectRef thisObject,
139 size_t argumentCount,
140 const JSValueRef arguments[],
141 JSValueRef* exception)
147 MediaKeyManager *priv = MediaKeyManager::getInstance();
150 priv ->unsetMediaKeyEventListener();
152 return JSValueMakeUndefined(context);
153 } catch (const BasePlatformException &err) {
154 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
155 return JSWebAPIErrorFactory::postException(context, exception, err);
157 DeviceAPI::Common::UnknownException err("Unknown Error in MediaKeyManager.unsetMediaKeyEventListener().");
158 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
159 return JSWebAPIErrorFactory::postException(context, exception, err);