tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Mediakey / JSMediaKeyManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include "JSMediaKeyManager.h"
19 #include "MediaKeyManager.h"
20 #include "plugin_config.h"
21
22 #include <SecurityExceptions.h>
23 #include <JSUtil.h>
24 #include <JSWebAPIError.h>
25 #include <ArgumentValidator.h>
26 #include <GlobalContextManager.h>
27 #include <MultiCallbackUserData.h>
28 #include <PlatformException.h>
29
30 using namespace WrtDeviceApis::Commons;
31 using namespace DeviceAPI::Common;
32
33 namespace DeviceAPI {
34 namespace MediaKey {
35
36 JSClassDefinition JSMediaKeyManager::m_classInfo = {
37     0,
38     kJSClassAttributeNone,
39     "MediaKeyManager",
40     NULL, //ParentClass
41     NULL, //StaticValues
42     m_function, //StaticFunctions
43     initialize, //Initialize
44     finalize, //Finalize
45     NULL, //HasProperty,
46     NULL, //GetProperty,
47     NULL, //SetProperty,
48     NULL, //DeleteProperty,
49     NULL, //GetPropertyNames,
50     NULL, //CallAsFunction,
51     NULL, //CallAsConstructor,
52     NULL, //HasInstance,
53     NULL //ConvertToType
54 };
55
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 },
59     { 0, 0, 0 }
60 };
61
62 JSClassRef JSMediaKeyManager::m_jsClassRef = JSClassCreate(JSMediaKeyManager::getClassInfo());
63
64 const JSClassRef JSMediaKeyManager::getClassRef()
65 {
66     if (!m_jsClassRef) {
67         m_jsClassRef = JSClassCreate(&m_classInfo);
68     }
69     return m_jsClassRef;
70 }
71
72 const JSClassDefinition* JSMediaKeyManager::getClassInfo()
73 {
74     return &m_classInfo;
75 }
76
77 void JSMediaKeyManager::initialize(JSContextRef context, JSObjectRef object)
78 {
79     LOGD("Entered");
80 }
81
82 void JSMediaKeyManager::finalize(JSObjectRef object)
83 {
84     LOGD("Entered");
85 }
86
87 JSValueRef JSMediaKeyManager::setMediaKeyEventListener(JSContextRef context,
88         JSObjectRef object,
89         JSObjectRef thisObject,
90         size_t argumentCount,
91         const JSValueRef arguments[],
92         JSValueRef* exception)
93 {
94     try {
95         //Private Object
96         LOGD("Entered");
97
98         MediaKeyManager *priv = MediaKeyManager::getInstance();
99
100         ArgumentValidator validator(context, argumentCount, arguments);
101
102         //callback
103         JSObjectRef callbackObj = validator.toCallbackObject(0, false, "onpressed","onreleased",NULL);
104
105         JSValueRef onpressed = JSUtil::getProperty(context, callbackObj, "onpressed");
106         JSValueRef onreleased = JSUtil::getProperty(context, callbackObj, "onreleased");
107
108         JSObjectRef pressedCallback = NULL;
109         if(!JSValueIsUndefined(context, onpressed)){
110             pressedCallback = JSUtil::JSValueToObject(context, onpressed);
111         }
112
113         JSObjectRef releasedCallback = NULL;
114         if(!JSValueIsUndefined(context, onreleased)){
115             releasedCallback = JSUtil::JSValueToObject(context, onreleased);
116         }
117
118         MediaKeyEventListenerPtr callback(new MediaKeyEventCallback(
119             GlobalContextManager::getInstance()->getGlobalContext(context),
120             pressedCallback,
121             releasedCallback));
122
123         priv -> setMediaKeyEventListener(callback);
124
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);
129     } catch (...) {
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);
133     }
134 }
135
136 JSValueRef JSMediaKeyManager::unsetMediaKeyEventListener(JSContextRef context,
137         JSObjectRef object,
138         JSObjectRef thisObject,
139         size_t argumentCount,
140         const JSValueRef arguments[],
141         JSValueRef* exception)
142 {
143     try {
144         // Private Object
145         LOGD("Entered");
146
147         MediaKeyManager *priv = MediaKeyManager::getInstance();
148
149         // perform
150         priv ->unsetMediaKeyEventListener();
151
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);
156     } catch (...) {
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);
160     }
161 }
162
163 } // MediaKey
164 } // DeviceAPI