~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/modules/plugin/samples/SanePlugin/nsSanePluginFactory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * Contributor(s): 
 
13
 *
 
14
 *   Rusty Lynch <rusty.lynch@intel.com>
 
15
 */
 
16
/*
 
17
 * Implements the SANE plugin factory class.
 
18
 */
 
19
 
 
20
#include "nsString.h"
 
21
#include "nsCOMPtr.h"
 
22
#include "nsIServiceManager.h"
 
23
#include "nsSanePlugin_CID.h"
 
24
#include "nsSanePlugin.h"
 
25
#include "nsSanePluginFactory.h"
 
26
#include "plstr.h"
 
27
 
 
28
#define PLUGIN_NAME             "SANE Plugin"
 
29
#define PLUGIN_DESCRIPTION      "SANE Plugin is a generic scanner interface"
 
30
#define PLUGIN_MIME_DESCRIPTION "application/X-sane-plugin::Scanner/Camera"
 
31
#define PLUGIN_MIME_TYPE        "application/X-sane-plugin"
 
32
 
 
33
static NS_DEFINE_IID( kISupportsIID,            NS_ISUPPORTS_IID           );
 
34
static NS_DEFINE_IID( kIFactoryIID,             NS_IFACTORY_IID            );
 
35
static NS_DEFINE_IID( kIPluginIID,              NS_IPLUGIN_IID             );
 
36
static NS_DEFINE_CID( kComponentManagerCID,     NS_COMPONENTMANAGER_CID    );
 
37
static NS_DEFINE_CID( knsSanePluginControlCID,  NS_SANE_PLUGIN_CONTROL_CID );
 
38
static NS_DEFINE_CID( knsSanePluginInst,        NS_SANE_PLUGIN_CID         );
 
39
 
 
40
////////////////////////////////////////////////////////////////////////
 
41
 
 
42
nsSanePluginFactoryImpl::nsSanePluginFactoryImpl( const nsCID &aClass,
 
43
                                                  const char* className,
 
44
                                                  const char* contractID )
 
45
    : mClassID(aClass), mClassName(className), mContractID(contractID)
 
46
{
 
47
#ifdef DEBUG
 
48
    printf("nsSanePluginFactoryImpl::nsSanePluginFactoryImpl()\n");
 
49
#endif
 
50
}
 
51
 
 
52
nsSanePluginFactoryImpl::~nsSanePluginFactoryImpl()
 
53
{
 
54
#ifdef DEBUG
 
55
    printf("nsSanePluginFactoryImpl::~nsSanePluginFactoryImpl()\n");
 
56
#endif
 
57
 
 
58
    printf("mRefCnt = %i\n", mRefCnt);
 
59
    NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction");
 
60
}
 
61
 
 
62
NS_IMETHODIMP
 
63
nsSanePluginFactoryImpl::QueryInterface(const nsIID &aIID, 
 
64
                                        void **aInstancePtr)
 
65
{
 
66
#ifdef DEBUG
 
67
    printf("nsSanePluginFactoryImpl::QueryInterface()\n");
 
68
#endif
 
69
 
 
70
    if (!aInstancePtr)
 
71
        return NS_ERROR_NULL_POINTER;
 
72
 
 
73
    if (aIID.Equals(kISupportsIID)) {
 
74
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,this);
 
75
    } else if (aIID.Equals(kIFactoryIID)) {
 
76
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,
 
77
                                       NS_STATIC_CAST(nsIFactory*,this));
 
78
    } else if (aIID.Equals(kIPluginIID)) {
 
79
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,
 
80
                                       NS_STATIC_CAST(nsIPlugin*,this));
 
81
    } else {
 
82
        *aInstancePtr = nsnull;
 
83
        return NS_ERROR_NO_INTERFACE;
 
84
    }
 
85
 
 
86
    NS_ADDREF(NS_REINTERPRET_CAST(nsISupports*,*aInstancePtr));
 
87
 
 
88
    return NS_OK;
 
89
}
 
90
 
 
91
// Standard implementation of AddRef and Release
 
92
NS_IMPL_ADDREF( nsSanePluginFactoryImpl )
 
93
NS_IMPL_RELEASE( nsSanePluginFactoryImpl )
 
