Update change log and spec for wrt-plugins-tizen_0.4.57
[platform/framework/web/wrt-plugins-tizen.git] / src / Content / ContentController.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 <CommonsJavaScript/JSCallbackManager.h>
19 #include <JSWebAPIErrorFactory.h>
20 #include "ContentController.h"
21 #include "ContentConverter.h"
22 #include "ContentAsyncCallbackManager.h"
23 #include <Logger.h>
24
25 using namespace DeviceAPI::Common;
26 using namespace WrtDeviceApis::Commons;
27 using namespace WrtDeviceApis::CommonsJavaScript;
28
29 namespace DeviceAPI {
30 namespace Content {
31
32 MediacontentManagerController& MediacontentManagerController::getInstance()
33 {
34     static MediacontentManagerController instance;
35     return instance;
36 }
37
38 MediacontentManagerController::MediacontentManagerController():
39         EventBrowseFolderAnswerReceiver(ThreadEnum::NULL_THREAD),       
40         EventUpdateMediaItemsAnswerReceiver(ThreadEnum::NULL_THREAD),           
41         EventFindFolderAnswerReceiver(ThreadEnum::NULL_THREAD)
42 {
43 }
44
45 MediacontentManagerController::~MediacontentManagerController() 
46 {
47 }
48
49
50 void MediacontentManagerController::OnAnswerReceived(const IEventUpdateMediaItemsPtr &event)
51 {
52
53     JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
54     if(!cbm)
55     {
56         LoggerE("no callback manager");
57         return;
58     }
59     Try
60     {
61         MediaContentAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
62         if (event->getResult())
63         {
64             cbm->callOnSuccess();
65         }
66         else
67         {
68             JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
69                         cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
70             cbm->callOnError(errorObject);
71         }
72         return;
73     }
74     Catch(Exception)
75     {
76         LoggerE("error during processing answer");
77     }
78 }
79
80
81 void MediacontentManagerController::OnAnswerReceived(const IEventBrowseFolderPtr &event)
82 {
83     JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
84     if (!cbm) 
85     {
86         LoggerE("no callback manager");
87         return;
88     }
89     Try
90     {
91         MediaContentAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
92         if (event->getResult()) 
93         {
94             MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(cbm->getContext());
95             const std::vector<MediacontentMediaPtr> &results = event->getMedia();
96             JSValueRef result = converter->toJSValueRef(results);
97             cbm->callOnSuccess(result);
98         }
99         else
100         {
101             JSValueRef error = NULL;
102             if (ExceptionCodes::None != event->getExceptionCode())
103             {
104                 switch (event->getExceptionCode())
105                 {
106                     case ExceptionCodes::InvalidArgumentException:
107                     error = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "The value is not valid");
108                     break;
109                     default:
110                     error = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"unknown error");
111                     break;
112                 }
113                 cbm->callOnError(error);
114                 return;
115             }
116         }
117         return;
118     }
119     Catch(Exception)
120     {
121         LoggerE("error during processing answer");
122     }
123 }
124
125
126
127 void MediacontentManagerController::OnAnswerReceived(const IEventFindFolderPtr &event)
128 {
129     JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
130     if (!cbm)
131     {
132         LoggerE("no callback manager");
133         return;
134     }
135     Try
136     {
137         MediaContentAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
138         if (event->getResult()) 
139         {
140             MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(cbm->getContext());
141             const std::vector<MediacontentFolderPtr> &results = event->getFolder();
142             JSValueRef result = converter->toJSValueRef(results);
143             cbm->callOnSuccess(result);
144         }
145         else
146         {
147             JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
148                         cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
149             cbm->callOnError(errorObject);
150         }
151         return;
152     }
153     Catch(Exception)
154     {
155         LoggerE("error during processing answer");
156     }
157
158 }
159
160
161 }
162 }
163