Bug 575195 - Send size mode events to dom from web shell window, and insure new windo...
[mozilla-central.git] / extensions / jssh / nsJSShServer.cpp
blob705d65ddf5753db6d6d8afea8a92876e31fdead3
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla JavaScript Shell project.
17 * The Initial Developer of the Original Code is
18 * Alex Fritze.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Alex Fritze <alex@croczilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsJSShServer.h"
40 #include "nsNetCID.h"
41 #include "nsISocketTransport.h"
42 #include "nsIComponentManager.h"
43 #include "nsIInputStream.h"
44 #include "nsIOutputStream.h"
45 #include "nsThreadUtils.h"
46 #include "nsJSSh.h"
47 #include "nsComponentManagerUtils.h"
49 //**********************************************************************
50 // ConnectionListener helper class
52 class ConnectionListener : public nsIServerSocketListener
54 public:
55 ConnectionListener(const nsACString &startupURI);
56 ~ConnectionListener();
57 NS_DECL_ISUPPORTS
58 NS_DECL_NSISERVERSOCKETLISTENER
60 private:
61 nsCString mStartupURI;
64 //----------------------------------------------------------------------
65 // Implementation
67 ConnectionListener::ConnectionListener(const nsACString &startupURI)
68 : mStartupURI(startupURI)
72 ConnectionListener::~ConnectionListener()
77 already_AddRefed<nsIServerSocketListener> CreateConnectionListener(const nsACString &startupURI)
79 nsIServerSocketListener* obj = new ConnectionListener(startupURI);
80 NS_IF_ADDREF(obj);
81 return obj;
84 //----------------------------------------------------------------------
85 // nsISupports methods:
87 NS_IMPL_THREADSAFE_ADDREF(ConnectionListener)
88 NS_IMPL_THREADSAFE_RELEASE(ConnectionListener)
90 NS_INTERFACE_MAP_BEGIN(ConnectionListener)
91 NS_INTERFACE_MAP_ENTRY(nsISupports)
92 NS_INTERFACE_MAP_ENTRY(nsIServerSocketListener)
93 NS_INTERFACE_MAP_END
95 //----------------------------------------------------------------------
96 // nsIServerSocketListener methods:
98 /* void onSocketAccepted (in nsIServerSocket aServ, in nsISocketTransport aTransport); */
99 NS_IMETHODIMP ConnectionListener::OnSocketAccepted(nsIServerSocket *aServ, nsISocketTransport *aTransport)
101 nsCOMPtr<nsIInputStream> input;
102 nsCOMPtr<nsIOutputStream> output;
103 aTransport->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input));
104 aTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output));
106 LOG(("JSSh server: new connection!\n"));
108 nsCOMPtr<nsIRunnable> shell = CreateJSSh(input, output, mStartupURI);
110 nsCOMPtr<nsIThread> thread;
111 NS_NewThread(getter_AddRefs(thread), shell);
112 return NS_OK;
115 /* void onStopListening (in nsIServerSocket aServ, in nsresult aStatus); */
116 NS_IMETHODIMP ConnectionListener::OnStopListening(nsIServerSocket *aServ, nsresult aStatus)
118 LOG(("JSSh server: stopped listening!\n"));
119 return NS_OK;
122 //**********************************************************************
123 // nsJSShServer implementation
125 nsJSShServer::nsJSShServer()
127 LOG(("nsJSShServer ctor\n"));
130 nsJSShServer::~nsJSShServer()
132 LOG(("nsJSShServer dtor\n"));
133 // XXX should we stop listening or not??
134 StopServerSocket();
137 //----------------------------------------------------------------------
138 // nsISupports methods:
140 NS_IMPL_ADDREF(nsJSShServer)
141 NS_IMPL_RELEASE(nsJSShServer)
143 NS_INTERFACE_MAP_BEGIN(nsJSShServer)
144 NS_INTERFACE_MAP_ENTRY(nsISupports)
145 NS_INTERFACE_MAP_ENTRY(nsIJSShServer)
146 NS_INTERFACE_MAP_END
148 //----------------------------------------------------------------------
149 // nsIJSShServer methods:
151 /* void startServerSocket (in unsigned long port, in AUTF8String startupURI,
152 in boolean loopbackOnly); */
153 NS_IMETHODIMP
154 nsJSShServer::StartServerSocket(PRUint32 port, const nsACString & startupURI,
155 PRBool loopbackOnly)
157 if (mServerSocket) {
158 NS_ERROR("server socket already running");
159 return NS_ERROR_FAILURE;
162 mServerSocket = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID);
163 if (!mServerSocket) {
164 NS_ERROR("could not create server socket");
165 return NS_ERROR_FAILURE;
168 if (NS_FAILED(mServerSocket->Init(port, loopbackOnly, -1))) {
169 mServerSocket = nsnull;
170 NS_ERROR("server socket initialization error");
171 return NS_ERROR_FAILURE;
174 nsCOMPtr<nsIServerSocketListener> listener = CreateConnectionListener(startupURI);
175 mServerSocket->AsyncListen(listener);
177 mServerPort = port;
178 mServerStartupURI = startupURI;
179 mServerLoopbackOnly = loopbackOnly;
181 return NS_OK;
184 /* void stopServerSocket (); */
185 NS_IMETHODIMP nsJSShServer::StopServerSocket()
187 if (mServerSocket)
188 mServerSocket->Close();
189 mServerSocket = nsnull;
190 mServerPort = 0;
191 mServerStartupURI.Truncate();
192 mServerLoopbackOnly = PR_FALSE;
193 return NS_OK;
196 /* void runShell (in nsIInputStream input, in nsIOutputStream output,
197 in string startupURI, in boolean blocking); */
198 NS_IMETHODIMP
199 nsJSShServer::RunShell(nsIInputStream *input, nsIOutputStream *output,
200 const char *startupURI, PRBool blocking)
202 nsCOMPtr<nsIRunnable> shell = CreateJSSh(input, output, nsCString(startupURI));
203 if (!blocking) {
204 nsCOMPtr<nsIThread> thread;
205 NS_NewThread(getter_AddRefs(thread), shell);
207 else
208 shell->Run();
210 return NS_OK;
213 /* readonly attribute boolean serverListening; */
214 NS_IMETHODIMP
215 nsJSShServer::GetServerListening(PRBool *aServerListening)
217 if (mServerSocket)
218 *aServerListening = PR_TRUE;
219 else
220 *aServerListening = PR_FALSE;
222 return NS_OK;
225 /* readonly attribute unsigned long serverPort; */
226 NS_IMETHODIMP
227 nsJSShServer::GetServerPort(PRUint32 *aServerPort)
229 *aServerPort = mServerPort;
230 return NS_OK;
233 /* readonly attribute AUTF8String serverStartupURI; */
234 NS_IMETHODIMP
235 nsJSShServer::GetServerStartupURI(nsACString & aServerStartupURI)
237 aServerStartupURI = mServerStartupURI;
238 return NS_OK;
241 /* readonly attribute boolean serverLoopbackOnly; */
242 NS_IMETHODIMP
243 nsJSShServer::GetServerLoopbackOnly(PRBool *aServerLoopbackOnly)
245 *aServerLoopbackOnly = mServerLoopbackOnly;
246 return NS_OK;