sbRemoteAPIService.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
27 #include "sbRemoteAPIService.h"
28 
29 #include "nsComponentManagerUtils.h"
30 #include "nsServiceManagerUtils.h"
31 #include "nsIObserverService.h"
32 
33 /*
34  * To log this module, set the following environment variable:
35  * NSPR_LOG_MODULES=sbRemoteAPIService:5
36  */
37 #ifdef PR_LOGGING
38 static PRLogModuleInfo* gRemoteAPIServiceLog = nsnull;
39 #endif
40 
41 #undef LOG
42 #define LOG(args) PR_LOG(gRemoteAPIServiceLog, PR_LOG_WARN, args)
43 
47 
49 {
50 #ifdef PR_LOGGING
51  if (!gRemoteAPIServiceLog) {
52  gRemoteAPIServiceLog = PR_NewLogModule("sbRemoteAPIService");
53  }
54 #endif
55  LOG(("sbRemoteAPIService::sbRemoteAPIService()"));
56 }
57 
58 sbRemoteAPIService::~sbRemoteAPIService()
59 {
60  LOG(("sbRemoteAPIService::~sbRemoteAPIService()"));
61 }
62 
63 // ---------------------------------------------------------------------------
64 //
65 // sbIRemoteAPIService
66 //
67 // ---------------------------------------------------------------------------
68 
69 NS_IMETHODIMP
71 {
72  const char* remotes[] = {
73  "faceplate.volume",
74  "playlist.repeat",
75  "playlist.shuffle",
76  "playback.control.triggered",
77  nsnull };
78 
79  nsresult rv;
80 
81  nsCOMPtr<nsIObserverService> obs =
82  do_GetService( "@mozilla.org/observer-service;1", &rv );
83  NS_ENSURE_SUCCESS( rv, rv );
84  obs->AddObserver( this, "quit-application", PR_FALSE );
85 
86  for (int i = 0; remotes[i]; ++i) {
87  nsCOMPtr<sbIDataRemote> dr =
88  do_CreateInstance( "@songbirdnest.com/Songbird/DataRemote;1", &rv );
89  NS_ENSURE_SUCCESS( rv, rv );
90 
91  rv = dr->Init( NS_ConvertASCIItoUTF16(remotes[i]), EmptyString() );
92  NS_ENSURE_SUCCESS( rv, rv );
93 
94  rv = dr->BindObserver( this, PR_FALSE );
95  NS_ENSURE_SUCCESS( rv, rv );
96 
97  mDataRemotes.AppendObject(dr);
98  }
99  return NS_OK;
100 }
101 
102 NS_IMETHODIMP
103 sbRemoteAPIService::HasPlaybackControl(nsIURI *aURI, PRBool *_retval)
104 {
105  NS_ENSURE_ARG(aURI);
106  NS_ENSURE_ARG(_retval);
107 
108 #ifdef PR_LOGGING
109  nsCString spec, controllerSpec;
110  nsresult rv = aURI->GetSpec( spec );
111  if ( NS_FAILED(rv) ) {
112  spec.AssignLiteral("[unknown spec]");
113  }
114 
116  rv = aURI->GetSpec( controllerSpec );
117  if ( NS_FAILED(rv) ) {
118  controllerSpec.AssignLiteral("[unknown spec]");
119  }
120  } else {
121  controllerSpec.AssignLiteral("[no uri]");
122  }
123 
124  LOG(("sbRemoteAPIService::HasPlaybackControl() - \n\t%s\n\t%s",
125  spec.BeginReading(),
126  controllerSpec.BeginReading()));
127 #endif /* PR_LOGGING */
128  if (!mPlaybackControllerURI) {
129  // not playing from the web
130  *_retval = PR_FALSE;
131  return NS_OK;
132  }
133  return aURI->Equals(mPlaybackControllerURI, _retval);
134 }
135 
136 NS_IMETHODIMP
137 sbRemoteAPIService::TakePlaybackControl(nsIURI *aURI, nsIURI **_retval)
138 {
139  #ifdef PR_LOGGING
140  nsCString spec;
141  if (aURI) {
142  nsresult rv = aURI->GetSpec( spec );
143  if ( NS_FAILED(rv) ) {
144  spec.AssignLiteral("[no spec]");
145  }
146  LOG(("sbRemoteAPIService::TakePlaybackControl() - assigned spec %s",
147  spec.BeginReading() ));
148  } else {
149  LOG(("sbRemoteAPIService::TakePlaybackControl() - clearing" ));
150  }
151  #endif
152  if ( NS_LIKELY(_retval) ) {
153  NS_IF_ADDREF( *_retval = mPlaybackControllerURI );
154  }
155  mPlaybackControllerURI = aURI;
156  return NS_OK;
157 }
158 
159 
160 // ---------------------------------------------------------------------------
161 //
162 // nsIObserver
163 //
164 // ---------------------------------------------------------------------------
165 
166 NS_IMETHODIMP
167 sbRemoteAPIService::Observe( nsISupports *aSubject,
168  const char *aTopic,
169  const PRUnichar *aData )
170 {
171  LOG(("sbRemoteAPIService::Observe() - %s", aTopic));
172  nsresult rv;
173 
174  if (!strcmp( "quit-application", aTopic ) ) {
175  // clean up
176  for (int i = 0; i < mDataRemotes.Count(); ++i) {
177  mDataRemotes[i]->Unbind();
178  }
179 
180  nsCOMPtr<nsIObserverService> obs =
181  do_GetService( "@mozilla.org/observer-service;1", &rv );
182  NS_ENSURE_SUCCESS( rv, rv );
183  rv = obs->RemoveObserver( this, "quit-application" );
184  NS_ENSURE_SUCCESS( rv, rv );
185  } else {
186  // triggered by data remotes
187 
188  if (!mPlaybackControllerURI) {
189  // we have no content page in control
190  return NS_OK;
191  }
192 
193  // assume it's one of the data remotes
194  // if the change was in fact initiated by the remote player, we'll clear the
195  // URI here and then re-set it in the caller, so we don't have to worry about
196  // clearing things when we shouldn't
197 
198  LOG(( "Cleared controller due to observing %s", aTopic ));
199  mPlaybackControllerURI = nsnull;
200  }
201 
202  return NS_OK;
203 }
return NS_OK
NS_IMPL_ISUPPORTS2(sbRemoteAPIService, sbIRemoteAPIService, nsIObserver) sbRemoteAPIService
#define LOG(args)
nsCOMPtr< nsIURI > mPlaybackControllerURI
nsCOMArray< sbIDataRemote > mDataRemotes
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData