Update change log and spec for wrt-plugins-tizen_0.4.11
[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 "JSUtil.h"
38
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.h"
48 #include "ContentAsyncCallbackManager.h"
49 #include "ContentListener.h"
50 #include "ContentVideo.h"
51 #include "ContentImage.h"
52
53
54 using namespace DeviceAPI::Common;
55 using namespace DeviceAPI::Tizen;
56 using namespace WrtDeviceApis::Commons;
57 using namespace WrtDeviceApis::CommonsJavaScript;
58
59 #define TIZEN_CONTENT_MANAGER_ATTRIBUTENAME                             "content"
60
61 namespace DeviceAPI {
62 namespace Content {
63
64 JSStaticValue JSMediacontentManager::m_property[] =
65 {
66     { 0, 0, 0, 0 }
67 };
68
69 JSStaticFunction JSMediacontentManager::m_function[] = 
70 {
71         { CONTENT_FUNCTION_API_FIND_ITEMS, findItems, kJSPropertyAttributeNone },
72         { CONTENT_FUNCTION_API_GET_FOLDERS, getFolders, kJSPropertyAttributeNone },
73         { CONTENT_FUNCTION_API_UPDATE_ITEM, updateItem, kJSPropertyAttributeNone },
74         { CONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH, updateItemsBatch, kJSPropertyAttributeNone },
75         { CONTENT_FUNCTION_API_SCAN_FILE, scanFile, kJSPropertyAttributeNone },
76         { CONTENT_FUNCTION_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
77         { CONTENT_FUNCTION_API_UNSET_CHANGE_LISTENER, unsetChangeListener,kJSPropertyAttributeNone},
78         { CONTENT_FUNCTION_API_UPDATE_METADATA, updateMetadata,kJSPropertyAttributeNone},
79     { 0, 0, 0 }
80 };
81
82
83 JSClassRef JSMediacontentManager::m_jsClassRef = NULL;
84
85 JSClassDefinition JSMediacontentManager::m_classInfo =
86 {
87                 0,
88                 kJSClassAttributeNone,
89                 TIZEN_CONTENT_MANAGER_ATTRIBUTENAME,
90                 0,
91                 m_property,
92                 m_function,
93                 initialize,
94                 finalize,
95                 NULL, //hasProperty,
96                 NULL, //getProperty,
97                 NULL, //setProperty,
98                 NULL, //deleteProperty,
99                 NULL, //getPropertyNames,
100                 NULL, //callAsFunction,
101                 NULL, //callAsConstructor,
102                 NULL,
103                 NULL, //convertToType
104 };
105
106 JSValueRef JSMediacontentManager::getFolders(
107                                                         JSContextRef context,
108                                                         JSObjectRef object,
109                                                         JSObjectRef thisObject,
110                                                         size_t argumentCount,
111                                                         const JSValueRef arguments[],
112                                                         JSValueRef* exception )
113 {
114         LogDebug("JSContentManagerManager::getFolders entered");
115
116
117         MediacontentManagerPrivObject *privateObject;
118         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
119         if (!privateObject) {
120                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
121         }
122
123         JSContextRef globalContext = privateObject->getContext();
124         Validator validator(context);
125     JSCallbackManagerPtr cbm(NULL);
126     FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
127         MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
128
129         cbm = JSCallbackManager::createObject(globalContext);
130         cbm->setObject(thisObject);
131
132     Try
133     {
134                 IMediacontentManagerPtr contentMgr = getContentManagerPrivObject(context, thisObject, exception);
135
136                 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
137
138                 if(argumentCount >= 1)  //MediaFolderArraySuccessCB successCallback
139                 {
140                         if(validator.isCallback(arguments[0]))
141                         {
142                                 onSuccessForCbm = arguments[0];
143                         }
144                         else
145                         {
146                                 ThrowMsg(ConversionException, "SuccessCallback type mismatched.");
147                         }
148                 }
149                 else
150                 {
151                         ThrowMsg(ConversionException,"There is no successCallback.");
152                 }
153                 if(argumentCount >= 2)  //optional ErrorCallback? errorCallback
154                 {
155                         if(validator.isCallback(arguments[1]))
156                         {
157                                 onErrorForCbm = arguments[1];
158                         }
159                         else if(!validator.isNullOrUndefined(arguments[1]))             //nullable
160                         {
161                                 ThrowMsg(ConversionException,"ErrorCallback type mismatched.");
162                         }
163                 }
164                 if(cbm)
165                 {
166                         cbm->setOnSuccess(onSuccessForCbm);
167                         cbm->setOnError(onErrorForCbm);
168                 }
169
170                 IEventFindFolderPtr dplEvent(new IEventFindFolder());
171                 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
172                 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
173
174                 contentMgr->findFolder(dplEvent);
175
176                 MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
177     }
178     Catch(WrtDeviceApis::Commons::UnsupportedException)
179     {
180                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
181         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
182     }
183     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
184     {
185                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
186         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
187     }
188     Catch(WrtDeviceApis::Commons::ConversionException)
189     {
190                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
191         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
192     }
193     Catch(WrtDeviceApis::Commons::NotFoundException)
194     {
195                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
196         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
197     }
198     Catch(WrtDeviceApis::Commons::Exception)
199     {
200                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
201         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
202     }
203
204     return JSValueMakeUndefined(context);
205
206 }
207
208
209
210 JSValueRef JSMediacontentManager::findItems(
211                                                         JSContextRef context,
212                                                         JSObjectRef object,
213                                                         JSObjectRef thisObject,
214                                                         size_t argumentCount,
215                                                         const JSValueRef arguments[],
216                                                         JSValueRef* exception )
217 {
218
219         LogDebug("JSContentManagerManager::findItems entered");
220
221         MediacontentManagerPrivObject *privateObject;
222         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
223         if (!privateObject) {
224                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
225         }
226
227
228
229         AceSecurityStatus status = CONTENT_CHECK_ACCESS(
230             CONTENT_FUNCTION_API_FIND_ITEMS);
231
232         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
233
234
235         JSContextRef globalContext = privateObject->getContext();
236         Validator validator(context);
237     JSCallbackManagerPtr cbm(NULL);
238     FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
239         MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
240         MediaConverter mediaConverter(globalContext);
241
242         cbm = JSCallbackManager::createObject(globalContext);
243         cbm->setObject(thisObject);
244
245
246     Try
247     {
248                 IMediacontentManagerPtr contentMgr = getContentManagerPrivObject(context, thisObject, exception);
249                 IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
250                 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
251
252                 if(argumentCount >= 1)  //MediaItemArraySuccessCB successCallback
253                 {
254                         if(validator.isCallback(arguments[0]))
255                         {
256                                 onSuccessForCbm = arguments[0];
257                         }
258                         else
259                         {
260                                 ThrowMsg(ConversionException,"SuccessCallback type mismatched.");
261                         }
262                 }
263                 else
264                 {
265                         ThrowMsg(ConversionException,"There is no SuccessCallback.");
266                 }
267                 if(argumentCount >= 2)  //ErrorCallback? errorCallback
268                 {
269                         if(validator.isCallback(arguments[1]))
270                         {
271                                 onErrorForCbm = arguments[1];
272                         }
273                         else if(!validator.isNullOrUndefined(arguments[1]))             //nullable
274                         {
275                                 ThrowMsg(ConversionException,"ErrorCallback type mismatched.");
276                         }
277                 }
278                 if(cbm)
279                 {
280                         cbm->setOnSuccess(onSuccessForCbm);
281                         cbm->setOnError(onErrorForCbm);
282                 }
283
284                 if(argumentCount >= 3)  //MediaFolderId id,
285                 {
286                         string folderId = mediaConverter.toString(arguments[2]);
287                         if(!(validator.isNullOrUndefined(arguments[2])) && (folderId.length() > 0)  )
288                         {
289                                 LogDebug("folderId:"+ folderId);
290
291                                 int nFolderId = 0;
292                                 istringstream(folderId) >> nFolderId; //string -> int
293
294                                 if (nFolderId<0)
295                                 {
296                                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
297                                                 JSTizenException::INVALID_VALUES_ERROR, "Argument is invalid"));
298                                         return JSValueMakeUndefined(context);
299                                 }
300
301                                 dplEvent->setFolderID(folderId);
302                         }
303                 }
304
305                 if(argumentCount >= 4)  //optional AbstractFilter? filter
306                 {
307                         if(JSValueIsObject(context, arguments[3]))
308                         {
309                                 dplEvent->setFilter(filterConverter->toFilter(arguments[3]));
310                         }
311                         else if(!validator.isNullOrUndefined(arguments[3]))             //nullable
312                         {
313                                 ThrowMsg(ConversionException,"Filter type mismatched.");
314                         }
315                 }
316                 if(argumentCount >= 5)  //optional SortMode? sortMode
317                 {
318
319                         if ( JSValueIsObject(context, arguments[4]) &&
320                                 !JSValueIsNull(context, arguments[4]) &&
321                                 !JSValueIsUndefined(context, arguments[4]) &&
322                                 !JSIsArrayValue(context, arguments[4]))
323                         {
324                                 LogDebug("sortmode is object");
325                                 dplEvent->setSortMode(filterConverter->toSortMode(arguments[4]));
326                         }
327                         else if(!validator.isNullOrUndefined(arguments[4]))             //nullable
328                         {
329                                 ThrowMsg(ConversionException,"SortMode type mismatched.");
330                         }
331                 }
332                 if(argumentCount >= 6)  //optional unsigned long? count
333                 {
334                         if(!JSValueIsNull(context, arguments[5]) && !JSValueIsUndefined(context, arguments[5])){
335                                 long count = filterConverter->toLong(arguments[5]);
336                                 if( count >= 0 ){
337                                         dplEvent->setLimit(count);
338                                 }
339                                 else{
340                                         ThrowMsg(InvalidArgumentException, "count should be positive.");
341                                 }
342                         }
343                 }
344                 if(argumentCount >= 7)  //optional unsigned long? offset
345                 {
346                         long offset = filterConverter->toLong(arguments[6]);
347                         if( offset >= 0 ){
348                                 dplEvent->setOffset(offset);
349                         }
350                         else{
351                                 ThrowMsg(InvalidArgumentException, "offset should be positive.");
352                         }
353                 }
354         dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
355         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
356         contentMgr->browseFolder(dplEvent);
357
358                 MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
359     }
360     Catch(WrtDeviceApis::Commons::UnsupportedException)
361     {
362                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
363         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
364     }
365     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
366     {
367                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
368         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
369     }
370     Catch(WrtDeviceApis::Commons::ConversionException)
371     {
372                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
373         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
374     }
375     Catch(WrtDeviceApis::Commons::NotFoundException)
376     {
377                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
378         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
379     }
380     Catch(WrtDeviceApis::Commons::Exception)
381     {
382                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
383         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
384     }
385
386     return JSValueMakeUndefined(context);
387 }
388
389
390
391 JSValueRef JSMediacontentManager::updateItemsBatch(JSContextRef context,
392         JSObjectRef object,
393         JSObjectRef thisObject,
394         size_t argumentCount,
395         const JSValueRef arguments[],
396         JSValueRef* exception)
397 {
398     LogDebug("JSContentManagerManager::updateItemsBatch entered");
399         MediacontentManagerPrivObject *privateObject;
400         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
401         if (!privateObject) {
402                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
403         }
404
405
406         JSContextRef globalContext = privateObject->getContext();
407         AceSecurityStatus status = CONTENT_CHECK_ACCESS(
408             CONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH);
409         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
410
411         Validator validator(context);
412     JSCallbackManagerPtr cbm(NULL);
413     FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
414         MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
415
416         cbm = JSCallbackManager::createObject(globalContext);
417         cbm->setObject(thisObject);
418
419         Try
420     {
421
422                 IMediacontentManagerPtr contentMgr = getContentManagerPrivObject(context, thisObject, exception);
423
424                 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
425                 MediacontentMediaListPtr events;
426                 if((argumentCount >= 1) && (JSIsArrayValue(context, arguments[0])))     //MediaItem[] items
427                 {
428                         events = converter->toVectorOfMediaItem(arguments[0]);
429                         if(!events)
430                         {
431                                 ThrowMsg(ConversionException, "Items type mismatched.");
432                         }
433                 }
434                 else
435                 {
436                         ThrowMsg(ConversionException, "Items type mismatched.");
437                 }
438
439                 if(argumentCount >= 2)  //Function? successCallback,
440                 {
441                         if(validator.isCallback(arguments[1]))
442                         {
443                                 onSuccessForCbm = arguments[1];
444                         }
445                         else if(!validator.isNullOrUndefined(arguments[1]))             //nullable
446                         {
447                                 ThrowMsg(ConversionException,"successCallback type mismatched.");
448                         }
449                 }
450
451                 if(argumentCount >= 3)  //ErrorCallback? errorCallback,
452                 {
453                         if(validator.isCallback(arguments[2]))
454                         {
455                                 onErrorForCbm = arguments[2];
456                         }
457                         else if(!validator.isNullOrUndefined(arguments[2]))             //nullable
458                         {
459                                 ThrowMsg(ConversionException,"ErrorCallback type is mismatched.");
460                         }
461                 }
462
463                 if(cbm)
464                 {
465                         cbm->setOnSuccess(onSuccessForCbm);
466                         cbm->setOnError(onErrorForCbm);
467                 }
468
469         IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
470         dplEvent->setMediaItems(events);
471         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
472
473         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
474         contentMgr->updateMediaItems(dplEvent);
475
476                 MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
477     }
478     Catch(WrtDeviceApis::Commons::UnsupportedException)
479     {
480                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
481         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
482     }
483     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
484     {
485                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
486         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
487     }
488     Catch(WrtDeviceApis::Commons::ConversionException)
489     {
490                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
491         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
492     }
493     Catch(WrtDeviceApis::Commons::NotFoundException)
494     {
495                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
496         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
497     }
498     Catch(WrtDeviceApis::Commons::Exception)
499     {
500                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
501         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
502     }
503
504     return JSValueMakeUndefined(context);
505 }
506
507
508
509 JSValueRef JSMediacontentManager::updateItem(
510                                                 JSContextRef context,
511                                                 JSObjectRef object,
512                                                 JSObjectRef thisObject,
513                                                 size_t argumentCount,
514                                                 const JSValueRef arguments[],
515                                                 JSValueRef* exception)
516 {
517 LogDebug("JSContentManagerManager::updateItem entered");
518
519
520         MediacontentManagerPrivObject *privateObject;
521         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
522         if (!privateObject) {
523                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
524         }
525
526
527
528         AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_UPDATE_ITEM);
529
530         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
531
532         Validator validator(context);
533
534         Try
535         {
536                 IMediacontentManagerPtr contentMgr = getContentManagerPrivObject(context, thisObject, exception);
537
538                 //parameter : MediaItem item
539
540                 JSObjectRef arg;
541                 if(argumentCount >= 1)
542                 {
543                         if (!JSValueIsObjectOfClass(context, arguments[0], JSMedia::getClassRef()) &&
544                                 !JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef()) &&
545                                 !JSValueIsObjectOfClass(context, arguments[0], JSAudio::getClassRef()) &&                       
546                                 !JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())) {
547                                     ThrowMsg(ConversionException, "Item type mismatched.");
548                         }
549                         arg = JSValueToObject(context, arguments[0], exception);
550                 }
551                 else
552                 {
553                          ThrowMsg(ConversionException, "Item type mismatched.");
554                 }
555
556                 MediacontentMediaPtr event;
557                 IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
558
559                 if(JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef())){
560
561                         JSValueRef geoValRef = JSUtil::getProperty(context ,
562                                 JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
563                         JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
564                         double latitude = JSUtil::JSValueToDouble(context,
565                                 JSUtil::getProperty(context, geoObjRef, "latitude"));
566
567                         double longitude = JSUtil::JSValueToDouble(context,
568                                 JSUtil::getProperty(context, geoObjRef, "longitude"));
569
570                         MediacontentImagePtr imgPtr = JSImage::getImageObject(arg);
571                         imgPtr->setImageLatitude(latitude);
572                         imgPtr->setImageLongitude(longitude);
573
574                 }
575                 else if(JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())){
576                         JSValueRef geoValRef = JSUtil::getProperty(context ,
577                                 JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
578                         JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
579
580                         double latitude = JSUtil::JSValueToDouble(context,
581                                 JSUtil::getProperty(context, geoObjRef, "latitude"));
582
583                         double longitude = JSUtil::JSValueToDouble(context,
584                                 JSUtil::getProperty(context, geoObjRef, "longitude"));
585
586                         MediacontentVideoPtr vedioPtr = JSVideo::getVideoObject(arg);
587                         vedioPtr->setVideoLatitude(latitude);
588                         vedioPtr->setVideoLongitude(longitude);
589                 }
590
591                 event = JSMedia::getMediaObject(arg);
592                 dplEvent->setMediaItem(event);
593
594                 dplEvent->setForSynchronousCall();
595                 contentMgr->updateMedia(dplEvent);
596
597         if (!(dplEvent->getResult())) {
598             ThrowMsg(WrtDeviceApis::Commons::Exception, "updating failed by unknown reason.");
599         }
600         }
601         Catch(WrtDeviceApis::Commons::UnsupportedException)
602         {
603                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
604                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
605         }
606         Catch(WrtDeviceApis::Commons::InvalidArgumentException)
607         {
608                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
609                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
610         }
611         Catch(WrtDeviceApis::Commons::ConversionException)
612         {
613                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
614                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
615         }
616         Catch(WrtDeviceApis::Commons::NotFoundException)
617         {
618                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
619                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
620         }
621         Catch(WrtDeviceApis::Commons::Exception)
622         {
623                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
624                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
625         }
626
627         return JSValueMakeUndefined(context);
628
629 }
630
631 static void _scanCompletedCallback(std::string err_msg, std::string path, void *user_data){
632     CallbackUserData *cb = static_cast<CallbackUserData*>(user_data);
633         if(cb != NULL){
634                         if(err_msg.empty() ){
635                                 Converter converter(cb->getContext());
636                                 JSValueRef jsPath = converter.toJSValueRef(path);
637                                 cb->callSuccessCallback(jsPath);
638                         }
639                         else{
640                                 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(cb->getContext(), JSTizenException::UNKNOWN_ERROR, err_msg);
641                                 cb->callErrorCallback(errorObject);
642                         }
643                         delete cb;
644         }
645 }
646
647 JSValueRef JSMediacontentManager::scanFile(
648                                                 JSContextRef context,
649                                                 JSObjectRef object,
650                                                 JSObjectRef thisObject,
651                                                 size_t argumentCount,
652                                                 const JSValueRef arguments[],
653                                                 JSValueRef* exception)
654 {
655         LogDebug("JSContentManagerManager::scanFile entered");
656
657
658         MediacontentManagerPrivObject *privateObject;
659         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
660
661         AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SCAN_FILE);
662         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
663
664         Try
665         {
666
667                 if(!privateObject){
668                         ThrowMsg(ConversionException, "private object is null");
669                 }
670
671                 CallbackUserData *callback = new CallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
672
673                 Converter convert(context);
674                 Validator validator(context);
675                 IMediacontentManagerPtr contentMgr = privateObject->getObject();
676                 string path;
677
678                 if(argumentCount >= 1){                                 //directoryURI
679                         path = convert.toString(arguments[0]);
680                         if(validator.isNullOrUndefined(arguments[0])){
681                                 ThrowMsg(InvalidArgumentException, path+" is not available");
682                         }
683                 }
684                 else{
685                         ThrowMsg(InvalidArgumentException, "file path is not available");
686                 }
687
688                 if(argumentCount >= 2){                                 //successCallback[optional|nullable]
689                         if(validator.isCallback(arguments[1])){
690                                 callback->setSuccessCallback(arguments[1]);
691                         }
692                         else if(!(JSValueIsNull(context,arguments[1]))){
693                                 ThrowMsg(ConversionException, path + ":successCallback is not available");
694                         }
695                 }
696
697                 if(argumentCount >= 3){                                 //errorCallback
698                         if(validator.isCallback(arguments[2])){
699                                 callback->setErrorCallback(arguments[2]);
700                         }
701                         else if(!(JSValueIsNull(context,arguments[2]))){
702                                 ThrowMsg(ConversionException, path + ":errorCallback is not available");
703                         }
704                 }
705
706                 contentMgr->scanFile(_scanCompletedCallback,path,(void*)callback);
707
708         }
709         Catch(WrtDeviceApis::Commons::UnsupportedException)
710         {
711                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
712                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
713         }
714         Catch(WrtDeviceApis::Commons::InvalidArgumentException)
715         {
716                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
717                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
718         }
719         Catch(WrtDeviceApis::Commons::ConversionException)
720         {
721                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
722                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
723         }
724         Catch(WrtDeviceApis::Commons::NotFoundException)
725         {
726                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
727                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
728         }
729         Catch(WrtDeviceApis::Commons::Exception)
730         {
731                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
732                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
733         }
734
735         return JSValueMakeUndefined(context);
736
737 }
738
739
740 JSValueRef JSMediacontentManager::setChangeListener(
741                                                 JSContextRef context,
742                                                 JSObjectRef object,
743                                                 JSObjectRef thisObject,
744                                                 size_t argumentCount,
745                                                 const JSValueRef arguments[],
746                                                 JSValueRef* exception)
747 {
748         LogDebug("JSContentManagerManager::setChangeListener entered");
749
750         MediacontentManagerPrivObject *privateObject;
751         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
752
753         AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
754         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
755
756         Try
757         {
758
759                 if(!privateObject){
760                         ThrowMsg(ConversionException, "private object is null");
761                 }
762
763         if(argumentCount>=1) {
764             if (!JSValueIsObject(context, arguments[0])) {
765                 ThrowMsg(ConversionException, "Wrong first parameter type.");
766             }
767         }
768                 else{
769             ThrowMsg(ConversionException, "Wrong first parameter type.");
770         }
771
772                 Converter convert(context);
773                 Validator validator(context);
774
775                 JSContextRef globalCtx = GlobalContextManager::getInstance()->getGlobalContext(context);
776
777                 // changeCallback Object
778                 JSObjectRef callbackObj = convert.toJSObjectRef(arguments[0]);
779
780                 ContentListener *listener = new ContentListener(globalCtx, callbackObj);
781
782                 // perform
783                 IMediacontentManagerPtr contentMgr = privateObject->getObject();
784                 if(!(contentMgr->setListener(listener)))
785                 {
786                         ThrowMsg(Exception, "Unknown exception is occured by platfrom");
787                 }
788         }
789         Catch(WrtDeviceApis::Commons::UnsupportedException)
790         {
791                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
792                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
793         }
794         Catch(WrtDeviceApis::Commons::InvalidArgumentException)
795         {
796                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
797                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
798         }
799         Catch(WrtDeviceApis::Commons::ConversionException)
800         {
801                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
802                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
803         }
804         Catch(WrtDeviceApis::Commons::NotFoundException)
805         {
806                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
807                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
808         }
809         Catch(WrtDeviceApis::Commons::Exception)
810         {
811                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
812                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
813         }
814
815         return JSValueMakeUndefined(context);
816
817
818 }
819
820 JSValueRef JSMediacontentManager::unsetChangeListener(
821                                                 JSContextRef context,
822                                                 JSObjectRef object,
823                                                 JSObjectRef thisObject,
824                                                 size_t argumentCount,
825                                                 const JSValueRef arguments[],
826                                                 JSValueRef* exception)
827 {
828         LogDebug("JSContentManagerManager::unsetChangeListener entered");
829
830         MediacontentManagerPrivObject *privateObject;
831         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
832
833         AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
834         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
835
836         Try
837         {
838                 if(!privateObject){
839                         ThrowMsg(ConversionException, "private object is null");
840                 }
841
842                 // perform
843                 IMediacontentManagerPtr contentMgr = privateObject->getObject();
844                 if(!(contentMgr->unsetListener()))
845                 {
846                         ThrowMsg(Exception, "Unknown exception is occured by platfrom");
847                 }
848         }
849         Catch(WrtDeviceApis::Commons::UnsupportedException)
850         {
851                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
852                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
853         }
854         Catch(WrtDeviceApis::Commons::InvalidArgumentException)
855         {
856                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
857                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
858         }
859         Catch(WrtDeviceApis::Commons::ConversionException)
860         {
861                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
862                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
863         }
864         Catch(WrtDeviceApis::Commons::NotFoundException)
865         {
866                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
867                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
868         }
869         Catch(WrtDeviceApis::Commons::Exception)
870         {
871                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
872                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
873         }
874
875         return JSValueMakeUndefined(context);
876
877 }
878
879 JSValueRef JSMediacontentManager::updateMetadata(
880                                                 JSContextRef context,
881                                                 JSObjectRef object,
882                                                 JSObjectRef thisObject,
883                                                 size_t argumentCount,
884                                                 const JSValueRef arguments[],
885                                                 JSValueRef* exception)
886 {
887 LogDebug("JSContentManagerManager::updateMetadata entered");
888
889
890         MediacontentManagerPrivObject *privateObject;
891         privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
892         if (!privateObject) {
893                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "private object is null");
894         }
895
896         AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_UPDATE_METADATA);
897         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
898
899         Validator validator(context);
900
901         Try
902         {
903                 IMediacontentManagerPtr contentMgr = getContentManagerPrivObject(context, thisObject, exception);
904
905                 JSObjectRef arg;
906                 if(argumentCount >= 1){
907                         if (!JSValueIsObjectOfClass(context, arguments[0],JSImage::getClassRef())){
908                                     ThrowMsg(UnsupportedException, "Not supported type.");
909                         }
910                         arg = JSValueToObject(context, arguments[0], exception);
911                 }
912                 else{
913                          ThrowMsg(ConversionException, "Invalid content type.");
914                 }
915
916                 MediacontentImagePtr content = JSImage::getImageObject(arg);
917
918         if (!(contentMgr->updateMetadata(content))) {
919             ThrowMsg(WrtDeviceApis::Commons::Exception, "updating metadata is failed by unknown reason.");
920         }
921         }
922         Catch(WrtDeviceApis::Commons::UnsupportedException)
923         {
924                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
925                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
926         }
927         Catch(WrtDeviceApis::Commons::InvalidArgumentException)
928         {
929                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
930                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
931         }
932         Catch(WrtDeviceApis::Commons::ConversionException)
933         {
934                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
935                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
936         }
937         Catch(WrtDeviceApis::Commons::NotFoundException)
938         {
939                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
940                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
941         }
942         Catch(WrtDeviceApis::Commons::Exception)
943         {
944                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
945                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
946         }
947
948         return JSValueMakeUndefined(context);
949
950 }
951
952
953 const JSClassRef JSMediacontentManager::getClassRef() {
954         if (!m_jsClassRef) {
955                 m_jsClassRef = JSClassCreate(&m_classInfo);
956         }
957         return m_jsClassRef;
958 }
959
960
961 const JSClassDefinition* JSMediacontentManager::getClassInfo()
962 {
963         return &m_classInfo;
964 }
965
966 void JSMediacontentManager::initialize(JSContextRef context, JSObjectRef object)
967 {
968     LogDebug("JSContentManager  initialize entered");
969     MediacontentManagerPrivObject *privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
970     if (NULL == privateObject)
971         {
972         LogDebug("Create ContenttManagerPrivObject");
973         IMediacontentManagerPtr contentManager = MediacontentFactory::getInstance().createMediacontentManagerObject();
974         privateObject = new MediacontentManagerPrivObject(context, contentManager);
975         if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject)))
976                 {
977             delete privateObject;
978         }
979     }
980
981 }
982
983 void JSMediacontentManager::finalize(JSObjectRef object)
984 {
985         LogDebug("Entered");
986         MediacontentManagerPrivObject* priv = static_cast<MediacontentManagerPrivObject*> (JSObjectGetPrivate(object));
987         if(priv != NULL)
988         {
989                 LogDebug("Deleting coordinates object");
990                 delete priv;
991                 JSObjectSetPrivate(object, NULL);
992                 priv = NULL;
993         }
994
995 }
996
997 IMediacontentManagerPtr JSMediacontentManager::getContentManagerPrivObject(
998                                                 JSContextRef ctx,
999                                         const JSObjectRef object,
1000                                         JSValueRef* exception)
1001 {
1002     MediacontentManagerPrivObject *priv = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
1003     if (priv)
1004         {
1005         return priv->getObject();
1006     }
1007     ThrowMsg(ConversionException, "Private object is NULL.");
1008 }
1009
1010
1011 }
1012 }
1013
1014