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