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"
44 #include "MediaContentAsyncCallbackManager.h"
46 using namespace TizenApis::Commons;
47 using namespace TizenApis::Api::Tizen;
48 using namespace TizenApis::Tizen1_0::Tizen;
49 using namespace WrtDeviceApis::Commons;
50 using namespace WrtDeviceApis::CommonsJavaScript;
53 #define TIZEN_MEDIACONTENT_ATTRIBUTENAME "MediaSource"
57 * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
59 JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg)
61 if (Validator(ctx).isCallback(arg))
65 else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg))
67 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
75 namespace Mediacontent {
78 JSClassDefinition JSMediacontent::m_classInfo =
81 kJSClassAttributeNone,
82 TIZEN_MEDIACONTENT_ATTRIBUTENAME,
91 NULL,//DeleteProperty,
92 NULL,//GetPropertyNames,
93 NULL,//CallAsFunction,
94 NULL,//CallAsConstructor,
99 JSStaticValue JSMediacontent::m_property[] =
104 JSStaticFunction JSMediacontent::m_function[] =
106 { "findItems", findItems, kJSPropertyAttributeNone },
107 { "getFolders", getFolders, kJSPropertyAttributeNone },
108 { "updateItem", updateItem, kJSPropertyAttributeNone },
109 { "updateItemsBatch", updateItemsBatch, kJSPropertyAttributeNone },
113 JSClassRef JSMediacontent::m_jsClassRef = JSClassCreate(JSMediacontent::getClassInfo());
115 void JSMediacontent::initialize(JSContextRef context, JSObjectRef object)
117 LogDebug("JSMediacontent::initialize entered");
118 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
121 //create default instance
122 LogWarning("create default instance");
123 IMediacontentPtr mediacontent = MediacontentFactory::getInstance().createMediacontentObject();
124 priv = new MediacontentPrivObject(context, mediacontent);
125 if (!JSObjectSetPrivate(object, static_cast<void*>(priv)))
132 LogDebug("private object alrerady exists");
137 void JSMediacontent::finalize(JSObjectRef object)
140 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( object ) ) ;
145 LogDebug("JSMediacontent::finalize entered");
150 const JSClassRef JSMediacontent::getClassRef()
154 m_jsClassRef = JSClassCreate(&m_classInfo);
159 const JSClassDefinition* JSMediacontent::getClassInfo()
164 JSValueRef JSMediacontent::getFolders(
165 JSContextRef context,
167 JSObjectRef thisObject,
168 size_t argumentCount,
169 const JSValueRef arguments[],
170 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);
194 cbm->setObject(thisObject);
198 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
200 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
202 if(argumentCount >= 1) //MediaFolderArraySuccessCB successCallback
204 if(validator.isCallback(arguments[0]))
206 onSuccessForCbm = arguments[0];
210 LogDebug("SuccessCallback type mismatched.");
211 Throw(ConversionException);
216 LogDebug("There is no successCallback.");
217 Throw(ConversionException);
219 if(argumentCount >= 2) //optional ErrorCallback? errorCallback
221 if(validator.isCallback(arguments[1]))
223 onErrorForCbm = arguments[1];
225 else if(!validator.isNullOrUndefined(arguments[1])) //nullable
227 LogDebug("ErrorCallback type mismatched.");
228 Throw(ConversionException);
233 cbm->setOnSuccess(onSuccessForCbm);
234 cbm->setOnError(onErrorForCbm);
237 IEventFindFolderPtr dplEvent(new IEventFindFolder());
238 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
239 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
241 mediacontent->findFolder(dplEvent);
243 MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
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");
295 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
296 MEDIACONTENT_FUNCTION_API_FIND_ITEMS);
298 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
301 JSContextRef globalContext = privateObject->getContext();
302 Validator validator(context);
303 JSCallbackManagerPtr cbm(NULL);
304 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
305 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
306 MediaConverter mediaConverter(globalContext);
308 cbm = JSCallbackManager::createObject(globalContext);
309 cbm->setObject(thisObject);
314 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
315 IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
316 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
318 if(argumentCount >= 1) //MediaItemArraySuccessCB successCallback
320 if(validator.isCallback(arguments[0]))
322 onSuccessForCbm = arguments[0];
326 LogDebug("SuccessCallback type mismatched.");
327 Throw(ConversionException);
332 LogDebug("There is no SuccessCallback.");
333 Throw(ConversionException);
335 if(argumentCount >= 2) //ErrorCallback? errorCallback
337 if(validator.isCallback(arguments[1]))
339 onErrorForCbm = arguments[1];
341 else if(!validator.isNullOrUndefined(arguments[1])) //nullable
343 LogDebug("ErrorCallback type mismatched.");
344 Throw(ConversionException);
349 cbm->setOnSuccess(onSuccessForCbm);
350 cbm->setOnError(onErrorForCbm);
353 if(argumentCount >= 3) //MediaFolderId id,
355 string folderId = mediaConverter.toString(arguments[2]);
356 if(!(validator.isNullOrUndefined(arguments[2])) && (folderId.length() > 0) )
358 LogDebug("folderId:"+ folderId);
361 istringstream(folderId) >> nFolderId; //string -> int
365 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
366 JSTizenException::INVALID_VALUES_ERROR, "Argument is invalid"));
367 return JSValueMakeUndefined(context);
370 dplEvent->setFolderID(folderId);
374 if(argumentCount >= 4) //optional AbstractFilter? filter
376 if(JSValueIsObject(context, arguments[3]))
378 dplEvent->setFilter(filterConverter->toFilter(arguments[3]));
380 else if(!validator.isNullOrUndefined(arguments[3])) //nullable
382 LogDebug("Filter type mismatched.");
383 Throw(ConversionException);
386 if(argumentCount >= 5) //optional SortMode? sortMode
389 if ( JSValueIsObject(context, arguments[4]) &&
390 !JSValueIsNull(context, arguments[4]) &&
391 !JSValueIsUndefined(context, arguments[4]) &&
392 !JSIsArrayValue(context, arguments[4]))
394 LogDebug("sortmode is object");
395 dplEvent->setSortMode(filterConverter->toSortMode(arguments[4]));
397 else if(!validator.isNullOrUndefined(arguments[4])) //nullable
399 LogDebug("SortMode type mismatched.");
400 Throw(ConversionException);
403 if(argumentCount >= 6) //optional unsigned long? count
405 long count = filterConverter->toLong(arguments[5]);
409 dplEvent->setLimit(count);
411 else if ( count == 0.0)
413 dplEvent->setLimit(-1);
417 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
418 JSTizenException::INVALID_VALUES_ERROR, "Argument is invalid"));
419 return JSValueMakeUndefined(context);
423 if(argumentCount >= 7) //optional unsigned long? offset
425 long offset = filterConverter->toLong(arguments[6]);
428 dplEvent->setOffset(offset);
432 dplEvent->setOffset(-1);
435 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
436 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
437 mediacontent->browseFolder(dplEvent);
439 MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
441 Catch(UnsupportedException)
443 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
444 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
446 Catch(InvalidArgumentException)
448 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
449 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
451 Catch(ConversionException)
453 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
454 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
456 Catch (NotFoundException)
458 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
459 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
463 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
464 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
467 return JSValueMakeUndefined(context);
472 JSValueRef JSMediacontent::updateItemsBatch(JSContextRef context,
474 JSObjectRef thisObject,
475 size_t argumentCount,
476 const JSValueRef arguments[],
477 JSValueRef* exception)
481 MediacontentPrivObject *privateObject;
483 privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
486 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
489 JSContextRef globalContext = privateObject->getContext();
490 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
491 MEDIACONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH);
492 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
494 Validator validator(context);
495 JSCallbackManagerPtr cbm(NULL);
496 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
497 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
499 cbm = JSCallbackManager::createObject(globalContext);
500 cbm->setObject(thisObject);
505 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
507 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
508 MediacontentMediaListPtr events;
509 if((argumentCount >= 1) && (JSIsArrayValue(context, arguments[0]))) //MediaItem[] items
511 events = converter->toVectorOfMediaItem(arguments[0]);
514 ThrowMsg(ConversionException, "Items type mismatched.");
519 ThrowMsg(ConversionException, "Items type mismatched.");
522 if(argumentCount >= 2) //Function? successCallback,
524 if(validator.isCallback(arguments[1]))
526 onSuccessForCbm = arguments[1];
528 else if(!validator.isNullOrUndefined(arguments[1])) //nullable
530 LogDebug("successCallback type mismatched.");
531 Throw(ConversionException);
535 if(argumentCount >= 3) //ErrorCallback? errorCallback,
537 if(validator.isCallback(arguments[2]))
539 onErrorForCbm = arguments[2];
541 else if(!validator.isNullOrUndefined(arguments[2])) //nullable
543 LogDebug("ErrorCallback type is mismatched.");
544 Throw(ConversionException);
550 cbm->setOnSuccess(onSuccessForCbm);
551 cbm->setOnError(onErrorForCbm);
554 IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
555 dplEvent->setMediaItems(events);
556 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
558 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
559 mediacontent->updateMediaItems(dplEvent);
561 MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
563 Catch(UnsupportedException)
565 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
566 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
568 Catch(InvalidArgumentException)
570 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
571 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
573 Catch(ConversionException)
575 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
576 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
578 Catch (NotFoundException)
580 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
581 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
585 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
586 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
589 return JSValueMakeUndefined(context);
594 JSValueRef JSMediacontent::updateItem(
595 JSContextRef context,
597 JSObjectRef thisObject,
598 size_t argumentCount,
599 const JSValueRef arguments[],
600 JSValueRef* exception)
602 LogDebug("updateItem::entered");
605 MediacontentPrivObject *privateObject;
607 privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
610 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
613 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(MEDIACONTENT_FUNCTION_API_UPDATE_ITEM);
615 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
617 Validator validator(context);
621 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
623 //parameter : MediaItem item
626 if(argumentCount >= 1)
628 if (!JSValueIsObjectOfClass(context, arguments[0], JSMedia::getClassRef()) &&
629 !JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef()) &&
630 !JSValueIsObjectOfClass(context, arguments[0], JSAudio::getClassRef()) &&
631 !JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())) {
632 ThrowMsg(ConversionException, "Item type mismatched.");
634 arg = JSValueToObject(context, arguments[0], exception);
638 ThrowMsg(ConversionException, "Item type mismatched.");
641 MediacontentMediaPtr event = JSMedia::getMediaObject(arg);
643 IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
644 dplEvent->setMediaItem(event);
645 dplEvent->setForSynchronousCall();
646 mediacontent->updateMedia(dplEvent);
648 if (dplEvent->getResult()) {
649 return JSValueMakeUndefined(context);
651 ThrowMsg(UnknownException, "updating failed by unknown reason.");
655 Catch(UnsupportedException)
657 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
658 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
660 Catch(InvalidArgumentException)
662 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
663 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
665 Catch(ConversionException)
667 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
668 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
670 Catch (NotFoundException)
672 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
673 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
677 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
678 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
681 return JSValueMakeUndefined(context);
686 IMediacontentPtr JSMediacontent::getMediacontentObject(
688 const JSObjectRef object,
689 JSValueRef* exception)
691 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
694 return priv->getObject();
696 ThrowMsg(ConversionException, "Private object is NULL.");