5 #include <Commons/Exception.h>
6 #include <CommonsJavaScript/Utils.h>
7 #include <CommonsJavaScript/JSCallbackManager.h>
8 #include <JSWebAPIErrorFactory.h>
9 #include <ArgumentValidator.h>
10 #include <CommonsJavaScript/Converter.h>
11 #include <dpl/scoped_ptr.h>
15 #include <json-glib/json-gvariant.h>
21 using namespace DeviceAPI::Common;
22 using namespace WrtDeviceApis::Commons;
23 using namespace WrtDeviceApis::CommonsJavaScript;
25 #define PHONE_ACTIVECALL_PROP "activeCall"
27 JSClassDefinition JSPhone::m_classInfo = {
29 kJSClassAttributeNone,
39 NULL, //DeleteProperty,
40 NULL, //GetPropertyNames,
41 NULL, //CallAsFunction,
42 NULL, //CallAsConstructor,
47 JSStaticFunction JSPhone::m_function[] = {
48 { "invokeCall", JSPhone::invokeCall, kJSPropertyAttributeNone },
49 { "answerCall", JSPhone::answerCall, kJSPropertyAttributeNone },
50 { "hangupCall", JSPhone::hangupCall, kJSPropertyAttributeNone },
51 { "muteCall", JSPhone::muteCall, kJSPropertyAttributeNone },
52 { "selectRemoteDevice", JSPhone::selectRemoteDevice, kJSPropertyAttributeNone },
53 { "unselectRemoteDevice", JSPhone::unselectRemoteDevice, kJSPropertyAttributeNone },
54 { "addRemoteDeviceSelectedListener", JSPhone::addRemoteDeviceSelectedListener, kJSPropertyAttributeNone },
55 { "removeRemoteDeviceSelectedListener", JSPhone::removeRemoteDeviceSelectedListener, kJSPropertyAttributeNone },
56 { "getSelectedRemoteDevice", JSPhone::getSelectedRemoteDevice, kJSPropertyAttributeNone },
57 { "getContacts", JSPhone::getContacts, kJSPropertyAttributeNone },
58 { "addContactsChangedListener", JSPhone::addContactsChangedListener, kJSPropertyAttributeNone },
59 { "removeContactsChangedListener", JSPhone::removeContactsChangedListener, kJSPropertyAttributeNone },
60 { "getCallHistory", JSPhone::getCallHistory, kJSPropertyAttributeNone },
61 { "addCallHistoryChangedListener", JSPhone::addCallHistoryChangedListener, kJSPropertyAttributeNone },
62 { "removeCallHistoryChangedListener", JSPhone::removeCallHistoryChangedListener, kJSPropertyAttributeNone },
63 { "addCallChangedListener", JSPhone::addCallChangedListener, kJSPropertyAttributeNone },
64 { "removeCallChangedListener", JSPhone::removeCallChangedListener, kJSPropertyAttributeNone },
65 { "addCallHistoryEntryAddedListener", JSPhone::addCallHistoryEntryAddedListener, kJSPropertyAttributeNone },
66 { "removeCallHistoryEntryAddedListener", JSPhone::removeCallHistoryEntryAddedListener, kJSPropertyAttributeNone },
70 JSStaticValue JSPhone::m_property[] = {
71 {PHONE_ACTIVECALL_PROP, getProperty, NULL, kJSPropertyAttributeNone},
75 const JSClassRef JSPhone::getClassRef()
79 m_jsClassRef = JSClassCreate(&m_classInfo);
84 const JSClassDefinition* JSPhone::getClassInfo()
89 JSValueRef JSPhone::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
92 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(object));
93 if (NULL == privateObject)
95 LoggerE("private object is null");
96 return JSValueMakeUndefined(context);
100 if (JSStringIsEqualToUTF8CString(propertyName, PHONE_ACTIVECALL_PROP)) {
101 PhonePtr phone(privateObject->getObject());
102 JSStringRef state = JSStringCreateWithUTF8CString(phone->activeCall().c_str());
103 JSValueRef result = JSValueMakeFromJSONString(context, state);
107 return JSValueMakeUndefined(context);
110 return JSValueMakeUndefined(context);
113 JSClassRef JSPhone::m_jsClassRef = JSClassCreate(JSPhone::getClassInfo());
115 void JSPhone::initialize(JSContextRef context, JSObjectRef object)
119 PhonePrivObject* priv = static_cast<PhonePrivObject*>(JSObjectGetPrivate(object));
122 PhonePtr phone(new PhoneMaster());
123 priv = new PhonePrivObject( context, phone);
124 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
126 LoggerE("Object can't store private data.");
131 LoggerD("JSPhone::initialize ");
134 void JSPhone::finalize(JSObjectRef object)
138 PhonePrivObject* priv = static_cast<PhonePrivObject*>(JSObjectGetPrivate(object));
139 JSObjectSetPrivate(object, NULL);
140 LoggerD("Deleting timeutil");
144 bool JSPhone::hasInstance(JSContextRef context,
145 JSObjectRef constructor,
146 JSValueRef possibleInstance,
147 JSValueRef* exception)
149 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
152 JSValueRef JSPhone::invokeCall(JSContextRef context,
154 JSObjectRef thisObject,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
161 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
162 if (NULL == privateObject)
164 LoggerE("private object is null");
165 return JSValueMakeUndefined(context);
168 PhonePtr phone(privateObject->getObject());
170 JSContextRef gContext = privateObject->getContext();
172 ArgumentValidator validator(context, argumentCount, arguments);
173 std::string phoneNumber = validator.toString(0);
174 LoggerD("Dialing phone number: " << phoneNumber);
176 JSObjectRef errorCallback = validator.toFunction(1, false);
178 JSValueProtect(context, errorCallback);
180 phone->invokeCall(phoneNumber, errorCallback, gContext);
182 return JSValueMakeUndefined(context);
185 JSValueRef JSPhone::hangupCall(JSContextRef context,
187 JSObjectRef thisObject,
188 size_t argumentCount,
189 const JSValueRef arguments[],
190 JSValueRef* exception)
194 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
195 if (NULL == privateObject)
197 LoggerE("private object is null");
198 return JSValueMakeUndefined(context);
201 PhonePtr phone(privateObject->getObject());
203 JSContextRef gContext = privateObject->getContext();
205 ArgumentValidator validator(context, argumentCount, arguments);
207 JSObjectRef errorCallback = validator.toFunction(0, false);
209 JSValueProtect(context, errorCallback);
211 phone->hangupCall(errorCallback, gContext);
213 return JSValueMakeUndefined(context);
216 JSValueRef JSPhone::answerCall(JSContextRef context,
218 JSObjectRef thisObject,
219 size_t argumentCount,
220 const JSValueRef arguments[],
221 JSValueRef* exception)
225 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
226 if (NULL == privateObject)
228 LoggerE("private object is null");
229 return JSValueMakeUndefined(context);
232 PhonePtr phone(privateObject->getObject());
234 JSContextRef gContext = privateObject->getContext();
236 ArgumentValidator validator(context, argumentCount, arguments);
238 JSObjectRef errorCallback = validator.toFunction(0, false);
240 JSValueProtect(context, errorCallback);
242 phone->answerCall(errorCallback, gContext);
244 return JSValueMakeUndefined(context);
247 JSValueRef JSPhone::muteCall(JSContextRef context,
249 JSObjectRef thisObject,
250 size_t argumentCount,
251 const JSValueRef arguments[],
252 JSValueRef* exception)
256 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
257 if (NULL == privateObject)
259 LoggerE("private object is null");
260 return JSValueMakeUndefined(context);
263 PhonePtr phone(privateObject->getObject());
265 JSContextRef gContext = privateObject->getContext();
267 ArgumentValidator validator(context, argumentCount, arguments);
268 bool mute = validator.toBool(0);
269 JSObjectRef errorCallback = validator.toFunction(1, false);
270 JSValueProtect(context, errorCallback);
272 phone->muteCall(mute, errorCallback, gContext);
274 return JSValueMakeUndefined(context);
277 JSValueRef JSPhone::getContacts(JSContextRef context,
279 JSObjectRef thisObject,
280 size_t argumentCount,
281 const JSValueRef arguments[],
282 JSValueRef* exception)
286 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
287 if (NULL == privateObject)
289 LoggerE("private object is null");
290 return JSValueMakeUndefined(context);
293 PhonePtr phone(privateObject->getObject());
295 JSContextRef gContext = privateObject->getContext();
297 ArgumentValidator validator(context, argumentCount, arguments);
299 unsigned long count = validator.toULong(0);
300 LoggerD("Retrieving contacts - the latest " << count << " contacts");
301 JSObjectRef successCallback = validator.toFunction(1, false);
302 JSObjectRef errorCallback = validator.toFunction(2, false);
304 JSValueProtect(context, successCallback);
305 JSValueProtect(context, errorCallback);
307 phone->getContacts(count, successCallback, errorCallback, gContext);
309 return JSValueMakeUndefined(context);
312 JSValueRef JSPhone::getCallHistory(JSContextRef context,
314 JSObjectRef thisObject,
315 size_t argumentCount,
316 const JSValueRef arguments[],
317 JSValueRef* exception)
321 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
322 if (NULL == privateObject)
324 LoggerE("private object is null");
325 return JSValueMakeUndefined(context);
328 PhonePtr phone(privateObject->getObject());
330 JSContextRef gContext = privateObject->getContext();
332 ArgumentValidator validator(context, argumentCount, arguments);
334 unsigned long count = validator.toULong(0);
335 LoggerD("Retrieving call history - the latest " << count << " calls");
336 JSObjectRef successCallback = validator.toFunction(1, false);
337 JSObjectRef errorCallback = validator.toFunction(2, false);
339 JSValueProtect(context, successCallback);
340 JSValueProtect(context, errorCallback);
342 phone->getCallHistory(count, successCallback, errorCallback, gContext);
344 return JSValueMakeUndefined(context);
347 JSValueRef JSPhone::addCallChangedListener(JSContextRef context,
349 JSObjectRef thisObject,
350 size_t argumentCount,
351 const JSValueRef arguments[],
352 JSValueRef* exception)
356 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
357 if (NULL == privateObject)
359 LoggerE("private object is null");
360 return JSValueMakeUndefined(context);
363 PhonePtr phone(privateObject->getObject());
365 JSContextRef gContext = privateObject->getContext();
367 ArgumentValidator validator(context, argumentCount, arguments);
369 JSObjectRef callback = validator.toFunction(0, false);
371 JSValueProtect(gContext, callback);
373 int id = phone->addCallChangedListener(callback, gContext);
375 return JSValueMakeNumber(context, id);
377 return JSValueMakeUndefined(context);
380 JSValueRef JSPhone::selectRemoteDevice(JSContextRef context,
382 JSObjectRef thisObject,
383 size_t argumentCount,
384 const JSValueRef arguments[],
385 JSValueRef* exception)
389 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
390 if (NULL == privateObject)
392 LoggerE("private object is null");
393 return JSValueMakeUndefined(context);
396 PhonePtr phone(privateObject->getObject());
398 ArgumentValidator validator(context, argumentCount, arguments);
399 std::string btAddress = validator.toString(0);
400 LoggerD("Selecting device: " << btAddress);
402 phone->selectRemoteDevice(btAddress);
404 return JSValueMakeUndefined(context);
407 JSValueRef JSPhone::unselectRemoteDevice(JSContextRef context,
409 JSObjectRef thisObject,
410 size_t argumentCount,
411 const JSValueRef arguments[],
412 JSValueRef* exception)
416 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
417 if (NULL == privateObject)
419 LoggerE("private object is null");
420 return JSValueMakeUndefined(context);
423 PhonePtr phone(privateObject->getObject());
425 LoggerD("Un-selecting selected device");
427 phone->unselectRemoteDevice();
429 return JSValueMakeUndefined(context);
432 JSValueRef JSPhone::addRemoteDeviceSelectedListener(JSContextRef context,
434 JSObjectRef thisObject,
435 size_t argumentCount,
436 const JSValueRef arguments[],
437 JSValueRef* exception)
441 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
442 if (NULL == privateObject)
444 LoggerE("private object is null");
445 return JSValueMakeUndefined(context);
448 PhonePtr phone(privateObject->getObject());
449 JSContextRef gContext = privateObject->getContext();
451 ArgumentValidator validator(context, argumentCount, arguments);
452 JSObjectRef callback = validator.toFunction(0, false);
453 JSValueProtect(gContext, callback);
455 int id = phone->addRemoteDeviceSelectedListener(callback, gContext);
457 return JSValueMakeNumber(context, id);
459 return JSValueMakeUndefined(context);
462 JSValueRef JSPhone::removeRemoteDeviceSelectedListener(JSContextRef context,
464 JSObjectRef thisObject,
465 size_t argumentCount,
466 const JSValueRef arguments[],
467 JSValueRef* exception)
471 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
472 if (NULL == privateObject)
474 LoggerE("private object is null");
475 return JSValueMakeUndefined(context);
478 PhonePtr phone(privateObject->getObject());
480 JSContextRef gContext = privateObject->getContext();
482 ArgumentValidator validator(context, argumentCount, arguments);
484 int id = validator.toNumber(0);
486 phone->removeRemoteDeviceSelectedListener(id, gContext);
488 return JSValueMakeUndefined(context);
491 JSValueRef JSPhone::getSelectedRemoteDevice(JSContextRef context,
493 JSObjectRef thisObject,
494 size_t argumentCount,
495 const JSValueRef arguments[],
496 JSValueRef* exception)
500 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
501 if (NULL == privateObject)
503 LoggerE("private object is null");
504 return JSValueMakeUndefined(context);
507 PhonePtr phone(privateObject->getObject());
508 JSContextRef gContext = privateObject->getContext();
509 ArgumentValidator validator(context, argumentCount, arguments);
510 JSObjectRef callback = validator.toFunction(0, false);
511 JSValueProtect(gContext, callback);
512 phone->getSelectedRemoteDevice(callback, gContext);
514 return JSValueMakeUndefined(context);
517 JSValueRef JSPhone::removeCallChangedListener(JSContextRef context,
519 JSObjectRef thisObject,
520 size_t argumentCount,
521 const JSValueRef arguments[],
522 JSValueRef* exception)
526 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
527 if (NULL == privateObject)
529 LoggerE("private object is null");
530 return JSValueMakeUndefined(context);
533 PhonePtr phone(privateObject->getObject());
535 JSContextRef gContext = privateObject->getContext();
537 ArgumentValidator validator(context, argumentCount, arguments);
539 int id = validator.toNumber(0);
541 phone->removeCallChangedListener(id, gContext);
543 return JSValueMakeUndefined(context);
546 JSValueRef JSPhone::addCallHistoryEntryAddedListener(JSContextRef context,
548 JSObjectRef thisObject,
549 size_t argumentCount,
550 const JSValueRef arguments[],
551 JSValueRef* exception)
555 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
556 if (NULL == privateObject)
558 LoggerE("private object is null");
559 return JSValueMakeUndefined(context);
562 PhonePtr phone(privateObject->getObject());
564 JSContextRef gContext = privateObject->getContext();
566 ArgumentValidator validator(context, argumentCount, arguments);
568 JSObjectRef callback = validator.toFunction(0, false);
570 JSValueProtect(gContext, callback);
572 int id = phone->addCallHistoryEntryAddedListener(callback, gContext);
574 return JSValueMakeNumber(context, id);
576 return JSValueMakeUndefined(context);
579 JSValueRef JSPhone::removeCallHistoryEntryAddedListener(JSContextRef context,
581 JSObjectRef thisObject,
582 size_t argumentCount,
583 const JSValueRef arguments[],
584 JSValueRef* exception)
588 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
589 if (NULL == privateObject)
591 LoggerE("private object is null");
592 return JSValueMakeUndefined(context);
595 PhonePtr phone(privateObject->getObject());
597 JSContextRef gContext = privateObject->getContext();
599 ArgumentValidator validator(context, argumentCount, arguments);
601 int id = validator.toNumber(0);
603 phone->removeCallHistoryEntryAddedListener(id, gContext);
605 return JSValueMakeUndefined(context);
608 JSValueRef JSPhone::addContactsChangedListener(JSContextRef context,
610 JSObjectRef thisObject,
611 size_t argumentCount,
612 const JSValueRef arguments[],
613 JSValueRef* exception)
617 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
618 if (NULL == privateObject)
620 LoggerE("private object is null");
621 return JSValueMakeUndefined(context);
624 PhonePtr phone(privateObject->getObject());
626 JSContextRef gContext = privateObject->getContext();
628 ArgumentValidator validator(context, argumentCount, arguments);
630 JSObjectRef callback = validator.toFunction(0, false);
632 JSValueProtect(gContext, callback);
634 int id = phone->addContactsChangedListener(callback, gContext);
636 return JSValueMakeNumber(context, id);
638 return JSValueMakeUndefined(context);
641 JSValueRef JSPhone::removeContactsChangedListener(JSContextRef context,
643 JSObjectRef thisObject,
644 size_t argumentCount,
645 const JSValueRef arguments[],
646 JSValueRef* exception)
650 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
651 if (NULL == privateObject)
653 LoggerE("private object is null");
654 return JSValueMakeUndefined(context);
657 PhonePtr phone(privateObject->getObject());
659 JSContextRef gContext = privateObject->getContext();
661 ArgumentValidator validator(context, argumentCount, arguments);
663 int id = validator.toNumber(0);
665 phone->removeContactsChangedListener(id, gContext);
667 return JSValueMakeUndefined(context);
670 JSValueRef JSPhone::addCallHistoryChangedListener(JSContextRef context,
672 JSObjectRef thisObject,
673 size_t argumentCount,
674 const JSValueRef arguments[],
675 JSValueRef* exception)
679 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
680 if (NULL == privateObject)
682 LoggerE("private object is null");
683 return JSValueMakeUndefined(context);
686 PhonePtr phone(privateObject->getObject());
688 JSContextRef gContext = privateObject->getContext();
690 ArgumentValidator validator(context, argumentCount, arguments);
692 JSObjectRef callback = validator.toFunction(0, false);
694 JSValueProtect(gContext, callback);
696 int id = phone->addCallHistoryChangedListener(callback, gContext);
698 return JSValueMakeNumber(context, id);
700 return JSValueMakeUndefined(context);
703 JSValueRef JSPhone::removeCallHistoryChangedListener(JSContextRef context,
705 JSObjectRef thisObject,
706 size_t argumentCount,
707 const JSValueRef arguments[],
708 JSValueRef* exception)
712 PhonePrivObject* privateObject = static_cast<PhonePrivObject*>(JSObjectGetPrivate(thisObject));
713 if (NULL == privateObject)
715 LoggerE("private object is null");
716 return JSValueMakeUndefined(context);
719 PhonePtr phone(privateObject->getObject());
721 JSContextRef gContext = privateObject->getContext();
723 ArgumentValidator validator(context, argumentCount, arguments);
725 int id = validator.toNumber(0);
727 phone->removeCallHistoryChangedListener(id, gContext);
729 return JSValueMakeUndefined(context);