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