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.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <CommonsJavaScript/JSPendingOperation.h>
25 #include <CommonsJavaScript/PrivateObject.h>
26 #include <CommonsJavaScript/Utils.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
29 #include <Tizen/Common/JSTizenExceptionFactory.h>
30 #include <Tizen/Common/JSTizenException.h>
31 #include <Tizen/Common/SecurityExceptions.h>
32 #include <Tizen/Tizen/FilterConverter.h>
34 #include <API/Mediacontent/MediacontentFactory.h>
35 #include "MediacontentController.h"
36 #include "JSMediacontent.h"
37 #include "JSMediacontentManager.h"
42 #include "MediaConverter.h"
43 #include "plugin_config.h"
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Tizen;
47 using namespace TizenApis::Tizen1_0::Tizen;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
52 #define TIZEN_MEDIACONTENT_ATTRIBUTENAME "MediaSource"
56 * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
58 JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg)
60 if (Validator(ctx).isCallback(arg))
64 else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg))
66 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
74 namespace Mediacontent {
77 JSClassDefinition JSMediacontent::m_classInfo =
80 kJSClassAttributeNone,
81 TIZEN_MEDIACONTENT_ATTRIBUTENAME,
90 NULL,//DeleteProperty,
91 NULL,//GetPropertyNames,
92 NULL,//CallAsFunction,
93 NULL,//CallAsConstructor,
98 JSStaticValue JSMediacontent::m_property[] =
103 JSStaticFunction JSMediacontent::m_function[] =
105 { "findItems", findItems, kJSPropertyAttributeNone },
106 { "getFolders", getFolders, kJSPropertyAttributeNone },
107 { "updateItem", updateItem, kJSPropertyAttributeNone },
108 { "updateItemsBatch", updateItemsBatch, kJSPropertyAttributeNone },
112 JSClassRef JSMediacontent::m_jsClassRef = JSClassCreate(JSMediacontent::getClassInfo());
114 void JSMediacontent::initialize(JSContextRef context, JSObjectRef object)
116 LogDebug("JSMediacontent::initialize entered");
117 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
120 //create default instance
121 LogWarning("create default instance");
122 IMediacontentPtr mediacontent = MediacontentFactory::getInstance().createMediacontentObject();
123 priv = new MediacontentPrivObject(context, mediacontent);
124 if (!JSObjectSetPrivate(object, static_cast<void*>(priv)))
131 LogDebug("private object alrerady exists");
136 void JSMediacontent::finalize(JSObjectRef object)
139 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( object ) ) ;
144 LogDebug("JSMediacontent::finalize entered");
149 const JSClassRef JSMediacontent::getClassRef()
153 m_jsClassRef = JSClassCreate(&m_classInfo);
158 const JSClassDefinition* JSMediacontent::getClassInfo()
163 JSValueRef JSMediacontent::getFolders(
164 JSContextRef context,
166 JSObjectRef thisObject,
167 size_t argumentCount,
168 const JSValueRef arguments[],
169 JSValueRef* exception )
172 LogDebug("JSMediacontent::findFolders entered");
174 MediacontentPrivObject *privateObject;
176 privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
179 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
182 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
183 MEDIACONTENT_FUNCTION_API_GET_FOLDERS);
185 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
187 JSContextRef globalContext = privateObject->getContext();
188 Validator validator(context);
189 JSCallbackManagerPtr cbm(NULL);
190 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
191 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
193 cbm = JSCallbackManager::createObject(globalContext);
195 // Protect the super object until the callback operation is finished.
196 JSValueProtect(globalContext, thisObject);
200 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
202 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
204 if(argumentCount >= 1) //MediaFolderArraySuccessCB successCallback
206 if(validator.isCallback(arguments[0]))
208 onSuccessForCbm = arguments[0];
212 LogDebug("SuccessCallback type mismatched.");
213 Throw(ConversionException);
218 LogDebug("There is no successCallback.");
219 Throw(ConversionException);
221 if(argumentCount >= 2) //optional ErrorCallback? errorCallback
223 if(validator.isCallback(arguments[1]))
225 onErrorForCbm = arguments[1];
227 else if(!validator.isNullOrUndefined(arguments[1])) //nullable
229 LogDebug("ErrorCallback type mismatched.");
230 Throw(ConversionException);
235 cbm->setOnSuccess(onSuccessForCbm);
236 cbm->setOnError(onErrorForCbm);
239 IEventFindFolderPtr dplEvent(new IEventFindFolder());
240 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
241 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
243 mediacontent->findFolder(dplEvent);
245 Catch(UnsupportedException)
247 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
248 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
250 Catch(InvalidArgumentException)
252 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
253 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
255 Catch(ConversionException)
257 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
258 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
260 Catch (NotFoundException)
262 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
263 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
267 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
268 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
271 return JSValueMakeUndefined(context);
276 JSValueRef JSMediacontent::findItems(
277 JSContextRef context,
279 JSObjectRef thisObject,
280 size_t argumentCount,
281 const JSValueRef arguments[],
282 JSValueRef* exception )
285 LogDebug("JSMediacontent::findItems entered");
287 MediacontentPrivObject *privateObject;
289 privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
292 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
296 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
297 MEDIACONTENT_FUNCTION_API_FIND_ITEMS);
299 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
302 JSContextRef globalContext = privateObject->getContext();
303 Validator validator(context);
304 JSCallbackManagerPtr cbm(NULL);
305 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
306 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
307 MediaConverter mediaConverter(globalContext);
309 cbm = JSCallbackManager::createObject(globalContext);
311 // Protect the super object until the callback operation is finished.
312 JSValueProtect(globalContext, thisObject);
316 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
317 IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
318 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
320 if(argumentCount >= 1) //MediaItemArraySuccessCB successCallback
322 if(validator.isCallback(arguments[0]))
324 onSuccessForCbm = arguments[0];
328 LogDebug("SuccessCallback type mismatched.");
329 Throw(ConversionException);
334 LogDebug("There is no SuccessCallback.");
335 Throw(ConversionException);
337 if(argumentCount >= 2) //ErrorCallback? errorCallback
339 if(validator.isCallback(arguments[1]))
341 onErrorForCbm = arguments[1];
343 else if(!validator.isNullOrUndefined(arguments[1])) //nullable
345 LogDebug("ErrorCallback type mismatched.");
346 Throw(ConversionException);
351 cbm->setOnSuccess(onSuccessForCbm);
352 cbm->setOnError(onErrorForCbm);
355 if(argumentCount >= 3) //MediaFolderId id,
357 string folderId = mediaConverter.toString(arguments[2]);
358 if(!(validator.isNullOrUndefined(arguments[2])) && (folderId.length() > 0) )
360 LogDebug("folderId:"+ folderId);
361 dplEvent->setFolderID(folderId);
365 if(argumentCount >= 4) //optional AbstractFilter? filter
367 if(JSValueIsObject(context, arguments[3]))
369 dplEvent->setFilter(filterConverter->toFilter(arguments[3]));
371 else if(!validator.isNullOrUndefined(arguments[3])) //nullable
373 LogDebug("Filter type mismatched.");
374 Throw(ConversionException);
377 if(argumentCount >= 5) //optional SortMode? sortMode
379 if(JSValueIsObject(context, arguments[4]))
381 LogDebug("sortmode is object");
382 // Though the sortMode is a single type, we save it in an array internally.
383 TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
384 sortModes->push_back(filterConverter->toSortMode(arguments[4]));
385 dplEvent->setSortModes(sortModes);
387 else if(!validator.isNullOrUndefined(arguments[4])) //nullable
389 LogDebug("SortMode type mismatched.");
390 Throw(ConversionException);
393 if(argumentCount >= 6) //optional unsigned long? count
395 long count = filterConverter->toLong(arguments[5]);
398 dplEvent->setLimit(count);
402 dplEvent->setLimit(-1);
406 if(argumentCount >= 7) //optional unsigned long? offset
408 long limit = filterConverter->toLong(arguments[6]);
411 dplEvent->setLimit(limit);
415 dplEvent->setLimit(-1);
418 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
419 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
420 mediacontent->browseFolder(dplEvent);
422 Catch(UnsupportedException)
424 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
425 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
427 Catch(InvalidArgumentException)
429 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
430 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
432 Catch(ConversionException)
434 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
435 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
437 Catch (NotFoundException)
439 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
440 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
444 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
445 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
448 return JSValueMakeUndefined(context);
453 JSValueRef JSMediacontent::updateItemsBatch(JSContextRef context,
455 JSObjectRef thisObject,
456 size_t argumentCount,
457 const JSValueRef arguments[],
458 JSValueRef* exception)
462 MediacontentPrivObject *privateObject;
464 privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
467 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
470 JSContextRef globalContext = privateObject->getContext();
471 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
472 MEDIACONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH);
473 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
475 Validator validator(context);
476 JSCallbackManagerPtr cbm(NULL);
477 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
478 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
480 cbm = JSCallbackManager::createObject(globalContext);
482 // Protect the super object until the callback operation is finished.
483 JSValueProtect(globalContext, thisObject);
488 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
490 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
491 MediacontentMediaListPtr events;
492 if((argumentCount >= 1) && (JSIsArrayValue(context, arguments[0]))) //MediaItem[] items
494 events = converter->toVectorOfMediaItem(arguments[0]);
497 ThrowMsg(ConversionException, "Items type mismatched.");
502 ThrowMsg(ConversionException, "Items type mismatched.");
505 if(argumentCount >= 2) //Function? successCallback,
507 if(validator.isCallback(arguments[1]))
509 onSuccessForCbm = arguments[1];
511 else if(!validator.isNullOrUndefined(arguments[1])) //nullable
513 LogDebug("successCallback type mismatched.");
514 Throw(ConversionException);
518 if(argumentCount >= 3) //ErrorCallback? errorCallback,
520 if(validator.isCallback(arguments[2]))
522 onErrorForCbm = arguments[2];
524 else if(!validator.isNullOrUndefined(arguments[2])) //nullable
526 LogDebug("ErrorCallback type is mismatched.");
527 Throw(ConversionException);
533 cbm->setOnSuccess(onSuccessForCbm);
534 cbm->setOnError(onErrorForCbm);
537 IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
538 dplEvent->setMediaItems(events);
539 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
541 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
542 mediacontent->updateMediaItems(dplEvent);
545 Catch(UnsupportedException)
547 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
548 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
550 Catch(InvalidArgumentException)
552 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
553 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
555 Catch(ConversionException)
557 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
558 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
560 Catch (NotFoundException)
562 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
563 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
567 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
568 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
571 return JSValueMakeUndefined(context);
576 JSValueRef JSMediacontent::updateItem(
577 JSContextRef context,
579 JSObjectRef thisObject,
580 size_t argumentCount,
581 const JSValueRef arguments[],
582 JSValueRef* exception)
584 LogDebug("updateItem::entered");
587 MediacontentPrivObject *privateObject;
589 privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
592 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
595 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
596 MEDIACONTENT_FUNCTION_API_UPDATE_ITEM);
598 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
600 Validator validator(context);
604 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
606 //parameter : MediaItem item
609 if(argumentCount >= 1)
611 if (!JSValueIsObjectOfClass(context, arguments[0], JSMedia::getClassRef()) &&
612 !JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef()) &&
613 !JSValueIsObjectOfClass(context, arguments[0], JSAudio::getClassRef()) &&
614 !JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())) {
615 ThrowMsg(ConversionException, "Item type mismatched.");
617 arg = JSValueToObject(context, arguments[0], exception);
621 ThrowMsg(ConversionException, "Item type mismatched.");
624 MediacontentMediaPtr event = JSMedia::getMediaObject(arg);
626 IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
627 dplEvent->setMediaItem(event);
628 dplEvent->setForSynchronousCall();
629 mediacontent->updateMedia(dplEvent);
631 if (dplEvent->getResult()) {
632 return JSValueMakeUndefined(context);
634 ThrowMsg(UnknownException, "updating failed by unknown reason.");
638 Catch(UnsupportedException)
640 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
641 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
643 Catch(InvalidArgumentException)
645 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
646 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
648 Catch(ConversionException)
650 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
651 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
653 Catch (NotFoundException)
655 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
656 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
660 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
661 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
664 return JSValueMakeUndefined(context);
669 IMediacontentPtr JSMediacontent::getMediacontentObject(
671 const JSObjectRef object,
672 JSValueRef* exception)
674 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
677 return priv->getObject();
679 ThrowMsg(ConversionException, "Private object is NULL.");