sbScriptableFunction.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 "sbScriptableFunction.h"
28 
29 #include <sbClassInfoUtils.h>
30 
31 #include <nsMemory.h>
32 #include <nsIClassInfoImpl.h>
33 #include <nsStringGlue.h>
34 
35 #include <jsapi.h>
36 
37 /*
38  * To log this module, set the following environment variable:
39  * NSPR_LOG_MODULES=sbScriptableFunction:5
40  */
41 #ifdef PR_LOGGING
42 static PRLogModuleInfo* gScriptableFunctionLog = nsnull;
43 #endif
44 
45 #undef LOG
46 #define LOG(args) PR_LOG( gScriptableFunctionLog, PR_LOG_WARN, args )
47 #define TRACE(args) PR_LOG( gScriptableFunctionLog, PR_LOG_DEBUG, args )
48 
54 
55 
57 {
58 #ifdef PR_LOGGING
59  if (!gScriptableFunctionLog) {
60  gScriptableFunctionLog = PR_NewLogModule( "sbScriptableFunction" );
61  }
62 #endif
63  TRACE(("sbScriptableFunctionBase::sbScriptableFunctionBase()"));
64 }
65 
67 {
68  TRACE(("sbScriptableFunctionBase::~sbScriptableFunctionBase()"));
69 }
70 
71 // ---------------------------------------------------------------------------
72 //
73 // nsIXPCScriptable
74 //
75 // ---------------------------------------------------------------------------
76 
77 NS_IMETHODIMP
79 {
80  LOG(("sbScriptableFunctionBase::GetClassName()"));
81  NS_ENSURE_ARG_POINTER(aClassName);
82 
83  NS_NAMED_LITERAL_CSTRING( kClassName, "sbScriptableFunction" );
84  *aClassName = ToNewCString(kClassName);
85  return aClassName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
86 }
87 
88 NS_IMETHODIMP
90 {
91  LOG(("sbScriptableFunctionBase::GetScriptableFlags()"));
92  NS_ENSURE_ARG_POINTER(aScriptableFlags);
93  *aScriptableFlags = DONT_ENUM_STATIC_PROPS |
94  DONT_ENUM_QUERY_INTERFACE |
95  WANT_CALL |
96  DONT_REFLECT_INTERFACE_NAMES ;
97  return NS_OK;
98 }
99 
100 // ---------------------------------------------------------------------------
101 //
102 // nsISecurityCheckedComponent
103 //
104 // ---------------------------------------------------------------------------
105 
106 NS_IMETHODIMP
107 sbScriptableFunctionBase::CanCreateWrapper( const nsIID *iid, char **_retval )
108 {
109  TRACE(("sbScriptableFunctionBase::CanCreateWrapper()"));
110 
111  NS_ENSURE_ARG_POINTER(_retval);
112  *_retval = ToNewCString( NS_LITERAL_CSTRING("AllAccess") );
113  return NS_OK;
114 }
115 
116 NS_IMETHODIMP
117 sbScriptableFunctionBase::CanCallMethod( const nsIID *iid,
118  const PRUnichar *methodName,
119  char **_retval )
120 {
121  TRACE(( "sbScriptableFunctionBase::CanCallMethod() - %s",
122  NS_LossyConvertUTF16toASCII(methodName).BeginReading() ));
123 
124  NS_ENSURE_ARG_POINTER(_retval);
125  *_retval = ToNewCString( NS_LITERAL_CSTRING("AllAccess") );
126  return NS_OK;
127 }
128 
129 NS_IMETHODIMP
130 sbScriptableFunctionBase::CanGetProperty( const nsIID *iid,
131  const PRUnichar *propertyName,
132  char **_retval )
133 {
134  TRACE(( "sbScriptableFunctionBase::CanGetProperty() - %s",
135  NS_LossyConvertUTF16toASCII(propertyName).BeginReading() ));
136 
137  NS_ENSURE_ARG_POINTER(_retval);
138  *_retval = ToNewCString( NS_LITERAL_CSTRING("AllAccess") );
139  return NS_OK;
140 }
141 
142 NS_IMETHODIMP
143 sbScriptableFunctionBase::CanSetProperty( const nsIID *iid,
144  const PRUnichar *propertyName,
145  char **_retval )
146 {
147  TRACE(( "sbScriptableFunctionBase::CanSetProperty() - %s",
148  NS_LossyConvertUTF16toASCII(propertyName).BeginReading() ));
149 
150  NS_ENSURE_ARG_POINTER(_retval);
151  *_retval = ToNewCString( NS_LITERAL_CSTRING("NoAccess") );
152  return NS_OK;
153 }
154 
155 // ---------------------------------------------------------------------------
156 //
157 // Class: sbScriptableLibraryFunction
158 //
159 // ---------------------------------------------------------------------------
160 
163 
165  const nsIID& aIID )
166  : mObject(aObject),
167  mIID(aIID)
168 {
169 #ifdef PR_LOGGING
170  if (!gScriptableFunctionLog) {
171  gScriptableFunctionLog = PR_NewLogModule( "sbScriptableFunction" );
172  }
173 #endif
174  TRACE(("sbScriptableLibraryFunction::sbScriptableLibraryFunction()"));
175 }
176 
178 {
179  TRACE(("sbScriptableLibraryFunction::~sbScriptableLibraryFunction()"));
180 }
181 
182 NS_IMETHODIMP
183 sbScriptableLibraryFunction::Call( nsIXPConnectWrappedNative *wrapper,
184  JSContext *cx,
185  JSObject *obj,
186  PRUint32 argc,
187  jsval *argv,
188  jsval *vp,
189  PRBool *_retval )
190 {
191  TRACE(("sbScriptableLibraryFunction::Call()"));
192 
193  // this can get called because library.getArtists should map to a function
194  // that returns library.artists, but instead it returns the object itself.
195  // So we make the object callable and return itself.
196  NS_ENSURE_ARG_POINTER(obj);
197  NS_ENSURE_ARG_POINTER(vp);
198  NS_ENSURE_ARG_POINTER(_retval);
199 
200  nsresult rv;
201 
202  nsCOMPtr<nsIXPConnect> xpc;
203  rv = wrapper->GetXPConnect( getter_AddRefs(xpc) );
204  NS_ENSURE_SUCCESS( rv, rv );
205 
206  nsCOMPtr<nsIXPConnectJSObjectHolder> objectHolder;
207  rv = xpc->WrapNative( cx, obj, mObject, mIID, getter_AddRefs(objectHolder) );
208  NS_ENSURE_SUCCESS( rv, rv );
209 
210  JSObject* object;
211  rv = objectHolder->GetJSObject( &object );
212  NS_ENSURE_SUCCESS( rv, rv );
213 
214  *vp = OBJECT_TO_JSVAL(object);
215  *_retval = PR_TRUE;
216 
217  return NS_OK;
218 }
219 
220 // ---------------------------------------------------------------------------
221 //
222 // Class: sbScriptableMediaListFunction
223 //
224 // ---------------------------------------------------------------------------
225 
228 
230 {
231 #ifdef PR_LOGGING
232  if (!gScriptableFunctionLog) {
233  gScriptableFunctionLog = PR_NewLogModule( "sbScriptableFunction" );
234  }
235 #endif
236  TRACE(("sbScriptableMediaListFunction::sbScriptableMediaListFunction()"));
237 }
238 
240 {
241  TRACE(("sbScriptableMediaListFunction::~sbScriptableMediaListFunction()"));
242 }
243 
244 NS_IMETHODIMP
245 sbScriptableMediaListFunction::Call( nsIXPConnectWrappedNative *wrapper,
246  JSContext *cx,
247  JSObject *obj,
248  PRUint32 argc,
249  jsval *argv,
250  jsval *vp,
251  PRBool *_retval )
252 {
253  TRACE(("sbScriptableMediaListFunction::Call()"));
254  return NS_OK;
255 }
NS_IMPL_ISUPPORTS_INHERITED0(sbScriptableLibraryFunction, sbScriptableFunctionBase) sbScriptableLibraryFunction
return NS_OK
#define TRACE(args)
NS_DECL_ISUPPORTS_INHERITED NS_IMETHOD Call(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, PRUint32 argc, jsval *argv, jsval *vp, PRBool *_retval)
NS_DECL_ISUPPORTS_INHERITED NS_IMETHOD Call(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, PRUint32 argc, jsval *argv, jsval *vp, PRBool *_retval)
nsISecurityCheckedComponent
NS_IMPL_ISUPPORTS2_CI(sbScriptableFunctionBase, nsISecurityCheckedComponent, nsIXPCScriptable) sbScriptableFunctionBase
#define LOG(args)
nsCOMPtr< nsISupports > mObject
sbIJobCancelable NS_DECL_CLASSINFO(sbGstreamerMediaInspector)
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_NSISECURITYCHECKEDCOMPONENT NS_IMETHOD GetClassName(char **aClassName)
NS_IMETHOD GetScriptableFlags(PRUint32 *aScriptableFlags)
#define SB_IMPL_CLASSINFO_INTERFACES_ONLY(_class)