aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/MozRegisterComponents.cpp
blob: 3c5e64be76c65d0cafdf6a354b79122a9b79582d (plain) (blame)
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
/*
 *  Copyright (C) 2001 Philip Langdale
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "StartHereProtocolHandler.h"
#include "ContentHandler.h"
#include "ExternalProtocolService.h"
#include "FilePicker.h"
#include "FtpProtocolHandler.h"
#include "IRCProtocolHandler.h"
#include "MailtoProtocolHandler.h"
#include "PromptService.h"
#include "PrintingPromptService.h"
#include "ProgressListener.h"

#include <nsIFactory.h>
#include <nsIComponentManager.h>
#include <nsCOMPtr.h>
#include <nsILocalFile.h>

#include <glib.h>

static NS_DEFINE_CID(kContentHandlerCID, G_CONTENTHANDLER_CID);
static NS_DEFINE_CID(kProtocolServiceCID, G_EXTERNALPROTOCOLSERVICE_CID);
static NS_DEFINE_CID(kFilePickerCID, G_FILEPICKER_CID);
static NS_DEFINE_CID(kStartHereProcotolHandlerCID, G_START_HERE_PROTOCOLHANDLER_CID);
static NS_DEFINE_CID(knsFtpProtocolHandlerCID, NS_FTPPROTOCOLHANDLER_CID);
static NS_DEFINE_CID(kFtpHandlerCID, G_FTP_PROTOCOL_CID);
static NS_DEFINE_CID(kIRCHandlerCID, G_IRC_PROTOCOL_CID);
static NS_DEFINE_CID(kMailtoHandlerCID, G_MAILTO_PROTOCOL_CID);
static NS_DEFINE_CID(kPromptServiceCID, G_PROMPTSERVICE_CID);
static NS_DEFINE_CID(kPrintingPromptServiceCID, G_PRINTINGPROMPTSERVICE_CID);
static NS_DEFINE_CID(kProgressDialogCID, G_PROGRESSDIALOG_CID);

//RegisterFactory is local
NS_METHOD RegisterFactory (nsresult (aFactoryFunc)(nsIFactory** aFactory),
               const nsCID & aClass, const char *aClassName,
               const char *aContractID, PRBool aReplace);

NS_METHOD RegisterComponent (const nsCID & aClass, const char *aClassName,
                 const char *aContractID, const char *aDLLPath,
                 PRBool aReplace);

//Annoying globals to track the mozilla ftp handler so it can be restored.
static PRBool ftpRegistered = PR_FALSE;
static nsCOMPtr<nsIFactory> nsFtpFactory;

/* FIXME why we need to use "C" here ???? */

extern  "C" gboolean
mozilla_register_components (void)
{
    gboolean ret = TRUE;
    nsresult rv;

        rv = RegisterFactory (NS_NewProgressListenerFactory, kProgressDialogCID,
                              G_PROGRESSDIALOG_CLASSNAME,
                              NS_DOWNLOAD_CONTRACTID, PR_TRUE);
        if (NS_FAILED(rv)) ret = FALSE;

    rv = RegisterFactory (NS_NewContentHandlerFactory, kContentHandlerCID,
                  NS_IHELPERAPPLAUNCHERDLG_CLASSNAME,
                  NS_IHELPERAPPLAUNCHERDLG_CONTRACTID, PR_TRUE);
    if (NS_FAILED(rv)) ret = FALSE;
    
    rv = RegisterFactory   (NS_NewExternalProtocolServiceFactory,
                kProtocolServiceCID,
                G_EXTERNALPROTOCOLSERVICE_CLASSNAME,
                NS_EXTERNALPROTOCOLSERVICE_CONTRACTID,
                PR_TRUE);
    if (NS_FAILED(rv)) ret = FALSE;

    rv = RegisterFactory (NS_NewFilePickerFactory, kFilePickerCID,
                  G_FILEPICKER_CLASSNAME, G_FILEPICKER_CONTRACTID,
                  PR_TRUE);
    if (NS_FAILED(rv)) ret = FALSE;

    rv = RegisterFactory (NS_NewStartHereHandlerFactory,
                  kStartHereProcotolHandlerCID,
                  G_START_HERE_PROTOCOLHANDLER_CLASSNAME,
                  G_START_HERE_PROTOCOLHANDLER_CONTRACTID,
                  PR_TRUE);
    if (NS_FAILED(rv)) ret = FALSE;

        rv = RegisterFactory (NS_NewFtpHandlerFactory, kFtpHandlerCID,
                  G_FTP_CONTENT_CLASSNAME, G_FTP_CONTENT_CONTRACTID,
                  PR_TRUE);
    if (NS_FAILED(rv)) ret = FALSE;

        rv = RegisterFactory (NS_NewIRCHandlerFactory, kIRCHandlerCID,
                  G_IRC_PROTOCOL_CLASSNAME,   
                  G_IRC_PROTOCOL_CONTRACTID, PR_TRUE);
        if (NS_FAILED(rv)) ret = FALSE;

        rv = RegisterFactory (NS_NewIRCHandlerFactory, kIRCHandlerCID,
                  G_IRC_CONTENT_CLASSNAME,   
                  G_IRC_CONTENT_CONTRACTID, PR_TRUE);        
        if (NS_FAILED(rv)) ret = FALSE;

        rv = RegisterFactory (NS_NewPromptServiceFactory, kPromptServiceCID,
                  G_PROMPTSERVICE_CLASSNAME,   
                  G_PROMPTSERVICE_CONTRACTID, PR_TRUE);        
        if (NS_FAILED(rv)) ret = FALSE;
    
    rv = RegisterFactory (NS_NewPrintingPromptServiceFactory,
                  kPrintingPromptServiceCID,
                  G_PRINTINGPROMPTSERVICE_CLASSNAME, 
                  G_PRINTINGPROMPTSERVICE_CONTRACTID, PR_TRUE);
    if (NS_FAILED(rv)) ret = FALSE;

    return ret;
}

