File: nsSanePluginFactory.cpp

package info (click to toggle)
iceweasel 2.0.0.19-0etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 298,784 kB
  • ctags: 317,912
  • sloc: cpp: 1,796,902; ansic: 987,677; xml: 109,036; makefile: 47,777; asm: 35,201; perl: 26,983; sh: 20,879; cs: 6,232; java: 5,513; python: 3,249; pascal: 459; lex: 306; php: 244; csh: 132; objc: 97; yacc: 79; ada: 49; awk: 14; sql: 4; sed: 4
file content (342 lines) | stat: -rw-r--r-- 9,736 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org Code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Rusty Lynch <rusty.lynch@intel.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Implements the SANE plugin factory class.
 */

#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsSanePlugin_CID.h"
#include "nsSanePlugin.h"
#include "nsSanePluginFactory.h"
#include "plstr.h"

#define PLUGIN_NAME             "SANE Plugin"
#define PLUGIN_DESCRIPTION      "SANE Plugin is a generic scanner interface"
#define PLUGIN_MIME_DESCRIPTION "application/X-sane-plugin::Scanner/Camera"
#define PLUGIN_MIME_TYPE        "application/X-sane-plugin"

static NS_DEFINE_IID( kISupportsIID,            NS_ISUPPORTS_IID           );
static NS_DEFINE_IID( kIFactoryIID,             NS_IFACTORY_IID            );
static NS_DEFINE_IID( kIPluginIID,              NS_IPLUGIN_IID             );
static NS_DEFINE_CID( kComponentManagerCID,     NS_COMPONENTMANAGER_CID    );
static NS_DEFINE_CID( knsSanePluginControlCID,  NS_SANE_PLUGIN_CONTROL_CID );
static NS_DEFINE_CID( knsSanePluginInst,        NS_SANE_PLUGIN_CID         );

////////////////////////////////////////////////////////////////////////

nsSanePluginFactoryImpl::nsSanePluginFactoryImpl( const nsCID &aClass,
                                                  const char* className,
                                                  const char* contractID )
    : mClassID(aClass), mClassName(className), mContractID(contractID)
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::nsSanePluginFactoryImpl()\n");
#endif
}

nsSanePluginFactoryImpl::~nsSanePluginFactoryImpl()
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::~nsSanePluginFactoryImpl()\n");
#endif

    printf("mRefCnt = %i\n", mRefCnt);
    NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction");
}

NS_IMETHODIMP
nsSanePluginFactoryImpl::QueryInterface(const nsIID &aIID, 
                                        void **aInstancePtr)
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::QueryInterface()\n");
#endif

    if (!aInstancePtr)
        return NS_ERROR_NULL_POINTER;

    if (aIID.Equals(kISupportsIID)) {
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,this);
    } else if (aIID.Equals(kIFactoryIID)) {
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,
                                       NS_STATIC_CAST(nsIFactory*,this));
    } else if (aIID.Equals(kIPluginIID)) {
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,
                                       NS_STATIC_CAST(nsIPlugin*,this));
    } else {
        *aInstancePtr = nsnull;
        return NS_ERROR_NO_INTERFACE;
    }

    NS_ADDREF(NS_REINTERPRET_CAST(nsISupports*,*aInstancePtr));

    return NS_OK;
}

// Standard implementation of AddRef and Release
NS_IMPL_ADDREF( nsSanePluginFactoryImpl )
NS_IMPL_RELEASE( nsSanePluginFactoryImpl )

NS_IMETHODIMP
nsSanePluginFactoryImpl::CreateInstance( nsISupports *aOuter,
                                         const nsIID &aIID,
                                         void **aResult)
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::CreateInstance()\n");
#endif

    if ( !aResult )
        return NS_ERROR_NULL_POINTER;
  
    if ( aOuter )
        return NS_ERROR_NO_AGGREGATION;

    nsSanePluginInstance * inst = new nsSanePluginInstance();
    if (!inst)
        return NS_ERROR_OUT_OF_MEMORY;
           
    inst->AddRef();
    *aResult = inst;
    return NS_OK;
}

nsresult 
nsSanePluginFactoryImpl::LockFactory(PRBool aLock)
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::LockFactory()\n");
#endif

    // Needs to be implemented

    return NS_OK;
}


NS_METHOD
nsSanePluginFactoryImpl::Initialize()
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::Initialize()\n");
#endif

    return NS_OK;
}


NS_METHOD
nsSanePluginFactoryImpl::Shutdown( void )
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::Shutdown()\n");
#endif

    return NS_OK;
}


NS_METHOD 
nsSanePluginFactoryImpl::GetMIMEDescription(const char* *result)
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::GetMIMEDescription()\n");
#endif

    // caller is responsible for releasing
    *result = PL_strdup( PLUGIN_MIME_DESCRIPTION );

    return NS_OK;
}

NS_METHOD
nsSanePluginFactoryImpl::GetValue( nsPluginVariable variable, void *value )
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::GetValue()\n");
#endif

    nsresult err = NS_OK;

    if ( variable == nsPluginVariable_NameString ) {
    
        *( ( char ** )value ) = strdup( PLUGIN_NAME );

    } else if ( variable == nsPluginVariable_DescriptionString ) {

        *( ( char ** )value ) = strdup( PLUGIN_DESCRIPTION );

    } else {
    
        err = NS_ERROR_FAILURE;

    }
  
    return err;
}


NS_IMETHODIMP 
nsSanePluginFactoryImpl::CreatePluginInstance( nsISupports *aOuter, 
                                               REFNSIID aIID, 
                                               const char* aPluginMIMEType,
                                               void **aResult)
{
#ifdef DEBUG
    printf("nsSanePluginFactoryImpl::CreatePluginInstance()\n");
#endif

    // Need to find out what this is used for.  The npsimple
    // plugin looks like it just does a CreateInstance and 
    // ignores the mime type.
    return NS_ERROR_NOT_IMPLEMENTED;
}


////////////////////////////////////////////////////////////////////////

/**
 * The XPCOM runtime will call this to get a new factory object for the
 * CID/contractID it passes in.  XPCOM is responsible for caching the resulting
 * factory.
 *
 * return the proper factory to the caller
 */
extern "C" PR_IMPLEMENT(nsresult)
NSGetFactory( nsISupports* aServMgr,
              const nsCID &aClass,
              const char *aClassName,
              const char *aContractID,
              nsIFactory **aFactory)
{
    if (! aFactory)
        return NS_ERROR_NULL_POINTER;
  
    nsSanePluginFactoryImpl* factory = new nsSanePluginFactoryImpl(aClass, 
                                                                   aClassName,
                                                                   aContractID);
    if ( factory == nsnull )
        return NS_ERROR_OUT_OF_MEMORY;
  
    NS_ADDREF(factory);
    *aFactory = factory;
    return NS_OK;

}

char *buf;

extern "C" PR_IMPLEMENT( nsresult )
NSRegisterSelf( nsISupports* aServMgr, const char* aPath )
{
    nsresult rv;
  
    nsCOMPtr<nsIServiceManager> servMgr( do_QueryInterface( aServMgr, &rv ) );
    if ( NS_FAILED( rv ) )
        return rv;
  
    nsCOMPtr<nsIComponentManager> compMgr = 
             do_GetService( kComponentManagerCID, &rv );
    if ( NS_FAILED( rv ) )
        return rv;
  
    // Register the plugin control portion.
    rv = compMgr->RegisterComponent(knsSanePluginControlCID,
                                    "SANE Plugin Control",
                                    "@mozilla.org/plugins/sane-control;1",
                                    aPath, PR_TRUE, PR_TRUE );
  
    // Register the plugin portion.
    nsString contractID;
    contractID.AssignWithConversion( NS_INLINE_PLUGIN_CONTRACTID_PREFIX );

    contractID.AppendWithConversion(PLUGIN_MIME_TYPE);
    buf = ( char * )calloc( 2000, sizeof( char ) );
    contractID.ToCString( buf, 1999 );
  
    rv = compMgr->RegisterComponent( knsSanePluginInst,
                                     "SANE Plugin Component",
                                     buf,
                                     aPath, PR_TRUE, PR_TRUE);
    free( buf );
  
    if ( NS_FAILED( rv ) )
        return rv;
  
    return NS_OK;

}

extern "C" PR_IMPLEMENT( nsresult )
NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
{
    nsresult rv;
  
    nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
    if (NS_FAILED(rv)) return rv;
  
    nsCOMPtr<nsIComponentManager> compMgr = 
             do_GetService(kComponentManagerCID, &rv);
    if (NS_FAILED(rv)) return rv;
  
    rv = compMgr->UnregisterComponent(knsSanePluginControlCID, aPath);
    if (NS_FAILED(rv)) return rv;
  
    return NS_OK;
}