summaryrefslogtreecommitdiff
path: root/wearable_src/Content/JSContentManager.cpp
blob: a5d864b50bf66be2cdf14df17797b5f54b9fdb9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//
// Tizen Web Device API
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <cassert>
#include <memory>
#include <CommonsJavaScript/Utils.h>
#include <CommonsJavaScript/Validator.h>
#include <CommonsJavaScript/Converter.h>
#include <CommonsJavaScript/JSCallbackManager.h>
#include <CommonsJavaScript/JSUtils.h>
#include <CommonsJavaScript/JSPendingOperation.h>
#include <CommonsJavaScript/PrivateObject.h>
#include <CommonsJavaScript/ScopedJSStringRef.h>
#include <SecurityExceptions.h>
#include <CallbackUserData.h>
#include <MultiCallbackUserData.h>
#include <GlobalContextManager.h>
#include <FilterConverter.h>
#include <JSWebAPIErrorFactory.h>
#include <ArgumentValidator.h>
#include <TimeTracer.h>

#include "JSUtil.h"
#include "ContentFactory.h"
#include "ContentController.h"
#include "JSContentManager.h"
#include "JSContent.h"
#include "JSImage.h"
#include "JSVideo.h"
#include "JSAudio.h"
#include "ContentConverter.h"
#include "plugin_config.h"
#include "ContentAsyncCallbackManager.h"
#include "ContentListener.h"
#include "ContentVideo.h"
#include "ContentImage.h"
#include "ContentFilterConverter.h"
#include "ContentUtility.h"
#include <Logger.h>

using namespace DeviceAPI::Common;
using namespace DeviceAPI::Tizen;
using namespace WrtDeviceApis::Commons;
using namespace WrtDeviceApis::CommonsJavaScript;

#define TIZEN_CONTENT_MANAGER_ATTRIBUTENAME                 "content"

namespace DeviceAPI {
namespace Content {

JSStaticValue JSMediacontentManager::m_property[] =
{
    { 0, 0, 0, 0 }
};

JSStaticFunction JSMediacontentManager::m_function[] = 
{
    { CONTENT_FUNCTION_API_FIND_ITEMS, findItems, kJSPropertyAttributeNone },
    { CONTENT_FUNCTION_API_GET_FOLDERS, getFolders, kJSPropertyAttributeNone },
    { CONTENT_FUNCTION_API_UPDATE_ITEM, updateItem, kJSPropertyAttributeNone },
    { CONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH, updateItemsBatch, kJSPropertyAttributeNone },
    { CONTENT_FUNCTION_API_SCAN_FILE, scanFile, kJSPropertyAttributeNone },
    { CONTENT_FUNCTION_API_SET_CHANGE_LISTENER, setChangeListener, kJSPropertyAttributeNone },
    { CONTENT_FUNCTION_API_UNSET_CHANGE_LISTENER, unsetChangeListener,kJSPropertyAttributeNone},
    { 0, 0, 0 }
};


JSClassRef JSMediacontentManager::m_jsClassRef = NULL;

JSClassDefinition JSMediacontentManager::m_classInfo =
{
        0,
        kJSClassAttributeNone,
        TIZEN_CONTENT_MANAGER_ATTRIBUTENAME,
        0,
        m_property,
        m_function,
        initialize,
        finalize,
        NULL, //hasProperty,
        NULL, //getProperty,
        NULL, //setProperty,
        NULL, //deleteProperty,
        NULL, //getPropertyNames,
        NULL, //callAsFunction,
        NULL, //callAsConstructor,
        NULL,
        NULL, //convertToType
};

JSValueRef JSMediacontentManager::getFolders(
                            JSContextRef context,
                            JSObjectRef object,
                            JSObjectRef thisObject,
                            size_t argumentCount,
                            const JSValueRef arguments[],
                            JSValueRef* exception )
{
    TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);

    MediacontentManagerPrivObject *privateObject;
    privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
    if (!privateObject) {
        DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);

    JSCallbackManagerPtr cbm(NULL);
    cbm = JSCallbackManager::createObject(globalContext);
    
