Update change log and spec for wrt-plugins-tizen_0.4.47
[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             JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
488             JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
489             double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
490             double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
491
492             MediacontentImagePtr imgPtr = JSImage::getImageObject(arg);
493             imgPtr->setImageLatitude(latitude);
494             imgPtr->setImageLongitude(longitude);
495         }
496         else if(JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())){
497             JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
498             JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
499
500             double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
501
502             double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
503
504             MediacontentVideoPtr vedioPtr = JSVideo::getVideoObject(arg);
505             vedioPtr->setVideoLatitude(latitude);
506             vedioPtr->setVideoLongitude(longitude);
507         }
508
509         event = JSMedia::getMediaObject(arg);
510         dplEvent->setMediaItem(event);
511
512         dplEvent->setForSynchronousCall();
513         contentMgr->updateMedia(dplEvent);
514
515         if (!(dplEvent->getResult())) {
516             ThrowMsg(WrtDeviceApis::Commons::Exception, "updating failed by unknown reason.");
517         }
518     }
519     Catch(WrtDeviceApis::Commons::UnsupportedException)
520     {
521     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
522     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
523     }
524     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
525     {
526     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
527     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
528     }
529     Catch(WrtDeviceApis::Commons::ConversionException)
530     {
531     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
532     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
533     }
534     Catch(WrtDeviceApis::Commons::NotFoundException)
535     {
536     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
537     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
538     }
539     Catch(WrtDeviceApis::Commons::Exception)
540     {
541     LoggerW("Exception: "<<_rethrown_exception.GetMessage());
542     return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
543     }
544
545         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
546     return JSValueMakeUndefined(context);
547
548 }
549
550 static void _scanCompletedCallback(std::string err_msg, std::string path, void *user_data){
551     CallbackUserData *cb = static_cast<CallbackUserData*>(user_data);
552     if(cb != NULL){
553             if(err_msg.empty() ){
554                         Converter converter(cb->getContext());
555                         JSValueRef jsPath = converter.toJSValueRef(ContentUtility::convertPathToUri(path));
556                         cb->callSuccessCallback(jsPath);
557             }
558             else{
559                         DeviceAPI::Common::UnknownException err(err_msg.c_str());
560                         JSObjectRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cb->getContext(),err);
561                         cb->callErrorCallback(errorObject);
562             }
563             delete cb;
564     }
565 }
566
567 JSValueRef JSMediacontentManager::scanFile(
568                         JSContextRef context,
569                         JSObjectRef object,
570                         JSObjectRef thisObject,
571                         size_t argumentCount,
572                         const JSValueRef arguments[],
573                         JSValueRef* exception)
574 {
575     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
576
577     MediacontentManagerPrivObject *privateObject;
578     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
579     if(!privateObject) {
580         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
581         return JSWebAPIErrorFactory::postException(context, exception, err);
582     }
583
584     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SCAN_FILE);
585     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
586
587     string path;
588     CallbackUserData *callback = NULL;
589     try{
590         callback = new CallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));    
591         ArgumentValidator argValidator(context, argumentCount, arguments);    
592         path = argValidator.toString(0);
593         if(path.empty()){
594             throw TypeMismatchException("File path type mismatched.");
595         }
596         else if(path == "null" || path == "undefined"){
597             throw InvalidValuesException("File path is not valid.");
598         }
599         path = ContentUtility::convertUriToPath(path);
600
601         JSObjectRef successCallbackObj = argValidator.toFunction(1,true);        
602         if(successCallbackObj){
603             callback->setSuccessCallback(successCallbackObj);
604         }
605
606         JSObjectRef errorCallbackObj = argValidator.toFunction(2,true);
607         if(errorCallbackObj){
608             callback->setErrorCallback(errorCallbackObj);
609         }
610     }catch(const BasePlatformException &err){
611         return JSWebAPIErrorFactory::postException(context,exception,err);
612     }
613     catch(...){
614         DeviceAPI::Common::UnknownException err("Unknown Error in scanFile().");
615         return JSWebAPIErrorFactory::postException(context, exception, err);
616     }
617
618     Try
619     {
620         IMediacontentManagerPtr contentMgr = privateObject->getObject();
621         contentMgr->scanFile(_scanCompletedCallback,path,(void*)callback);
622     }
623     Catch(WrtDeviceApis::Commons::UnsupportedException)
624     {
625         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
626         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
627     }
628     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
629     {
630         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
631         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
632     }
633     Catch(WrtDeviceApis::Commons::ConversionException)
634     {
635         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
636         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
637     }
638     Catch(WrtDeviceApis::Commons::NotFoundException)
639     {
640         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
641         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
642     }
643     Catch(WrtDeviceApis::Commons::Exception)
644     {
645         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
646         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
647     }
648
649         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
650     return JSValueMakeUndefined(context);
651 }
652
653
654 JSValueRef JSMediacontentManager::setChangeListener(
655                         JSContextRef context,
656                         JSObjectRef object,
657                         JSObjectRef thisObject,
658                         size_t argumentCount,
659                         const JSValueRef arguments[],
660                         JSValueRef* exception)
661 {
662     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
663
664     MediacontentManagerPrivObject *privateObject;
665     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
666     if(!privateObject) {
667         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
668         return JSWebAPIErrorFactory::postException(context, exception, err);
669     }
670
671     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
672     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
673
674     try{
675         ArgumentValidator argValidator(context, argumentCount, arguments);
676         JSContextRef globalCtx = GlobalContextManager::getInstance()->getGlobalContext(context);        
677         JSObjectRef callbackObj = argValidator.toCallbackObject(0,false,"oncontentadded","oncontentupdated","oncontentremoved",NULL);
678         ContentListener *listener = new ContentListener(globalCtx, callbackObj);
679         IMediacontentManagerPtr contentMgr = privateObject->getObject();
680         if(!(contentMgr->setListener(listener)))
681         {
682             throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
683         }
684     }catch(const BasePlatformException &err){
685         return JSWebAPIErrorFactory::postException(context,exception,err);
686     }
687     catch(...){
688         DeviceAPI::Common::UnknownException err("Unknown Error in setChangeListener().");
689         return JSWebAPIErrorFactory::postException(context, exception, err);
690     }
691
692         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
693     return JSValueMakeUndefined(context);
694 }
695
696 JSValueRef JSMediacontentManager::unsetChangeListener(
697                         JSContextRef context,
698                         JSObjectRef object,
699                         JSObjectRef thisObject,
700                         size_t argumentCount,
701                         const JSValueRef arguments[],
702                         JSValueRef* exception)
703 {
704     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
705     MediacontentManagerPrivObject *privateObject;
706     privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
707     if(!privateObject) {
708         DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
709         return JSWebAPIErrorFactory::postException(context, exception, err);
710     }
711
712     AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
713     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
714
715     try{
716         // perform
717         IMediacontentManagerPtr contentMgr = privateObject->getObject();
718         if(!(contentMgr->unsetListener()))
719         {
720             throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
721         }
722     }catch(const BasePlatformException &err){
723         return JSWebAPIErrorFactory::postException(context,exception,err);
724     }
725     catch(...){
726         DeviceAPI::Common::UnknownException err("Unknown Error in unsetChangeListener().");
727         return JSWebAPIErrorFactory::postException(context, exception, err);
728     }
729
730         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
731     return JSValueMakeUndefined(context);
732 }
733
734 const JSClassRef JSMediacontentManager::getClassRef() {
735     if (!m_jsClassRef) {
736         m_jsClassRef = JSClassCreate(&m_classInfo);
737     }
738     return m_jsClassRef;
739 }
740
741
742 const JSClassDefinition* JSMediacontentManager::getClassInfo()
743 {
744     return &m_classInfo;
745 }
746
747 void JSMediacontentManager::initialize(JSContextRef context, JSObjectRef object)
748 {
749     MediacontentManagerPrivObject *privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
750     if (NULL == privateObject)
751     {
752         IMediacontentManagerPtr contentManager = MediacontentFactory::getInstance().createMediacontentManagerObject();
753         privateObject = new MediacontentManagerPrivObject(context, contentManager);
754         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject)))
755         {
756             delete privateObject;
757         }
758     }
759
760 }
761
762 void JSMediacontentManager::finalize(JSObjectRef object)
763 {
764     MediacontentManagerPrivObject* priv = static_cast<MediacontentManagerPrivObject*> (JSObjectGetPrivate(object));
765     if(priv != NULL)
766     {
767         delete priv;
768         JSObjectSetPrivate(object, NULL);
769         priv = NULL;
770     }
771
772 }
773
774 IMediacontentManagerPtr JSMediacontentManager::getContentManagerPrivObject(
775                         JSContextRef ctx,
776                         const JSObjectRef object,
777                         JSValueRef* exception)
778 {
779     MediacontentManagerPrivObject *priv = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
780     if (priv)
781     {
782         return priv->getObject();
783     }
784     ThrowMsg(ConversionException, "Private object is NULL.");
785 }
786
787
788 }
789 }
790
791