1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Mozilla Browser.
16 * The Initial Developer of the Original Code is
17 * Fredrik Holmqvist <thesuckiestemail@yahoo.se>.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
22 * Sergei Dolgov <sergei_d@fi.tartu.ee>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 //This define requires DebugConsole (see BeBits.com) to be installed
39 //#define DC_PROGRAMNAME "firefox-bin"
40 # ifdef DC_PROGRAMNAME
41 #include <DebugConsole.h>
44 #include "nsIServiceManager.h"
45 #include "nsNativeAppSupportBase.h"
46 #include "nsICommandLineRunner.h"
48 #include "nsIProxyObjectManager.h"
49 //#include "nsIBrowserDOMWindow.h"
50 #include "nsPIDOMWindow.h"
51 #include "nsIDOMChromeWindow.h"
52 #include "nsIWindowMediator.h"
53 #include "nsXPIDLString.h"
54 #include "nsIBaseWindow.h"
55 #include "nsIWidget.h"
56 #include "nsIDocShell.h"
58 #include <Application.h>
59 #include <AppFileInfo.h>
60 #include <Resources.h>
65 // Two static helpers for future - if we decide to use OpenBrowserWindow, like we do in SeaMonkey
67 GetMostRecentWindow(const PRUnichar
* aType
, nsIDOMWindowInternal
** aWindow
)
70 nsCOMPtr
<nsIWindowMediator
> med(do_GetService( NS_WINDOWMEDIATOR_CONTRACTID
, &rv
));
76 nsCOMPtr
<nsIWindowMediator
> medProxy
;
77 rv
= NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD
, NS_GET_IID(nsIWindowMediator
),
78 med
, NS_PROXY_SYNC
| NS_PROXY_ALWAYS
,
79 getter_AddRefs(medProxy
));
82 return medProxy
->GetMostRecentWindow( aType
, aWindow
);
84 return NS_ERROR_FAILURE
;
88 ActivateWindow(nsIDOMWindowInternal
* aWindow
)
90 nsCOMPtr
<nsPIDOMWindow
> window(do_QueryInterface(aWindow
));
91 NS_ENSURE_TRUE(window
, NS_ERROR_FAILURE
);
92 nsCOMPtr
<nsIBaseWindow
> baseWindow(do_QueryInterface(window
->GetDocShell()));
93 NS_ENSURE_TRUE(baseWindow
, NS_ERROR_FAILURE
);
94 nsCOMPtr
<nsIWidget
> mainWidget
;
95 baseWindow
->GetMainWidget(getter_AddRefs(mainWidget
));
96 NS_ENSURE_TRUE(mainWidget
, NS_ERROR_FAILURE
);
97 BWindow
*bwindow
= (BWindow
*)(mainWidget
->GetNativeData(NS_NATIVE_WINDOW
));
99 bwindow
->Activate(true);
104 class nsNativeAppSupportBeOS
: public nsNativeAppSupportBase
108 NS_DECL_NSINATIVEAPPSUPPORT
109 static void HandleCommandLine( int32 argc
, char **argv
, PRUint32 aState
);
110 }; // nsNativeAppSupportBeOS
113 class nsBeOSApp
: public BApplication
116 nsBeOSApp(sem_id sem
) : BApplication( GetAppSig() ), init(sem
), mMessage(NULL
)
129 static int32
Main( void *args
)
131 nsBeOSApp
*app
= new nsBeOSApp((sem_id
)args
);
137 void ArgvReceived(int32 argc
, char **argv
)
141 #ifdef DC_PROGRAMNAME
142 TRACE("ArgvReceived Launching\n");
146 PRInt32 aState
= /*IsLaunching() ?
147 nsICommandLine::STATE_INITIAL_LAUNCH :*/
148 nsICommandLine::STATE_REMOTE_AUTO
;
149 nsNativeAppSupportBeOS::HandleCommandLine(argc
, argv
, aState
);
152 void RefsReceived(BMessage
* msg
)
154 #ifdef DC_PROGRAMNAME
155 TRACE("RefsReceived\n");
159 mMessage
= new BMessage(*msg
);
164 for (uint32 i
= 0; msg
->FindRef("refs", i
, &er
) == B_OK
; i
++)
167 char **Argv
= new char*[ 3 ];
168 BEntry
entry(&er
, true);
169 BEntry
fentry(GetAppFile(), false);
170 entry
.GetPath(&path
);
172 Argv
[0] = strdup( GetAppFile() ? GetAppFile() : "" );
173 Argv
[1] = strdup( path
.Path() ? path
.Path() : "" );
176 // Is started, call ArgReceived, delete mArgv, else store for future usage
177 // after ::Enable() was called
178 ArgvReceived(2, Argv
);
185 void MessageReceived(BMessage
* msg
)
187 // BMessage from nsNativeAppBeOS::Enable() received.
188 // Services are ready, so we can supply stored refs
189 if (msg
->what
== 'enbl' && mMessage
)
191 #ifdef DC_PROGRAMNAME
192 TRACE("enbl received");
194 be_app_messenger
.SendMessage(mMessage
);
196 // Processing here file drop events from BWindow
197 // - until we implement native DnD in widget.
198 else if (msg
->what
== B_SIMPLE_DATA
)
203 BApplication::MessageReceived(msg
);
211 BAppFileInfo appFileInfo
;
212 static char sig
[B_MIME_TYPE_LENGTH
];
215 if (get_next_image_info(0, &cookie
, &info
) == B_OK
&&
216 file
.SetTo(info
.name
, B_READ_ONLY
) == B_OK
&&
217 appFileInfo
.SetTo(&file
) == B_OK
&&
218 appFileInfo
.GetSignature(sig
) == B_OK
)
221 return "application/x-vnd.Mozilla";
228 if (get_next_image_info(0, &cookie
, &info
) == B_OK
&& strlen(info
.name
) > 0)
238 // Create and return an instance of class nsNativeAppSupportBeOS.
240 NS_CreateNativeAppSupport(nsINativeAppSupport
**aResult
)
243 return NS_ERROR_NULL_POINTER
;
245 nsNativeAppSupportBeOS
*pNative
= new nsNativeAppSupportBeOS
;
247 return NS_ERROR_OUT_OF_MEMORY
;
254 NS_IMPL_ISUPPORTS1(nsNativeAppSupportBeOS
, nsINativeAppSupport
)
258 nsNativeAppSupportBeOS::HandleCommandLine(int32 argc
, char **argv
, PRUint32 aState
)
261 // Here we get stuck when starting from file-click or "OpenWith".
262 // No cmdLine or any other service can be created
263 // To workaround the problem, we store arguments if IsLaunching()
264 // and using this after ::Enable() was called.
265 nsCOMPtr
<nsICommandLineRunner
> cmdLine(do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
268 #ifdef DC_PROGRAMNAME
269 TRACE("Couldn't create command line!");
274 // nsICommandLineRunner::Init() should be called from main mozilla thread
275 // but we are at be_app thread. Using proxy to switch thread
276 nsCOMPtr
<nsICommandLineRunner
> cmdLineProxy
;
277 rv
= NS_GetProxyForObject( NS_PROXY_TO_MAIN_THREAD
, NS_GET_IID(nsICommandLineRunner
),
278 cmdLine
, NS_PROXY_ASYNC
| NS_PROXY_ALWAYS
, getter_AddRefs(cmdLineProxy
));
281 #ifdef DC_PROGRAMNAME
282 TRACE("Couldn't get command line Proxy!");
287 // nsICommandLineRunner::Init(,,workingdir,) requires some folder to be provided
288 // but that's unclear if we need it, so using 0 instead atm
289 rv
= cmdLine
->Init(argc
, argv
, 0 , aState
);
292 #ifdef DC_PROGRAMNAME
293 TRACE("Couldn't init command line!");
298 nsCOMPtr
<nsIDOMWindowInternal
> navWin
;
299 GetMostRecentWindow( NS_LITERAL_STRING( "navigator:browser" ).get(),
300 getter_AddRefs(navWin
));
303 # ifdef DC_PROGRAMNAME
306 cmdLine
->SetWindowContext(navWin
);
309 // TODO: try to use OpenURI here if there is navWin, maybe using special function
310 // OpenBrowserWindow which calls OpenURI like we do for SeaMonkey,
311 // else let CommandLineRunner to do its work.
312 // Problem with current implementation is unsufficient tabbed browsing support
317 nsNativeAppSupportBeOS::Start(PRBool
*aResult
)
319 NS_ENSURE_ARG(aResult
);
320 NS_ENSURE_TRUE(be_app
== NULL
, NS_ERROR_NOT_INITIALIZED
);
321 sem_id initsem
= create_sem(0, "Mozilla BApplication init");
323 return NS_ERROR_FAILURE
;
324 thread_id tid
= spawn_thread(nsBeOSApp::Main
, "Mozilla XUL BApplication", B_NORMAL_PRIORITY
, (void *)initsem
);
325 #ifdef DC_PROGRAMNAME
326 TRACE("BeApp created");
329 if (tid
< B_OK
|| B_OK
!= resume_thread(tid
))
332 if (B_OK
!= acquire_sem(initsem
))
335 if (B_OK
!= delete_sem(initsem
))
337 return *aResult
== PR_TRUE
? NS_OK
: NS_ERROR_FAILURE
;
341 nsNativeAppSupportBeOS::Stop(PRBool
*aResult
)
343 NS_ENSURE_ARG(aResult
);
344 NS_ENSURE_TRUE(be_app
, NS_ERROR_NOT_INITIALIZED
);
351 nsNativeAppSupportBeOS::Quit()
358 return NS_ERROR_FAILURE
;
362 nsNativeAppSupportBeOS::ReOpen()
364 return NS_ERROR_NOT_IMPLEMENTED
;
368 nsNativeAppSupportBeOS::Enable()
370 // Informing be_app that UI and services are ready to use.
373 be_app_messenger
.SendMessage('enbl');
379 nsNativeAppSupportBeOS::OnLastWindowClosing()