34 #include <sbIRemoteMediaList.h>
35 #include <sbIMediaItem.h>
36 #include <sbIMediaList.h>
37 #include <sbIMediaListView.h>
38 #include <sbIMediaListViewTreeView.h>
41 #include <sbIPropertyManager.h>
45 #include <nsAutoPtr.h>
46 #include <nsITreeSelection.h>
47 #include <nsITreeView.h>
48 #include <nsStringGlue.h>
52 #include <nsNetUtil.h>
53 #include <nsIXPConnect.h>
56 #include <sbIPropertyArray.h>
57 #include <nsServiceManagerUtils.h>
58 #include <nsIScriptSecurityManager.h>
65 PRLogModuleInfo* gRemoteMediaListLog = nsnull;
69 #define LOG(args) LOG_LIST(args)
71 #define SB_ENSURE_WITH_JSTHROW( _cx, _rv, _msg ) \
72 if ( NS_FAILED(_rv) ) { \
73 ThrowJSException( _cx, NS_LITERAL_CSTRING(_msg) ); \
92 mRemotePlayer(aRemotePlayer),
93 mMediaList(aMediaList),
94 mMediaListView(aMediaListView)
96 NS_ASSERTION(aRemotePlayer,
"Null remote player!");
97 NS_ASSERTION(aMediaList,
"Null media list!");
98 NS_ASSERTION(aMediaListView,
"Null media list view!");
100 mMediaItem = do_QueryInterface(mMediaList);
101 NS_ASSERTION(mMediaItem,
"Could not QI media list to media item");
103 mMediaItem->GetLibrary(getter_AddRefs(mLibrary));
106 if (!gRemoteMediaListLog) {
107 gRemoteMediaListLog = PR_NewLogModule(
"sbRemoteMediaList");
109 LOG_LIST((
"sbRemoteMediaListBase::sbRemoteMediaListBase()"));
115 LOG_LIST((
"sbRemoteMediaListBase::~sbRemoteMediaListBase()"));
125 sbRemoteMediaListBase::GetRemotePlayer(
sbIRemotePlayer * *aRemotePlayer)
128 NS_ENSURE_ARG_POINTER(aRemotePlayer);
131 *aRemotePlayer = nsnull;
136 NS_ENSURE_SUCCESS( rv, rv );
138 remotePlayer.swap( *aRemotePlayer );
149 already_AddRefed<sbIMediaItem>
154 nsCOMPtr<sbIMediaItem> mediaItem = do_QueryInterface(
mMediaList, &rv);
155 NS_ASSERTION(mediaItem,
"Could not QI list to item");
157 return mediaItem.forget();
160 already_AddRefed<sbIMediaList>
177 LOG_LIST((
"sbRemoteMediaListBase::GetClassName()"));
178 NS_ENSURE_ARG_POINTER(aClassName);
179 *aClassName = ToNewCString( NS_LITERAL_CSTRING(
"SongbirdMediaList") );
180 NS_ENSURE_TRUE( aClassName, NS_ERROR_OUT_OF_MEMORY );
187 LOG_LIST((
"sbRemoteMediaListBase::GetScriptableFlags()"));
188 NS_ENSURE_ARG_POINTER(aScriptableFlags);
189 *aScriptableFlags = WANT_NEWRESOLVE |
190 ALLOW_PROP_MODS_DURING_RESOLVE |
191 DONT_ENUM_STATIC_PROPS |
192 DONT_ENUM_QUERY_INTERFACE |
193 DONT_REFLECT_INTERFACE_NAMES ;
206 LOG_LIST((
"sbRemoteMediaListBase::NewResolve()"));
207 NS_ENSURE_ARG_POINTER(_retval);
208 NS_ENSURE_ARG_POINTER(objp);
210 if ( JSVAL_IS_STRING(
id) ) {
211 nsDependentString jsid( (PRUnichar *)
212 ::JS_GetStringChars( JSVAL_TO_STRING(
id) ),
213 ::JS_GetStringLength( JSVAL_TO_STRING(
id) ) );
215 TRACE_LIB((
" resolving %s", NS_LossyConvertUTF16toASCII(jsid).
get() ));
219 if ( jsid.EqualsLiteral(
"add") ) {
220 JSString *
str = JSVAL_TO_STRING(
id);
221 JSFunction *fnc = ::JS_DefineFunction( cx,
223 ::JS_GetStringBytes(str),
230 return fnc ?
NS_OK : NS_ERROR_UNEXPECTED;
239 const nsACString &aExceptionMsg ) {
240 JSAutoRequest ar(cx);
242 JSString *
str = JS_NewStringCopyN( cx,
243 aExceptionMsg.BeginReading(),
244 aExceptionMsg.Length() );
250 JS_SetPendingException( cx, STRING_TO_JSVAL(str) );
293 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argc is %d", argc ));
307 OBJ_TO_INNER_OBJECT(cx, obj);
309 nsCOMPtr<nsIXPConnect> xpc(
310 do_GetService(
"@mozilla.org/js/xpc/XPConnect;1", &rv ) );
313 nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
314 rv = xpc->GetWrappedNativeOfJSObject( cx, obj, getter_AddRefs(wrapper) );
317 nsCOMPtr<nsISecurityCheckedComponent> checkedComponent(
318 do_QueryWrappedNative( wrapper, &rv ) );
324 rv = checkedComponent->CanCallMethod( &iid,
325 NS_LITERAL_STRING(
"add").
get(),
329 PRBool canCallMethod = NS_SUCCEEDED(rv);
332 canCallMethod = PR_FALSE;
334 canCallMethod = !strcmp( access,
"AllAccess" );
339 if (!canCallMethod) {
340 ThrowJSException( cx, NS_LITERAL_CSTRING(
"Permission Denied to call method RemoteMediaList.add()") );
350 nsCOMPtr<sbIMediaItem> selfItem( do_QueryWrappedNative( wrapper, &rv ) );
355 PRBool isTargetMain = PR_FALSE;
356 rv =
SB_IsFromLibName( selfItem, NS_LITERAL_STRING(
"main"), &isTargetMain );
362 JSBool shouldDownload = JS_FALSE;
364 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[1] exists, make sure it's not an item"));
365 if ( JSVAL_IS_OBJECT( argv[1] ) ) {
368 nsCOMPtr<nsIXPConnectWrappedNative> wn;
369 rv = xpc->GetWrappedNativeOfJSObject(cx, JSVAL_TO_OBJECT( argv[1] ), getter_AddRefs(wn) );
370 if ( NS_SUCCEEDED(rv) && wn ) {
372 nsCOMPtr<sbIMediaItem> tempItem;
373 tempItem = do_QueryWrappedNative( wn, &rv );
374 if ( NS_SUCCEEDED(rv) ) {
376 ThrowJSException( cx, NS_LITERAL_CSTRING(
"Second arg should NOT be a media item.") );
382 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[1] exists, is not a mediaitem"));
383 if (!JS_ValueToBoolean(cx, argv[1], &shouldDownload)) {
384 shouldDownload = JS_FALSE;
387 if ( shouldDownload && !isTargetMain ) {
389 shouldDownload = PR_FALSE;
390 NS_WARNING((
"Non-Fatal Error: Tried to download an item to non-mainLibrary."));
396 nsCOMPtr<sbISecurityAggregator> secAgg( do_QueryInterface( selfItem, &rv ) );
401 rv = secAgg->GetRemotePlayer( getter_AddRefs(remotePlayer) );
405 nsCOMPtr<sbIMediaItem> item;
407 JSAutoRequest ar(cx);
411 if ( JSVAL_IS_STRING(argv[0]) ) {
416 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[0] exists, is a string"));
417 LOG_LIST((
" length: %d", ::JS_GetStringLength( JSVAL_TO_STRING(argv[0]) ) ) );
419 if ( ::JS_GetStringLength( JSVAL_TO_STRING(argv[0]) ) == 0 ) {
425 nsDependentString
url( (PRUnichar *)
426 ::JS_GetStringChars( JSVAL_TO_STRING(argv[0]) ),
427 ::JS_GetStringLength( JSVAL_TO_STRING(argv[0]) ) );
429 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[0] exists, is a string"));
430 LOG_LIST((
" str %s", NS_LossyConvertUTF16toASCII(url).
get() ));
433 nsCOMPtr<nsIURI>
uri;
434 rv = NS_NewURI( getter_AddRefs(uri), url );
439 nsCOMPtr<sbIRemoteLibrary>
library;
442 if ( isTargetMain ) {
444 rv = remotePlayer->GetMainLibrary( getter_AddRefs(library) );
447 PRBool isTargetWeb = PR_FALSE;
451 if ( !isTargetMain ) {
453 rv = remotePlayer->GetWebLibrary( getter_AddRefs(library) );
457 rv = remotePlayer->GetSiteLibrary( getter_AddRefs(library) );
463 rv = uri->GetSpec(uriCStr);
467 rv = library->CreateMediaItem( NS_ConvertUTF8toUTF16(uriCStr),
468 getter_AddRefs(item) );
471 }
else if ( JSVAL_IS_OBJECT(argv[0]) ) {
472 JSObject* jsobj = JSVAL_TO_OBJECT(argv[0]);
478 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[0] exists, is an object"));
480 if ( JS_IsArrayObject(cx, jsobj) ) {
481 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[0] exists, is an array"));
486 if ( JS_GetArrayLength( cx, jsobj, &len ) ) {
487 for ( JSUint32 index = 0; index < len; index++ ) {
488 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - checking index; %d.", index));
489 if ( JS_GetElement( cx, jsobj, index, &val ) ) {
490 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - found a val in the array."));
492 if ( JSVAL_IS_STRING(val) ) {
493 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - found a val in the array, recursing"));
496 uintN
length = (shouldDownload ? 2 : 1);
497 jsval *newval = (jsval*) JS_malloc( cx,
sizeof(jsval) *
length );
499 if (shouldDownload) {
500 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - should download, setting value"));
501 newval[1] = BOOLEAN_TO_JSVAL(shouldDownload);
503 if ( !
AddHelper( cx, obj, length, newval, rval ) ) {
504 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - failed return from recursing"));
506 JS_free( cx, newval );
509 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - successful return from recursing"));
510 JS_free( cx, newval );
512 ThrowJSException( cx, NS_LITERAL_CSTRING(
"Arrays should only contain strings.") );
519 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[0] exists, is an object, not an array"));
522 nsCOMPtr<nsIXPConnectWrappedNative> wn;
523 rv = xpc->GetWrappedNativeOfJSObject(cx, JSVAL_TO_OBJECT( argv[0] ), getter_AddRefs(wn) );
526 item = do_QueryWrappedNative( wn, &rv );
531 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - argv[0] not a string, or object, fail silently"));
541 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - Have item to add."));
543 nsCOMPtr<sbIMediaList> selfList( do_QueryWrappedNative( wrapper, &rv ) );
547 if (shouldDownload) {
548 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - Downloading item."));
551 PRBool isItemMain = PR_FALSE;
558 NS_LITERAL_STRING(
"1") );
561 rv = remotePlayer->DownloadItem(item);
566 NS_WARNING((
"sbRemoteMediaListBase::AddHelper() - Failed to download."));
569 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - Not downloading"));
572 rv = selfList->Add(item);
573 if ( NS_FAILED(rv) ) {
574 NS_WARNING((
"sbRemoteMediaListBase::AddHelper() - Failed to add the item."));
578 LOG_LIST((
"sbRemoteMediaListBase::AddHelper() - returning JS_TRUE"));
592 NS_ENSURE_ARG_POINTER(_retval);
594 nsCOMPtr<sbIMediaItem> item;
595 nsresult rv =
mMediaList->GetItemByGuid(aGuid, getter_AddRefs(item));
596 if (rv == NS_ERROR_NOT_AVAILABLE) {
602 NS_ENSURE_SUCCESS(rv, rv);
611 NS_ENSURE_ARG_POINTER(_retval);
613 nsCOMPtr<sbIMediaItem> item;
614 nsresult rv =
mMediaList->GetItemByIndex(aIndex, getter_AddRefs(item));
615 if (rv == NS_ERROR_NOT_AVAILABLE) {
621 NS_ENSURE_SUCCESS(rv, rv);
628 const nsAString & aPropertyValue,
631 return NS_ERROR_NOT_IMPLEMENTED;
637 return NS_ERROR_NOT_IMPLEMENTED;
642 PRUint16 aEnumerationType)
644 NS_ENSURE_ARG_POINTER(aEnumerationListener);
646 nsRefPtr<sbMediaListEnumerationListenerWrapper> wrapper(
648 NS_ENSURE_TRUE(wrapper, NS_ERROR_OUT_OF_MEMORY);
650 nsresult rv =
mMediaList->EnumerateAllItems(wrapper, aEnumerationType);
651 NS_ENSURE_SUCCESS(rv, rv);
658 const nsAString& aPropertyValue,
660 PRUint16 aEnumerationType)
662 NS_ENSURE_ARG_POINTER(aEnumerationListener);
664 nsRefPtr<sbMediaListEnumerationListenerWrapper> wrapper(
666 NS_ENSURE_TRUE(wrapper, NS_ERROR_OUT_OF_MEMORY);
668 nsresult rv =
mMediaList->EnumerateItemsByProperty(aPropertyID,
672 NS_ENSURE_SUCCESS(rv, rv);
682 NS_ENSURE_ARG_POINTER(aMediaItem);
683 NS_ENSURE_ARG_POINTER(_retval);
685 nsCOMPtr<sbIMediaItem> mediaItem;
688 getter_AddRefs(mediaItem));
689 NS_ENSURE_SUCCESS(rv, rv);
691 return mMediaList->IndexOf(mediaItem, aStartFrom, _retval);
699 NS_ENSURE_ARG_POINTER(aMediaItem);
700 NS_ENSURE_ARG_POINTER(_retval);
702 nsCOMPtr<sbIMediaItem> mediaItem;
705 getter_AddRefs(mediaItem));
706 NS_ENSURE_SUCCESS(rv, rv);
708 return mMediaList->LastIndexOf(mediaItem, aStartFrom, _retval);
714 NS_ENSURE_ARG_POINTER(aMediaItem);
715 NS_ENSURE_ARG_POINTER(_retval);
717 nsCOMPtr<sbIMediaItem> mediaItem;
720 getter_AddRefs(mediaItem));
721 NS_ENSURE_SUCCESS(rv, rv);
723 return mMediaList->Contains(mediaItem, _retval);
729 return AddItem(aMediaItem, nsnull);
736 LOG_LIST((
"sbRemoteMediaListBase::Add()"));
737 NS_ENSURE_ARG_POINTER(aMediaItem);
740 nsCOMPtr<sbIWrappedMediaItem> wrappedMediaItem =
741 do_QueryInterface(aMediaItem, &rv);
742 NS_ENSURE_SUCCESS(rv, rv);
744 nsCOMPtr<sbIMediaItem> internalMediaItem = wrappedMediaItem->GetMediaItem();
745 NS_ENSURE_TRUE(internalMediaItem, NS_ERROR_FAILURE);
747 rv =
mMediaList->AddItem(internalMediaItem, aNewItem);
748 if (NS_SUCCEEDED(rv)) {
749 LOG_LIST((
"sbRemoteMediaListBase::Add() - added the item"));
753 NS_ENSURE_SUCCESS(rv, rv);
761 NS_ENSURE_ARG_POINTER(aMediaList);
764 nsCOMPtr<sbIWrappedMediaList> wrappedMediaList =
765 do_QueryInterface(aMediaList, &rv);
766 NS_ENSURE_SUCCESS(rv, rv);
768 nsCOMPtr<sbIMediaList> internalMediaList = wrappedMediaList->GetMediaList();
769 NS_ENSURE_TRUE(internalMediaList, NS_ERROR_FAILURE);
772 if (NS_SUCCEEDED(rv)) {
776 NS_ENSURE_SUCCESS(rv, rv);
792 NS_ENSURE_ARG_POINTER(aMediaItems);
796 NS_ENSURE_FALSE(aAsync, NS_ERROR_NOT_IMPLEMENTED);
798 nsRefPtr<sbUnwrappingSimpleEnumerator> wrapper(
800 NS_ENSURE_TRUE(wrapper, NS_ERROR_OUT_OF_MEMORY);
802 nsresult rv =
mMediaList->AddMediaItems(wrapper, aListener, aAsync);
803 if (NS_SUCCEEDED(rv)) {
807 NS_ENSURE_SUCCESS(rv, rv);
815 NS_ENSURE_ARG_POINTER(aMediaItem);
816 LOG_LIST((
"sbRemoteMediaListBase::Remove()"));
819 nsCOMPtr<sbIWrappedMediaItem> wrappedMediaItem =
820 do_QueryInterface(aMediaItem, &rv);
821 NS_ENSURE_SUCCESS(rv, rv);
823 nsCOMPtr<sbIMediaItem> internalMediaItem = wrappedMediaItem->GetMediaItem();
824 NS_ENSURE_TRUE(internalMediaItem, NS_ERROR_FAILURE);
827 if (NS_SUCCEEDED(rv)) {
831 NS_ENSURE_SUCCESS(rv, rv);
840 NS_ENSURE_ARG_POINTER(_retval);
841 LOG_LIST((
"sbRemoteMediaListBase::GetDistinctValuesForProperty()"));
844 nsCOMPtr<nsIStringEnumerator> enumeration;
846 mMediaList->GetDistinctValuesForProperty( aPropertyID,
847 getter_AddRefs(enumeration) );
848 NS_ENSURE_SUCCESS( rv, rv );
850 nsRefPtr<sbRemoteWrappingStringEnumerator> wrapped(
852 NS_ENSURE_TRUE( wrapped, NS_ERROR_OUT_OF_MEMORY );
854 rv = wrapped->Init();
855 NS_ENSURE_SUCCESS( rv, rv );
857 NS_ADDREF( *_retval = wrapped );
871 LOG_LIST((
"sbRemoteMediaListBase::GetView()"));
872 NS_ENSURE_ARG_POINTER(aView);
890 mRemotePlayer(aRemotePlayer),
893 NS_ASSERTION(aRemotePlayer,
"Null remote player!");
894 NS_ASSERTION(aWrapped,
"Null wrapped enumerator!");
898 sbMediaListEnumerationListenerWrapper::OnEnumerationBegin(
sbIMediaList *aMediaList,
901 NS_ENSURE_ARG_POINTER(aMediaList);
902 NS_ENSURE_ARG_POINTER(_retval);
904 nsCOMPtr<sbIMediaList> mediaList;
907 getter_AddRefs(mediaList));
908 NS_ENSURE_SUCCESS(rv, rv);
910 return mWrapped->OnEnumerationBegin(mediaList, _retval);
914 sbMediaListEnumerationListenerWrapper::OnEnumeratedItem(
sbIMediaList *aMediaList,
918 NS_ENSURE_ARG_POINTER(aMediaList);
919 NS_ENSURE_ARG_POINTER(aMediaItem);
920 NS_ENSURE_ARG_POINTER(_retval);
922 nsCOMPtr<sbIMediaList> mediaList;
925 getter_AddRefs(mediaList));
926 NS_ENSURE_SUCCESS(rv, rv);
928 nsCOMPtr<sbIMediaItem> mediaItem;
929 rv =
SB_WrapMediaItem(mRemotePlayer, aMediaItem, getter_AddRefs(mediaItem));
930 NS_ENSURE_SUCCESS(rv, rv);
932 return mWrapped->OnEnumeratedItem(mediaList, mediaItem, _retval);
936 sbMediaListEnumerationListenerWrapper::OnEnumerationEnd(
sbIMediaList *aMediaList,
937 nsresult aStatusCode)
939 NS_ENSURE_ARG_POINTER(aMediaList);
941 nsCOMPtr<sbIMediaList> mediaList;
944 getter_AddRefs(mediaList));
945 NS_ENSURE_SUCCESS(rv, rv);
947 return mWrapped->OnEnumerationEnd(mediaList, aStatusCode);;
961 NS_ASSERTION(aWrapped,
"Null wrapped enumerator!");
965 sbUnwrappingSimpleEnumerator::HasMoreElements(PRBool *_retval)
967 NS_ENSURE_ARG_POINTER(_retval);
969 return mWrapped->HasMoreElements(_retval);
973 sbUnwrappingSimpleEnumerator::GetNext(
nsISupports **_retval)
975 NS_ENSURE_ARG_POINTER(_retval);
979 nsCOMPtr<nsISupports> supports;
980 rv = mWrapped->GetNext(getter_AddRefs(supports));
981 NS_ENSURE_SUCCESS(rv, rv);
984 nsCOMPtr<sbIIndexedMediaItem> indexedMediaItem = do_QueryInterface(supports, &rv);
985 if (NS_SUCCEEDED(rv)) {
986 nsCOMPtr<sbIMediaItem> item;
987 indexedMediaItem->GetMediaItem( getter_AddRefs(item) );
988 NS_ENSURE_SUCCESS( rv, rv );
991 supports = do_QueryInterface( item, &rv );
992 NS_ENSURE_SUCCESS( rv, rv );
995 nsCOMPtr<sbIWrappedMediaList> mediaList = do_QueryInterface(supports, &rv);
996 if (NS_SUCCEEDED(rv)) {
997 *_retval = mediaList->GetMediaList().get();
1001 nsCOMPtr<sbIWrappedMediaItem> mediaItem = do_QueryInterface(supports, &rv);
1002 if (NS_SUCCEEDED(rv)) {
1003 *_retval = mediaItem->GetMediaItem().get();
1007 return NS_ERROR_UNEXPECTED;
static nsresult SB_WrapMediaList(sbRemotePlayer *aRemotePlayer, sbIMediaList *aMediaList, sbIMediaList **aRemoteMediaList)
sbDeviceFirmwareAutoCheckForUpdate prototype flags
readonly attribute sbIRemotePlayer remotePlayer
This interface is a composition of many of our other interfaces with the goal of exposing a more web-...
nsISecurityCheckedComponent
static nsresult SB_IsFromLibName(sbIMediaItem *aMediaItem, const nsAString &aLibName, PRBool *aIsFromLib)
#define SB_PROPERTY_ENABLE_AUTO_DOWNLOAD
A marker interface for objects that aggregate the security mixin.
General interface to data resources.
this _dialogInput val(dateText)
static nsresult SB_WrapMediaItem(sbRemotePlayer *aRemotePlayer, sbIMediaItem *aMediaItem, sbIMediaItem **aRemoteMediaItem)