    try{
        ArgumentValidator argValidator(context, argumentCount, arguments);
        JSObjectRef successCallbackObj = argValidator.toFunction(0);
        if(successCallbackObj){
            cbm->setOnSuccess(successCallbackObj);
        }
        else{
            throw TypeMismatchException("SuccessCallback type mismatched.");
        }

        JSObjectRef errorCallbackObj = argValidator.toFunction(1,true);
        if(errorCallbackObj){
            cbm->setOnError(errorCallbackObj);
        }
    }catch(const BasePlatformException &err){
        return JSWebAPIErrorFactory::postException(context,exception,err);
    }
    catch(...){
        DeviceAPI::Common::UnknownException err("Unknown Error in ContentManager.getDirectories().");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    Try
    {
        IMediacontentManagerPtr contentMgr = privateObject->getObject();

        IEventFindFolderPtr dplEvent(new IEventFindFolder());
        dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
        dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());

        contentMgr->findFolder(dplEvent);

        MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
    }
    Catch(WrtDeviceApis::Commons::UnsupportedException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::InvalidArgumentException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::ConversionException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::NotFoundException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::Exception)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
    }

	TIME_TRACER_ITEM_END(__FUNCTION__, 0);
    return JSValueMakeUndefined(context);

}



JSValueRef JSMediacontentManager::findItems(
                            JSContextRef context,
                            JSObjectRef object,
                            JSObjectRef thisObject,
                            size_t argumentCount,
                            const JSValueRef arguments[],
                            JSValueRef* exception )
{

    TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);

    MediacontentManagerPrivObject *privateObject;
    privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
    if(!privateObject) {
        DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_FIND_ITEMS);

    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);

    JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);

    JSCallbackManagerPtr cbm(NULL);
    MediaContentFilterConverterFactory::ConverterType fConverter = MediaContentFilterConverterFactory::getConverter(context);
    MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);

    cbm = JSCallbackManager::createObject(globalContext);

    string folderId;
    Tizen::FilterPtr filter;
    Tizen::SortModePtr sortPtr;
    JSObjectRef filterObj;
    JSObjectRef sortModeObj;
    IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
    try{
        ArgumentValidator argValidator(context, argumentCount, arguments);
        JSObjectRef successCallbackObj = argValidator.toFunction(0);
        if(successCallbackObj){
            cbm->setOnSuccess(successCallbackObj);
        }
        else{
            throw TypeMismatchException("SuccessCallback type mismatched.");
        }

        JSObjectRef errorCallbackObj = argValidator.toFunction(1,true);
        if(errorCallbackObj){
            cbm->setOnError(errorCallbackObj);
        }

        folderId = argValidator.toString(2, true);
        if(!folderId.empty()){
            if(folderId != "null" ){
                dplEvent->setFolderID(folderId);
            }
            else if(folderId == "undefined"){
                throw InvalidValuesException("folderId is not valid.");
            }
        }

        //filter
        filterObj = argValidator.toObject(3,true);

        //sortMode
        sortModeObj= argValidator.toObject(4,true);

        // count
        if(argumentCount >= 6){
            if(!JSValueIsNull(context, arguments[5])){
                long count = argValidator.toLong(5, true, 0);
                if( count >= 0L ){
                    dplEvent->setLimit(count);
                }
                else{
                    throw InvalidValuesException( "count should be positive.");
                }
            }
        }
        if(argumentCount >= 7){
            // offset
            long offset = argValidator.toLong(6, true);
            if( offset >= 0L ){
                dplEvent->setOffset(offset);
            }
            else{
                throw InvalidValuesException("offset should be positive.");
            }
        }
    }catch(const BasePlatformException &err){
        return JSWebAPIErrorFactory::postException(context,exception,err);
    }
    catch(...){
        DeviceAPI::Common::UnknownException err("Unknown Error in find().");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    Try
    {
        if(filterObj){
            dplEvent->setFilter(fConverter->toFilter(filterObj));
        }
        if(sortModeObj){
            dplEvent->setSortMode(fConverter->toSortMode(sortModeObj));
        }
        IMediacontentManagerPtr contentMgr = privateObject->getObject();

        dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
        dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
        contentMgr->browseFolder(dplEvent);
        MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
    }
    Catch(WrtDeviceApis::Commons::UnsupportedException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::InvalidArgumentException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::ConversionException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::NotFoundException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::Exception)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
    }

	TIME_TRACER_ITEM_END(__FUNCTION__, 0);
    return JSValueMakeUndefined(context);
}



