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