sbRemoteLibraryResource.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 <sbILibrary.h>
28 #include <sbIPropertyInfo.h>
29 #include <sbIPropertyManager.h>
30 
32 #include <sbStandardProperties.h>
33 #include <prlog.h>
34 #include <nsServiceManagerUtils.h>
35 
36 /*
37  * To log this module, set the following environment variable:
38  * NSPR_LOG_MODULES=sbRemoteLibraryResource:5
39  */
40 #ifdef PR_LOGGING
41 PRLogModuleInfo* gRemoteLibResLog = nsnull;
42 #endif
43 
46 
48  sbIMediaItem *aMediaItem ) :
49  mRemotePlayer(aRemotePlayer),
50  mMediaItem(aMediaItem)
51 {
52  NS_ASSERTION( aRemotePlayer, "Null remote player!" );
53  NS_ASSERTION( aMediaItem, "Null media item!" );
54 
55 #ifdef PR_LOGGING
56  if (!gRemoteLibResLog) {
57  gRemoteLibResLog = PR_NewLogModule("sbRemoteLibraryResource");
58  }
59  LOG_RES(("sbRemoteLibraryResource::sbRemoteLibraryResource()"));
60 #endif
61 }
62 
64  LOG_RES(("sbRemoteLibraryResource::~sbRemoteLibraryResource()"));
65 }
66 
67 // ----------------------------------------------------------------------------
68 //
69 // sbILibraryResource
70 //
71 // ----------------------------------------------------------------------------
72 NS_IMETHODIMP
74  nsAString &_retval )
75 {
76  NS_ENSURE_TRUE( mMediaItem, NS_ERROR_NULL_POINTER );
77  LOG_RES(( "sbRemoteLibraryResource::GetProperty(%s)",
78  NS_LossyConvertUTF16toASCII(aID).get() ));
79 
80  nsresult rv;
81 
82  // get the property manager service
83  nsCOMPtr<sbIPropertyManager> propertyManager =
84  do_GetService( "@songbirdnest.com/Songbird/Properties/PropertyManager;1",
85  &rv );
86  NS_ENSURE_SUCCESS( rv, rv );
87 
88  // get the property info for the property being requested
89  nsCOMPtr<sbIPropertyInfo> propertyInfo;
90  rv = propertyManager->GetPropertyInfo( aID, getter_AddRefs(propertyInfo) );
91  NS_ENSURE_SUCCESS( rv, rv );
92 
93  // ask if this property is remotely readable
94  PRBool readable;
95  rv = propertyInfo->GetRemoteReadable(&readable);
96  NS_ENSURE_SUCCESS( rv, rv );
97 
98  // well, is it readable?
99  if (!readable) {
100  LOG_RES(( "Attempting to get a property's (%s) value that is not allowed "
101  "to be read from the remote API!",
102  NS_LossyConvertUTF16toASCII(aID).get() ));
103  // if not return an error
104  return NS_ERROR_FAILURE;
105  }
106 
107  // readable property
108  nsString propVal;
109  rv = mMediaItem->GetProperty( aID, propVal );
110  NS_ENSURE_SUCCESS( rv, rv );
111 
112  // Protect against exposing file:// uris to the world.
113  if ( !aID.EqualsLiteral(SB_PROPERTY_ORIGINURL) ||
114  !aID.EqualsLiteral(SB_PROPERTY_COPYRIGHTURL) ||
115  !aID.EqualsLiteral(SB_PROPERTY_PRIMARYIMAGEURL) ) {
116 
117  if ( StringBeginsWith( propVal, NS_LITERAL_STRING("file:") ) ) {
118  LOG_RES(( "sbRemoteLibraryResource::GetProperty() - "
119  "Disallowing access to 'file:' URI." ));
120  // Assign a dummy signal value
121  propVal.AssignLiteral("__BLOCKED__");
122  } else {
123  LOG_RES(( "sbRemoteLibraryResource::GetProperty() - "
124  "Allowing access to non-file value: %s",
125  NS_LossyConvertUTF16toASCII(propVal).get() ));
126  }
127  }
128 
129  _retval.Assign(propVal);
130  return NS_OK;
131 }
132 
133 NS_IMETHODIMP
135  const nsAString &aValue )
136 {
137  LOG_RES(( "sbRemoteLibraryResource::SetProperty( %s, %s )",
138  NS_LossyConvertUTF16toASCII(aID).get(),
139  NS_LossyConvertUTF16toASCII(aValue).get() ));
140 
141  NS_ASSERTION( mMediaItem, "SetProperty called before Initialization" );
142 
143  nsresult rv = NS_OK;
144 
145  // get the property manager service
146  nsCOMPtr<sbIPropertyManager> propertyManager =
147  do_GetService( "@songbirdnest.com/Songbird/Properties/PropertyManager;1",
148  &rv );
149  NS_ENSURE_SUCCESS( rv, rv );
150 
151  // Check to see if we have the property first, if not we must create it with
152  // the right settings so websites can modify it.
153  PRBool hasProp;
154  rv = propertyManager->HasProperty( aID, &hasProp );
155 
156  // get the property info for the property being requested - this will create
157  // it if it didn't already exist.
158  nsCOMPtr<sbIPropertyInfo> propertyInfo;
159  rv = propertyManager->GetPropertyInfo( aID, getter_AddRefs(propertyInfo) );
160  NS_ENSURE_SUCCESS( rv, rv );
161 
162  // check to see if the prop already existed or if we created it
163  if (hasProp) {
164  // ask if this property is remotely writable
165  PRBool writable = PR_FALSE;
166  rv = propertyInfo->GetRemoteWritable(&writable);
167  NS_ENSURE_SUCCESS( rv, rv );
168 
169  // well, is it writeable?
170  if (!writable) {
171  // if not return an error
172  LOG_RES(( "Attempting to set a property's (%s) value that is not allowed "
173  "to be set from the remote API!",
174  NS_LossyConvertUTF16toASCII(aID).get() ));
175  return NS_ERROR_FAILURE;
176  }
177  }
178  else {
179  // we created a new property in the system so enable remote write/read
180  rv = propertyInfo->SetRemoteWritable(PR_TRUE);
181  NS_ENSURE_SUCCESS( rv, rv );
182 
183  rv = propertyInfo->SetRemoteReadable(PR_TRUE);
184  NS_ENSURE_SUCCESS( rv, rv );
185  }
186 
187  // it all looks ok, pass this request on to the real media item
188  rv = mMediaItem->SetProperty( aID, aValue );
189  NS_ENSURE_SUCCESS( rv, rv );
190 
191  nsCOMPtr<sbILibrary> library;
192  rv = mMediaItem->GetLibrary( getter_AddRefs(library) );
193  NS_ENSURE_SUCCESS( rv, rv );
194 
195  mRemotePlayer->GetNotificationManager()->Action(
197 
198  return NS_OK;
199 }
200 
201 NS_IMETHODIMP
203 {
204  LOG_RES(("sbRemoteLibraryResource::SetProperties()"));
205  NS_ENSURE_ARG_POINTER( aProperties );
206  NS_ASSERTION( mMediaItem, "SetProperties called before Initialization" );
207 
208  nsresult rv = mMediaItem->SetProperties(aProperties);
209  NS_ENSURE_SUCCESS( rv, rv );
210 
211  nsCOMPtr<sbILibrary> library;
212  rv = mMediaItem->GetLibrary( getter_AddRefs(library) );
213  NS_ENSURE_SUCCESS( rv, rv );
214 
215  mRemotePlayer->GetNotificationManager()->Action(
217 
218  return NS_OK;
219 }
220 
return NS_OK
#define SB_PROPERTY_ORIGINURL
NS_IMETHOD GetProperty(const nsAString &aID, nsAString &_retval)
onPageChanged aValue
Definition: FeedWriter.js:1395
nsCOMPtr< sbIMediaItem > mMediaItem
NS_IMETHOD SetProperties(sbIPropertyArray *aProperties)
General interface to data resources.
NS_IMPL_ISUPPORTS1(sbRemoteLibraryResource, sbILibraryResource) sbRemoteLibraryResource
nsRefPtr< sbRemotePlayer > mRemotePlayer
Interface that defines a single item of media in the system.
NS_IMETHOD SetProperty(const nsAString &aID, const nsAString &aValue)
#define SB_PROPERTY_COPYRIGHTURL
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
#define SB_PROPERTY_PRIMARYIMAGEURL
#define LOG_RES(args)