JSValueRef JSMediacontentManager::updateItemsBatch(JSContextRef context,
        JSObjectRef object,
        JSObjectRef thisObject,
        size_t argumentCount,
        const JSValueRef arguments[],
        JSValueRef* exception)
{
    TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
    MediacontentManagerPrivObject *privateObject;
    privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
    if(!privateObject) {
        DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
    AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_UPDATE_ITEMS_BATCH);
    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);

    JSCallbackManagerPtr cbm(NULL);
    MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);

    cbm = JSCallbackManager::createObject(globalContext);

    MediacontentMediaListPtr contents;
    try{
        ArgumentValidator argValidator(context, argumentCount, arguments);

        if((argumentCount >= 1) && (JSIsArrayValue(context, arguments[0]))){
            contents = converter->toVectorOfMediaItem(arguments[0]);
            if(!contents)
            {
                throw TypeMismatchException( "content type mismatched.");
            }
        }
        else{
            throw TypeMismatchException("content type mismatched.");
        }
        
        JSObjectRef successCallbackObj = argValidator.toFunction(1,true);        
        if(successCallbackObj){
            cbm->setOnSuccess(successCallbackObj);
        }

        JSObjectRef errorCallbackObj = argValidator.toFunction(2,true);
        if(errorCallbackObj){
            cbm->setOnError(errorCallbackObj);
        }
    }catch(const BasePlatformException &err){
        return JSWebAPIErrorFactory::postException(context,exception,err);
    }
    catch(...){
        DeviceAPI::Common::UnknownException err("Unknown Error in updateBatch().");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    Try
    {
        IMediacontentManagerPtr contentMgr = privateObject->getObject();

        IEventUpdateMediaItemsPtr dplEvent(new IEventUpdateMediaItems());
        dplEvent->setMediaItems(contents);
        dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));

        dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
        contentMgr->updateMediaItems(dplEvent);

        MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
    }
    Catch(WrtDeviceApis::Commons::UnsupportedException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::InvalidArgumentException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::ConversionException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::NotFoundException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::Exception)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
    }

	TIME_TRACER_ITEM_END(__FUNCTION__, 0);
    return JSValueMakeUndefined(context);
}



JSValueRef JSMediacontentManager::updateItem(
                        JSContextRef context,
                        JSObjectRef object,
                        JSObjectRef thisObject,
                        size_t argumentCount,
                        const JSValueRef arguments[],
                        JSValueRef* exception)
{

    TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);

    MediacontentManagerPrivObject *privateObject;
    privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
    if (!privateObject) {
        DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_UPDATE_ITEM);
    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);

    Try
    {
        IMediacontentManagerPtr contentMgr = privateObject->getObject();

        //parameter : MediaItem item

        JSObjectRef arg;
        if(argumentCount >= 1)
        {
            if (!JSValueIsObjectOfClass(context, arguments[0], JSMedia::getClassRef()) &&
                !JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef()) &&
                !JSValueIsObjectOfClass(context, arguments[0], JSAudio::getClassRef()) &&           
                !JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())) {
                    ThrowMsg(ConversionException, "Content type mismatched.");
            }
            arg = JSValueToObject(context, arguments[0], exception);
        }
        else
        {
             ThrowMsg(ConversionException, "Content type mismatched.");
        }

        MediacontentMediaPtr event;
        IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());

        if(JSValueIsObjectOfClass(context, arguments[0], JSImage::getClassRef())){
            MediacontentImagePtr imgPtr = JSImage::getImageObject(arg);
            JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
            if(!(JSValueIsNull(context, geoValRef) || JSValueIsUndefined(context, geoValRef))){
                JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
                double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
                double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
                if(ContentUtility::checkLocation(latitude, latitude)){
                    imgPtr->setImageLatitude(latitude);
                    imgPtr->setImageLongitude(longitude);
                }
                else{
                    ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
                }
            }
            else
            {
                imgPtr->setImageLatitude(DEFAULT_GEOLOCATION);
                imgPtr->setImageLongitude(DEFAULT_GEOLOCATION);
            }
        }
        else if(JSValueIsObjectOfClass(context, arguments[0], JSVideo::getClassRef())){
            MediacontentVideoPtr vedioPtr = JSVideo::getVideoObject(arg);
            JSValueRef geoValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "geolocation");
            if(!(JSValueIsNull(context, geoValRef) || JSValueIsUndefined(context, geoValRef))){
                JSObjectRef geoObjRef = JSUtil::JSValueToObject(context, geoValRef);
                double latitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "latitude"));
                double longitude = JSUtil::JSValueToDouble(context, JSUtil::getProperty(context, geoObjRef, "longitude"));
                if(ContentUtility::checkLocation(latitude, latitude)){
                    vedioPtr->setVideoLatitude(latitude);
                    vedioPtr->setVideoLongitude(longitude);
                }
                else{
                    ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
                }
            }
            else
            {
                vedioPtr->setVideoLatitude(DEFAULT_GEOLOCATION);
                vedioPtr->setVideoLongitude(DEFAULT_GEOLOCATION);
            }
        }

        JSValueRef nameValRef = JSUtil::getProperty(context , JSUtil::JSValueToObject(context, arguments[0]), "name");
        if((JSValueIsNull(context, nameValRef) || JSValueIsUndefined(context, nameValRef)) ||
            JSUtil::JSValueToString(context, nameValRef) == ""){
            ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "name is not valid.");
        }
        event = JSMedia::getMediaObject(arg);
        dplEvent->setMediaItem(event);

        dplEvent->setForSynchronousCall();
        contentMgr->updateMedia(dplEvent);

        if (!(dplEvent->getResult())) {
            ThrowMsg(WrtDeviceApis::Commons::Exception, "updating failed by unknown reason.");
        }
    }
    Catch(WrtDeviceApis::Commons::UnsupportedException)
    {
    LoggerW("Exception: "<<_rethrown_exception.GetMessage());
    return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::InvalidArgumentException)
    {
    LoggerW("Exception: "<<_rethrown_exception.GetMessage());
    return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::ConversionException)
    {
    LoggerW("Exception: "<<_rethrown_exception.GetMessage());
    return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::NotFoundException)
    {
    LoggerW("Exception: "<<_rethrown_exception.GetMessage());
    return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::Exception)
    {
    LoggerW("Exception: "<<_rethrown_exception.GetMessage());
    return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
    }

	TIME_TRACER_ITEM_END(__FUNCTION__, 0);
    return JSValueMakeUndefined(context);

}

static void _scanCompletedCallback(std::string err_msg, std::string path, void *user_data){
    CallbackUserData *cb = static_cast<CallbackUserData*>(user_data);
    if(cb != NULL){
            if(err_msg.empty() ){
                        Converter converter(cb->getContext());
                        JSValueRef jsPath = converter.toJSValueRef(ContentUtility::convertPathToUri(path));
                        cb->callSuccessCallback(jsPath);
            }
            else{
                        DeviceAPI::Common::UnknownException err(err_msg.c_str());
                        JSObjectRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cb->getContext(),err);
                        cb->callErrorCallback(errorObject);
            }
            delete cb;
    }
}

JSValueRef JSMediacontentManager::scanFile(
                        JSContextRef context,
                        JSObjectRef object,
                        JSObjectRef thisObject,
                        size_t argumentCount,
                        const JSValueRef arguments[],
                        JSValueRef* exception)
{
    TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);

    MediacontentManagerPrivObject *privateObject;
    privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
    if(!privateObject) {
        DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SCAN_FILE);
    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);

    string path;
    CallbackUserData *callback = NULL;
    try{
        callback = new CallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));    
        ArgumentValidator argValidator(context, argumentCount, arguments);    
        path = argValidator.toString(0);
        if(path.empty()){
            throw TypeMismatchException("File path type mismatched.");
        }
        else if(path == "null" || path == "undefined"){
            throw InvalidValuesException("File path is not valid.");
        }
        path = ContentUtility::convertUriToPath(path);

        JSObjectRef successCallbackObj = argValidator.toFunction(1,true);        
        if(successCallbackObj){
            callback->setSuccessCallback(successCallbackObj);
        }

        JSObjectRef errorCallbackObj = argValidator.toFunction(2,true);
        if(errorCallbackObj){
            callback->setErrorCallback(errorCallbackObj);
        }
    }catch(const BasePlatformException &err){
        return JSWebAPIErrorFactory::postException(context,exception,err);
    }
    catch(...){
        DeviceAPI::Common::UnknownException err("Unknown Error in scanFile().");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    Try
    {
        IMediacontentManagerPtr contentMgr = privateObject->getObject();
        contentMgr->scanFile(_scanCompletedCallback,path,(void*)callback);
    }
    Catch(WrtDeviceApis::Commons::UnsupportedException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::InvalidArgumentException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::ConversionException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::NotFoundException)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
    }
    Catch(WrtDeviceApis::Commons::Exception)
    {
        LoggerW("Exception: "<<_rethrown_exception.GetMessage());
        return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
    }

	TIME_TRACER_ITEM_END(__FUNCTION__, 0);
    return JSValueMakeUndefined(context);
}


