Update change log and spec for wrt-plugins-tizen_0.4.29
[platform/framework/web/wrt-plugins-tizen.git] / src / Content / JSContentManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <cassert>
19 #include <memory>
20 #include <CommonsJavaScript/Utils.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/JSPendingOperation.h>
26 #include <CommonsJavaScript/PrivateObject.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
28 #include <SecurityExceptions.h>
29 #include <CallbackUserData.h>
30 #include <MultiCallbackUserData.h>
31 #include <GlobalContextManager.h>
32 #include <FilterConverter.h>
33 #include <JSWebAPIErrorFactory.h>
34 #include <ArgumentValidator.h>
35 #include <TimeTracer.h>
36
37 #include "JSUtil.h"
38 #include "ContentFactory.h"
39 #include "ContentController.h"
40 #include "JSContentManager.h"
41 #include "JSContent.h"
42 #include "JSImage.h"
43 #include "JSVideo.h"
44 #include "JSAudio.h"
45 #include "ContentConverter.h"
46 #include "plugin_config.h"
47 #include "ContentAsyncCallbackManager.h"
48 #include "ContentListener.h"
49 #include "ContentVideo.h"
50 #include "ContentImage.h"
51 #include "ContentFilterConverter.h"
52 #include "ContentUtility.h"
53 #include <Logger.h>
54
55 using namespace DeviceAPI::Common;
56 using namespace DeviceAPI::Tizen;
57 using namespace WrtDeviceApis::Commons;
58 using namespace WrtDeviceApis::CommonsJavaScript;
59
60 #define TIZEN_CONTENT_MANAGER_ATTRIBUTENAME                 "content"
61
62 namespace DeviceAPI {
63 namespace Content {
64
65 JSStaticValue JSMediacontentManager::m_property[] =
66 {
67     { 0, 0, 0, 0 }
68 };
69
70 JSStaticFunction JSMediacontentManager::m_function[] = 
71 {
72     { CONTENT_FUNCTION_API_FIND_ITEMS, findItems, kJSPropertyAttributeNone },
73     { CONTENT_FUNCTION_API_GET_FOLDERS, getFolders, kJSPropertyAttributeNone },
74     { CONTENT_FUNCTION_API_UPDATE_ITEM, updateItem, kJSPropertyAttributeNone },
75     { CONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH, updateItemsBatch, kJSPropertyAttributeNone },
76     { CONTENT_FUNCTION_API_SCAN_FILE, scanFile, kJSPropertyAttributeNone },
77     { CONTENT_FUNCTION_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
78     { CONTENT_FUNCTION_API_UNSET_CHANGE_LISTENER, unsetChangeListener,kJSPropertyAttributeNone},
79     { 0, 0, 0 }
80 };
81
82
83 JSClassRef JSMediacontentManager::m_jsClassRef = NULL;
84
85 JSClassDefinition JSMediacontentManager::m_classInfo =
86 {
87         0,
88         kJSClassAttributeNone,
89         TIZEN_CONTENT_MANAGER_ATTRIBUTENAME,
90         0,
91         m_property,
92         m_function,
93         initialize,
94         finalize,
95         NULL, //hasProperty,
96         NULL, //getProperty,
97         NULL, //setProperty,
98         NULL, //deleteProperty,
99         NULL, //getPropertyNames,
100         NULL, //callAsFunction,
101         NULL, //callAsConstructor,
102         NULL,
103         NULL, //convertToType
104 };
105
106 JSValueRef JSMediacontentManager::getFolders(
107                             JSContextRef context,
108                             JSObjectRef object,
109                             JSObjectRef thisObject,
110                             size_t argumentCount,
111                             const JSValueRef arguments[],
112                             JSValueRef* exception )
113 {
114     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
115
116     MediacontentManagerPrivObject *privateObject;
117     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
118     if (!privateObject) {
119         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
120         return JSWebAPIErrorFactory::postException(context, exception, err);
121     }
122
123     JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
124
125     JSCallbackManagerPtr cbm(NULL);
126     cbm = JSCallbackManager::createObject(globalContext);
127     
128     try{
129         ArgumentValidator argValidator(context, argumentCount, arguments);
130         JSObjectRef successCallbackObj = argValidator.toFunction(0);
131         if(successCallbackObj){
132             cbm->setOnSuccess(successCallbackObj);
133         }
134         else{
135             throw TypeMismatchException("SuccessCallback type mismatched.");
136         }
137
138         JSObjectRef errorCallbackObj = argValidator.toFunction(1,true);
139         if(errorCallbackObj){
140             cbm->setOnError(errorCallbackObj);
141         }
142     }catch(const BasePlatformException &err){
143         return JSWebAPIErrorFactory::postException(context,exception,err);
144     }
145     catch(...){
146         DeviceAPI::Common::UnknownException err("Unknown Error in ContentManager.getDirectories().");
147         return JSWebAPIErrorFactory::postException(context, exception, err);
148     }
149
150     Try
151     {
152         IMediacontentManagerPtr contentMgr = privateObject->getObject();
153
154         IEventFindFolderPtr dplEvent(new IEventFindFolder());
155         dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
156         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
157
158         contentMgr->findFolder(dplEvent);
159
160         MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
161     }
162     Catch(WrtDeviceApis::Commons::UnsupportedException)
163     {
164         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
165         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
166     }
167     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
168     {
169         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
170         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
171     }
172     Catch(WrtDeviceApis::Commons::ConversionException)
173     {
174         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
175         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
176     }
177     Catch(WrtDeviceApis::Commons::NotFoundException)
178     {
179         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
180         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
181     }
182     Catch(WrtDeviceApis::Commons::Exception)
183     {
184         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
185         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
186     }
187
188         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
189     return JSValueMakeUndefined(context);
190
191 }
192
193
194
195 JSValueRef JSMediacontentManager::findItems(
196                             JSContextRef context,
197                             JSObjectRef object,
198                             JSObjectRef thisObject,
199                             size_t argumentCount,
200                             const JSValueRef arguments[],
201                             JSValueRef* exception )
202 {
203
204     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
205
206     MediacontentManagerPrivObject *privateObject;
207     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
208     if(!privateObject) {
209         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
210         return JSWebAPIErrorFactory::postException(context, exception, err);
211     }
212
213     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_FIND_ITEMS);
214
215     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
216
217     JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
218
219     JSCallbackManagerPtr cbm(NULL);
220     MediaContentFilterConverterFactory::ConverterType fConverter = MediaContentFilterConverterFactory::getConverter(context);
221     MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
222
223     cbm = JSCallbackManager::createObject(globalContext);
224
225     string folderId;
226     Tizen::FilterPtr filter;
227     Tizen::SortModePtr sortPtr;
228     JSObjectRef filterObj;
229     JSObjectRef sortModeObj;
230     IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
231     try{
232         ArgumentValidator argValidator(context, argumentCount, arguments);
233         JSObjectRef successCallbackObj = argValidator.toFunction(0);
234         if(successCallbackObj){
235             cbm->setOnSuccess(successCallbackObj);
236         }
237         else{
238             throw TypeMismatchException("SuccessCallback type mismatched.");
239         }
240
241         JSObjectRef errorCallbackObj = argValidator.toFunction(1,true);
242         if(errorCallbackObj){
243             cbm->setOnError(errorCallbackObj);
244         }
245
246         folderId = argValidator.toString(2, true);
247         if(!folderId.empty()){
248             if(folderId != "null" ){
249                 dplEvent->setFolderID(folderId);
250             }
251             else if(folderId == "undefined"){
252                 throw InvalidValuesException("folderId is not valid.");
253             }
254         }
255
256         //filter
257         filterObj = argValidator.toObject(3,true);
258
259         //sortMode
260         sortModeObj= argValidator.toObject(4,true);
261
262         // count
263         if(argumentCount >= 6){
264             if(!JSValueIsNull(context, arguments[5])){
265                 long count = argValidator.toLong(5, true, 0);
266                 if( count >= 0L ){
267                     dplEvent->setLimit(count);
268                 }
269                 else{
270                     throw InvalidValuesException( "count should be positive.");
271                 }
272             }
273         }
274         if(argumentCount >= 7){
275             // offset
276             long offset = argValidator.toLong(6, true);
277             if( offset >= 0L ){
278                 dplEvent->setOffset(offset);
279             }
280             else{
281                 throw InvalidValuesException("offset should be positive.");
282             }
283         }
284     }catch(const BasePlatformException &err){
285         return JSWebAPIErrorFactory::postException(context,exception,err);
286     }
287     catch(...){
288         DeviceAPI::Common::UnknownException err("Unknown Error in find().");
289         return JSWebAPIErrorFactory::postException(context, exception, err);
290     }
291
292     Try
293     {
294         if(filterObj){
295             dplEvent->setFilter(fConverter->toFilter(filterObj));
296         }
297         if(sortModeObj){
298             dplEvent->setSortMode(fConverter->toSortMode(sortModeObj));
299         }
300         IMediacontentManagerPtr contentMgr = privateObject->getObject();
301
302         dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
303         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
304         contentMgr->browseFolder(dplEvent);
305         MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
306     }
307     Catch(WrtDeviceApis::Commons::UnsupportedException)
308     {
309         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
310         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
311     }
312     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
313     {
314         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
315         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
316     }
317     Catch(WrtDeviceApis::Commons::ConversionException)
318     {
319         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
320         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
321     }
322     Catch(WrtDeviceApis::Commons::NotFoundException)
323     {
324         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
325         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
326     }
327     Catch(WrtDeviceApis::Commons::Exception)
328     {
329         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
330         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
331     }
332
333         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
334     return JSValueMakeUndefined(context);
335 }
336
337
338
339 JSValueRef JSMediacontentManager::updateItemsBatch(JSContextRef context,
340         JSObjectRef object,
341         JSObjectRef thisObject,
342         size_t argumentCount,
343         const JSValueRef arguments[],
344         JSValueRef* exception)
345 {
346     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
347     MediacontentManagerPrivObject *privateObject;
348     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
349     if(!privateObject) {
350         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
351         return JSWebAPIErrorFactory::postException(context, exception, err);
352     }
353
354     JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
355     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH);
356     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
357
358     JSCallbackManagerPtr cbm(NULL);
359     MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
360
361     cbm = JSCallbackManager::createObject(globalContext);
362
363     MediacontentMediaListPtr contents;
364     try{
365         ArgumentValidator argValidator(context, argumentCount, arguments);
366
367         if((argumentCount >= 1) && (JSIsArrayValue(context, arguments[0]))){
368             contents = converter->toVectorOfMediaItem(arguments[0]);
369             if(!contents)
370             {
371                 throw TypeMismatchException( "content type mismatched.");
372             }
373         }
374         else{
375             throw TypeMismatchException("content type mismatched.");
376         }
377         
378         JSObjectRef successCallbackObj = argValidator.toFunction(1,true);        
379         if(successCallbackObj){
380             cbm->setOnSuccess(successCallbackObj);
381         }
382         else{
383             throw TypeMismatchException("SuccessCallback type mismatched.");
384         }
385
386         JSObjectRef errorCallbackObj = argValidator.toFunction(2,true);
387         if(errorCallbackObj){
388             cbm->setOnError(errorCallbackObj);
389         }
390     }catch(const BasePlatformException &err){
391         return JSWebAPIErrorFactory::postException(context,exception,err);
392     }
393     catch(...){
394         DeviceAPI::Common::UnknownException err("Unknown Error in updateBatch().");
395         return JSWebAPIErrorFactory::postException(context, exception, err);
396     }
397
398     Try
399     {
400         IMediacontentManagerPtr contentMgr = privateObject->getObject();
401
402         IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
403         dplEvent->setMediaItems(contents);
404         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
405
406         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
407         contentMgr->updateMediaItems(dplEvent);
408
409         MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
410     }
411     Catch(WrtDeviceApis::Commons::UnsupportedException)
412     {
413         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
414         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
415     }
416     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
417     {
418         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
419         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
420     }
421     Catch(WrtDeviceApis::Commons::ConversionException)
422     {
423         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
424         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
425     }
426     Catch(WrtDeviceApis::Commons::NotFoundException)
427     {
428         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
429         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
430     }
431     Catch(WrtDeviceApis::Commons::Exception)
432     {
433         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
434         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
435     }
436
437         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
438     return JSValueMakeUndefined(context);
439 }
440
441
442
443 JSValueRef JSMediacontentManager::updateItem(
444                         JSContextRef context,
445                         JSObjectRef object,
446                         JSObjectRef thisObject,
447                         size_t argumentCount,
448                         const JSValueRef arguments[],
449                         JSValueRef* exception)
450 {
451
452     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
453
454     MediacontentManagerPrivObject *privateObject;
455     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
456     if (!privateObject) {
457         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
458         return JSWebAPIErrorFactory::postException(context, exception, err);
459     }
460
461     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_UPDATE_ITEM);
462     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
463
464     Try
465     {
466         IMediacontentManagerPtr contentMgr = privateObject->getObject();
467
468         //parameter : MediaItem item
469
470         JSObjectRef arg;
471         if(argumentCount >= 1)
472         {
473             if (!JSValueIsObjectOfClass(context, arguments[0], JSMedia::getClassRef()) &&
474                 !JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef()) &&
475                 !JSValueIsObjectOfClass(context, arguments[0], JSAudio::getClassRef()) &&           
476                 !JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())) {
477                     ThrowMsg(ConversionException, "Content type mismatched.");
478             }
479             arg = JSValueToObject(context, arguments[0], exception);
480         }
481         else
482         {
483              ThrowMsg(ConversionException, "Content type mismatched.");
484         }
485
486         MediacontentMediaPtr event;
487         IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
488
489         if(JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef())){
490             JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
491             JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
492             double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
493             double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
494
495             MediacontentImagePtr imgPtr = JSImage::getImageObject(arg);
496             imgPtr->setImageLatitude(latitude);
497             imgPtr->setImageLongitude(longitude);
498         }
499         else if(JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())){
500             JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
501             JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
502
503             double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
504
505             double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
506
507             MediacontentVideoPtr vedioPtr = JSVideo::getVideoObject(arg);
508             vedioPtr->setVideoLatitude(latitude);
509             vedioPtr->setVideoLongitude(longitude);
510         }
511
512         event = JSMedia::getMediaObject(arg);
513         dplEvent->setMediaItem(event);
514
515         dplEvent->setForSynchronousCall();
516         contentMgr->updateMedia(dplEvent);
517
518         if (!(dplEvent->getResult())) {
519             ThrowMsg(WrtDeviceApis::Commons::Exception, "updating failed by unknown reason.");
520         }
521     }
522     Catch(WrtDeviceApis::Commons::UnsupportedException)
523     {
524     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
525     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
526     }
527     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
528     {
529     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
530     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
531     }
532     Catch(WrtDeviceApis::Commons::ConversionException)
533     {
534     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
535     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
536     }
537     Catch(WrtDeviceApis::Commons::NotFoundException)
538     {
539     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
540     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
541     }
542     Catch(WrtDeviceApis::Commons::Exception)
543     {
544     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
545     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
546     }
547
548         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
549     return JSValueMakeUndefined(context);
550
551 }
552
553 static void _scanCompletedCallback(std::string err_msg, std::string path, void *user_data){
554     CallbackUserData *cb = static_cast<CallbackUserData*>(user_data);
555     if(cb != NULL){
556             if(err_msg.empty() ){
557                         Converter converter(cb->getContext());
558                         JSValueRef jsPath = converter.toJSValueRef(ContentUtility::convertPathToUri(path));
559                         cb->callSuccessCallback(jsPath);
560             }
561             else{
562                         DeviceAPI::Common::UnknownException err(err_msg.c_str());
563                         JSObjectRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cb->getContext(),err);
564                         cb->callErrorCallback(errorObject);
565             }
566             delete cb;
567     }
568 }
569
570 JSValueRef JSMediacontentManager::scanFile(
571                         JSContextRef context,
572                         JSObjectRef object,
573                         JSObjectRef thisObject,
574                         size_t argumentCount,
575                         const JSValueRef arguments[],
576                         JSValueRef* exception)
577 {
578     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
579
580     MediacontentManagerPrivObject *privateObject;
581     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
582     if(!privateObject) {
583         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
584         return JSWebAPIErrorFactory::postException(context, exception, err);
585     }
586
587     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SCAN_FILE);
588     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
589
590     string path;
591     CallbackUserData *callback = NULL;
592     try{
593         callback = new CallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));    
594         ArgumentValidator argValidator(context, argumentCount, arguments);    
595         path = argValidator.toString(0);
596         if(path.empty()){
597             throw TypeMismatchException("File path type mismatched.");
598         }
599         else if(path == "null" || path == "undefined"){
600             throw InvalidValuesException("File path is not valid.");
601         }
602         path = ContentUtility::convertUriToPath(path);
603
604         JSObjectRef successCallbackObj = argValidator.toFunction(1,true);        
605         if(successCallbackObj){
606             callback->setSuccessCallback(successCallbackObj);
607         }
608
609         JSObjectRef errorCallbackObj = argValidator.toFunction(2,true);
610         if(errorCallbackObj){
611             callback->setErrorCallback(errorCallbackObj);
612         }
613     }catch(const BasePlatformException &err){
614         return JSWebAPIErrorFactory::postException(context,exception,err);
615     }
616     catch(...){
617         DeviceAPI::Common::UnknownException err("Unknown Error in scanFile().");
618         return JSWebAPIErrorFactory::postException(context, exception, err);
619     }
620
621     Try
622     {
623         IMediacontentManagerPtr contentMgr = privateObject->getObject();
624         contentMgr->scanFile(_scanCompletedCallback,path,(void*)callback);
625     }
626     Catch(WrtDeviceApis::Commons::UnsupportedException)
627     {
628         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
629         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
630     }
631     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
632     {
633         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
634         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
635     }
636     Catch(WrtDeviceApis::Commons::ConversionException)
637     {
638         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
639         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
640     }
641     Catch(WrtDeviceApis::Commons::NotFoundException)
642     {
643         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
644         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
645     }
646     Catch(WrtDeviceApis::Commons::Exception)
647     {
648         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
649         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
650     }
651
652         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
653     return JSValueMakeUndefined(context);
654 }
655
656
657 JSValueRef JSMediacontentManager::setChangeListener(
658                         JSContextRef context,
659                         JSObjectRef object,
660                         JSObjectRef thisObject,
661                         size_t argumentCount,
662                         const JSValueRef arguments[],
663                         JSValueRef* exception)
664 {
665     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
666
667     MediacontentManagerPrivObject *privateObject;
668     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
669     if(!privateObject) {
670         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
671         return JSWebAPIErrorFactory::postException(context, exception, err);
672     }
673
674     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
675     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
676
677     try{
678         ArgumentValidator argValidator(context, argumentCount, arguments);
679         JSContextRef globalCtx = GlobalContextManager::getInstance()->getGlobalContext(context);        
680         JSObjectRef callbackObj = argValidator.toCallbackObject(0,false,"oncontentadded","oncontentupdated","oncontentremoved",NULL);
681         ContentListener *listener = new ContentListener(globalCtx, callbackObj);
682         IMediacontentManagerPtr contentMgr = privateObject->getObject();
683         if(!(contentMgr->setListener(listener)))
684         {
685             throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
686         }
687     }catch(const BasePlatformException &err){
688         return JSWebAPIErrorFactory::postException(context,exception,err);
689     }
690     catch(...){
691         DeviceAPI::Common::UnknownException err("Unknown Error in setChangeListener().");
692         return JSWebAPIErrorFactory::postException(context, exception, err);
693     }
694
695         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
696     return JSValueMakeUndefined(context);
697 }
698
699 JSValueRef JSMediacontentManager::unsetChangeListener(
700                         JSContextRef context,
701                         JSObjectRef object,
702                         JSObjectRef thisObject,
703                         size_t argumentCount,
704                         const JSValueRef arguments[],
705                         JSValueRef* exception)
706 {
707     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
708     MediacontentManagerPrivObject *privateObject;
709     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
710     if(!privateObject) {
711         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
712         return JSWebAPIErrorFactory::postException(context, exception, err);
713     }
714
715     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
716     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
717
718     try{
719         // perform
720         IMediacontentManagerPtr contentMgr = privateObject->getObject();
721         if(!(contentMgr->unsetListener()))
722         {
723             throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
724         }
725     }catch(const BasePlatformException &err){
726         return JSWebAPIErrorFactory::postException(context,exception,err);
727     }
728     catch(...){
729         DeviceAPI::Common::UnknownException err("Unknown Error in unsetChangeListener().");
730         return JSWebAPIErrorFactory::postException(context, exception, err);
731     }
732
733         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
734     return JSValueMakeUndefined(context);
735 }
736
737 const JSClassRef JSMediacontentManager::getClassRef() {
738     if (!m_jsClassRef) {
739         m_jsClassRef = JSClassCreate(&m_classInfo);
740     }
741     return m_jsClassRef;
742 }
743
744
745 const JSClassDefinition* JSMediacontentManager::getClassInfo()
746 {
747     return &m_classInfo;
748 }
749
750 void JSMediacontentManager::initialize(JSContextRef context, JSObjectRef object)
751 {
752     MediacontentManagerPrivObject *privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
753     if (NULL == privateObject)
754     {
755         IMediacontentManagerPtr contentManager = MediacontentFactory::getInstance().createMediacontentManagerObject();
756         privateObject = new MediacontentManagerPrivObject(context, contentManager);
757         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject)))
758         {
759             delete privateObject;
760         }
761     }
762
763 }
764
765 void JSMediacontentManager::finalize(JSObjectRef object)
766 {
767     MediacontentManagerPrivObject* priv = static_cast<MediacontentManagerPrivObject*> (JSObjectGetPrivate(object));
768     if(priv != NULL)
769     {
770         delete priv;
771         JSObjectSetPrivate(object, NULL);
772         priv = NULL;
773     }
774
775 }
776
777 IMediacontentManagerPtr JSMediacontentManager::getContentManagerPrivObject(
778                         JSContextRef ctx,
779                         const JSObjectRef object,
780                         JSValueRef* exception)
781 {
782     MediacontentManagerPrivObject *priv = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
783     if (priv)
784     {
785         return priv->getObject();
786     }
787     ThrowMsg(ConversionException, "Private object is NULL.");
788 }
789
790
791 }
792 }
793
794