NS_METHOD RegisterFactory (nsresult (aFactoryFunc)(nsIFactory** aFactory),
               const nsCID & aClass, const char *aClassName,
               const char *aContractID, PRBool aReplace)
{
    nsresult rv = NS_OK;

    nsCOMPtr<nsIFactory> factory;
    rv = aFactoryFunc(getter_AddRefs(factory));
    if (NS_FAILED(rv)) return rv;
    rv = nsComponentManager::RegisterFactory(aClass, aClassName,
                         aContractID,
                         factory, aReplace);
    return rv;
}

NS_METHOD RegisterComponent (const nsCID & aClass, const char *aClassName,
                 const char *aContractID, const char *aDLLPath,
                 PRBool aReplace)
{
    nsresult rv = NS_OK;

    nsCOMPtr<nsILocalFile> dllFile;
    rv = NS_NewLocalFile (NS_ConvertUTF8toUCS2(aDLLPath), PR_TRUE, getter_AddRefs (dllFile));
    if (NS_FAILED(rv)) return NS_ERROR_FAILURE;

    rv = nsComponentManager::RegisterComponentSpec (aClass,
                            aClassName,
                            aContractID,
                            dllFile,
                            aReplace,
                            PR_FALSE);
    return rv;
}

/**
 * mozilla_register_FtpProtocolHandler: Register Ftp Protocol Handler
 */
extern "C" gboolean 
mozilla_register_FtpProtocolHandler (void)
{
    if (ftpRegistered == PR_TRUE) return TRUE;

    nsresult rv = NS_OK;
     
        rv = nsComponentManager::FindFactory (knsFtpProtocolHandlerCID,
                                              getter_AddRefs(nsFtpFactory));
        if (NS_FAILED(rv)) return FALSE;

    rv = RegisterFactory (NS_NewFtpHandlerFactory, kFtpHandlerCID,
                  G_FTP_PROTOCOL_CLASSNAME, 
                  G_FTP_PROTOCOL_CONTRACTID, PR_TRUE);  

    if (NS_FAILED(rv)) return FALSE;

    ftpRegistered = PR_TRUE;
    return NS_SUCCEEDED (rv) ? TRUE : FALSE;
}

/**
 * mozilla_unregister_FtpProtocolHandler: Unregister Ftp Protocol Handler
 */
extern "C" gboolean 
mozilla_unregister_FtpProtocolHandler (void)
{
    if (ftpRegistered == PR_FALSE) return FALSE;
        
        nsresult rv = NS_OK;
    
    rv = nsComponentManager::RegisterFactory(knsFtpProtocolHandlerCID,
                         NS_FTPPROTOCOLHANDLER_CLASSNAME,
                         G_FTP_PROTOCOL_CONTRACTID,
                         nsFtpFactory, PR_TRUE);

    ftpRegistered = PR_FALSE;
        return NS_SUCCEEDED (rv) ? TRUE : FALSE;
}

/**
 * mozilla_register_MailtoProtocolHandler: Register Mailto Protocol Handler
 */
extern "C" gboolean 
mozilla_register_MailtoProtocolHandler (void)
{
        nsresult rv = NS_OK;

        rv = RegisterFactory (NS_NewMailtoHandlerFactory, kMailtoHandlerCID,
                  G_MAILTO_PROTOCOL_CLASSNAME,   
                  G_MAILTO_PROTOCOL_CONTRACTID, PR_TRUE);
        if (NS_FAILED(rv)) return FALSE;

        rv = RegisterFactory (NS_NewMailtoHandlerFactory, kMailtoHandlerCID,
                  G_MAILTO_CONTENT_CLASSNAME,   
                  G_MAILTO_CONTENT_CONTRACTID, PR_TRUE);        
        return NS_SUCCEEDED (rv) ? TRUE : FALSE;
}