2
* The contents of this file are subject to the Mozilla Public
3
* License Version 1.1 (the "MPL"); you may not use this file
4
* except in compliance with the MPL. You may obtain a copy of
5
* the MPL at http://www.mozilla.org/MPL/
7
* Software distributed under the MPL is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the MPL for the specific language governing
10
* rights and limitations under the MPL.
12
* The Original Code is XMLterm.
14
* The Initial Developer of the Original Code is Ramalingam Saravanan.
15
* Portions created by Ramalingam Saravanan <svn@xmlterm.org> are
16
* Copyright (C) 1999 Ramalingam Saravanan. All Rights Reserved.
19
* Pierre Phaneuf <pp@ludusdesign.com>
22
// mozXMLTermShell.cpp: implementation of mozIXMLTermShell
23
// providing an XPCONNECT wrapper to the XMLTerminal interface,
24
// thus allowing easy (and controlled) access from scripts
28
#undef NEW_XMLTERM_IMP
34
#include "nsIDocumentViewer.h"
35
#include "nsIDocument.h"
37
#include "nsIDocShell.h"
38
#include "nsIPresShell.h"
39
#include "nsIPresContext.h"
40
#include "nsIScriptGlobalObject.h"
42
#include "nsIServiceManager.h"
44
#include "nsIAppShellService.h"
45
#include "nsAppShellCIDs.h"
47
#include "nsIDOMDocument.h"
48
#include "nsISelection.h"
49
#include "nsIDOMWindowInternal.h"
52
#include "mozLineTerm.h"
53
#include "mozXMLTermUtils.h"
54
#include "mozXMLTermShell.h"
57
static NS_DEFINE_IID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID);
60
/////////////////////////////////////////////////////////////////////////
61
// mozXMLTermShell implementation
62
/////////////////////////////////////////////////////////////////////////
64
NS_GENERIC_FACTORY_CONSTRUCTOR(mozXMLTermShell)
66
NS_IMPL_THREADSAFE_ISUPPORTS1(mozXMLTermShell,
69
PRBool mozXMLTermShell::mLoggingInitialized = PR_FALSE;
72
mozXMLTermShell::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
75
if (!mLoggingInitialized) {
76
// Set initial debugging message level for XMLterm
78
char* debugStr = (char*) PR_GetEnv("XMLT_DEBUG");
80
if (debugStr && (strlen(debugStr) == 1)) {
86
tlog_set_level(XMLT_TLOG_MODULE, messageLevel, debugStr);
87
mLoggingInitialized = PR_TRUE;
90
return mozXMLTermShellConstructor( aOuter,
96
mozXMLTermShell::RegisterProc(nsIComponentManager *aCompMgr,
98
const char *registryLocation,
99
const char *componentType,
100
const nsModuleComponentInfo *info)
102
// Component specific actions at registration time
103
PR_LogPrint("mozXMLTermShell::RegisterProc: registered mozXMLTermShell\n");
108
mozXMLTermShell::UnregisterProc(nsIComponentManager *aCompMgr,
110
const char *registryLocation,
111
const nsModuleComponentInfo *info)
113
// Component specific actions at unregistration time
114
return NS_OK; // Return value is not used
118
mozXMLTermShell::mozXMLTermShell() :
119
mInitialized(PR_FALSE),
120
mContentWindow(nsnull),
121
mContentAreaDocShell(nsnull),
126
mozXMLTermShell::~mozXMLTermShell()
133
NS_IMETHODIMP mozXMLTermShell::GetCurrentEntryNumber(PRInt32 *aNumber)
136
return mXMLTerminal->GetCurrentEntryNumber(aNumber);
138
return NS_ERROR_NOT_INITIALIZED;
143
/** Sets command history buffer count
144
* @param aHistory history buffer count
145
* @param aCookie document.cookie string for authentication
147
NS_IMETHODIMP mozXMLTermShell::SetHistory(PRInt32 aHistory,
148
const PRUnichar* aCookie)
152
PRBool matchesCookie;
153
result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
154
if (NS_FAILED(result) || !matchesCookie)
155
return NS_ERROR_FAILURE;
157
return mXMLTerminal->SetHistory(aHistory);
160
return NS_ERROR_NOT_INITIALIZED;
165
/** Sets command prompt
166
* @param aPrompt command prompt string (HTML)
167
* @param aCookie document.cookie string for authentication
169
NS_IMETHODIMP mozXMLTermShell::SetPrompt(const PRUnichar* aPrompt,
170
const PRUnichar* aCookie)
174
PRBool matchesCookie;
175
result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
176
if (NS_FAILED(result) || !matchesCookie)
177
return NS_ERROR_FAILURE;
179
return mXMLTerminal->SetPrompt(aPrompt);
182
return NS_ERROR_NOT_INITIALIZED;
187
/** Exports HTML to file, with META REFRESH, if refreshSeconds is non-zero.
188
* Nothing is done if display has not changed since last export, unless
189
* forceExport is true. Returns true if export actually takes place.
190
* If filename is a null string, HTML is written to STDERR.
192
NS_IMETHODIMP mozXMLTermShell::ExportHTML(const PRUnichar* aFilename,
194
const PRUnichar* style,
195
PRUint32 refreshSeconds,
197
const PRUnichar* aCookie,
201
return NS_ERROR_NOT_INITIALIZED;
204
PRBool matchesCookie;
205
result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
206
if (NS_FAILED(result) || !matchesCookie)
207
return NS_ERROR_FAILURE;
209
return mXMLTerminal->ExportHTML( aFilename, permissions, style,
210
refreshSeconds, forceExport,
214
/** Ignore key press events
215
* (workaround for form input being transmitted to xmlterm)
216
* @param aIgnore ignore flag (PR_TRUE/PR_FALSE)
217
* @param aCookie document.cookie string for authentication
219
NS_IMETHODIMP mozXMLTermShell::IgnoreKeyPress(PRBool aIgnore,
220
const PRUnichar* aCookie)
224
PRBool matchesCookie;
225
result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
226
if (NS_FAILED(result) || !matchesCookie)
227
return NS_ERROR_FAILURE;
229
return mXMLTerminal->SetKeyIgnore(aIgnore);
232
return NS_ERROR_NOT_INITIALIZED;
237
// Initialize XMLTermShell
239
mozXMLTermShell::Init(nsIDOMWindowInternal* aContentWin,
240
const PRUnichar* URL,
241
const PRUnichar* args)
245
XMLT_LOG(mozXMLTermShell::Init,10,("\n"));
248
return NS_ERROR_ALREADY_INITIALIZED;
251
return NS_ERROR_NULL_POINTER;
253
mInitialized = PR_TRUE;
255
mContentWindow = aContentWin; // no addref
257
nsCOMPtr<nsIScriptGlobalObject> globalObj = do_QueryInterface(mContentWindow,
259
if (NS_FAILED(result) || !globalObj)
260
return NS_ERROR_FAILURE;
262
nsIDocShell *docShell = globalObj->GetDocShell();
264
return NS_ERROR_FAILURE;
266
mContentAreaDocShell = docShell; // SVN: does this assignment addref?
268
// Create XMLTerminal
269
nsCOMPtr<mozIXMLTerminal> newXMLTerminal = do_CreateInstance(
270
MOZXMLTERMINAL_CONTRACTID,
272
if(NS_FAILED(result))
275
// Initialize XMLTerminal with non-owning reference to us
276
result = newXMLTerminal->Init(mContentAreaDocShell, this, URL, args);
278
if (NS_FAILED(result))
281
mXMLTerminal = newXMLTerminal;
286
/** Closes XMLterm, freeing resources
287
* @param aCookie document.cookie string for authentication
290
mozXMLTermShell::Close(const PRUnichar* aCookie)
292
XMLT_LOG(mozXMLTermShell::Close,10,("\n"));
294
if (mInitialized && mXMLTerminal) {
296
PRBool matchesCookie;
297
result = mXMLTerminal->MatchesCookie(aCookie, &matchesCookie);
298
if (NS_FAILED(result) || !matchesCookie)
299
return NS_ERROR_FAILURE;
308
// De-initialize XMLTermShell and free resources
310
mozXMLTermShell::Finalize(void)
315
XMLT_LOG(mozXMLTermShell::Finalize,10,("\n"));
317
mInitialized = PR_FALSE;
320
// Finalize and release reference to XMLTerm object owned by us
321
mXMLTerminal->Finalize();
322
mXMLTerminal = nsnull;
325
mContentAreaDocShell = nsnull;
326
mContentWindow = nsnull;
328
XMLT_LOG(mozXMLTermShell::Finalize,12,("END\n"));
334
// Poll for readable data from XMLTerminal
335
NS_IMETHODIMP mozXMLTermShell::Poll(void)
338
return NS_ERROR_NOT_INITIALIZED;
340
return mXMLTerminal->Poll();
344
/** Resizes XMLterm to match a resized window.
346
NS_IMETHODIMP mozXMLTermShell::Resize(void)
349
return NS_ERROR_NOT_INITIALIZED;
351
return mXMLTerminal->Resize();
355
// Send string to LineTerm as if the user had typed it
356
NS_IMETHODIMP mozXMLTermShell::SendText(const PRUnichar* aString,
357
const PRUnichar* aCookie)
360
return NS_ERROR_FAILURE;
362
XMLT_LOG(mozXMLTermShell::SendText,10,("\n"));
364
return mXMLTerminal->SendText(aString, aCookie);
368
// Exit XMLTerm window
370
mozXMLTermShell::Exit()
372
nsIAppShellService* appShell = nsnull;
374
XMLT_LOG(mozXMLTermShell::Exit,10,("\n"));
376
// Create the Application Shell instance...
377
nsresult result = nsServiceManager::GetService(kAppShellServiceCID,
378
NS_GET_IID(nsIAppShellService),
379
(nsISupports**)&appShell);
380
if (NS_SUCCEEDED(result)) {
381
nsServiceManager::ReleaseService(kAppShellServiceCID, appShell);