2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <Tizen/Tizen/FilterConverter.h>
23 #include <API/Call/EventFindCallHistory.h>
24 #include <API/Call/EventRemoveBatch.h>
25 #include <API/Call/EventRemoveAll.h>
26 #include <API/Call/EventCallHistoryListener.h>
27 #include <API/Call/CallHistoryFactory.h>
28 #include <API/Call/ICallHistory.h>
29 #include <Tizen/Common/JSTizenExceptionFactory.h>
30 #include <Tizen/Common/JSTizenException.h>
31 #include <Tizen/Common/SecurityExceptions.h>
33 #include "JSCallHistoryEntry.h"
34 #include "JSCallHistory.h"
35 #include "CallStaticController.h"
36 #include "CallMultiCallback.h"
37 #include "ResponseDispatcher.h"
38 #include "Converter.h"
39 #include "plugin_config.h"
43 using namespace TizenApis::Api::Tizen;
44 using namespace TizenApis::Api::Call;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47 using namespace TizenApis::Tizen1_0::Tizen;
48 using namespace TizenApis::Commons;
53 JSClassRef JSCallHistory::m_jsClassRef = NULL;
55 JSClassDefinition JSCallHistory::m_classInfo =
58 kJSClassAttributeNone,
76 JSStaticValue JSCallHistory::m_property[] = {
80 JSStaticFunction JSCallHistory::m_function[] =
82 { "find", JSCallHistory::find, kJSPropertyAttributeNone },
83 { "remove", JSCallHistory::remove, kJSPropertyAttributeNone },
84 { "removeBatch", JSCallHistory::removeBatch, kJSPropertyAttributeNone },
85 { "removeAll", JSCallHistory::removeAll, kJSPropertyAttributeNone },
86 { "deleteRecording", JSCallHistory::deleteRecording, kJSPropertyAttributeNone },
87 { "addListener", JSCallHistory::addListener, kJSPropertyAttributeNone },
88 { "removeListener", JSCallHistory::removeListener, kJSPropertyAttributeNone },
92 const JSClassRef JSCallHistory::getClassRef() {
94 m_jsClassRef = JSClassCreate(&m_classInfo);
99 const JSClassDefinition* JSCallHistory::getClassInfo(){
103 void JSCallHistory::initialize(JSContextRef context, JSObjectRef object) {
104 JSCallHistoryPriv* priv = static_cast<JSCallHistoryPriv*>(JSObjectGetPrivate(object));
107 ICallHistoryPtr CallHistory(CallHistoryFactory::getInstance().getCallHistoryObject());
108 priv = new JSCallHistoryPriv(context, CallHistory);
109 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
115 void JSCallHistory::finalize(JSObjectRef object) {
116 JSCallHistoryPriv* priv = static_cast<JSCallHistoryPriv*>(JSObjectGetPrivate(object));
118 JSObjectSetPrivate(object, NULL);
123 JSObjectRef JSCallHistory::createJSObject(JSContextRef context, JSObjectRef object)
125 ICallHistoryPtr CallHistory(CallHistoryFactory::getInstance().getCallHistoryObject());
126 JSCallHistoryPriv* priv = new JSCallHistoryPriv(context, CallHistory);
128 return JSObjectMake(context, getClassRef(), priv);
131 bool JSCallHistory::hasInstance(JSContextRef context, JSObjectRef constructor,
132 JSValueRef possibleInstance, JSValueRef* exception) {
133 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
136 JSValueRef JSCallHistory::find(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
137 const JSValueRef arguments[], JSValueRef* exception) {
139 if (argumentCount < 1 || argumentCount > 6) {
140 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
143 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
144 JSContextRef gContext = priv->getContext();
146 assert(priv && "Invalid private pointer.");
147 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
148 Validator check(context, exception);
151 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
153 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
155 if (argumentCount >= 2) {
156 if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
157 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[1]))) {
158 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : error callback ");
160 cbm->setOnError(arguments[1]);
164 if (argumentCount >= 1) {
165 if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])) {
166 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : success callback ");
167 } else if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[0]))) {
168 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : success callback ");
170 cbm->setOnSuccess(arguments[0]);
173 if (argumentCount >= 3) {
174 if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
175 if (!JSValueIsObject(context, arguments[2])) {
176 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : filter");
181 AceSecurityStatus status = CALL_CHECK_ACCESS(
183 CALL_HISTORY_FUNCTION_API_FIND);
185 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
187 EventFindCallHistoryPtr event(new EventFindCallHistory());
188 ICallHistoryPtr callHistory(priv->getObject());
189 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
190 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
192 if (argumentCount >= 3) {
193 if (!check.isNullOrUndefined(arguments[2])) {
194 FilterPtr filter = filterConverter->toFilter(arguments[2]);
195 event ->setFilter(filter);
199 if (argumentCount >= 4) {
200 if (!check.isNullOrUndefined(arguments[3])) {
201 event->setSortMode(filterConverter->toSortMode(arguments[3]));
205 if (argumentCount >= 5) {
206 if (!check.isNullOrUndefined(arguments[4])) {
207 event->setLimit(converter.toULong(arguments[4]));
211 if (argumentCount >= 6) {
212 if (!check.isNullOrUndefined(arguments[5])) {
213 event->setOffset(converter.toULong(arguments[5]));
217 callHistory->find(event);
218 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
219 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
220 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
221 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
222 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
223 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
224 } catch(const WrtDeviceApis::Commons::Exception& ex) {
225 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
228 return JSValueMakeUndefined(context);
231 JSValueRef JSCallHistory::remove(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
232 const JSValueRef arguments[], JSValueRef* exception) {
234 if (argumentCount < 1 || argumentCount > 1) {
235 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
238 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
239 JSContextRef gContext = priv->getContext();
241 assert(priv && "Invalid private pointer.");
242 Converter converter(context);
243 Validator check(context, exception);
245 if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])) {
246 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : CallHistoryEntry");
247 } else if (!JSValueIsObject(context, arguments[0])) {
248 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : CallHistoryEntry");
252 AceSecurityStatus status = CALL_CHECK_ACCESS(
254 CALL_HISTORY_FUNCTION_API_REMOVE);
256 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
258 ICallHistoryPtr callHistory(priv->getObject());
259 CallHistoryEntryPropertiesPtr entry = converter.toCallHistoryEntryProperties(arguments[0]);
260 callHistory->remove(entry->getEntryId());
261 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
262 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
263 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
264 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
265 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
266 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
267 } catch(const WrtDeviceApis::Commons::Exception& ex) {
268 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
271 return JSValueMakeUndefined(context);
274 JSValueRef JSCallHistory::removeBatch(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
275 const JSValueRef arguments[], JSValueRef* exception) {
277 if (argumentCount < 1 || argumentCount > 3) {
278 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
281 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
282 JSContextRef gContext = priv->getContext();
284 assert(priv && "Invalid private pointer.");
285 Converter converter(context);
286 Validator check(context, exception);
288 if (!JSIsArrayValue(context, arguments[0])) {
289 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : CallHistoryEntry array");
293 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
295 if (argumentCount >= 3) {
296 if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
297 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[2]))) {
298 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : error callback");
300 cbm->setOnError(arguments[2]);
304 if (argumentCount >= 2) {
305 if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
306 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[1]))) {
307 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : success callback");
309 cbm->setOnSuccess(arguments[1]);
313 AceSecurityStatus status = CALL_CHECK_ACCESS(
315 CALL_HISTORY_FUNCTION_API_REMOVE_BATCH);
317 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
319 EventRemoveBatchPtr event(new EventRemoveBatch());
320 ICallHistoryPtr callHistory(priv->getObject());
322 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
323 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
325 CallHistoryEntryList entryList = converter.toVectorOfCallHistoryEntryProperties(arguments[0]);
327 std::vector<unsigned long> entryIds;
329 if (entryList.size() > 0) {
330 for (unsigned int i = 0; i < entryList.size(); i++) {
331 entryIds.push_back(entryList[i]->getEntryId());
333 event->setEntryIds(entryIds);
334 callHistory->removeBatch(event);
336 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : Entry array is null");
338 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
339 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
340 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
341 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
342 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
343 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
344 } catch(const WrtDeviceApis::Commons::Exception& ex) {
345 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
348 return JSValueMakeUndefined(context);
351 JSValueRef JSCallHistory::removeAll(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
352 const JSValueRef arguments[], JSValueRef* exception) {
354 if (argumentCount > 2) {
355 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
358 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
359 JSContextRef gContext = priv->getContext();
361 assert(priv && "Invalid private pointer.");
362 Converter converter(context);
363 Validator check(context, exception);
366 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
368 if (argumentCount >= 2) {
369 if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
370 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[1]))) {
371 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : error callback");
373 cbm->setOnError(arguments[1]);
377 if (argumentCount >= 1) {
378 if (!JSValueIsNull(context, arguments[0]) && !JSValueIsUndefined(context, arguments[0])) {
379 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[0]))) {
380 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : success callback");
382 cbm->setOnSuccess(arguments[0]);
386 AceSecurityStatus status = CALL_CHECK_ACCESS(
388 CALL_HISTORY_FUNCTION_API_REMOVE_ALL);
390 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
392 EventRemoveAllPtr event(new EventRemoveAll());
393 ICallHistoryPtr callHistory(priv->getObject());
394 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
395 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
397 callHistory->removeAll(event);
398 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
399 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
400 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
401 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
402 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
403 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
404 } catch(const WrtDeviceApis::Commons::Exception& ex) {
405 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
408 return JSValueMakeUndefined(context);
411 JSValueRef JSCallHistory::deleteRecording(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
412 const JSValueRef arguments[], JSValueRef* exception) {
414 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, "Not supported error : deleteRecording");
417 JSValueRef JSCallHistory::addListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
418 const JSValueRef arguments[], JSValueRef* exception) {
420 if (argumentCount < 1 || argumentCount > 1) {
421 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
424 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
425 JSContextRef gContext = priv->getContext();
427 assert(priv && "Invalid private pointer.");
428 Converter converter(context);
429 Validator check(context, exception);
432 EventCallHistoryListenerPrivateDataPtr privData(converter.toEventCallHistoryListenerPrivateData(arguments[0], gContext));
434 AceSecurityStatus status = CALL_CHECK_ACCESS(
436 CALL_HISTORY_FUNCTION_API_ADDLISTENER);
438 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
440 Api::Call::EventCallHistoryListenerEmitterPtr emitter(new Api::Call::EventCallHistoryListenerEmitter);
441 emitter->setListener(&CallStaticController::getInstance());
442 emitter->setEventPrivateData(DPL::StaticPointerCast<Api::Call::EventCallHistoryListener::PrivateDataType>(privData));
444 ICallHistoryPtr callHistory(priv->getObject());
445 long id = callHistory->addListener(emitter);
447 return converter.toJSValueRefLong(id);
448 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
449 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
450 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
451 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
452 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
453 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
454 } catch(const WrtDeviceApis::Commons::Exception& ex) {
455 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
458 return JSValueMakeUndefined(context);
461 JSValueRef JSCallHistory::removeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
462 const JSValueRef arguments[], JSValueRef* exception) {
464 if (argumentCount < 1 || argumentCount > 1) {
465 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
468 JSCallHistoryPriv *priv = static_cast<JSCallHistoryPriv*> (JSObjectGetPrivate(thisObject));
469 JSContextRef gContext = priv->getContext();
471 assert(priv && "Invalid private pointer.");
472 Converter converter(context);
473 Validator check(context, exception);
476 if (check.isNullOrUndefined(arguments[0])) {
477 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error : handle");
480 AceSecurityStatus status = CALL_CHECK_ACCESS(
482 CALL_HISTORY_FUNCTION_API_REMOVELISTENER);
484 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
486 long id = static_cast<long>(converter.toLong(arguments[0]));
489 ICallHistoryPtr callHistory(priv->getObject());
490 callHistory->removeListener(id);
492 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
493 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
494 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
495 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
496 } catch(const WrtDeviceApis::Commons::Exception& ex) {
497 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
500 return JSValueMakeUndefined(context);