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