sbMediacoreWrapper.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
29 #include "sbMediacoreWrapper.h"
30 
31 #include <nsIClassInfoImpl.h>
32 #include <nsIDOMDocument.h>
33 #include <nsIProgrammingLanguage.h>
34 #include <nsISupportsPrimitives.h>
35 
36 #include <nsComponentManagerUtils.h>
37 #include <nsMemory.h>
38 #include <nsXPCOMCID.h>
39 #include <prlog.h>
40 
41 #include <sbIPropertyArray.h>
42 
44 #include <sbStringUtils.h>
45 #include <sbThreadUtils.h>
46 #include <sbVariantUtils.h>
47 
49 #include "sbMediacoreError.h"
50 
55 #ifdef PR_LOGGING
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)
59 #else
60 #define TRACE(args) /* nothing */
61 #define LOG(args) /* nothing */
62 #endif
63 
66 
76 
77 NS_IMPL_CI_INTERFACE_GETTER7(sbMediacoreWrapper,
78  sbIMediacore,
79  sbIMediacorePlaybackControl,
80  sbIMediacoreVolumeControl,
81  sbIMediacoreVotingParticipant,
82  sbIMediacoreEventTarget,
83  sbIMediacoreWrapper,
85 
86 NS_DECL_CLASSINFO(sbMediacoreWrapper)
87 NS_IMPL_THREADSAFE_CI(sbMediacoreWrapper)
88 
89 sbMediacoreWrapper::sbMediacoreWrapper()
90 : mProxiedObjectsMonitor(nsnull)
91 , mWindowIsReady(PR_FALSE)
92 {
93 #ifdef PR_LOGGING
94  if (!gMediacoreWrapper)
95  gMediacoreWrapper = PR_NewLogModule("sbMediacoreWrapper");
96 #endif
97  TRACE(("sbMediacoreWrapper[0x%x] - Created", this));
98 
99  mBaseEventTarget = new sbBaseMediacoreEventTarget(this);
100 }
101 
103 {
104  TRACE(("sbMediacoreWrapper[0x%x] - Destroyed", this));
105 
107  nsAutoMonitor::DestroyMonitor(mProxiedObjectsMonitor);
108  }
109 }
110 
111 nsresult
113 {
114  TRACE(("sbMediacoreWrapper[0x%x] - Init", this));
115 
116  nsresult rv = sbBaseMediacore::InitBaseMediacore();
117  NS_ENSURE_SUCCESS(rv, rv);
118 
120  NS_ENSURE_SUCCESS(rv, rv);
121 
123  NS_ENSURE_SUCCESS(rv, rv);
124 
126  nsAutoMonitor::NewMonitor("sbMediacoreWrapper::mProxiedObjectsMonitor");
127  NS_ENSURE_TRUE(mProxiedObjectsMonitor, NS_ERROR_OUT_OF_MEMORY);
128 
129  return NS_OK;
130 }
131 
132 //
133 // sbBaseMediacore overrides
134 //
135 
136 /*virtual*/ nsresult
138 {
139  return NS_OK;
140 }
141 
142 /*virtual*/ nsresult
144 {
145  return NS_OK;
146 }
147 
148 /*virtual*/ nsresult
150 {
151  nsresult rv = NS_ERROR_UNEXPECTED;
152 
153  rv = RemoveSelfDOMListener();
154  NS_ENSURE_SUCCESS(rv, rv);
155 
156  rv = mPrompter->Cancel();
157  NS_ENSURE_SUCCESS(rv, rv);
158 
159  return NS_OK;
160 }
161 
162 //
163 // sbBaseMediacorePlaybackControl overrides
164 //
165 
166 /*virtual*/ nsresult
168 {
169  return NS_OK;
170 }
171 
172 /*virtual*/ nsresult
174 {
175  NS_ENSURE_ARG_POINTER(aURI);
176 
177  nsCString uriSpec;
178  nsresult rv = aURI->GetSpec(uriSpec);
179  NS_ENSURE_SUCCESS(rv, rv);
180 
181  rv =
182  SendDOMEvent(NS_LITERAL_STRING("seturi"), uriSpec);
183  NS_ENSURE_SUCCESS(rv, rv);
184 
185  return NS_OK;
186 }
187 
188 /*virtual*/ nsresult
190 {
191  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
192 
193  nsresult rv = SendDOMEvent(NS_LITERAL_STRING("getduration"),
194  EmptyString(),
195  getter_AddRefs(dataEvent));
196  NS_ENSURE_SUCCESS(rv, rv);
197 
198  nsString retvalStr;
199  rv = GetRetvalFromEvent(dataEvent, retvalStr);
200  NS_ENSURE_SUCCESS(rv, rv);
201 
202  *aDuration = 0;
203 
204  if(!retvalStr.IsEmpty()) {
205  PRUint64 duration = nsString_ToUint64(retvalStr, &rv);
206  if(NS_SUCCEEDED(rv)) {
207  *aDuration = duration;
208  }
209  }
210 
211  return NS_OK;
212 }
213 
214 /*virtual*/ nsresult
216 {
217  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
218 
219  nsresult rv = SendDOMEvent(NS_LITERAL_STRING("getposition"),
220  EmptyString(),
221  getter_AddRefs(dataEvent));
222  NS_ENSURE_SUCCESS(rv, rv);
223 
224  nsString retvalStr;
225  rv = GetRetvalFromEvent(dataEvent, retvalStr);
226  NS_ENSURE_SUCCESS(rv, rv);
227 
228  *aPosition = 0;
229 
230  if(!retvalStr.IsEmpty()) {
231  PRUint64 position = nsString_ToUint64(retvalStr, &rv);
232  if(NS_SUCCEEDED(rv)) {
233  *aPosition = position;
234  }
235  }
236 
237  return NS_OK;
238 }
239 
240 /*virtual*/ nsresult
242 {
243  nsresult rv =
244  SendDOMEvent(NS_LITERAL_STRING("setposition"), sbAutoString(aPosition));
245  NS_ENSURE_SUCCESS(rv, rv);
246 
247  return NS_OK;
248 }
249 
250 /*virtual*/ nsresult
252 {
253  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
254 
255  nsresult rv = SendDOMEvent(NS_LITERAL_STRING("getisplayingaudio"),
256  EmptyString(),
257  getter_AddRefs(dataEvent));
258  NS_ENSURE_SUCCESS(rv, rv);
259 
260  nsString retvalStr;
261  rv = GetRetvalFromEvent(dataEvent, retvalStr);
262  NS_ENSURE_SUCCESS(rv, rv);
263 
264  *aIsPlayingAudio = PR_FALSE;
265 
266  if(retvalStr.EqualsLiteral("true") || retvalStr.EqualsLiteral("1")) {
267  *aIsPlayingAudio = PR_TRUE;
268  }
269 
270  return NS_OK;
271 }
272 
273 /*virtual*/ nsresult
275 {
276  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
277 
278  nsresult rv = SendDOMEvent(NS_LITERAL_STRING("getisplayingvideo"),
279  EmptyString(),
280  getter_AddRefs(dataEvent));
281  NS_ENSURE_SUCCESS(rv, rv);
282 
283  nsString retvalStr;
284  rv = GetRetvalFromEvent(dataEvent, retvalStr);
285  NS_ENSURE_SUCCESS(rv, rv);
286 
287  *aIsPlayingVideo = PR_FALSE;
288 
289  if(retvalStr.EqualsLiteral("true") || retvalStr.EqualsLiteral("1")) {
290  *aIsPlayingVideo = PR_TRUE;
291  }
292 
293  return NS_OK;
294 }
295 
296 /*virtual*/ nsresult
298 {
299  nsresult rv =
300  SendDOMEvent(NS_LITERAL_STRING("play"), EmptyString());
301  NS_ENSURE_SUCCESS(rv, rv);
302 
303  return NS_OK;
304 }
305 
306 /*virtual*/ nsresult
308 {
309  nsresult rv =
310  SendDOMEvent(NS_LITERAL_STRING("pause"), EmptyString());
311  NS_ENSURE_SUCCESS(rv, rv);
312 
313  return NS_OK;
314 }
315 
316 /*virtual*/ nsresult
318 {
319  nsresult rv =
320  SendDOMEvent(NS_LITERAL_STRING("stop"), EmptyString());
321  NS_ENSURE_SUCCESS(rv, rv);
322 
323  rv = DispatchMediacoreEvent(sbIMediacoreEvent::STREAM_STOP);
324  NS_ENSURE_SUCCESS(rv, rv);
325 
326  return NS_OK;
327 }
328 
329 /*virtual*/ nsresult
330 sbMediacoreWrapper::OnSeek(PRUint64 aPosition, PRUint32 aFlags)
331 {
332  return OnSetPosition(aPosition);
333 }
334 
335 //
336 // sbBaseMediacoreVolumeControl overrides
337 //
338 
339 /*virtual*/ nsresult
341 {
342  return NS_OK;
343 }
344 
345 /*virtual*/ nsresult
347 {
348  nsresult rv =
349  SendDOMEvent(NS_LITERAL_STRING("setmute"), sbAutoString(aMute));
350  NS_ENSURE_SUCCESS(rv, rv);
351 
352  return NS_OK;
353 }
354 
355 /*virtual*/ nsresult
357 {
358  nsCString volStr;
359  SB_ConvertFloatVolToJSStringValue(aVolume, volStr);
360 
361  nsresult rv = SendDOMEvent(NS_LITERAL_STRING("setvolume"), volStr);
362  NS_ENSURE_SUCCESS(rv, rv);
363 
364  return NS_OK;
365 }
366 
367 //
368 // sbIMediacoreVotingParticipant
369 //
370 
371 NS_IMETHODIMP
372 sbMediacoreWrapper::VoteWithURI(nsIURI *aURI, PRUint32 *_retval)
373 {
374  NS_ENSURE_ARG_POINTER(aURI);
375  NS_ENSURE_ARG_POINTER(_retval);
376 
377  nsCString uriSpec;
378  nsresult rv = aURI->GetSpec(uriSpec);
379  NS_ENSURE_SUCCESS(rv, rv);
380 
381  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
382  rv = SendDOMEvent(NS_LITERAL_STRING("votewithuri"),
383  uriSpec,
384  getter_AddRefs(dataEvent));
385  NS_ENSURE_SUCCESS(rv, rv);
386 
387  nsString retvalStr;
388  rv = GetRetvalFromEvent(dataEvent, retvalStr);
389  NS_ENSURE_SUCCESS(rv, rv);
390 
391  *_retval = 0;
392 
393  if(!retvalStr.IsEmpty()) {
394  PRUint32 voteResult = static_cast<PRUint32>(retvalStr.ToInteger(&rv, 10));
395  if(NS_SUCCEEDED(rv)) {
396  *_retval = voteResult;
397  }
398  }
399 
400  return NS_OK;
401 }
402 
403 NS_IMETHODIMP
404 sbMediacoreWrapper::VoteWithChannel(nsIChannel *aChannel, PRUint32 *_retval)
405 {
406  return NS_ERROR_NOT_IMPLEMENTED;
407 }
408 
409 //
410 // sbIMediacoreWrapper
411 //
412 
413 NS_IMETHODIMP
414 sbMediacoreWrapper::Initialize(const nsAString &aInstanceName,
415  sbIMediacoreCapabilities *aCapabilities,
416  const nsACString &aChromePageURL)
417 {
418  NS_ENSURE_ARG_POINTER(aCapabilities);
419 
420  nsresult rv = SetInstanceName(aInstanceName);
421  NS_ENSURE_SUCCESS(rv, rv);
422 
423  rv = SetCapabilities(aCapabilities);
424  NS_ENSURE_SUCCESS(rv, rv);
425 
426  nsCOMPtr<sbIPrompter> prompter =
427  do_CreateInstance(SONGBIRD_PROMPTER_CONTRACTID, &rv);
428  NS_ENSURE_SUCCESS(rv, rv);
429 
430  prompter.swap(mPrompter);
431 
432  rv = mPrompter->SetParentWindowType(NS_LITERAL_STRING("Songbird:Main"));
433  NS_ENSURE_SUCCESS(rv, rv);
434 
435  // we don't need to wait for that to show up - it might never show up, if
436  // we're using a non-default layout, for example
437  rv = mPrompter->SetWaitForWindow(PR_FALSE);
438  NS_ENSURE_SUCCESS(rv, rv);
439 
440  nsCOMPtr<nsIDOMWindow> domWindow;
441  rv = mPrompter->OpenWindow(nsnull,
442  NS_ConvertUTF8toUTF16(aChromePageURL),
443  aInstanceName,
444  NS_LITERAL_STRING("chrome,centerscreen"),
445  nsnull,
446  getter_AddRefs(domWindow));
447  NS_ENSURE_SUCCESS(rv, rv);
448 
449  domWindow.swap(mPluginHostWindow);
450 
451  nsCOMPtr<nsIDOMEventTarget> domEventTarget =
452  do_QueryInterface(mPluginHostWindow, &rv);
453  NS_ENSURE_SUCCESS(rv, rv);
454 
455  domEventTarget.swap(mDOMEventTarget);
456 
457  rv = AddSelfDOMListener();
458  NS_ENSURE_SUCCESS(rv, rv);
459 
460  // Wait for window to be ready.
461  nsCOMPtr<nsIThread> target;
462  rv = NS_GetMainThread(getter_AddRefs(target));
463  NS_ENSURE_SUCCESS(rv, rv);
464 
465  PRBool processed = PR_FALSE;
466  while(!mWindowIsReady) {
467  rv = target->ProcessNextEvent(PR_FALSE, &processed);
468  NS_ENSURE_SUCCESS(rv, rv);
469  }
470 
471  nsCOMPtr<nsIDOMDocument> document;
472  rv = mPluginHostWindow->GetDocument(getter_AddRefs(document));
473  NS_ENSURE_SUCCESS(rv, rv);
474 
475  nsCOMPtr<nsIDOMDocumentEvent> documentEvent =
476  do_QueryInterface(document, &rv);
477  NS_ENSURE_SUCCESS(rv, rv);
478 
479  documentEvent.swap(mDocumentEvent);
480 
481  return NS_OK;
482 }
483 
484 //
485 // nsIDOMEventListener
486 //
487 
488 NS_IMETHODIMP
489 sbMediacoreWrapper::HandleEvent(nsIDOMEvent *aEvent)
490 {
491  NS_ENSURE_ARG_POINTER(aEvent);
492 
493  nsString eventType;
494  nsresult rv = aEvent->GetType(eventType);
495  NS_ENSURE_SUCCESS(rv, rv);
496 
497  if(eventType.EqualsLiteral("resize")) {
498  nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mPluginHostWindow, &rv);
499  NS_ENSURE_SUCCESS(rv, rv);
500 
501  rv = target->RemoveEventListener(NS_LITERAL_STRING("resize"), this, PR_FALSE);
502  NS_ENSURE_SUCCESS(rv, rv);
503 
504  mWindowIsReady = PR_TRUE;
505  }
506  else if(eventType.EqualsLiteral("mediacore-buffering-begin")) {
507  rv = DispatchMediacoreEvent(sbIMediacoreEvent::BUFFERING,
508  sbNewVariant(0.0).get());
509  NS_ENSURE_SUCCESS(rv, rv);
510  }
511  else if(eventType.EqualsLiteral("mediacore-buffering-end")) {
512  rv = DispatchMediacoreEvent(sbIMediacoreEvent::STREAM_START);
513  NS_ENSURE_SUCCESS(rv, rv);
514  }
515  else if(eventType.EqualsLiteral("mediacore-error")) {
516  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent =
517  do_QueryInterface(aEvent, &rv);
518  NS_ENSURE_SUCCESS(rv, rv);
519 
520  nsCOMPtr<nsIVariant> codeVariant;
521  rv = dataEvent->GetData(NS_LITERAL_STRING("code"),
522  getter_AddRefs(codeVariant));
523  NS_ENSURE_SUCCESS(rv, rv);
524 
525  PRUint32 code;
526  rv = codeVariant->GetAsUint32(&code);
527  NS_ENSURE_SUCCESS(rv, rv);
528 
529  nsCOMPtr<nsIVariant> messageVariant;
530  rv = dataEvent->GetData(NS_LITERAL_STRING("message"),
531  getter_AddRefs(messageVariant));
532  NS_ENSURE_SUCCESS(rv, rv);
533 
534  nsString message;
535  rv = messageVariant->GetAsAString(message);
536  NS_ENSURE_SUCCESS(rv, rv);
537 
538  nsRefPtr<sbMediacoreError> error;
539  NS_NEWXPCOM(error, sbMediacoreError);
540  NS_ENSURE_TRUE(error, NS_ERROR_OUT_OF_MEMORY);
541 
542  rv = error->Init(code, message);
543  NS_ENSURE_SUCCESS(rv, rv);
544 
545  rv = DispatchMediacoreEvent(sbIMediacoreEvent::ERROR_EVENT, nsnull, error);
546  NS_ENSURE_SUCCESS(rv, rv);
547  }
548  else if(eventType.EqualsLiteral("mediacore-eos")) {
549  rv = DispatchMediacoreEvent(sbIMediacoreEvent::STREAM_END);
550  NS_ENSURE_SUCCESS(rv, rv);
551  }
552  else if(eventType.EqualsLiteral("mediacore-start")) {
553  rv = DispatchMediacoreEvent(sbIMediacoreEvent::STREAM_START);
554  NS_ENSURE_SUCCESS(rv, rv);
555  }
556  else if(eventType.EqualsLiteral("mediacore-pause")) {
557  rv = DispatchMediacoreEvent(sbIMediacoreEvent::STREAM_PAUSE);
558  NS_ENSURE_SUCCESS(rv, rv);
559  }
560  else if(eventType.EqualsLiteral("mediacore-stop")) {
561  rv = DispatchMediacoreEvent(sbIMediacoreEvent::STREAM_STOP);
562  NS_ENSURE_SUCCESS(rv, rv);
563  }
564  else if(eventType.EqualsLiteral("mediacore-metadata")) {
565  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent =
566  do_QueryInterface(aEvent, &rv);
567  NS_ENSURE_SUCCESS(rv, rv);
568 
569  nsCOMPtr<nsIVariant> variant;
570  rv = dataEvent->GetData(NS_LITERAL_STRING("properties"),
571  getter_AddRefs(variant));
572  NS_ENSURE_SUCCESS(rv, rv);
573 
574  rv = DispatchMediacoreEvent(sbIMediacoreEvent::METADATA_CHANGE, variant);
575  NS_ENSURE_SUCCESS(rv, rv);
576  }
577 
578  return NS_OK;
579 }
580 
581 //
582 // sbMediacoreWrapper
583 //
584 nsresult
585 sbMediacoreWrapper::AddSelfDOMListener()
586 {
587  nsresult rv = NS_ERROR_UNEXPECTED;
588  nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mPluginHostWindow, &rv);
589  NS_ENSURE_SUCCESS(rv, rv);
590 
591  rv = target->AddEventListener(NS_LITERAL_STRING("resize"), this, PR_FALSE);
592  NS_ENSURE_SUCCESS(rv, rv);
593 
594  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-error"),
595  this,
596  PR_FALSE);
597  NS_ENSURE_SUCCESS(rv, rv);
598 
599  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-eos"),
600  this,
601  PR_FALSE);
602  NS_ENSURE_SUCCESS(rv, rv);
603 
604  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-start"),
605  this,
606  PR_FALSE);
607  NS_ENSURE_SUCCESS(rv, rv);
608 
609  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-stop"),
610  this,
611  PR_FALSE);
612  NS_ENSURE_SUCCESS(rv, rv);
613 
614  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-pause"),
615  this,
616  PR_FALSE);
617  NS_ENSURE_SUCCESS(rv, rv);
618 
619  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-metadata"),
620  this,
621  PR_FALSE);
622  NS_ENSURE_SUCCESS(rv, rv);
623 
624  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-buffering-begin"),
625  this,
626  PR_FALSE);
627  NS_ENSURE_SUCCESS(rv, rv);
628 
629  rv = target->AddEventListener(NS_LITERAL_STRING("mediacore-buffering-end"),
630  this,
631  PR_FALSE);
632  NS_ENSURE_SUCCESS(rv, rv);
633 
634  return NS_OK;
635 }
636 
637 nsresult
638 sbMediacoreWrapper::RemoveSelfDOMListener()
639 {
640  nsresult rv = NS_ERROR_UNEXPECTED;
641  nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mPluginHostWindow, &rv);
642  NS_ENSURE_SUCCESS(rv, rv);
643 
644  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-error"),
645  this,
646  PR_FALSE);
647  NS_ENSURE_SUCCESS(rv, rv);
648 
649  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-eos"),
650  this,
651  PR_FALSE);
652  NS_ENSURE_SUCCESS(rv, rv);
653 
654  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-start"),
655  this,
656  PR_FALSE);
657  NS_ENSURE_SUCCESS(rv, rv);
658 
659  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-stop"),
660  this,
661  PR_FALSE);
662  NS_ENSURE_SUCCESS(rv, rv);
663 
664  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-pause"),
665  this,
666  PR_FALSE);
667  NS_ENSURE_SUCCESS(rv, rv);
668 
669  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-metadata"),
670  this,
671  PR_FALSE);
672  NS_ENSURE_SUCCESS(rv, rv);
673 
674  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-buffering-begin"),
675  this,
676  PR_FALSE);
677  NS_ENSURE_SUCCESS(rv, rv);
678 
679  rv = target->RemoveEventListener(NS_LITERAL_STRING("mediacore-buffering-end"),
680  this,
681  PR_FALSE);
682  NS_ENSURE_SUCCESS(rv, rv);
683 
684  return NS_OK;
685 }
686 
687 nsresult
688 sbMediacoreWrapper::SendDOMEvent(const nsAString &aEventName,
689  const nsAString &aEventData,
690  nsIDOMDataContainerEvent **aEvent)
691 {
692  nsresult rv = NS_ERROR_UNEXPECTED;
693  PRBool isMainThread = NS_IsMainThread();
694 
695  nsCOMPtr<nsIDOMDocumentEvent> documentEvent;
696  if(isMainThread) {
697  documentEvent = mDocumentEvent;
698  }
699  else {
700  // Scope monitor.
701  {
702  nsAutoMonitor mon(mProxiedObjectsMonitor);
703  if(!mProxiedDocumentEvent) {
704  mon.Exit();
705 
706  nsCOMPtr<nsIThread> target;
707  rv = NS_GetMainThread(getter_AddRefs(target));
708  NS_ENSURE_SUCCESS(rv, rv);
709 
710  rv = do_GetProxyForObject(target,
711  NS_GET_IID(nsIDOMDocumentEvent),
713  NS_PROXY_SYNC | NS_PROXY_ALWAYS,
714  getter_AddRefs(mProxiedDocumentEvent));
715  NS_ENSURE_SUCCESS(rv, rv);
716  }
717  }
718  documentEvent = mProxiedDocumentEvent;
719  }
720 
721  nsCOMPtr<nsIDOMEvent> domEvent;
722  nsCOMPtr<nsIDOMDataContainerEvent> dataEvent;
723 
724  if(isMainThread) {
725  rv = documentEvent->CreateEvent(NS_LITERAL_STRING("DataContainerEvent"),
726  getter_AddRefs(domEvent));
727  NS_ENSURE_SUCCESS(rv, rv);
728 
729  dataEvent = do_QueryInterface(domEvent, &rv);
730  NS_ENSURE_SUCCESS(rv, rv);
731  }
732  else {
733  nsCOMPtr<nsIThread> target;
734  rv = NS_GetMainThread(getter_AddRefs(target));
735  NS_ENSURE_SUCCESS(rv, rv);
736 
737  nsCOMPtr<nsIDOMEvent> tempEvent;
738  rv = documentEvent->CreateEvent(NS_LITERAL_STRING("DataContainerEvent"),
739  getter_AddRefs(tempEvent));
740  NS_ENSURE_SUCCESS(rv, rv);
741 
742  rv = do_GetProxyForObject(target,
743  NS_GET_IID(nsIDOMEvent),
744  tempEvent,
745  NS_PROXY_SYNC | NS_PROXY_ALWAYS,
746  getter_AddRefs(domEvent));
747  NS_ENSURE_SUCCESS(rv, rv);
748 
749  nsCOMPtr<nsIDOMDataContainerEvent> tempDataEvent =
750  do_QueryInterface(domEvent, &rv);
751  NS_ENSURE_SUCCESS(rv, rv);
752 
753  rv = do_GetProxyForObject(target,
754  NS_GET_IID(nsIDOMDataContainerEvent),
755  tempDataEvent,
756  NS_PROXY_SYNC | NS_PROXY_ALWAYS,
757  getter_AddRefs(dataEvent));
758  NS_ENSURE_SUCCESS(rv, rv);
759  }
760 
761  rv = domEvent->InitEvent(aEventName, PR_TRUE, PR_TRUE);
762  NS_ENSURE_SUCCESS(rv, rv);
763 
764  rv = dataEvent->SetData(NS_LITERAL_STRING("data"),
765  sbNewVariant(aEventData).get());
766  NS_ENSURE_SUCCESS(rv, rv);
767 
768  nsCOMPtr<nsISupportsString> retval =
769  do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
770  NS_ENSURE_SUCCESS(rv, rv);
771 
772  rv = dataEvent->SetData(NS_LITERAL_STRING("retval"),
773  sbNewVariant(retval).get());
774  NS_ENSURE_SUCCESS(rv, rv);
775 
776  nsCOMPtr<nsIDOMEventTarget> eventTarget;
777  if(isMainThread) {
778  eventTarget = mDOMEventTarget;
779  }
780  else {
781  // Scope monitor.
782  {
783  nsAutoMonitor mon(mProxiedObjectsMonitor);
785  mon.Exit();
786 
787  nsCOMPtr<nsIThread> target;
788  rv = NS_GetMainThread(getter_AddRefs(target));
789  NS_ENSURE_SUCCESS(rv, rv);
790 
791  rv = do_GetProxyForObject(target,
792  NS_GET_IID(nsIDOMEventTarget),
794  NS_PROXY_SYNC | NS_PROXY_ALWAYS,
795  getter_AddRefs(mProxiedDOMEventTarget));
796  NS_ENSURE_SUCCESS(rv, rv);
797  }
798  }
799  eventTarget = mProxiedDOMEventTarget;
800  }
801 
802  PRBool handled = PR_FALSE;
803  rv = eventTarget->DispatchEvent(dataEvent, &handled);
804  NS_ENSURE_SUCCESS(rv, rv);
805  NS_ENSURE_TRUE(handled, NS_ERROR_UNEXPECTED);
806 
807  if(aEvent) {
808  NS_ADDREF(*aEvent = dataEvent);
809  }
810 
811  return NS_OK;
812 }
813 
814 nsresult
815 sbMediacoreWrapper::SendDOMEvent(const nsAString &aEventName,
816  const nsACString &aEventData,
817  nsIDOMDataContainerEvent **aEvent)
818 {
819  nsresult rv = SendDOMEvent(aEventName,
820  NS_ConvertUTF8toUTF16(aEventData),
821  aEvent);
822  NS_ENSURE_SUCCESS(rv, rv);
823  return NS_OK;
824 }
825 
826 nsresult
827 sbMediacoreWrapper::GetRetvalFromEvent(nsIDOMDataContainerEvent *aEvent,
828  nsAString &aRetval)
829 {
830  nsCOMPtr<nsIVariant> variant;
831  nsresult rv = aEvent->GetData(NS_LITERAL_STRING("retval"),
832  getter_AddRefs(variant));
833  NS_ENSURE_SUCCESS(rv, rv);
834 
835  nsCOMPtr<nsISupports> supports;
836  rv = variant->GetAsISupports(getter_AddRefs(supports));
837  NS_ENSURE_SUCCESS(rv, rv);
838 
839  nsCOMPtr<nsISupportsString> supportsString =
840  do_QueryInterface(supports, &rv);
841  NS_ENSURE_SUCCESS(rv, rv);
842 
843  rv = supportsString->GetData(aRetval);
844  NS_ENSURE_SUCCESS(rv, rv);
845 
846  return NS_OK;
847 }
848 
849 nsresult
850 sbMediacoreWrapper::DispatchMediacoreEvent(PRUint32 aType,
851  nsIVariant *aData,
852  sbIMediacoreError *aError)
853 {
854  nsCOMPtr<sbIMediacoreEvent> event;
855  nsresult rv = sbMediacoreEvent::CreateEvent(aType,
856  aError,
857  aData,
858  this,
859  getter_AddRefs(event));
860  NS_ENSURE_SUCCESS(rv, rv);
861 
862  rv = DispatchEvent(event, PR_TRUE, nsnull);
863  NS_ENSURE_SUCCESS(rv, rv);
864 
865  return NS_OK;
866 }
nsresult InitBaseMediacore()
virtual nsresult OnSetPosition(PRUint64 aPosition)
Songbird Mediacore Error Definition.
virtual nsresult OnGetCapabilities()
return NS_OK
nsCOMPtr< nsIDOMEventTarget > mProxiedDOMEventTarget
virtual nsresult OnPause()
PRUint64 nsString_ToUint64(const nsAString &str, nsresult *rv)
virtual nsresult OnSetMute(PRBool aMute)
void SB_ConvertFloatVolToJSStringValue(PRFloat64 aVol, nsACString &aStrVol)
var event
PRMonitor * mProxiedObjectsMonitor
nsresult SetCapabilities(sbIMediacoreCapabilities *aCapabilities)
NS_IMPL_THREADSAFE_CI(sbMediaListEnumeratorWrapper)
const unsigned long ERROR_EVENT
Indicates the event is an error and will have its error member set.
nsCOMPtr< nsIDOMEventTarget > mDOMEventTarget
nsresult do_GetProxyForObject(nsIEventTarget *aTarget, REFNSIID aIID, nsISupports *aObj, PRInt32 aProxyType, void **aProxyObject)
const unsigned long METADATA_CHANGE
Metadata describing current item has changed.
virtual nsresult OnInitBaseMediacore()
Songbird Variant Utility Definitions.
#define SONGBIRD_PROMPTER_CONTRACTID
const unsigned long STREAM_STOP
Stream was stopped.
virtual nsresult OnShutdown()
NS_IMPL_THREADSAFE_RELEASE(sbRequestItem)
virtual nsresult OnPlay()
Songbird Mediacore Wrapper Definition.
NS_IMPL_THREADSAFE_ADDREF(sbRequestItem)
virtual nsresult OnGetDuration(PRUint64 *aDuration)
const unsigned long BUFFERING
Buffering.
nsCOMPtr< sbIPrompter > mPrompter
const unsigned long STREAM_START
Stream has started.
nsresult SetInstanceName(const nsAString &aInstanceName)
virtual nsresult OnSeek(PRUint64 aPosition, PRUint32 aFlag)
readonly attribute unsigned long long duration
const nsIChannel
virtual nsresult OnInitBaseMediacorePlaybackControl()
nsCOMPtr< nsIDOMWindow > mPluginHostWindow
attribute unsigned long long position
virtual nsresult OnSetUri(nsIURI *aURI)
virtual nsresult OnGetIsPlayingVideo(PRBool *aIsPlayingVideo)
Songbird Thread Utilities Definitions.
virtual nsresult OnInitBaseMediacoreVolumeControl()
PRPackedBool mWindowIsReady
GstMessage * message
static nsresult CreateEvent(PRUint32 aType, sbIMediacoreError *aError, nsIVariant *aData, sbIMediacore *aOrigin, sbIMediacoreEvent **retval)
const unsigned long STREAM_PAUSE
Stream is now paused.
virtual nsresult OnGetPosition(PRUint64 *aPosition)
virtual nsresult OnSetVolume(PRFloat64 aVolume)
sbIJobCancelable NS_DECL_CLASSINFO(sbGstreamerMediaInspector)
NS_IMPL_QUERY_INTERFACE8_CI(sbMediacoreWrapper, sbIMediacore, sbIMediacorePlaybackControl, sbIMediacoreVolumeControl, sbIMediacoreVotingParticipant, sbIMediacoreEventTarget, sbIMediacoreWrapper, nsIDOMEventListener, nsIClassInfo) NS_IMPL_CI_INTERFACE_GETTER7(sbMediacoreWrapper
#define TRACE(args)
nsCOMPtr< nsIDOMDocumentEvent > mDocumentEvent
virtual nsresult OnGetIsPlayingAudio(PRBool *aIsPlayingAudio)
nsCOMPtr< nsIDOMDocumentEvent > mProxiedDocumentEvent
const unsigned long STREAM_END
End of stream.
virtual nsresult OnStop()
nsIDOMEventListener
_updateTextAndScrollDataForFrame aData