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