merge wrt-plugins-tizen_0.2.0-2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSMediacontent.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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. 
15 */
16
17
18
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
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include <Tizen/Common/SecurityExceptions.h>
29 #include <Tizen/Tizen/FilterConverter.h>
30
31 #include <API/Mediacontent/MediacontentFactory.h>
32 #include "MediacontentController.h"
33 #include "JSMediacontent.h"
34 #include "JSMediacontentManager.h"
35 #include "JSMedia.h"
36 #include "MediaConverter.h"
37 #include "plugin_config.h"
38
39
40
41
42 using namespace TizenApis::Commons;
43 using namespace TizenApis::Api::Tizen;
44 using namespace TizenApis::Tizen1_0::Tizen;
45
46
47 #define TIZEN_MEDIACONTENT_ATTRIBUTENAME        "MediaSource"
48
49 namespace {
50     /**
51      * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
52      */
53     JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg) 
54     {
55       if (Validator(ctx).isCallback(arg)) 
56       {
57         return arg;
58       }
59       else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) 
60       {
61         ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
62       }
63       return NULL;
64     }
65 }
66
67 namespace TizenApis {
68 namespace Tizen1_0 { 
69 namespace Mediacontent {
70
71
72 JSClassDefinition JSMediacontent::m_classInfo =
73 {
74     0,
75     kJSClassAttributeNone,
76     TIZEN_MEDIACONTENT_ATTRIBUTENAME,
77     NULL,
78     m_property,
79     m_function,
80     initialize,
81     finalize,
82     NULL,//HasProperty,
83     NULL,//GetProperty,
84     NULL,//SetProperty,
85     NULL,//DeleteProperty,
86     NULL,//GetPropertyNames,
87     NULL,//CallAsFunction,
88     NULL,//CallAsConstructor,
89     NULL,//HasInstance,
90     NULL//ConvertToType
91 };
92
93 JSStaticValue JSMediacontent::m_property[] =
94 {
95     {0, 0, 0, 0}
96 };
97
98 JSStaticFunction JSMediacontent::m_function[] =
99 {
100     { "findItems", findMedia, kJSPropertyAttributeNone },
101     { "getFolders", getFolders, kJSPropertyAttributeNone },
102     { "browseFolder", browseFolder, kJSPropertyAttributeNone },    
103     { "updateItem", updateItem, kJSPropertyAttributeNone },        
104     { "updateItemsBatch", updateItemsBatch, kJSPropertyAttributeNone },            
105     { 0, 0, 0 }
106 };
107
108 JSClassRef JSMediacontent::m_jsClassRef = JSClassCreate(JSMediacontent::getClassInfo());
109
110 void JSMediacontent::initialize(JSContextRef context, JSObjectRef object)
111 {
112     LogDebug("JSMediacontent::initialize entered");
113     MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
114     if (!priv) 
115         {
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))) 
121                 {
122             delete priv;
123         }
124     } 
125         else
126     {
127         LogDebug("private object alrerady exists");
128     }
129
130 }
131
132 void JSMediacontent::finalize(JSObjectRef object)
133 {
134     LogDebug("entered");
135     MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( object ) ) ;
136     delete priv;
137 }
138
139 const JSClassRef JSMediacontent::getClassRef()
140 {
141     if (!m_jsClassRef) 
142         {
143         m_jsClassRef = JSClassCreate(&m_classInfo);
144     }
145     return m_jsClassRef;
146 }
147
148 const JSClassDefinition* JSMediacontent::getClassInfo()
149 {
150     return &m_classInfo;
151 }
152
153
154 JSValueRef JSMediacontent::findMedia(
155                                                         JSContextRef context,
156                                                         JSObjectRef object,
157                                                         JSObjectRef thisObject,
158                                                         size_t argumentCount,
159                                                         const JSValueRef arguments[],
160                                                         JSValueRef* exception )
161 {
162     LogDebug("JSMediacontent::findMedia entered");
163
164     MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
165     assert(privateObject);
166
167         AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_FIND_ITEMS);
168
169         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
170     
171         JSContextRef globalContext = privateObject->getContext();
172         
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);
178
179         IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
180
181     Try
182     {
183         if (argumentCount == 0 ||
184             (!validator.isCallback(arguments[0])) ||
185             (argumentCount >= 2 && (!validator.isCallback(arguments[1]))) )
186         {
187             LogDebug("Argument type mismatch");
188             Throw(InvalidArgumentException);
189         }
190         if (cbm) 
191         {
192             JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
193             onSuccessForCbm = arguments[0];
194             if (argumentCount >= 2) 
195             {
196                 onErrorForCbm = arguments[1];
197             }
198             cbm->setOnSuccess(onSuccessForCbm);
199             cbm->setOnError(onErrorForCbm);
200                 }
201
202         
203                 IEventFindMediaPtr dplEvent(new IEventFindMedia());
204                 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
205                 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
206                 
207         if (argumentCount >= 3 && !validator.isNullOrUndefined(arguments[2]))
208         {
209             dplEvent->setFilter(filterConverter->toFilter(arguments[2]));
210         }
211                 if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3])) 
212                 {
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);
217
218                 }
219                 if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4])) 
220                 {
221                         long limit = filterConverter->toLong(arguments[4]);
222                         dplEvent->setLimit(limit);
223                 }
224                 if (argumentCount >= 6 && !validator.isNullOrUndefined(arguments[5])) 
225                 {
226                         long offset = filterConverter->toLong(arguments[5]);
227                         dplEvent->setOffset(offset);
228                 }               
229
230                 mediacontent->findMedia(dplEvent);
231                 
232         }
233         Catch(InvalidArgumentException)
234         {
235                 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
236         }
237         Catch(ConversionException)
238         {
239                 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
240         }
241         Catch(Exception)
242         {
243                 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
244         }
245         
246         return JSValueMakeNull(context);
247
248 }
249
250
251 JSValueRef JSMediacontent::getFolders(
252                                                         JSContextRef context, 
253                                                         JSObjectRef object,
254                                                         JSObjectRef thisObject, 
255                                                         size_t argumentCount,
256                                                         const JSValueRef arguments[], 
257                                                         JSValueRef* exception )
258 {
259
260         LogDebug("JSMediacontent::findFolders entered");
261
262     MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
263     assert(privateObject);
264
265
266         AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_GET_FOLDERS);
267
268         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
269
270
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);
276
277         cbm = JSCallbackManager::createObject(globalContext);
278
279         IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
280
281     Try
282     {
283
284         if ((!validator.isCallback(arguments[0])) ||
285             (argumentCount >= 2 && (!validator.isCallback(arguments[1]))) ||
286             (argumentCount >= 3 && (!JSValueIsObject(context, arguments[2]) && !validator.isNullOrUndefined(arguments[2])))) 
287                 {
288             ThrowMsg(ConversionException, "Wrong parameter type.");
289         }
290
291         if (cbm) 
292         {
293             JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
294             onSuccessForCbm = arguments[0];
295             if (argumentCount >= 2) 
296             {
297                 onErrorForCbm = arguments[1];
298             }
299                         if (argumentCount >= 3) 
300             {
301                                 onErrorForCbm = arguments[2];
302             }
303             cbm->setOnSuccess(onSuccessForCbm);
304             cbm->setOnError(onErrorForCbm);
305                 }
306
307         IEventFindFolderPtr dplEvent(new IEventFindFolder());
308         dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
309         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
310
311
312         mediacontent->findFolder(dplEvent);
313     }
314     Catch(InvalidArgumentException)
315     {
316         return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
317     }
318     Catch(ConversionException)
319     {
320         return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
321     }
322     Catch(Exception)
323     {
324         return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
325     }
326
327     return JSValueMakeNull(context);
328
329 }
330
331
332 JSValueRef JSMediacontent::browseFolder(
333                                                         JSContextRef context, 
334                                                         JSObjectRef object,
335                                                         JSObjectRef thisObject, 
336                                                         size_t argumentCount,
337                                                         const JSValueRef arguments[], 
338                                                         JSValueRef* exception )
339 {
340
341         LogDebug("JSMediacontent::browseFolder entered");
342
343     MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
344     assert(privateObject);
345
346
347
348         AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_BROWSE_FOLDER);
349
350         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
351
352
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);
359
360         cbm = JSCallbackManager::createObject(globalContext);
361
362         IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
363
364     Try
365     {
366
367         if (argumentCount == 0 ||
368             (!validator.isCallback(arguments[1])) ||
369             (argumentCount >= 3 && (!validator.isCallback(arguments[2]))) ||
370             (argumentCount >= 4 && (!JSValueIsObject(context, arguments[3]) )) )
371         {
372             LogDebug("Argument type mismatch");
373             Throw(InvalidArgumentException);
374         }
375
376         if (cbm) 
377         {
378             JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
379             onSuccessForCbm = arguments[1];
380             if (argumentCount >= 3) 
381             {
382                 onErrorForCbm = arguments[2];
383             }
384             cbm->setOnSuccess(onSuccessForCbm);
385             cbm->setOnError(onErrorForCbm);
386                 }
387
388         IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
389         dplEvent->setFolderID(mediaConverter.toString(arguments[0]));
390         dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
391         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
392
393         if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3]))
394         {
395             dplEvent->setFilter(filterConverter->toFilter(arguments[3]));
396         }
397                 if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4])) 
398                 {
399             TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
400             sortModes->push_back(filterConverter->toSortMode(arguments[3]));
401             dplEvent->setSortModes(sortModes);
402
403                 }
404                 if (argumentCount >= 6 && !validator.isNullOrUndefined(arguments[5])) 
405                 {
406                         long limit = filterConverter->toLong(arguments[5]);
407                         dplEvent->setLimit(limit);
408                 }
409                 if (argumentCount >= 7 && !validator.isNullOrUndefined(arguments[6])) 
410                 {
411                         long offset = filterConverter->toLong(arguments[6]);
412                         dplEvent->setOffset(offset);
413                 }               
414
415         mediacontent->browseFolder(dplEvent);
416     }
417     Catch(InvalidArgumentException)
418     {
419         return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
420     }
421     Catch(ConversionException)
422     {
423         return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
424     }
425     Catch(Exception)
426     {
427         return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
428     }
429
430     return JSValueMakeNull(context);
431
432 }
433
434
435
436 JSValueRef JSMediacontent::updateItemsBatch(JSContextRef context,
437         JSObjectRef object,
438         JSObjectRef thisObject,
439         size_t argumentCount,
440         const JSValueRef arguments[],
441         JSValueRef* exception)
442 {
443     LogDebug("entered");
444
445     MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
446     assert(privateObject);
447
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);
451
452         Validator validator(context);
453     JSCallbackManagerPtr cbm(NULL);
454     FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
455         MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
456
457         cbm = JSCallbackManager::createObject(globalContext);
458
459         IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
460
461     Try
462     {
463    
464         if (argumentCount>4 || argumentCount<1) {
465             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
466         }
467
468         if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
469             ThrowMsg(ConversionException, "Wrong parameter type.");
470         }
471
472         MediacontentMediaListPtr events;
473         
474                 events = converter->toVectorOfEvents(arguments[0]);
475         if (!events) {
476             ThrowMsg(ConversionException, "Parameter conversion failed.");
477         }
478                 if(cbm) 
479         {
480             JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
481             if((argumentCount >= 2) && validator.isCallback(arguments[1]))
482             {
483                                 onSuccessForCbm = arguments[1];
484             }
485             
486             if((argumentCount >= 3) && validator.isCallback(arguments[2])) 
487             {
488                                 onErrorForCbm = arguments[2];
489             }
490             cbm->setOnSuccess(onSuccessForCbm);
491             cbm->setOnError(onErrorForCbm);
492                 }
493         IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
494         dplEvent->setMediaItems(events);
495         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
496         
497         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance()); 
498         mediacontent->updateMediaItems(dplEvent);
499     }
500     Catch(UnsupportedException)
501     {
502                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
503         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
504     }
505     Catch(InvalidArgumentException)
506     {
507                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
508         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
509     }
510     Catch(ConversionException)
511     {
512                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
513         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
514     }
515     Catch (NotFoundException)
516     {
517                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
518         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
519     }
520     Catch(Exception)
521     {
522                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
523         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
524     }
525
526     return JSValueMakeNull(context);
527 }
528
529
530
531 JSValueRef JSMediacontent::updateItem(
532                                                 JSContextRef context,
533                                                 JSObjectRef object,
534                                                 JSObjectRef thisObject,
535                                                 size_t argumentCount,
536                                                 const JSValueRef arguments[],
537                                                 JSValueRef* exception)
538 {
539                 LogDebug("updateItem::entered");
540
541         
542                 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
543                 assert(privateObject);
544                 
545                 AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(privateObject->getContext(), MEDIACONTENT_FUNCTION_API_UPDATE_ITEM);
546                 
547                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
548
549                 Validator validator(context);
550                 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
551
552                 Try
553                 {               
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])))) )
558                 {
559                     LogDebug("Argument type mismatch");
560                     Throw(InvalidArgumentException);
561                 }               
562                         JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
563                         MediacontentMediaPtr event = JSMedia::getMediaObject(arg);
564
565                         IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
566                         dplEvent->setMediaItem(event);
567                         dplEvent->setForSynchronousCall();
568                         mediacontent->updateMedia(dplEvent);
569                     if (dplEvent->getResult()) 
570                     {
571                     return JSValueMakeNull(context);
572             } 
573             else
574             {
575                 ThrowMsg(UnknownException, "Updating failed by unkown reason.");
576                 }
577                 }
578                 Catch(InvalidArgumentException)
579                 {
580                     return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
581                 }
582                 Catch(ConversionException)
583                 {
584                     return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
585                 }
586                 Catch(Exception)
587                 {
588                     return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
589                 }
590
591                 return JSValueMakeNull(context);                
592 }
593
594
595
596
597
598 IMediacontentPtr JSMediacontent::getMediacontentObject(
599                                                 JSContextRef ctx,
600                                         const JSObjectRef object,
601                                         JSValueRef* exception)
602 {
603     MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
604     if (priv) 
605         {
606         return priv->getObject();
607     }
608     ThrowMsg(NullPointerException, "Private object is NULL.");
609 }
610
611
612 }
613 }
614 }