94
 
 
95
NS_IMETHODIMP
 
96
nsSanePluginFactoryImpl::CreateInstance( nsISupports *aOuter,
 
97
                                         const nsIID &aIID,
 
98
                                         void **aResult)
 
99
{
 
100
#ifdef DEBUG
 
101
    printf("nsSanePluginFactoryImpl::CreateInstance()\n");
 
102
#endif
 
103
 
 
104
    if ( !aResult )
 
105
        return NS_ERROR_NULL_POINTER;
 
106
  
 
107
    if ( aOuter )
 
108
        return NS_ERROR_NO_AGGREGATION;
 
109
 
 
110
    nsSanePluginInstance * inst = new nsSanePluginInstance();
 
111
    if (!inst)
 
112
        return NS_ERROR_OUT_OF_MEMORY;
 
113
           
 
114
    inst->AddRef();
 
115
    *aResult = inst;
 
116
    return NS_OK;
 
117
}
 
118
 
 
119
nsresult 
 
120
nsSanePluginFactoryImpl::LockFactory(PRBool aLock)
 
121
{
 
122
#ifdef DEBUG
 
123
    printf("nsSanePluginFactoryImpl::LockFactory()\n");
 
124
#endif
 
125
 
 
126
    // Needs to be implemented
 
127
 
 
128
    return NS_OK;
 
129
}
 
130
 
 
131
 
 
132
NS_METHOD
 
133
nsSanePluginFactoryImpl::Initialize()
 
134
{
 
135
#ifdef DEBUG
 
136
    printf("nsSanePluginFactoryImpl::Initialize()\n");
 
137
#endif
 
138
 
 
139
    return NS_OK;
 
140
}
 
141
 
 
142
 
 
143
NS_METHOD
 
144
nsSanePluginFactoryImpl::Shutdown( void )
 
145
{
 
146
#ifdef DEBUG
 
147
    printf("nsSanePluginFactoryImpl::Shutdown()\n");
 
148
#endif
 
149
 
 
150
    return NS_OK;
 
151
}
 
152
 
 
153
 
 
154
NS_METHOD 
 
155
nsSanePluginFactoryImpl::GetMIMEDescription(const char* *result)
 
156
{
 
157
#ifdef DEBUG
 
158
    printf("nsSanePluginFactoryImpl::GetMIMEDescription()\n");
 
159
#endif
 
160
 
 
161
    // caller is responsible for releasing
 
162
    *result = PL_strdup( PLUGIN_MIME_DESCRIPTION );
 
163
 
 
164
    return NS_OK;
 
165
}
 
166
 
 
167
NS_METHOD
 
168
nsSanePluginFactoryImpl::GetValue( nsPluginVariable variable, void *value )
 
169
{
 
170
#ifdef DEBUG
 
171
    printf("nsSanePluginFactoryImpl::GetValue()\n");
 
172
#endif
 
173
 
 
174
    nsresult err = NS_OK;
 
175
 
 
176
    if ( variable == nsPluginVariable_NameString ) {
 
177
    
 
178
        *( ( char ** )value ) = strdup( PLUGIN_NAME );
 
179
 
 
180
    } else if ( variable == nsPluginVariable_DescriptionString ) {
 
181
 
 
182
        *( ( char ** )value ) = strdup( PLUGIN_DESCRIPTION );
 
183
 
 
184
    } else {
 
185
    
 
186
        err = NS_ERROR_FAILURE;
 
187
 
 
188
    }
 
189
  
 
190
    return err;
 
191
}
 
192
 
 
193
 
 
194
NS_IMETHODIMP 
 
195
nsSanePluginFactoryImpl::CreatePluginInstance( nsISupports *aOuter, 
 
196
                                               REFNSIID aIID, 
 
197
                                               const char* aPluginMIMEType,
 
198
                                               void **aResult)
 
199
{
 
200
#ifdef DEBUG
 
201
    printf("nsSanePluginFactoryImpl::CreatePluginInstance()\n");
 
202
#endif
 
203
 
 
204
    // Need to find out what this is used for.  The npsimple
 
205
    // plugin looks like it just does a CreateInstance and 
 
206
    // ignores the mime type.
 
207
    return NS_ERROR_NOT_IMPLEMENTED;
 
208
}
 
