Update change log and spec for wrt-plugins-tizen_0.4.55
[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
383         JSObjectRef errorCallbackObj = argValidator.toFunction(2,true);
384         if(errorCallbackObj){
385             cbm->setOnError(errorCallbackObj);
386         }
387     }catch(const BasePlatformException &err){
388         return JSWebAPIErrorFactory::postException(context,exception,err);
389     }
390     catch(...){
391         DeviceAPI::Common::UnknownException err("Unknown Error in updateBatch().");
392         return JSWebAPIErrorFactory::postException(context, exception, err);
393     }
394
395     Try
396     {
397         IMediacontentManagerPtr contentMgr = privateObject->getObject();
398
399         IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
400         dplEvent->setMediaItems(contents);
401         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
402
403         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
404         contentMgr->updateMediaItems(dplEvent);
405
406         MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
407     }
408     Catch(WrtDeviceApis::Commons::UnsupportedException)
409     {
410         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
411         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
412     }
413     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
414     {
415         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
416         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
417     }
418     Catch(WrtDeviceApis::Commons::ConversionException)
419     {
420         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
421         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
422     }
423     Catch(WrtDeviceApis::Commons::NotFoundException)
424     {
425         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
426         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
427     }
428     Catch(WrtDeviceApis::Commons::Exception)
429     {
430         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
431         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
432     }
433
434         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
435     return JSValueMakeUndefined(context);
436 }
437
438
439
440 JSValueRef JSMediacontentManager::updateItem(
441                         JSContextRef context,
442                         JSObjectRef object,
443                         JSObjectRef thisObject,
444                         size_t argumentCount,
445                         const JSValueRef arguments[],
446                         JSValueRef* exception)
447 {
448
449     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
450
451     MediacontentManagerPrivObject *privateObject;
452     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
453     if (!privateObject) {
454         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
455         return JSWebAPIErrorFactory::postException(context, exception, err);
456     }
457
458     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_UPDATE_ITEM);
459     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
460
461     Try
462     {
463         IMediacontentManagerPtr contentMgr = privateObject->getObject();
464
465         //parameter : MediaItem item
466
467         JSObjectRef arg;
468         if(argumentCount >= 1)
469         {
470             if (!JSValueIsObjectOfClass(context, arguments[0], JSMedia::getClassRef()) &&
471                 !JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef()) &&
472                 !JSValueIsObjectOfClass(context, arguments[0], JSAudio::getClassRef()) &&           
473                 !JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())) {
474                     ThrowMsg(ConversionException, "Content type mismatched.");
475             }
476             arg = JSValueToObject(context, arguments[0], exception);
477         }
478         else
479         {
480              ThrowMsg(ConversionException, "Content type mismatched.");
481         }
482
483         MediacontentMediaPtr event;
484         IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
485
486         if(JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef())){
487             MediacontentImagePtr imgPtr = JSImage::getImageObject(arg);
488             JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
489             if(!(JSValueIsNull(context, geoValRef) || JSValueIsUndefined(context, geoValRef))){
490                 JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
491                 double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
492                 double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
493                 if(ContentUtility::checkLocation(latitude, latitude)){
494                     imgPtr->setImageLatitude(latitude);
495                     imgPtr->setImageLongitude(longitude);
496                 }
497                 else{
498                     ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
499                 }
500             }
501             else
502             {
503                 imgPtr->setImageLatitude(DEFAULT_GEOLOCATION);
504                 imgPtr->setImageLongitude(DEFAULT_GEOLOCATION);
505             }
506         }
507         else if(JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())){
508             MediacontentVideoPtr vedioPtr = JSVideo::getVideoObject(arg);
509             JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
510             if(!(JSValueIsNull(context, geoValRef) || JSValueIsUndefined(context, geoValRef))){
511                 JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
512                 double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
513                 double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
514                 if(ContentUtility::checkLocation(latitude, latitude)){
515                     vedioPtr->setVideoLatitude(latitude);
516                     vedioPtr->setVideoLongitude(longitude);
517                 }
518                 else{
519                     ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
520                 }
521             }
522             else
523             {
524                 vedioPtr->setVideoLatitude(DEFAULT_GEOLOCATION);
525                 vedioPtr->setVideoLongitude(DEFAULT_GEOLOCATION);
526             }
527         }
528
529         event = JSMedia::getMediaObject(arg);
530         dplEvent->setMediaItem(event);
531
532         dplEvent->setForSynchronousCall();
533         contentMgr->updateMedia(dplEvent);
534
535         if (!(dplEvent->getResult())) {
536             ThrowMsg(WrtDeviceApis::Commons::Exception, "updating failed by unknown reason.");
537         }
538     }
539     Catch(WrtDeviceApis::Commons::UnsupportedException)
540     {
541     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
542     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
543     }
544     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
545     {
546     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
547     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
548     }
549     Catch(WrtDeviceApis::Commons::ConversionException)
550     {
551     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
552     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
553     }
554     Catch(WrtDeviceApis::Commons::NotFoundException)
555     {
556     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
557     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
558     }
559     Catch(WrtDeviceApis::Commons::Exception)
560     {
561     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
562     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
563     }
564
565         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
566     return JSValueMakeUndefined(context);
567
568 }
569
570 static void _scanCompletedCallback(std::string err_msg, std::string path, void *user_data){
571     CallbackUserData *cb = static_cast<CallbackUserData*>(user_data);
572     if(cb != NULL){
573             if(err_msg.empty() ){
574                         Converter converter(cb->getContext());
575                         JSValueRef jsPath = converter.toJSValueRef(ContentUtility::convertPathToUri(path));
576                         cb->callSuccessCallback(jsPath);
577             }
578             else{
579                         DeviceAPI::Common::UnknownException err(err_msg.c_str());
580                         JSObjectRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cb->getContext(),err);
581                         cb->callErrorCallback(errorObject);
582             }
583             delete cb;
584     }
585 }
586
587 JSValueRef JSMediacontentManager::scanFile(
588                         JSContextRef context,
589                         JSObjectRef object,
590                         JSObjectRef thisObject,
591                         size_t argumentCount,
592                         const JSValueRef arguments[],
593                         JSValueRef* exception)
594 {
595     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
596
597     MediacontentManagerPrivObject *privateObject;
598     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
599     if(!privateObject) {
600         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
601         return JSWebAPIErrorFactory::postException(context, exception, err);
602     }
603
604     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SCAN_FILE);
605     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
606
607     string path;
608     CallbackUserData *callback = NULL;
609     try{
610         callback = new CallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));    
611         ArgumentValidator argValidator(context, argumentCount, arguments);    
612         path = argValidator.toString(0);
613         if(path.empty()){
614             throw TypeMismatchException("File path type mismatched.");
615         }
616         else if(path == "null" || path == "undefined"){
617             throw InvalidValuesException("File path is not valid.");
618         }
619         path = ContentUtility::convertUriToPath(path);
620
621         JSObjectRef successCallbackObj = argValidator.toFunction(1,true);        
622         if(successCallbackObj){
623             callback->setSuccessCallback(successCallbackObj);
624         }
625
626         JSObjectRef errorCallbackObj = argValidator.toFunction(2,true);
627         if(errorCallbackObj){
628             callback->setErrorCallback(errorCallbackObj);
629         }
630     }catch(const BasePlatformException &err){
631         return JSWebAPIErrorFactory::postException(context,exception,err);
632     }
633     catch(...){
634         DeviceAPI::Common::UnknownException err("Unknown Error in scanFile().");
635         return JSWebAPIErrorFactory::postException(context, exception, err);
636     }
637
638     Try
639     {
640         IMediacontentManagerPtr contentMgr = privateObject->getObject();
641         contentMgr->scanFile(_scanCompletedCallback,path,(void*)callback);
642     }
643     Catch(WrtDeviceApis::Commons::UnsupportedException)
644     {
645         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
646         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
647     }
648     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
649     {
650         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
651         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
652     }
653     Catch(WrtDeviceApis::Commons::ConversionException)
654     {
655         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
656         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
657     }
658     Catch(WrtDeviceApis::Commons::NotFoundException)
659     {
660         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
661         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
662     }
663     Catch(WrtDeviceApis::Commons::Exception)
664     {
665         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
666         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
667     }
668
669         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
670     return JSValueMakeUndefined(context);
671 }
672
673
674 JSValueRef JSMediacontentManager::setChangeListener(
675                         JSContextRef context,
676                         JSObjectRef object,
677                         JSObjectRef thisObject,
678                         size_t argumentCount,
679                         const JSValueRef arguments[],
680                         JSValueRef* exception)
681 {
682     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
683
684     MediacontentManagerPrivObject *privateObject;
685     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
686     if(!privateObject) {
687         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
688         return JSWebAPIErrorFactory::postException(context, exception, err);
689     }
690
691     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
692     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
693
694     try{
695         ArgumentValidator argValidator(context, argumentCount, arguments);
696         JSContextRef globalCtx = GlobalContextManager::getInstance()->getGlobalContext(context);        
697         JSObjectRef callbackObj = argValidator.toCallbackObject(0,false,"oncontentadded","oncontentupdated","oncontentremoved",NULL);
698         ContentListener *listener = new ContentListener(globalCtx, callbackObj);
699         IMediacontentManagerPtr contentMgr = privateObject->getObject();
700         if(!(contentMgr->setListener(listener)))
701         {
702             throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
703         }
704     }catch(const BasePlatformException &err){
705         return JSWebAPIErrorFactory::postException(context,exception,err);
706     }
707     catch(...){
708         DeviceAPI::Common::UnknownException err("Unknown Error in setChangeListener().");
709         return JSWebAPIErrorFactory::postException(context, exception, err);
710     }
711
712         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
713     return JSValueMakeUndefined(context);
714 }
715
716 JSValueRef JSMediacontentManager::unsetChangeListener(
717                         JSContextRef context,
718                         JSObjectRef object,
719                         JSObjectRef thisObject,
720                         size_t argumentCount,
721                         const JSValueRef arguments[],
722                         JSValueRef* exception)
723 {
724     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
725     MediacontentManagerPrivObject *privateObject;
726     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
727     if(!privateObject) {
728         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
729         return JSWebAPIErrorFactory::postException(context, exception, err);
730     }
731
732     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
733     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
734
735     try{
736         // perform
737         IMediacontentManagerPtr contentMgr = privateObject->getObject();
738         if(!(contentMgr->unsetListener()))
739         {
740             throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
741         }
742     }catch(const BasePlatformException &err){
743         return JSWebAPIErrorFactory::postException(context,exception,err);
744     }
745     catch(...){
746         DeviceAPI::Common::UnknownException err("Unknown Error in unsetChangeListener().");
747         return JSWebAPIErrorFactory::postException(context, exception, err);
748     }
749
750         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
751     return JSValueMakeUndefined(context);
752 }
753
754 const JSClassRef JSMediacontentManager::getClassRef() {
755     if (!m_jsClassRef) {
756         m_jsClassRef = JSClassCreate(&m_classInfo);
757     }
758     return m_jsClassRef;
759 }
760
761
762 const JSClassDefinition* JSMediacontentManager::getClassInfo()
763 {
764     return &m_classInfo;
765 }
766
767 void JSMediacontentManager::initialize(JSContextRef context, JSObjectRef object)
768 {
769     MediacontentManagerPrivObject *privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
770     if (NULL == privateObject)
771     {
772         IMediacontentManagerPtr contentManager = MediacontentFactory::getInstance().createMediacontentManagerObject();
773         privateObject = new MediacontentManagerPrivObject(context, contentManager);
774         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject)))
775         {
776             delete privateObject;
777         }
778     }
779
780 }
781
782 void JSMediacontentManager::finalize(JSObjectRef object)
783 {
784     MediacontentManagerPrivObject* priv = static_cast<MediacontentManagerPrivObject*> (JSObjectGetPrivate(object));
785     if(priv != NULL)
786     {
787         delete priv;
788         JSObjectSetPrivate(object, NULL);
789         priv = NULL;
790     }
791
792 }
793
794 IMediacontentManagerPtr JSMediacontentManager::getContentManagerPrivObject(
795                         JSContextRef ctx,
796                         const JSObjectRef object,
797                         JSValueRef* exception)
798 {
799     MediacontentManagerPrivObject *priv = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
800     if (priv)
801     {
802         return priv->getObject();
803     }
804     ThrowMsg(ConversionException, "Private object is NULL.");
805 }
806
807
808 }
809 }
810
811