sbRemoteCommands.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 
25 #include "sbRemoteCommands.h"
26 #include "sbSecurityMixin.h"
27 #include <sbClassInfoUtils.h>
28 
29 #include <nsAutoPtr.h>
30 #include <nsIDOMElement.h>
31 #include <nsIDocument.h>
32 #include <nsIDOMDocument.h>
33 #include <nsIDOMEvent.h>
34 #include <nsIDOMEventTarget.h>
35 #include <nsIDOMWindow.h>
36 #include <nsIURI.h>
37 #include <nsServiceManagerUtils.h>
38 #include <nsStringGlue.h>
39 #include <prlog.h>
40 
41 /*
42  * To log this module, set the following environment variable:
43  * NSPR_LOG_MODULES=sbRemoteCommands:5
44  */
45 #ifdef PR_LOGGING
46 static PRLogModuleInfo* gRemoteCommandsLog = nsnull;
47 #endif
48 
49 #undef LOG
50 #define LOG(args) PR_LOG(gRemoteCommandsLog, PR_LOG_WARN, args)
51 
52 const static char* sPublicWProperties[] = { "" };
53 
54 const static char* sPublicRProperties[] =
55  { // nsIClassInfo
56  "classinfo:classDescription",
57  "classinfo:contractID",
58  "classinfo:classID",
59  "classinfo:implementationLanguage",
60  "classinfo:flags" };
61 
62 const static char* sPublicMethods[] =
63  { // sbIPlaylistCommands
64  "site:getNumCommands",
65  "site:getCommandType",
66  "site:getCommandId",
67  "site:getCommandText",
68  "site:getCommandToolTipText",
69  "site:getCommandEnabled",
70  "site:getCommandFlag",
71  "site:getCommandVisible",
72  "site:getCommandValue",
73  "site:getCommandShortcutModifiers",
74  "site:getCommandShortcutKey",
75  "site:getCommandShortcutKeycode",
76  "site:getCommandShortcutLocal",
77 
78  // sbIRemoteCommands
79  "site:addCommand",
80  "site:removeCommand",
81  "site:setCommandData"
82  };
83 
90 
91 NS_IMPL_CI_INTERFACE_GETTER4( sbRemoteCommands,
96 
97 SB_IMPL_CLASSINFO_INTERFACES_ONLY(sbRemoteCommands)
98 
100 
101 sbRemoteCommands::sbRemoteCommands(sbRemotePlayer* aRemotePlayer) :
102  mRemotePlayer(aRemotePlayer)
103 {
104 #ifdef PR_LOGGING
105  if (!gRemoteCommandsLog) {
106  gRemoteCommandsLog = PR_NewLogModule("sbRemoteCommands");
107  }
108  LOG(("sbRemoteCommands::sbRemoteCommands()"));
109 #endif
110 }
111 
113 {
114  LOG(("sbRemoteCommands::~sbRemoteCommands()"));
115 }
116 
117 // ---------------------------------------------------------------------------
118 //
119 // sbISecurityAggregator
120 //
121 // ---------------------------------------------------------------------------
122 
123 NS_IMETHODIMP
124 sbRemoteCommands::GetRemotePlayer(sbIRemotePlayer * *aRemotePlayer)
125 {
126  NS_ENSURE_STATE(mWeakOwner);
127  NS_ENSURE_ARG_POINTER(aRemotePlayer);
128 
129  *aRemotePlayer = nsnull;
130 
131  nsCOMPtr<sbIRemotePlayer> remotePlayer( do_QueryReferent(mWeakOwner) );
132  NS_ENSURE_TRUE( remotePlayer, NS_ERROR_FAILURE );
133 
134  remotePlayer.swap( *aRemotePlayer );
135 
136  return NS_OK;
137 }
138 
139 // ---------------------------------------------------------------------------
140 //
141 // sbIRemoteCommands
142 //
143 // ---------------------------------------------------------------------------
144 
145 
146 NS_IMETHODIMP
147 sbRemoteCommands::SetCommandData( PRUint32 aNumCommands,
148  const PRUnichar **aTypeArray,
149  const PRUnichar **aIDArray,
150  const PRUnichar **aNameArray,
151  const PRUnichar **aTooltipArray )
152 {
153  LOG(( "sbRemoteCommands::SetCommandData(%d)", aNumCommands ));
154 
155  // holy argument checking Batman!!
156  NS_ENSURE_ARG_POINTER(aTypeArray);
157  NS_ENSURE_ARG_POINTER(aIDArray);
158  NS_ENSURE_ARG_POINTER(aNameArray);
159  NS_ENSURE_ARG_POINTER(aTooltipArray);
160 
161  for ( PRUint32 index = 0; index < aNumCommands; index++ ) {
162  // check in debug only
163  NS_ASSERTION( aTypeArray[index], "ERROR - null array element" );
164  NS_ASSERTION( aIDArray[index], "ERROR - null array element" );
165  NS_ASSERTION( aNameArray[index], "ERROR - null array element" );
166  NS_ASSERTION( aTooltipArray[index], "ERROR - null array element" );
167 
168  sbCommand command;
169  command.type = aTypeArray[index];
170  command.id = aIDArray[index];
171  command.name = aNameArray[index];
172  command.tooltip = aTooltipArray[index];
173  if ( !mCommands.AppendElement(command) ) {
174  // AppendElement copies the object so if it fails we are:
175  return NS_ERROR_OUT_OF_MEMORY;
176  }
177  }
179  return NS_OK;
180 }
181 
182 NS_IMETHODIMP
183 sbRemoteCommands::AddCommand( const nsAString &aType,
184  const nsAString &aID,
185  const nsAString &aName,
186  const nsAString &aTooltip )
187 {
188  LOG(( "sbRemoteCommands::AddCommand(%s)",
189  NS_LossyConvertUTF16toASCII(aID).get() ));
190  sbCommand command;
191  command.type = aType;
192  command.id = aID;
193  command.name = aName;
194  command.tooltip = aTooltip;
195 
196  if ( !mCommands.AppendElement(command) )
197  return NS_ERROR_OUT_OF_MEMORY;
199  return NS_OK;
200 }
201 
202 NS_IMETHODIMP
203 sbRemoteCommands::RemoveCommand( const nsAString &aID )
204 {
205  LOG(("sbRemoteCommands::RemoveCommand()"));
206  PRUint32 num = mCommands.Length();
207  for ( PRUint32 index = 0; index < num; index++ ) {
208  LOG(( "sbRemoteCommands::RemoveCommand(%d:%s)",
209  index,
210  NS_LossyConvertUTF16toASCII(mCommands.ElementAt(index).id).get()));
211  if ( mCommands.ElementAt(index).id == aID ) {
212  mCommands.RemoveElementAt(index);
214  return NS_OK;
215  }
216  }
217  // XXXredfive check an error code here and log a warning if the command
218  // isn't found
219  return NS_OK;
220 }
221 
222 void
224 {
225  nsCOMPtr<sbIRemotePlayer> owner( do_QueryReferent(mWeakOwner) );
226  if (owner)
227  owner->OnCommandsChanged();
228 }
229 
230 NS_IMETHODIMP
231 sbRemoteCommands::SetOwner( sbIRemotePlayer *aOwner )
232 {
233  LOG(("sbRemoteCommands::SetOwner()"));
234  nsresult rv;
235  mWeakOwner = do_GetWeakReference( aOwner, &rv );
236  return rv;
237 }
238 
239 NS_IMETHODIMP
240 sbRemoteCommands::GetOwner( sbIRemotePlayer **aOwner )
241 {
242  LOG(("sbRemoteCommands::GetOwner()"));
243  nsresult rv;
244  nsCOMPtr<sbIRemotePlayer> strong = do_QueryReferent( mWeakOwner, &rv );
245  NS_IF_ADDREF( *aOwner = strong );
246  return rv;
247 }
248 
249 // ---------------------------------------------------------------------------
250 //
251 // sbIPlaylistCommands
252 //
253 // ---------------------------------------------------------------------------
254 
255 /* void addListener (in sbIPlaylistCommandsListener listener); */
256 NS_IMETHODIMP
257 sbRemoteCommands::AddListener(sbIPlaylistCommandsListener *listener)
258 {
259  return NS_ERROR_NOT_IMPLEMENTED;
260 }
261 
262 /* void removeListener (in sbIPlaylistCommandsListener listener); */
263 NS_IMETHODIMP
264 sbRemoteCommands::RemoveListener(sbIPlaylistCommandsListener *listener)
265 {
266  return NS_ERROR_NOT_IMPLEMENTED;
267 }
268 
269 NS_IMETHODIMP
270 sbRemoteCommands::NotifyListeners(const nsAString &aTriggerFunction,
271  sbIPlaylistCommands *aCommandObject)
272 {
273  return NS_ERROR_NOT_IMPLEMENTED;
274 }
275 
276 NS_IMETHODIMP
277 sbRemoteCommands::AddCommandObject(sbIPlaylistCommands *aCommandObject)
278 {
279  return NS_ERROR_NOT_IMPLEMENTED;
280 }
281 
282 NS_IMETHODIMP
283 sbRemoteCommands::RemoveCommandObject(sbIPlaylistCommands *aCommandObject)
284 {
285  return NS_ERROR_NOT_IMPLEMENTED;
286 }
287 
288 NS_IMETHODIMP
289 sbRemoteCommands::SetContext( sbIPlaylistCommandsContext *aContext)
290 {
291  LOG(("sbRemoteCommands::SetContext()"));
292  mContext = aContext;
293  return NS_OK;
294 }
295 
296 NS_IMETHODIMP
297 sbRemoteCommands::GetVisible( const nsAString &aHost, PRBool *_retval)
298 {
299  NS_ENSURE_ARG_POINTER(_retval);
300  *_retval = PR_TRUE;
301  return NS_OK;
302 }
303 
304 NS_IMETHODIMP
305 sbRemoteCommands::GetNumCommands( const nsAString &aSubMenu,
306  const nsAString &aHost,
307  PRInt32 *_retval )
308 {
309  NS_ENSURE_ARG_POINTER(_retval);
310  *_retval = mCommands.Length();
311  return NS_OK;
312 }
313 
314 NS_IMETHODIMP
315 sbRemoteCommands::GetCommandType( const nsAString &aSubMenu,
316  PRInt32 aIndex,
317  const nsAString &aHost,
318  nsAString &_retval )
319 {
320  if ( aIndex >= 0 && aIndex < (PRInt32)mCommands.Length() ) {
321  _retval = mCommands.ElementAt(aIndex).type;
322  return NS_OK;
323  }
324  return NS_ERROR_INVALID_ARG;
325 }
326 
327 NS_IMETHODIMP
328 sbRemoteCommands::GetCommandId( const nsAString &aSubMenu,
329  PRInt32 aIndex,
330  const nsAString &aHost,
331  nsAString &_retval)
332 {
333  if ( aIndex >= 0 && aIndex < (PRInt32)mCommands.Length() ) {
334  _retval = mCommands.ElementAt(aIndex).id;
335  return NS_OK;
336  }
337  return NS_ERROR_INVALID_ARG;
338 }
339 
340 /* readonly attribute AString id */
341 NS_IMETHODIMP
342 sbRemoteCommands::GetId(nsAString & _retval NS_OUTPARAM)
343 {
344  return NS_ERROR_NOT_IMPLEMENTED;
345 }
346 
347 /* attribute PRUint16 targetFlags */
348 NS_IMETHODIMP
349 sbRemoteCommands::GetTargetFlags(PRUint16 *_retval)
350 {
351  return NS_ERROR_NOT_IMPLEMENTED;
352 }
353 
354 NS_IMETHODIMP
355 sbRemoteCommands::SetTargetFlags(PRUint16 targetFlags)
356 {
357  return NS_ERROR_NOT_IMPLEMENTED;
358 }
359 
360 /* attribute sbIPlaylistCommands parentCommandObject */
361 NS_IMETHODIMP
362 sbRemoteCommands::GetParentCommandObject(sbIPlaylistCommands **_retval)
363 {
364  return NS_ERROR_NOT_IMPLEMENTED;
365 }
366 
367 NS_IMETHODIMP
368 sbRemoteCommands::SetParentCommandObject(sbIPlaylistCommands *aParentCommandObject)
369 {
370  return NS_ERROR_NOT_IMPLEMENTED;
371 }
372 
373 NS_IMETHODIMP
374 sbRemoteCommands::GetCommandText( const nsAString &aSubMenu,
375  PRInt32 aIndex,
376  const nsAString &aHost,
377  nsAString &_retval)
378 {
379  if ( aIndex >= 0 && aIndex < (PRInt32)mCommands.Length() ) {
380  _retval = mCommands.ElementAt(aIndex).name;
381  return NS_OK;
382  }
383  return NS_ERROR_INVALID_ARG;
384 }
385 
386 NS_IMETHODIMP
387 sbRemoteCommands::GetCommandFlex( const nsAString &aSubMenu,
388  PRInt32 aIndex,
389  const nsAString &aHost,
390  PRInt32 *_retval)
391 {
392  NS_ENSURE_ARG_POINTER(_retval);
393  if ( aIndex >= 0 && aIndex < (PRInt32)mCommands.Length() ) {
394  nsAutoString cmdType = mCommands.ElementAt(aIndex).type;
395  if ( cmdType == NS_LITERAL_STRING("separator") )
396  *_retval = 1;
397  else
398  *_retval = 0;
399  return NS_OK;
400  }
401  return NS_ERROR_INVALID_ARG;
402 }
403 
404 NS_IMETHODIMP
405 sbRemoteCommands::GetCommandToolTipText( const nsAString &aSubMenu,
406  PRInt32 aIndex,
407  const nsAString & aHost,
408  nsAString &_retval)
409 {
410  if ( aIndex >= 0 && aIndex < (PRInt32)mCommands.Length() ) {
411  _retval = mCommands.ElementAt(aIndex).tooltip;
412  return NS_OK;
413  }
414  return NS_ERROR_INVALID_ARG;
415 }
416 
417 NS_IMETHODIMP
418 sbRemoteCommands::GetCommandEnabled( const nsAString &aSubMenu,
419  PRInt32 aIndex,
420  const nsAString &aHost,
421  PRBool *_retval)
422 {
423  NS_ENSURE_ARG_POINTER(_retval);
424  *_retval = PR_TRUE;
425  return NS_OK;
426 }
427 
428 NS_IMETHODIMP
429 sbRemoteCommands::GetCommandVisible( const nsAString &aSubMenu,
430  PRInt32 aIndex,
431  const nsAString &aHost,
432  PRBool *_retval)
433 {
434  NS_ENSURE_ARG_POINTER(_retval);
435  *_retval = PR_TRUE;
436  return NS_OK;
437 }
438 
439 NS_IMETHODIMP
440 sbRemoteCommands::GetVisibleCallback(sbIPlaylistCommandsBuilderPCCallback **_retval)
441 {
442  NS_ENSURE_ARG_POINTER(_retval);
443  *_retval = nsnull;
444  return NS_ERROR_NOT_IMPLEMENTED;
445 }
446 
447 NS_IMETHODIMP
448 sbRemoteCommands::GetCommandFlag( const nsAString &aSubMenu,
449  PRInt32 aIndex,
450  const nsAString &aHost,
451  PRBool *_retval)
452 {
453  NS_ENSURE_ARG_POINTER(_retval);
454  *_retval = PR_FALSE;
455  return NS_OK;
456 }
457 
458 NS_IMETHODIMP
459 sbRemoteCommands::GetCommandValue( const nsAString &aSubMenu,
460  PRInt32 aIndex,
461  const nsAString &aHost,
462  nsAString &_retval)
463 {
464  _retval = EmptyString();
465  return NS_OK;
466 }
467 
468 NS_IMETHODIMP
469 sbRemoteCommands::GetCommandShortcutModifiers( const nsAString &aSubMenu,
470  PRInt32 aIndex,
471  const nsAString &aHost,
472  nsAString &_retval)
473 {
474  _retval = EmptyString();
475  return NS_OK;
476 }
477 
478 NS_IMETHODIMP
479 sbRemoteCommands::GetCommandShortcutKey( const nsAString &aSubMenu,
480  PRInt32 aIndex,
481  const nsAString &aHost,
482  nsAString &_retval)
483 {
484  _retval = EmptyString();
485  return NS_OK;
486 }
487 
488 NS_IMETHODIMP
489 sbRemoteCommands::GetCommandShortcutKeycode( const nsAString &aSubMenu,
490  PRInt32 aIndex,
491  const nsAString &aHost,
492  nsAString &_retval)
493 {
494  _retval = EmptyString();
495  return NS_OK;
496 }
497 
498 NS_IMETHODIMP
499 sbRemoteCommands::GetCommandShortcutLocal( const nsAString &aSubMenu,
500  PRInt32 aIndex,
501  const nsAString &aHost,
502  PRBool *_retval)
503 {
504  NS_ENSURE_ARG_POINTER(_retval);
505  *_retval = PR_TRUE;
506  return NS_OK;
507 }
508 
509 NS_IMETHODIMP
510 sbRemoteCommands::GetCommandChoiceItem( const nsAString &aChoiceMenu,
511  const nsAString &aHost,
512  nsAString &_retval)
513 {
514  _retval = EmptyString();
515  return NS_OK;
516 }
517 
518 
519 NS_IMETHODIMP
520 sbRemoteCommands::GetCommandSubObject( const nsAString &aSubMenu,
521  PRInt32 aIndex,
522  const nsAString &aHost,
523  sbIPlaylistCommands **_retval)
524 {
525  NS_ENSURE_ARG_POINTER(_retval);
526  *_retval = nsnull;
527  return NS_OK;
528 }
529 
530 NS_IMETHODIMP
531 sbRemoteCommands::GetChildrenCommandObjects(nsISimpleEnumerator **_retval)
532 {
533  NS_ENSURE_ARG_POINTER(_retval);
534  *_retval = nsnull;
535  return NS_ERROR_NOT_IMPLEMENTED;
536 }
537 
538 NS_IMETHODIMP
539 sbRemoteCommands::InstantiateCustomCommand( const nsAString &aSubMenu,
540  PRInt32 aIndex,
541  const nsAString &aHost,
542  const nsAString &aID,
543  nsIDOMDocument *aDocument,
544  nsIDOMNode **_retval)
545 {
546  NS_ENSURE_ARG_POINTER(_retval);
547  *_retval = nsnull;
548  return NS_ERROR_NOT_IMPLEMENTED;
549 }
550 
551 NS_IMETHODIMP
552 sbRemoteCommands::RefreshCustomCommand( const nsAString &aSubMenu,
553  PRInt32 aIndex,
554  const nsAString &aHost,
555  const nsAString &aID,
556  nsIDOMNode *aCustomCommandElement)
557 {
558  return NS_ERROR_NOT_IMPLEMENTED;
559 }
560 
561 
562 NS_IMETHODIMP
563 sbRemoteCommands::OnCommand( const nsAString &aSubMenu,
564  const PRInt32 aIndex,
565  const nsAString &aHost,
566  const nsAString &aID,
567  const nsAString &aValue)
568 {
569  LOG(( "sbRemoteCommands::OnCommand(%s, %d, %s, %s, %s)",
570  NS_LossyConvertUTF16toASCII(aSubMenu).get(), aIndex,
571  NS_LossyConvertUTF16toASCII(aHost).get(),
572  NS_LossyConvertUTF16toASCII(aID).get(),
573  NS_LossyConvertUTF16toASCII(aValue).get()));
574 
575  nsresult rv;
576  nsCOMPtr<sbIRemotePlayer> owner( do_QueryReferent( mWeakOwner, &rv ) );
577  NS_ENSURE_SUCCESS( rv, rv );
578  return owner->FireEventToContent( NS_LITERAL_STRING("Events"), aID );
579 }
580 
581 NS_IMETHODIMP
582 sbRemoteCommands::InitCommands( const nsAString &aHost )
583 {
584  return NS_OK;
585 }
586 
587 NS_IMETHODIMP
588 sbRemoteCommands::ShutdownCommands( )
589 {
590  mContext = NULL;
591  return NS_OK;
592 }
593 
594 NS_IMETHODIMP
595 sbRemoteCommands::Duplicate( sbIPlaylistCommands **_retval )
596 {
597  NS_ENSURE_ARG_POINTER(_retval);
598  LOG(("sbRemoteCommands::Duplicate()"));
599 
600  nsRefPtr<sbRemoteCommands> copy = new sbRemoteCommands(mRemotePlayer);
601  NS_ENSURE_TRUE( copy, NS_ERROR_OUT_OF_MEMORY );
602 
603  nsresult rv = copy->Init();
604  NS_ENSURE_SUCCESS( rv, rv );
605 
606  PRUint32 num = mCommands.Length();
607  for ( PRUint32 index = 0; index < num; index++ ) {
608  sbCommand &command = mCommands.ElementAt(index);
609  rv = copy->AddCommand( command.type,
610  command.id,
611  command.name,
612  command.tooltip );
613  // if AddCommand fails we are out of memory
614  NS_ENSURE_SUCCESS( rv, rv );
615  }
616  nsCOMPtr<sbIRemotePlayer> owner( do_QueryReferent( mWeakOwner, &rv ) );
617  NS_ENSURE_SUCCESS( rv, rv );
618  rv = copy->SetOwner(owner);
619  NS_ENSURE_SUCCESS( rv, rv );
620  nsCOMPtr<sbIPlaylistCommands> plCommands(
621  do_QueryInterface( NS_ISUPPORTS_CAST( sbIRemoteCommands*, copy ), &rv ) );
622  NS_ENSURE_SUCCESS( rv, rv );
623  NS_ADDREF( *_retval = plCommands );
624  return NS_OK;
625 }
nsString name
#define LOG(args)
#define SB_IMPL_SECURITYCHECKEDCOMP_INIT(_class)
Definition: sbRemoteAPI.h:82
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
The interface for receiving notices when a Command Object is added to or removed from another Command...
nsString tooltip
readonly attribute sbIRemotePlayer remotePlayer
This interface is a composition of many of our other interfaces with the goal of exposing a more web-...
_updateCookies aHost
A callback interface used to receive callbacks from an sbIPlaylistCommandsBuilder object relating to ...
sbRemoteCommands(sbRemotePlayer *aRemotePlayer)
nsISecurityCheckedComponent
nsString id
_collectFormDataForFrame aDocument
An interface to specify playlist commands from remote web pages.
static const char * sPublicWProperties[]
A marker interface for objects that aggregate the security mixin.
function num(elem, prop)
NS_IMPL_ISUPPORTS5(sbRemoteCommands, nsIClassInfo, nsISecurityCheckedComponent, sbIPlaylistCommands, sbIRemoteCommands, sbISecurityAggregator) NS_IMPL_CI_INTERFACE_GETTER4(sbRemoteCommands
An interface that represents the set of command buttons and context menu items available on a display...
_updateCookies aName
nsRefPtr< sbRemotePlayer > mRemotePlayer
The interface for giving context to a playlistcommands object.
#define SB_IMPL_CLASSINFO_INTERFACES_ONLY(_class)
attribute sbIRemotePlayer owner
static const char * sPublicMethods[]
nsCOMPtr< nsIWeakReference > mWeakOwner
static const char * sPublicRProperties[]
nsTArray< sbCommand > mCommands
nsCOMPtr< sbIPlaylistCommandsContext > mContext
nsString type