JSValueRef JSMediacontentManager::setChangeListener(
                        JSContextRef context,
                        JSObjectRef object,
                        JSObjectRef thisObject,
                        size_t argumentCount,
                        const JSValueRef arguments[],
                        JSValueRef* exception)
{
    TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);

    MediacontentManagerPrivObject *privateObject;
    privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
    if(!privateObject) {
        DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);

    try{
        ArgumentValidator argValidator(context, argumentCount, arguments);
        JSContextRef globalCtx = GlobalContextManager::getInstance()->getGlobalContext(context);        
        JSObjectRef callbackObj = argValidator.toCallbackObject(0,false,"oncontentadded","oncontentupdated","oncontentremoved",NULL);
        ContentListener *listener = new ContentListener(globalCtx, callbackObj);
        IMediacontentManagerPtr contentMgr = privateObject->getObject();
        if(!(contentMgr->setListener(listener)))
        {
            throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
        }
    }catch(const BasePlatformException &err){
        return JSWebAPIErrorFactory::postException(context,exception,err);
    }
    catch(...){
        DeviceAPI::Common::UnknownException err("Unknown Error in setChangeListener().");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

	TIME_TRACER_ITEM_END(__FUNCTION__, 0);
    return JSValueMakeUndefined(context);
}

JSValueRef JSMediacontentManager::unsetChangeListener(
                        JSContextRef context,
                        JSObjectRef object,
                        JSObjectRef thisObject,
                        size_t argumentCount,
                        const JSValueRef arguments[],
                        JSValueRef* exception)
{
    TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
    MediacontentManagerPrivObject *privateObject;
    privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(thisObject));
    if(!privateObject) {
        DeviceAPI::Common::UnknownException err("Content's private object is NULL.");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

    AceSecurityStatus status = CONTENT_CHECK_ACCESS(CONTENT_FUNCTION_API_SET_CHANGE_LISTENER);
    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);

    try{
        // perform
        IMediacontentManagerPtr contentMgr = privateObject->getObject();
        if(!(contentMgr->unsetListener()))
        {
            throw DeviceAPI::Common::UnknownException( "Unknown exception is occured by platfrom");
        }
    }catch(const BasePlatformException &err){
        return JSWebAPIErrorFactory::postException(context,exception,err);
    }
    catch(...){
        DeviceAPI::Common::UnknownException err("Unknown Error in unsetChangeListener().");
        return JSWebAPIErrorFactory::postException(context, exception, err);
    }

	TIME_TRACER_ITEM_END(__FUNCTION__, 0);
    return JSValueMakeUndefined(context);
}

const JSClassRef JSMediacontentManager::getClassRef() {
    if (!m_jsClassRef) {
        m_jsClassRef = JSClassCreate(&m_classInfo);
    }
    return m_jsClassRef;
}


const JSClassDefinition* JSMediacontentManager::getClassInfo()
{
    return &m_classInfo;
}

void JSMediacontentManager::initialize(JSContextRef context, JSObjectRef object)
{
    MediacontentManagerPrivObject *privateObject = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
    if (NULL == privateObject)
    {
        IMediacontentManagerPtr contentManager = MediacontentFactory::getInstance().createMediacontentManagerObject();
        privateObject = new MediacontentManagerPrivObject(context, contentManager);
        if (!JSObjectSetPrivate(object, static_cast<void*>(privateObject)))
        {
            delete privateObject;
        }
    }

}

void JSMediacontentManager::finalize(JSObjectRef object)
{
    MediacontentManagerPrivObject* priv = static_cast<MediacontentManagerPrivObject*> (JSObjectGetPrivate(object));
    if(priv != NULL)
    {
        delete priv;
        JSObjectSetPrivate(object, NULL);
        priv = NULL;
    }

}

IMediacontentManagerPtr JSMediacontentManager::getContentManagerPrivObject(
                        JSContextRef ctx,
                        const JSObjectRef object,
                        JSValueRef* exception)
{
    MediacontentManagerPrivObject *priv = static_cast<MediacontentManagerPrivObject*>(JSObjectGetPrivate(object));
    if (priv)
    {
        return priv->getObject();
    }
    ThrowMsg(ConversionException, "Private object is NULL.");
}


}
}