209
 
 
210
 
 
211
////////////////////////////////////////////////////////////////////////
 
212
 
 
213
/**
 
214
 * The XPCOM runtime will call this to get a new factory object for the
 
215
 * CID/contractID it passes in.  XPCOM is responsible for caching the resulting
 
216
 * factory.
 
217
 *
 
218
 * return the proper factory to the caller
 
219
 */
 
220
extern "C" PR_IMPLEMENT(nsresult)
 
221
NSGetFactory( nsISupports* aServMgr,
 
222
              const nsCID &aClass,
 
223
              const char *aClassName,
 
224
              const char *aContractID,
 
225
              nsIFactory **aFactory)
 
226
{
 
227
    if (! aFactory)
 
228
        return NS_ERROR_NULL_POINTER;
 
229
  
 
230
    nsSanePluginFactoryImpl* factory = new nsSanePluginFactoryImpl(aClass, 
 
231
                                                                   aClassName,
 
232
                                                                   aContractID);
 
233
    if ( factory == nsnull )
 
234
        return NS_ERROR_OUT_OF_MEMORY;
 
235
  
 
236
    NS_ADDREF(factory);
 
237
    *aFactory = factory;
 
238
    return NS_OK;
 
239
 
 
240
}
 
241
 
 
242
char *buf;
 
243
 
 
244
extern "C" PR_IMPLEMENT( nsresult )
 
245
NSRegisterSelf( nsISupports* aServMgr, const char* aPath )
 
246
{
 
247
    nsresult rv;
 
248
  
 
249
    nsCOMPtr<nsIServiceManager> servMgr( do_QueryInterface( aServMgr, &rv ) );
 
250
    if ( NS_FAILED( rv ) )
 
251
        return rv;
 
252
  
 
253
    nsCOMPtr<nsIComponentManager> compMgr = 
 
254
             do_GetService( kComponentManagerCID, &rv );
 
255
    if ( NS_FAILED( rv ) )
 
256
        return rv;
 
257
  
 
258
    // Register the plugin control portion.
 
259
    rv = compMgr->RegisterComponent(knsSanePluginControlCID,
 
260
                                    "SANE Plugin Control",
 
261
                                    "@mozilla.org/plugins/sane-control;1",
 
262
                                    aPath, PR_TRUE, PR_TRUE );
 
263
  
 
264
    // Register the plugin portion.
 
265
    nsString contractID;
 
266
    contractID.AssignWithConversion( NS_INLINE_PLUGIN_CONTRACTID_PREFIX );
 
267
 
 
268
    contractID.AppendWithConversion(PLUGIN_MIME_TYPE);
 
269
    buf = ( char * )calloc( 2000, sizeof( char ) );
 
270
    contractID.ToCString( buf, 1999 );
 
271
  
 
272
    rv = compMgr->RegisterComponent( knsSanePluginInst,
 
273
                                     "SANE Plugin Component",
 
274
                                     buf,
 
275
                                     aPath, PR_TRUE, PR_TRUE);
 
276
    free( buf );
 
277
  
 
278
    if ( NS_FAILED( rv ) )
 
279
        return rv;
 
280
  
 
281
    return NS_OK;
 
282
 
 
283
}
 
284
 
 
285
extern "C" PR_IMPLEMENT( nsresult )
 
286
NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
 
287
{
 
288
    nsresult rv;
 
289
  
 
290
    nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
 
291
    if (NS_FAILED(rv)) return rv;
 
292
  
 
293
    nsCOMPtr<nsIComponentManager> compMgr = 
 
294
             do_GetService(kComponentManagerCID, &rv);
 
295
    if (NS_FAILED(rv)) return rv;
 
296
  
 
297
    rv = compMgr->UnregisterComponent(knsSanePluginControlCID, aPath);
 
298
    if (NS_FAILED(rv)) return rv;
 
299
  
 
300
    return NS_OK;
 
301
}
 
302
 
 
303
 
 
304
 
 
305
 
 
306
 
 
307
 
 
308
 
 
309
 
 
310
 
 
311
 
 
312
 
 
313
 
 
314
 
 
315
 
 
316
 
 
317
 
 
318
 
 
319