31 #include <nsIClassInfoImpl.h>
32 #include <nsIDOMDocument.h>
33 #include <nsIProgrammingLanguage.h>
34 #include <nsISupportsPrimitives.h>
36 #include <nsComponentManagerUtils.h>
38 #include <nsXPCOMCID.h>
41 #include <sbIPropertyArray.h>
56 static PRLogModuleInfo* gMediacoreWrapper = nsnull;
57 #define TRACE(args) PR_LOG(gMediacoreWrapper, PR_LOG_DEBUG, args)
58 #define LOG(args) PR_LOG(gMediacoreWrapper, PR_LOG_WARN, args)
77 NS_IMPL_CI_INTERFACE_GETTER7(sbMediacoreWrapper,
79 sbIMediacorePlaybackControl,
80 sbIMediacoreVolumeControl,
81 sbIMediacoreVotingParticipant,
82 sbIMediacoreEventTarget,
89 sbMediacoreWrapper::sbMediacoreWrapper()
90 : mProxiedObjectsMonitor(nsnull)
91 , mWindowIsReady(PR_FALSE)
94 if (!gMediacoreWrapper)
95 gMediacoreWrapper = PR_NewLogModule(
"sbMediacoreWrapper");
97 TRACE((
"sbMediacoreWrapper[0x%x] - Created",
this));
104 TRACE((
"sbMediacoreWrapper[0x%x] - Destroyed",
this));
114 TRACE((
"sbMediacoreWrapper[0x%x] - Init",
this));
117 NS_ENSURE_SUCCESS(rv, rv);
120 NS_ENSURE_SUCCESS(rv, rv);
123 NS_ENSURE_SUCCESS(rv, rv);
126 nsAutoMonitor::NewMonitor(
"sbMediacoreWrapper::mProxiedObjectsMonitor");
151 nsresult rv = NS_ERROR_UNEXPECTED;
153 rv = RemoveSelfDOMListener();
154 NS_ENSURE_SUCCESS(rv, rv);
157 NS_ENSURE_SUCCESS(rv, rv);
175 NS_ENSURE_ARG_POINTER(aURI);
178 nsresult rv = aURI->GetSpec(uriSpec);
179 NS_ENSURE_SUCCESS(rv, rv);
182 SendDOMEvent(NS_LITERAL_STRING(
"seturi"), uriSpec);
183 NS_ENSURE_SUCCESS(rv, rv);
191 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
193 nsresult rv = SendDOMEvent(NS_LITERAL_STRING(
"getduration"),
195 getter_AddRefs(dataEvent));
196 NS_ENSURE_SUCCESS(rv, rv);
199 rv = GetRetvalFromEvent(dataEvent, retvalStr);
200 NS_ENSURE_SUCCESS(rv, rv);
204 if(!retvalStr.IsEmpty()) {
206 if(NS_SUCCEEDED(rv)) {
217 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
219 nsresult rv = SendDOMEvent(NS_LITERAL_STRING(
"getposition"),
221 getter_AddRefs(dataEvent));
222 NS_ENSURE_SUCCESS(rv, rv);
225 rv = GetRetvalFromEvent(dataEvent, retvalStr);
226 NS_ENSURE_SUCCESS(rv, rv);
230 if(!retvalStr.IsEmpty()) {
232 if(NS_SUCCEEDED(rv)) {
244 SendDOMEvent(NS_LITERAL_STRING(
"setposition"),
sbAutoString(aPosition));
245 NS_ENSURE_SUCCESS(rv, rv);
253 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
255 nsresult rv = SendDOMEvent(NS_LITERAL_STRING(
"getisplayingaudio"),
257 getter_AddRefs(dataEvent));
258 NS_ENSURE_SUCCESS(rv, rv);
261 rv = GetRetvalFromEvent(dataEvent, retvalStr);
262 NS_ENSURE_SUCCESS(rv, rv);
264 *aIsPlayingAudio = PR_FALSE;
266 if(retvalStr.EqualsLiteral(
"true") || retvalStr.EqualsLiteral(
"1")) {
267 *aIsPlayingAudio = PR_TRUE;
276 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
278 nsresult rv = SendDOMEvent(NS_LITERAL_STRING(
"getisplayingvideo"),
280 getter_AddRefs(dataEvent));
281 NS_ENSURE_SUCCESS(rv, rv);
284 rv = GetRetvalFromEvent(dataEvent, retvalStr);
285 NS_ENSURE_SUCCESS(rv, rv);
287 *aIsPlayingVideo = PR_FALSE;
289 if(retvalStr.EqualsLiteral(
"true") || retvalStr.EqualsLiteral(
"1")) {
290 *aIsPlayingVideo = PR_TRUE;
300 SendDOMEvent(NS_LITERAL_STRING(
"play"), EmptyString());
301 NS_ENSURE_SUCCESS(rv, rv);
310 SendDOMEvent(NS_LITERAL_STRING(
"pause"), EmptyString());
311 NS_ENSURE_SUCCESS(rv, rv);
320 SendDOMEvent(NS_LITERAL_STRING(
"stop"), EmptyString());
321 NS_ENSURE_SUCCESS(rv, rv);
324 NS_ENSURE_SUCCESS(rv, rv);
349 SendDOMEvent(NS_LITERAL_STRING(
"setmute"),
sbAutoString(aMute));
350 NS_ENSURE_SUCCESS(rv, rv);
361 nsresult rv = SendDOMEvent(NS_LITERAL_STRING(
"setvolume"), volStr);
362 NS_ENSURE_SUCCESS(rv, rv);
372 sbMediacoreWrapper::VoteWithURI(nsIURI *aURI, PRUint32 *_retval)
374 NS_ENSURE_ARG_POINTER(aURI);
375 NS_ENSURE_ARG_POINTER(_retval);
378 nsresult rv = aURI->GetSpec(uriSpec);
379 NS_ENSURE_SUCCESS(rv, rv);
381 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
382 rv = SendDOMEvent(NS_LITERAL_STRING(
"votewithuri"),
384 getter_AddRefs(dataEvent));
385 NS_ENSURE_SUCCESS(rv, rv);
388 rv = GetRetvalFromEvent(dataEvent, retvalStr);
389 NS_ENSURE_SUCCESS(rv, rv);
393 if(!retvalStr.IsEmpty()) {
394 PRUint32 voteResult =
static_cast<PRUint32
>(retvalStr.ToInteger(&rv, 10));
395 if(NS_SUCCEEDED(rv)) {
396 *_retval = voteResult;
404 sbMediacoreWrapper::VoteWithChannel(
nsIChannel *aChannel, PRUint32 *_retval)
406 return NS_ERROR_NOT_IMPLEMENTED;
414 sbMediacoreWrapper::Initialize(
const nsAString &aInstanceName,
416 const nsACString &aChromePageURL)
418 NS_ENSURE_ARG_POINTER(aCapabilities);
421 NS_ENSURE_SUCCESS(rv, rv);
424 NS_ENSURE_SUCCESS(rv, rv);
426 nsCOMPtr<sbIPrompter> prompter =
428 NS_ENSURE_SUCCESS(rv, rv);
432 rv =
mPrompter->SetParentWindowType(NS_LITERAL_STRING(
"Songbird:Main"));
433 NS_ENSURE_SUCCESS(rv, rv);
437 rv =
mPrompter->SetWaitForWindow(PR_FALSE);
438 NS_ENSURE_SUCCESS(rv, rv);
440 nsCOMPtr<nsIDOMWindow> domWindow;
442 NS_ConvertUTF8toUTF16(aChromePageURL),
444 NS_LITERAL_STRING(
"chrome,centerscreen"),
446 getter_AddRefs(domWindow));
447 NS_ENSURE_SUCCESS(rv, rv);
451 nsCOMPtr<nsIDOMEventTarget> domEventTarget =
453 NS_ENSURE_SUCCESS(rv, rv);
457 rv = AddSelfDOMListener();
458 NS_ENSURE_SUCCESS(rv, rv);
461 nsCOMPtr<nsIThread>
target;
462 rv = NS_GetMainThread(getter_AddRefs(target));
463 NS_ENSURE_SUCCESS(rv, rv);
465 PRBool processed = PR_FALSE;
467 rv = target->ProcessNextEvent(PR_FALSE, &processed);
468 NS_ENSURE_SUCCESS(rv, rv);
471 nsCOMPtr<nsIDOMDocument> document;
473 NS_ENSURE_SUCCESS(rv, rv);
475 nsCOMPtr<nsIDOMDocumentEvent> documentEvent =
476 do_QueryInterface(document, &rv);
477 NS_ENSURE_SUCCESS(rv, rv);
489 sbMediacoreWrapper::HandleEvent(
nsIDOMEvent *aEvent)
491 NS_ENSURE_ARG_POINTER(aEvent);
494 nsresult rv = aEvent->GetType(eventType);
495 NS_ENSURE_SUCCESS(rv, rv);
497 if(eventType.EqualsLiteral(
"resize")) {
499 NS_ENSURE_SUCCESS(rv, rv);
501 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"resize"),
this, PR_FALSE);
502 NS_ENSURE_SUCCESS(rv, rv);
506 else if(eventType.EqualsLiteral(
"mediacore-buffering-begin")) {
509 NS_ENSURE_SUCCESS(rv, rv);
511 else if(eventType.EqualsLiteral(
"mediacore-buffering-end")) {
513 NS_ENSURE_SUCCESS(rv, rv);
515 else if(eventType.EqualsLiteral(
"mediacore-error")) {
516 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent =
517 do_QueryInterface(aEvent, &rv);
518 NS_ENSURE_SUCCESS(rv, rv);
520 nsCOMPtr<nsIVariant> codeVariant;
521 rv = dataEvent->GetData(NS_LITERAL_STRING(
"code"),
522 getter_AddRefs(codeVariant));
523 NS_ENSURE_SUCCESS(rv, rv);
526 rv = codeVariant->GetAsUint32(&code);
527 NS_ENSURE_SUCCESS(rv, rv);
529 nsCOMPtr<nsIVariant> messageVariant;
530 rv = dataEvent->GetData(NS_LITERAL_STRING(
"message"),
531 getter_AddRefs(messageVariant));
532 NS_ENSURE_SUCCESS(rv, rv);
535 rv = messageVariant->GetAsAString(message);
536 NS_ENSURE_SUCCESS(rv, rv);
538 nsRefPtr<sbMediacoreError> error;
540 NS_ENSURE_TRUE(error, NS_ERROR_OUT_OF_MEMORY);
542 rv = error->Init(code, message);
543 NS_ENSURE_SUCCESS(rv, rv);
546 NS_ENSURE_SUCCESS(rv, rv);
548 else if(eventType.EqualsLiteral(
"mediacore-eos")) {
550 NS_ENSURE_SUCCESS(rv, rv);
552 else if(eventType.EqualsLiteral(
"mediacore-start")) {
554 NS_ENSURE_SUCCESS(rv, rv);
556 else if(eventType.EqualsLiteral(
"mediacore-pause")) {
558 NS_ENSURE_SUCCESS(rv, rv);
560 else if(eventType.EqualsLiteral(
"mediacore-stop")) {
562 NS_ENSURE_SUCCESS(rv, rv);
564 else if(eventType.EqualsLiteral(
"mediacore-metadata")) {
565 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent =
566 do_QueryInterface(aEvent, &rv);
567 NS_ENSURE_SUCCESS(rv, rv);
569 nsCOMPtr<nsIVariant> variant;
570 rv = dataEvent->GetData(NS_LITERAL_STRING(
"properties"),
571 getter_AddRefs(variant));
572 NS_ENSURE_SUCCESS(rv, rv);
575 NS_ENSURE_SUCCESS(rv, rv);
585 sbMediacoreWrapper::AddSelfDOMListener()
587 nsresult rv = NS_ERROR_UNEXPECTED;
589 NS_ENSURE_SUCCESS(rv, rv);
591 rv = target->AddEventListener(NS_LITERAL_STRING(
"resize"),
this, PR_FALSE);
592 NS_ENSURE_SUCCESS(rv, rv);
594 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-error"),
597 NS_ENSURE_SUCCESS(rv, rv);
599 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-eos"),
602 NS_ENSURE_SUCCESS(rv, rv);
604 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-start"),
607 NS_ENSURE_SUCCESS(rv, rv);
609 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-stop"),
612 NS_ENSURE_SUCCESS(rv, rv);
614 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-pause"),
617 NS_ENSURE_SUCCESS(rv, rv);
619 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-metadata"),
622 NS_ENSURE_SUCCESS(rv, rv);
624 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-buffering-begin"),
627 NS_ENSURE_SUCCESS(rv, rv);
629 rv = target->AddEventListener(NS_LITERAL_STRING(
"mediacore-buffering-end"),
632 NS_ENSURE_SUCCESS(rv, rv);
638 sbMediacoreWrapper::RemoveSelfDOMListener()
640 nsresult rv = NS_ERROR_UNEXPECTED;
642 NS_ENSURE_SUCCESS(rv, rv);
644 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-error"),
647 NS_ENSURE_SUCCESS(rv, rv);
649 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-eos"),
652 NS_ENSURE_SUCCESS(rv, rv);
654 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-start"),
657 NS_ENSURE_SUCCESS(rv, rv);
659 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-stop"),
662 NS_ENSURE_SUCCESS(rv, rv);
664 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-pause"),
667 NS_ENSURE_SUCCESS(rv, rv);
669 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-metadata"),
672 NS_ENSURE_SUCCESS(rv, rv);
674 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-buffering-begin"),
677 NS_ENSURE_SUCCESS(rv, rv);
679 rv = target->RemoveEventListener(NS_LITERAL_STRING(
"mediacore-buffering-end"),
682 NS_ENSURE_SUCCESS(rv, rv);
688 sbMediacoreWrapper::SendDOMEvent(
const nsAString &aEventName,
689 const nsAString &aEventData,
690 nsIDOMDataContainerEvent **aEvent)
692 nsresult rv = NS_ERROR_UNEXPECTED;
693 PRBool isMainThread = NS_IsMainThread();
695 nsCOMPtr<nsIDOMDocumentEvent> documentEvent;
706 nsCOMPtr<nsIThread>
target;
707 rv = NS_GetMainThread(getter_AddRefs(target));
708 NS_ENSURE_SUCCESS(rv, rv);
711 NS_GET_IID(nsIDOMDocumentEvent),
713 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
715 NS_ENSURE_SUCCESS(rv, rv);
721 nsCOMPtr<nsIDOMEvent> domEvent;
722 nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
725 rv = documentEvent->CreateEvent(NS_LITERAL_STRING(
"DataContainerEvent"),
726 getter_AddRefs(domEvent));
727 NS_ENSURE_SUCCESS(rv, rv);
729 dataEvent = do_QueryInterface(domEvent, &rv);
730 NS_ENSURE_SUCCESS(rv, rv);
733 nsCOMPtr<nsIThread>
target;
734 rv = NS_GetMainThread(getter_AddRefs(target));
735 NS_ENSURE_SUCCESS(rv, rv);
737 nsCOMPtr<nsIDOMEvent> tempEvent;
738 rv = documentEvent->CreateEvent(NS_LITERAL_STRING(
"DataContainerEvent"),
739 getter_AddRefs(tempEvent));
740 NS_ENSURE_SUCCESS(rv, rv);
745 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
746 getter_AddRefs(domEvent));
747 NS_ENSURE_SUCCESS(rv, rv);
749 nsCOMPtr<nsIDOMDataContainerEvent> tempDataEvent =
750 do_QueryInterface(domEvent, &rv);
751 NS_ENSURE_SUCCESS(rv, rv);
754 NS_GET_IID(nsIDOMDataContainerEvent),
756 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
757 getter_AddRefs(dataEvent));
758 NS_ENSURE_SUCCESS(rv, rv);
761 rv = domEvent->InitEvent(aEventName, PR_TRUE, PR_TRUE);
762 NS_ENSURE_SUCCESS(rv, rv);
764 rv = dataEvent->SetData(NS_LITERAL_STRING(
"data"),
766 NS_ENSURE_SUCCESS(rv, rv);
768 nsCOMPtr<nsISupportsString> retval =
769 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
770 NS_ENSURE_SUCCESS(rv, rv);
772 rv = dataEvent->SetData(NS_LITERAL_STRING(
"retval"),
774 NS_ENSURE_SUCCESS(rv, rv);
787 nsCOMPtr<nsIThread>
target;
788 rv = NS_GetMainThread(getter_AddRefs(target));
789 NS_ENSURE_SUCCESS(rv, rv);
792 NS_GET_IID(nsIDOMEventTarget),
794 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
796 NS_ENSURE_SUCCESS(rv, rv);
803 rv = eventTarget->DispatchEvent(dataEvent, &handled);
804 NS_ENSURE_SUCCESS(rv, rv);
805 NS_ENSURE_TRUE(handled, NS_ERROR_UNEXPECTED);
808 NS_ADDREF(*aEvent = dataEvent);
815 sbMediacoreWrapper::SendDOMEvent(
const nsAString &aEventName,
816 const nsACString &aEventData,
817 nsIDOMDataContainerEvent **aEvent)
819 nsresult rv = SendDOMEvent(aEventName,
820 NS_ConvertUTF8toUTF16(aEventData),
822 NS_ENSURE_SUCCESS(rv, rv);
827 sbMediacoreWrapper::GetRetvalFromEvent(nsIDOMDataContainerEvent *aEvent,
830 nsCOMPtr<nsIVariant> variant;
831 nsresult rv = aEvent->GetData(NS_LITERAL_STRING(
"retval"),
832 getter_AddRefs(variant));
833 NS_ENSURE_SUCCESS(rv, rv);
835 nsCOMPtr<nsISupports> supports;
836 rv = variant->GetAsISupports(getter_AddRefs(supports));
837 NS_ENSURE_SUCCESS(rv, rv);
839 nsCOMPtr<nsISupportsString> supportsString =
840 do_QueryInterface(supports, &rv);
841 NS_ENSURE_SUCCESS(rv, rv);
843 rv = supportsString->GetData(aRetval);
844 NS_ENSURE_SUCCESS(rv, rv);
850 sbMediacoreWrapper::DispatchMediacoreEvent(PRUint32 aType,
854 nsCOMPtr<sbIMediacoreEvent>
event;
859 getter_AddRefs(event));
860 NS_ENSURE_SUCCESS(rv, rv);
862 rv = DispatchEvent(event, PR_TRUE, nsnull);
863 NS_ENSURE_SUCCESS(rv, rv);
PRUint64 nsString_ToUint64(const nsAString &str, nsresult *rv)
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
Songbird Variant Utility Definitions.
#define SONGBIRD_PROMPTER_CONTRACTID
NS_IMPL_THREADSAFE_RELEASE(sbRequestItem)
NS_IMPL_THREADSAFE_ADDREF(sbRequestItem)
Songbird Thread Utilities Definitions.
_updateTextAndScrollDataForFrame aData