2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <CommonsJavaScript/JSCallbackManager.h>
19 #include <JSWebAPIErrorFactory.h>
20 #include "ContentController.h"
21 #include "ContentConverter.h"
22 #include "ContentAsyncCallbackManager.h"
25 using namespace DeviceAPI::Common;
26 using namespace WrtDeviceApis::Commons;
27 using namespace WrtDeviceApis::CommonsJavaScript;
32 MediacontentManagerController& MediacontentManagerController::getInstance()
34 static MediacontentManagerController instance;
38 MediacontentManagerController::MediacontentManagerController():
39 EventBrowseFolderAnswerReceiver(ThreadEnum::NULL_THREAD),
40 EventUpdateMediaItemsAnswerReceiver(ThreadEnum::NULL_THREAD),
41 EventFindFolderAnswerReceiver(ThreadEnum::NULL_THREAD)
45 MediacontentManagerController::~MediacontentManagerController()
50 void MediacontentManagerController::OnAnswerReceived(const IEventUpdateMediaItemsPtr &event)
53 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
56 LoggerE("no callback manager");
61 MediaContentAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
62 if (event->getResult())
68 JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
69 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
70 cbm->callOnError(errorObject);
76 LoggerE("error during processing answer");
81 void MediacontentManagerController::OnAnswerReceived(const IEventBrowseFolderPtr &event)
83 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
86 LoggerE("no callback manager");
91 MediaContentAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
92 if (event->getResult())
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);
101 JSValueRef error = NULL;
102 if (ExceptionCodes::None != event->getExceptionCode())
104 switch (event->getExceptionCode())
106 case ExceptionCodes::InvalidArgumentException:
107 error = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "The value is not valid");
110 error = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"unknown error");
113 cbm->callOnError(error);
121 LoggerE("error during processing answer");
127 void MediacontentManagerController::OnAnswerReceived(const IEventFindFolderPtr &event)
129 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
132 LoggerE("no callback manager");
137 MediaContentAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
138 if (event->getResult())
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);
147 JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
148 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
149 cbm->callOnError(errorObject);
155 LoggerE("error during processing answer");