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>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include <Tizen/Common/SecurityExceptions.h>
29 #include <Tizen/Tizen/FilterConverter.h>
31 #include <API/Mediacontent/MediacontentFactory.h>
32 #include "MediacontentController.h"
33 #include "JSMediacontent.h"
34 #include "JSMediacontentManager.h"
36 #include "MediaConverter.h"
37 #include "plugin_config.h"
42 using namespace TizenApis::Commons;
43 using namespace TizenApis::Api::Tizen;
44 using namespace TizenApis::Tizen1_0::Tizen;
47 #define TIZEN_MEDIACONTENT_ATTRIBUTENAME "MediaSource"
51 * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
53 JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg)
55 if (Validator(ctx).isCallback(arg))
59 else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg))
61 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
69 namespace Mediacontent {
72 JSClassDefinition JSMediacontent::m_classInfo =
75 kJSClassAttributeNone,
76 TIZEN_MEDIACONTENT_ATTRIBUTENAME,
85 NULL,//DeleteProperty,
86 NULL,//GetPropertyNames,
87 NULL,//CallAsFunction,
88 NULL,//CallAsConstructor,
93 JSStaticValue JSMediacontent::m_property[] =
98 JSStaticFunction JSMediacontent::m_function[] =
100 { "findItems", findMedia, kJSPropertyAttributeNone },
101 { "getFolders", getFolders, kJSPropertyAttributeNone },
102 { "browseFolder", browseFolder, kJSPropertyAttributeNone },
103 { "updateItem", updateItem, kJSPropertyAttributeNone },
104 { "updateItemsBatch", updateItemsBatch, kJSPropertyAttributeNone },
108 JSClassRef JSMediacontent::m_jsClassRef = JSClassCreate(JSMediacontent::getClassInfo());
110 void JSMediacontent::initialize(JSContextRef context, JSObjectRef object)
112 LogDebug("JSMediacontent::initialize entered");
113 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
116 //create default instance
117 LogWarning("create default instance");
118 IMediacontentPtr mediacontent = MediacontentFactory::getInstance().createMediacontentObject();
119 priv = new MediacontentPrivObject(context, mediacontent);
120 if (!JSObjectSetPrivate(object, static_cast<void*>(priv)))
127 LogDebug("private object alrerady exists");
132 void JSMediacontent::finalize(JSObjectRef object)
135 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( object ) ) ;
139 const JSClassRef JSMediacontent::getClassRef()
143 m_jsClassRef = JSClassCreate(&m_classInfo);
148 const JSClassDefinition* JSMediacontent::getClassInfo()
154 JSValueRef JSMediacontent::findMedia(
155 JSContextRef context,
157 JSObjectRef thisObject,
158 size_t argumentCount,
159 const JSValueRef arguments[],
160 JSValueRef* exception )
162 LogDebug("JSMediacontent::findMedia entered");
164 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
165 assert(privateObject);
167 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_FIND_ITEMS);
169 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
171 JSContextRef globalContext = privateObject->getContext();
173 Validator validator(context);
174 JSCallbackManagerPtr cbm(NULL);
175 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
176 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
177 cbm = JSCallbackManager::createObject(globalContext);
179 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
183 if (argumentCount == 0 ||
184 (!validator.isCallback(arguments[0])) ||
185 (argumentCount >= 2 && (!validator.isCallback(arguments[1]))) )
187 LogDebug("Argument type mismatch");
188 Throw(InvalidArgumentException);
192 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
193 onSuccessForCbm = arguments[0];
194 if (argumentCount >= 2)
196 onErrorForCbm = arguments[1];
198 cbm->setOnSuccess(onSuccessForCbm);
199 cbm->setOnError(onErrorForCbm);
203 IEventFindMediaPtr dplEvent(new IEventFindMedia());
204 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
205 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
207 if (argumentCount >= 3 && !validator.isNullOrUndefined(arguments[2]))
209 dplEvent->setFilter(filterConverter->toFilter(arguments[2]));
211 if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3]))
213 // Though the sortMode is a single type, we save it in an array internally.
214 TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
215 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
216 dplEvent->setSortModes(sortModes);
219 if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4]))
221 long limit = filterConverter->toLong(arguments[4]);
222 dplEvent->setLimit(limit);
224 if (argumentCount >= 6 && !validator.isNullOrUndefined(arguments[5]))
226 long offset = filterConverter->toLong(arguments[5]);
227 dplEvent->setOffset(offset);
230 mediacontent->findMedia(dplEvent);
233 Catch(InvalidArgumentException)
235 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
237 Catch(ConversionException)
239 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
243 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
246 return JSValueMakeNull(context);
251 JSValueRef JSMediacontent::getFolders(
252 JSContextRef context,
254 JSObjectRef thisObject,
255 size_t argumentCount,
256 const JSValueRef arguments[],
257 JSValueRef* exception )
260 LogDebug("JSMediacontent::findFolders entered");
262 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
263 assert(privateObject);
266 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_GET_FOLDERS);
268 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
271 JSContextRef globalContext = privateObject->getContext();
272 Validator validator(context);
273 JSCallbackManagerPtr cbm(NULL);
274 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
275 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
277 cbm = JSCallbackManager::createObject(globalContext);
279 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
284 if ((!validator.isCallback(arguments[0])) ||
285 (argumentCount >= 2 && (!validator.isCallback(arguments[1]))) ||
286 (argumentCount >= 3 && (!JSValueIsObject(context, arguments[2]) && !validator.isNullOrUndefined(arguments[2]))))
288 ThrowMsg(ConversionException, "Wrong parameter type.");
293 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
294 onSuccessForCbm = arguments[0];
295 if (argumentCount >= 2)
297 onErrorForCbm = arguments[1];
299 if (argumentCount >= 3)
301 onErrorForCbm = arguments[2];
303 cbm->setOnSuccess(onSuccessForCbm);
304 cbm->setOnError(onErrorForCbm);
307 IEventFindFolderPtr dplEvent(new IEventFindFolder());
308 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
309 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
312 mediacontent->findFolder(dplEvent);
314 Catch(InvalidArgumentException)
316 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
318 Catch(ConversionException)
320 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
324 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
327 return JSValueMakeNull(context);
332 JSValueRef JSMediacontent::browseFolder(
333 JSContextRef context,
335 JSObjectRef thisObject,
336 size_t argumentCount,
337 const JSValueRef arguments[],
338 JSValueRef* exception )
341 LogDebug("JSMediacontent::browseFolder entered");
343 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
344 assert(privateObject);
348 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_BROWSE_FOLDER);
350 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
353 JSContextRef globalContext = privateObject->getContext();
354 Validator validator(context);
355 JSCallbackManagerPtr cbm(NULL);
356 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
357 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
358 MediaConverter mediaConverter(globalContext);
360 cbm = JSCallbackManager::createObject(globalContext);
362 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
367 if (argumentCount == 0 ||
368 (!validator.isCallback(arguments[1])) ||
369 (argumentCount >= 3 && (!validator.isCallback(arguments[2]))) ||
370 (argumentCount >= 4 && (!JSValueIsObject(context, arguments[3]) )) )
372 LogDebug("Argument type mismatch");
373 Throw(InvalidArgumentException);
378 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
379 onSuccessForCbm = arguments[1];
380 if (argumentCount >= 3)
382 onErrorForCbm = arguments[2];
384 cbm->setOnSuccess(onSuccessForCbm);
385 cbm->setOnError(onErrorForCbm);
388 IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
389 dplEvent->setFolderID(mediaConverter.toString(arguments[0]));
390 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
391 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
393 if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3]))
395 dplEvent->setFilter(filterConverter->toFilter(arguments[3]));
397 if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4]))
399 TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
400 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
401 dplEvent->setSortModes(sortModes);
404 if (argumentCount >= 6 && !validator.isNullOrUndefined(arguments[5]))
406 long limit = filterConverter->toLong(arguments[5]);
407 dplEvent->setLimit(limit);
409 if (argumentCount >= 7 && !validator.isNullOrUndefined(arguments[6]))
411 long offset = filterConverter->toLong(arguments[6]);
412 dplEvent->setOffset(offset);
415 mediacontent->browseFolder(dplEvent);
417 Catch(InvalidArgumentException)
419 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
421 Catch(ConversionException)
423 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
427 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
430 return JSValueMakeNull(context);
436 JSValueRef JSMediacontent::updateItemsBatch(JSContextRef context,
438 JSObjectRef thisObject,
439 size_t argumentCount,
440 const JSValueRef arguments[],
441 JSValueRef* exception)
445 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
446 assert(privateObject);
448 JSContextRef globalContext = privateObject->getContext();
449 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(globalContext, MEDIACONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH);
450 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
452 Validator validator(context);
453 JSCallbackManagerPtr cbm(NULL);
454 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
455 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
457 cbm = JSCallbackManager::createObject(globalContext);
459 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
464 if (argumentCount>4 || argumentCount<1) {
465 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
468 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
469 ThrowMsg(ConversionException, "Wrong parameter type.");
472 MediacontentMediaListPtr events;
474 events = converter->toVectorOfEvents(arguments[0]);
476 ThrowMsg(ConversionException, "Parameter conversion failed.");
480 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
481 if((argumentCount >= 2) && validator.isCallback(arguments[1]))
483 onSuccessForCbm = arguments[1];
486 if((argumentCount >= 3) && validator.isCallback(arguments[2]))
488 onErrorForCbm = arguments[2];
490 cbm->setOnSuccess(onSuccessForCbm);
491 cbm->setOnError(onErrorForCbm);
493 IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
494 dplEvent->setMediaItems(events);
495 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
497 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
498 mediacontent->updateMediaItems(dplEvent);
500 Catch(UnsupportedException)
502 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
503 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
505 Catch(InvalidArgumentException)
507 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
508 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
510 Catch(ConversionException)
512 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
513 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
515 Catch (NotFoundException)
517 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
518 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
522 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
523 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
526 return JSValueMakeNull(context);
531 JSValueRef JSMediacontent::updateItem(
532 JSContextRef context,
534 JSObjectRef thisObject,
535 size_t argumentCount,
536 const JSValueRef arguments[],
537 JSValueRef* exception)
539 LogDebug("updateItem::entered");
542 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
543 assert(privateObject);
545 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_UPDATE_ITEM);
547 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
549 Validator validator(context);
550 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
554 if (argumentCount == 0 ||
555 (!JSValueIsObject(context, arguments[0])) ||
556 (argumentCount >= 2 && (!validator.isNullOrUndefined(arguments[1]) && (!validator.isCallback(arguments[1])))) ||
557 (argumentCount >= 3 && (!validator.isNullOrUndefined(arguments[2]) && (!validator.isCallback(arguments[2])))) )
559 LogDebug("Argument type mismatch");
560 Throw(InvalidArgumentException);
562 JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
563 MediacontentMediaPtr event = JSMedia::getMediaObject(arg);
565 IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
566 dplEvent->setMediaItem(event);
567 dplEvent->setForSynchronousCall();
568 mediacontent->updateMedia(dplEvent);
569 if (dplEvent->getResult())
571 return JSValueMakeNull(context);
575 ThrowMsg(UnknownException, "Updating failed by unkown reason.");
578 Catch(InvalidArgumentException)
580 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
582 Catch(ConversionException)
584 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
588 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
591 return JSValueMakeNull(context);
598 IMediacontentPtr JSMediacontent::getMediacontentObject(
600 const JSObjectRef object,
601 JSValueRef* exception)
603 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
606 return priv->getObject();
608 ThrowMsg(NullPointerException, "Private object is NULL.");