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 "plugin_config.h"
20 #include "JSSoundManager.h"
21 #include "JSSoundDeviceInfo.h"
22 #include "SoundUtil.h"
23 #include "VolumeChangeCallback.h"
24 #include "SoundDeviceStateChangeCallback.h"
26 #include <SecurityExceptions.h>
29 #include <JSWebAPIError.h>
30 #include <ArgumentValidator.h>
31 #include <JSStringRefWrapper.h>
32 #include <GlobalContextManager.h>
33 #include <MultiCallbackUserData.h>
34 #include <PlatformException.h>
37 using namespace WrtDeviceApis::Commons;
38 using namespace DeviceAPI::Common;
44 JSClassDefinition JSSoundManager::m_classInfo = {
46 kJSClassAttributeNone,
50 m_function, //StaticFunctions
51 initialize, //Initialize
56 NULL, //DeleteProperty,
57 NULL, //GetPropertyNames,
58 NULL, //CallAsFunction,
59 NULL, //CallAsConstructor,
64 JSStaticFunction JSSoundManager::m_function[] = {
66 SOUND_MANAGER_API_GET_SOUND_MODE,
68 kJSPropertyAttributeNone
71 SOUND_MANAGER_API_SET_SOUND_MODE_LISTENER,
72 setSoundModeChangeListener,
73 kJSPropertyAttributeNone
76 SOUND_MANAGER_API_UNSET_SOUND_MODE_LISTENER,
77 unsetSoundModeChangeListener,
78 kJSPropertyAttributeNone
81 SOUND_MANAGER_API_SET_VOLUME,
83 kJSPropertyAttributeNone
86 SOUND_MANAGER_API_GET_VOLUME,
88 kJSPropertyAttributeNone
91 SOUND_MANAGER_API_SET_VOLUME_CHANGE_LISTENER,
92 setVolumeChangeListener,
93 kJSPropertyAttributeNone
96 SOUND_MANAGER_API_UNSET_VOLUME_CHANGE_LISTENER,
97 unsetVolumeChangeListener,
98 kJSPropertyAttributeNone
101 SOUND_MANAGER_API_GET_CONNECTED_DEVICE_LIST,
102 getConnectedDeviceList,
103 kJSPropertyAttributeNone
106 SOUND_MANAGER_API_GET_ACTIVATED_DEVICE_LIST,
107 getActivatedDeviceList,
108 kJSPropertyAttributeNone
111 SOUND_MANAGER_API_ADD_DEVICE_STATE_CHANGE_LISTENER,
112 addDeviceStateChangeListener,
113 kJSPropertyAttributeNone
116 SOUND_MANAGER_API_REMOVE_DEVICE_STATE_CHANGE_LISTENER,
117 removeDeviceStateChangeListener,
118 kJSPropertyAttributeNone
123 JSClassRef JSSoundManager::m_jsClassRef = JSClassCreate(JSSoundManager::getClassInfo());
125 const JSClassRef JSSoundManager::getClassRef()
130 m_jsClassRef = JSClassCreate(&m_classInfo);
135 const JSClassDefinition* JSSoundManager::getClassInfo()
142 void JSSoundManager::initialize(JSContextRef context, JSObjectRef object)
146 JSObjectSetPrivate(object, static_cast<void *>(SoundManager::getInstance()));
149 void JSSoundManager::finalize(JSObjectRef object)
154 JSValueRef JSSoundManager::getSoundMode(JSContextRef context,
156 JSObjectRef thisObject,
157 size_t argumentCount,
158 const JSValueRef arguments[],
159 JSValueRef* exception)
164 SoundModeType mode = SoundManager::getInstance()->getSoundMode();
165 return JSUtil::toJSValueRef(context, SoundUtil::soundModeToString(mode));
166 } catch (const BasePlatformException &err) {
167 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
168 return JSWebAPIErrorFactory::postException(context, exception, err);
170 DeviceAPI::Common::UnknownException
171 err("Unknown Error in SoundManager.getSoundMode().");
172 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
173 return JSWebAPIErrorFactory::postException(context, exception, err);
177 JSValueRef JSSoundManager::setSoundModeChangeListener(JSContextRef context,
179 JSObjectRef thisObject,
180 size_t argumentCount,
181 const JSValueRef arguments[],
182 JSValueRef* exception)
187 ArgumentValidator validator(context, argumentCount, arguments);
188 JSObjectRef functionObject = validator.toFunction(0, false);
190 SoundModeChangeListenerPtr eventListener(new SoundModeChangeListener(GlobalContextManager::getInstance()->getGlobalContext(context), thisObject, functionObject));
191 SoundManager::getInstance()->setSoundModeChangeListener(eventListener);
192 } catch (const BasePlatformException &err) {
193 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
194 return JSWebAPIErrorFactory::postException(context, exception, err);
196 DeviceAPI::Common::UnknownException
197 err("Unknown Error in SoundManager.setSoundModeChangeListener().");
198 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
199 return JSWebAPIErrorFactory::postException(context, exception, err);
202 return JSValueMakeUndefined(context);
205 JSValueRef JSSoundManager::unsetSoundModeChangeListener(JSContextRef context,
207 JSObjectRef thisObject,
208 size_t argumentCount,
209 const JSValueRef arguments[],
210 JSValueRef* exception)
215 SoundManager::getInstance()->unsetSoundModeChangeListener();
216 } catch (const BasePlatformException &err) {
217 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
218 return JSWebAPIErrorFactory::postException(context, exception, err);
220 DeviceAPI::Common::UnknownException
221 err("Unknown Error in SoundManager.unsetSoundModeChangeListener().");
222 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
223 return JSWebAPIErrorFactory::postException(context, exception, err);
225 return JSValueMakeUndefined(context);
229 JSValueRef JSSoundManager::setVolume(JSContextRef context,
231 JSObjectRef thisObject,
232 size_t argumentCount,
233 const JSValueRef arguments[],
234 JSValueRef* exception)
239 TIZEN_CHECK_ACCESS(context, exception, SoundManager::getInstance(), SOUND_MANAGER_API_SET_VOLUME);
242 SoundManager *priv = static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
244 throw TypeMismatchException("Private object is NULL.");
247 ArgumentValidator validator(context, argumentCount, arguments);
250 std::string type = validator.toString(0);
253 double volume = validator.toDouble(1);
256 priv->setVolume(SoundUtil::stringToSoundType(type), volume);
258 return JSValueMakeUndefined(context);
259 } catch (const BasePlatformException &err) {
260 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
261 return JSWebAPIErrorFactory::postException(context, exception, err);
263 DeviceAPI::Common::UnknownException err("Unknown Error in SoundManager.setVolume().");
264 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
265 return JSWebAPIErrorFactory::postException(context, exception, err);
269 JSValueRef JSSoundManager::getVolume(JSContextRef context,
271 JSObjectRef thisObject,
272 size_t argumentCount,
273 const JSValueRef arguments[],
274 JSValueRef* exception)
280 SoundManager *priv = static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
282 throw TypeMismatchException("Private object is NULL.");
285 ArgumentValidator validator(context, argumentCount, arguments);
288 std::string type = validator.toString(0);
291 double ret = priv->getVolume(SoundUtil::stringToSoundType(type));
293 return JSUtil::toJSValueRef(context, ret);
294 } catch (const BasePlatformException &err) {
295 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
296 return JSWebAPIErrorFactory::postException(context, exception, err);
298 DeviceAPI::Common::UnknownException err("Unknown Error in SoundManager.getVolume().");
299 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
300 return JSWebAPIErrorFactory::postException(context, exception, err);
304 JSValueRef JSSoundManager::setVolumeChangeListener(JSContextRef context,
306 JSObjectRef thisObject,
307 size_t argumentCount,
308 const JSValueRef arguments[],
309 JSValueRef* exception)
315 SoundManager *priv = static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
317 throw TypeMismatchException("Private object is NULL.");
320 ArgumentValidator validator(context, argumentCount, arguments);
323 JSObjectRef successCallback = validator.toFunction(0);
324 VolumeChangeCallback* volumeChangeCallback =
325 new VolumeChangeCallback(
326 GlobalContextManager::getInstance()->getGlobalContext(context));
328 volumeChangeCallback->setCallback("onsuccess", successCallback);
331 priv->setVolumeChangeListener(volumeChangeCallback);
333 return JSValueMakeUndefined(context);
334 } catch (const BasePlatformException &err) {
335 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
336 return JSWebAPIErrorFactory::postException(context, exception, err);
338 DeviceAPI::Common::UnknownException err(
339 "Unknown Error in SoundManager.setVolumeChangeListener().");
340 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
341 return JSWebAPIErrorFactory::postException(context, exception, err);
345 JSValueRef JSSoundManager::unsetVolumeChangeListener(JSContextRef context,
347 JSObjectRef thisObject,
348 size_t argumentCount,
349 const JSValueRef arguments[],
350 JSValueRef* exception)
357 static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
359 throw TypeMismatchException("Private object is NULL.");
363 priv->unsetVolumeChangeListener();
365 return JSValueMakeUndefined(context);
366 } catch (const BasePlatformException &err) {
367 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
368 return JSWebAPIErrorFactory::postException(context, exception, err);
370 DeviceAPI::Common::UnknownException err(
371 "Unknown Error in SoundManager.unsetVolumeChangeListener().");
372 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
373 return JSWebAPIErrorFactory::postException(context, exception, err);
377 //newly added API on 2.3.1
378 JSValueRef JSSoundManager::getConnectedDeviceList(JSContextRef context,
380 JSObjectRef thisObject,
381 size_t argumentCount,
382 const JSValueRef arguments[],
383 JSValueRef* exception)
389 SoundManager *priv = static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
391 SoundUtil::throwSoundException<TypeMismatchException>("Private object is NULL.");
395 std::vector<SoundDeviceInfoPtr> list = priv->getConnectedDeviceList();
397 LOGD("The length of the list: (%d)", list.size());
399 JSObjectRef objArr[list.size()];
400 for (int i = 0; i < list.size(); i++) {
401 objArr[i] = JSSoundDeviceInfo::makeJSObject(context, list[i]);
404 JSValueRef exception = NULL;
405 JSObjectRef JSResult = JSObjectMakeArray(context, list.size(), objArr, &exception);
406 if (exception != NULL) {
407 LOGD("exception is not null");
408 SoundUtil::throwSoundException("exception is not null");
412 } catch (const BasePlatformException &err) {
413 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
414 return JSWebAPIErrorFactory::postException(context, exception, err);
416 DeviceAPI::Common::UnknownException err("Unknown Error in SoundManager.getConnectedDeviceList().");
417 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
418 return JSWebAPIErrorFactory::postException(context, exception, err);
422 JSValueRef JSSoundManager::getActivatedDeviceList(JSContextRef context,
424 JSObjectRef thisObject,
425 size_t argumentCount,
426 const JSValueRef arguments[],
427 JSValueRef* exception)
433 SoundManager *priv = static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
435 SoundUtil::throwSoundException<TypeMismatchException>("Private object is NULL.");
439 std::vector<SoundDeviceInfoPtr> list = priv->getActivatedDeviceList();
441 LOGD("The length of the list: (%d)", list.size());
443 JSObjectRef objArr[list.size()];
444 for (int i = 0; i < list.size(); i++) {
445 objArr[i] = JSSoundDeviceInfo::makeJSObject(context, list[i]);
448 JSValueRef exception = NULL;
449 JSObjectRef JSResult = JSObjectMakeArray(context, list.size(), objArr, &exception);
450 if (exception != NULL) {
451 LOGD("exception is not null");
452 SoundUtil::throwSoundException("exception is not null");
456 } catch (const BasePlatformException &err) {
457 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
458 return JSWebAPIErrorFactory::postException(context, exception, err);
460 DeviceAPI::Common::UnknownException err("Unknown Error in SoundManager.getActivatedDeviceList().");
461 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
462 return JSWebAPIErrorFactory::postException(context, exception, err);
466 JSValueRef JSSoundManager::addDeviceStateChangeListener(JSContextRef context,
468 JSObjectRef thisObject,
469 size_t argumentCount,
470 const JSValueRef arguments[],
471 JSValueRef* exception)
477 SoundManager *priv = static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
479 SoundUtil::throwSoundException<TypeMismatchException>("Private object is NULL.");
482 ArgumentValidator validator(context, argumentCount, arguments);
485 JSContextRef g_ctx = GlobalContextManager::getInstance()->getGlobalContext(context);
486 SoundDeviceStateChangeCallbackPtr callback = SoundDeviceStateChangeCallbackPtr(new SoundDeviceStateChangeCallback(g_ctx));
488 JSObjectRef Listener = validator.toFunction(0);
490 callback->setCallback("onchanged", Listener);
494 const long id = priv->addDeviceStateChangeListener(callback);
495 LOGD("id: (%d)", id);
497 return JSUtil::toJSValueRef(context, id);
498 } catch (const BasePlatformException &err) {
499 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
500 return JSWebAPIErrorFactory::postException(context, exception, err);
502 DeviceAPI::Common::UnknownException err("Unknown Error in SoundManager.addDeviceStateChangeListener().");
503 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
504 return JSWebAPIErrorFactory::postException(context, exception, err);
508 JSValueRef JSSoundManager::removeDeviceStateChangeListener(JSContextRef context,
510 JSObjectRef thisObject,
511 size_t argumentCount,
512 const JSValueRef arguments[],
513 JSValueRef* exception)
519 SoundManager *priv = static_cast<SoundManager*>(JSObjectGetPrivate(thisObject));
521 SoundUtil::throwSoundException<TypeMismatchException>("Private object is NULL.");
524 ArgumentValidator validator(context, argumentCount, arguments);
527 const long id = validator.toLong(0);
528 LOGD("id: (%d)", id);
531 priv->removeDeviceStateChangeListener(id);
533 return JSValueMakeUndefined(context);
534 } catch (const BasePlatformException &err) {
535 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
536 return JSWebAPIErrorFactory::postException(context, exception, err);
538 DeviceAPI::Common::UnknownException err("Unknown Error in SoundManager.removeDeviceStateChangeListener().");
539 LOGE("%s: %s", err.getName().c_str(), err.getMessage().c_str());
540 return JSWebAPIErrorFactory::postException(context, exception, err);