Back to index

lightning-sunbird  0.9+nobinonly
nsNativeAppSupportBeOS.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Mozilla Browser.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Fredrik Holmqvist <thesuckiestemail@yahoo.se>.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036  
00037 //This define requires DebugConsole (see BeBits.com) to be installed
00038 //#define DC_PROGRAMNAME "firefox-bin"
00039 # ifdef DC_PROGRAMNAME
00040 #include <DebugConsole.h>
00041 #endif
00042 
00043 #include "nsIServiceManager.h"
00044 #include "nsNativeAppSupportBase.h"
00045 #include "nsICommandLineRunner.h"
00046 #include "nsCOMPtr.h"
00047 #include "nsIBrowserDOMWindow.h"
00048 #include "nsIProxyObjectManager.h"
00049 #include "nsIDOMWindowInternal.h"
00050 #include "nsIDOMChromeWindow.h"
00051 #include "nsIScriptGlobalObject.h"
00052 #include "nsIWindowMediator.h"
00053 #include "nsXPIDLString.h"
00054 #include "nsIBaseWindow.h"
00055 #include "nsIWidget.h"
00056 #include "nsIDocShell.h"
00057 
00058 #include <Application.h>
00059 #include <AppFileInfo.h>
00060 #include <Resources.h>
00061 #include <Path.h>
00062 #include <Window.h>
00063 #include <unistd.h>
00064 
00065 #if 0 // TODO for OpenURI, see comment in HandleCommandLine
00066 static nsresult
00067 GetMostRecentWindow(const PRUnichar* aType, nsIDOMWindowInternal** aWindow)
00068 {
00069     nsresult rv;
00070     nsCOMPtr<nsIWindowMediator> med(do_GetService( NS_WINDOWMEDIATOR_CONTRACTID, &rv));
00071     if (NS_FAILED(rv))
00072         return rv;
00073 
00074     if (med)
00075     {
00076         nsCOMPtr<nsIWindowMediator> medProxy;
00077         rv = NS_GetProxyForObject(NS_UI_THREAD_EVENTQ, NS_GET_IID(nsIWindowMediator), 
00078                                   med, PROXY_SYNC | PROXY_ALWAYS,
00079                                   getter_AddRefs(medProxy));
00080         if (NS_FAILED(rv))
00081             return rv;
00082         return medProxy->GetMostRecentWindow( aType, aWindow );
00083     }
00084     return NS_ERROR_FAILURE;
00085 }
00086 
00087 static nsresult
00088 ActivateWindow(nsIDOMWindowInternal* aWindow)
00089 {
00090        nsCOMPtr<nsIScriptGlobalObject> window(do_QueryInterface(aWindow));
00091        NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
00092     nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(window->GetDocShell()));
00093     NS_ENSURE_TRUE(baseWindow, NS_ERROR_FAILURE);
00094     nsCOMPtr<nsIWidget> mainWidget;
00095     baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
00096     NS_ENSURE_TRUE(mainWidget, NS_ERROR_FAILURE);
00097     BWindow *bwindow = (BWindow *)(mainWidget->GetNativeData(NS_NATIVE_WINDOW));
00098     if (bwindow)
00099         bwindow->Activate(true);
00100     return NS_OK;
00101 }
00102 #endif //0
00103 
00104 class nsNativeAppSupportBeOS : public nsNativeAppSupportBase
00105 {
00106 public:
00107     NS_DECL_ISUPPORTS
00108     NS_DECL_NSINATIVEAPPSUPPORT
00109     static void HandleCommandLine( int32 argc, char **argv, PRUint32 aState);
00110 }; // nsNativeAppSupportBeOS
00111 
00112 
00113 class nsBeOSApp : public BApplication
00114 {
00115 public:
00116     nsBeOSApp(sem_id sem) : BApplication( GetAppSig() ), init(sem), mMessage(NULL)
00117     {}
00118 
00119        ~nsBeOSApp()
00120        {
00121         delete mMessage;
00122        }
00123 
00124     void ReadyToRun() 
00125     {
00126         release_sem(init);
00127     }
00128 
00129     static int32 Main( void *args ) 
00130     {
00131         nsBeOSApp *app = new nsBeOSApp((sem_id)args);
00132         if (app == NULL)
00133             return B_ERROR;
00134         return app->Run();
00135     }
00136     
00137     void ArgvReceived(int32 argc, char **argv)
00138     {
00139         if (IsLaunching())
00140         {
00141 #ifdef DC_PROGRAMNAME
00142                      TRACE("ArgvReceived Launching\n");
00143 #endif
00144                      return;
00145         }
00146         PRInt32 aState = /*IsLaunching() ?
00147                          nsICommandLine::STATE_INITIAL_LAUNCH :*/
00148                          nsICommandLine::STATE_REMOTE_AUTO;
00149         nsNativeAppSupportBeOS::HandleCommandLine(argc, argv, aState);
00150     }
00151 
00152     void RefsReceived(BMessage* msg)
00153     {
00154 #ifdef DC_PROGRAMNAME
00155         TRACE("RefsReceived\n");
00156 #endif
00157         if (IsLaunching())
00158         {
00159            mMessage = new BMessage(*msg);
00160            return;
00161         }
00162         BPath path;
00163         entry_ref er;
00164         for (uint32 i = 0; msg->FindRef("refs", i, &er) == B_OK; i++)
00165         {
00166             int Argc = 2;
00167             char **Argv = new char*[ 3 ];
00168             BEntry entry(&er, true);
00169             BEntry fentry(GetAppFile(), false);
00170             entry.GetPath(&path);
00171             
00172             Argv[0] = strdup( GetAppFile() ? GetAppFile() : "" );
00173             Argv[1] = strdup( path.Path() ? path.Path() : "" );
00174             // Safety measure
00175             Argv[2] = 0;
00176             // Is started, call ArgReceived, delete mArgv, else store for future usage
00177             // after ::Enable() was called
00178             ArgvReceived(2, Argv);
00179             Argc = 0;
00180             delete [] Argv;
00181             Argv = NULL;
00182         } 
00183     }
00184     
00185     void MessageReceived(BMessage* msg)
00186     {
00187         // BMessage from nsNativeAppBeOS::Enable() received.
00188         // Services are ready, so we can supply stored refs
00189         if (msg->what == 'enbl' && mMessage)
00190         {
00191 #ifdef DC_PROGRAMNAME
00192             TRACE("enbl received");
00193 #endif
00194             be_app_messenger.SendMessage(mMessage);
00195         }
00196         // Processing here file drop events from BWindow
00197         // - until we implement native DnD in widget.
00198         else if (msg->what == B_SIMPLE_DATA)
00199         {
00200             RefsReceived(msg);
00201         }
00202         else
00203             BApplication::MessageReceived(msg);
00204     }
00205 private:
00206     char *GetAppSig()
00207     {
00208         image_info info;
00209         int32 cookie = 0;
00210         BFile file;
00211         BAppFileInfo appFileInfo;
00212         static char sig[B_MIME_TYPE_LENGTH];
00213 
00214         sig[0] = 0;
00215         if (get_next_image_info(0, &cookie, &info) == B_OK &&
00216             file.SetTo(info.name, B_READ_ONLY) == B_OK &&
00217             appFileInfo.SetTo(&file) == B_OK &&
00218             appFileInfo.GetSignature(sig) == B_OK)
00219             return sig;
00220 
00221         return "application/x-vnd.Mozilla";
00222     }
00223     
00224     char *GetAppFile()
00225     {
00226         image_info info;
00227         int32 cookie = 0;
00228         if (get_next_image_info(0, &cookie, &info) == B_OK && strlen(info.name) > 0)
00229             return info.name;
00230         
00231         return "";
00232     }
00233 
00234     sem_id init;
00235     BMessage *mMessage;
00236 }; //class nsBeOSApp
00237 
00238 // Create and return an instance of class nsNativeAppSupportBeOS.
00239 nsresult
00240 NS_CreateNativeAppSupport(nsINativeAppSupport **aResult)
00241 {
00242     if (!aResult)
00243         return NS_ERROR_NULL_POINTER;
00244 
00245     nsNativeAppSupportBeOS *pNative = new nsNativeAppSupportBeOS;
00246     if (!pNative)
00247         return NS_ERROR_OUT_OF_MEMORY;
00248 
00249     *aResult = pNative;
00250     NS_ADDREF(*aResult);
00251     return NS_OK;
00252 }
00253 
00254 NS_IMPL_ISUPPORTS1(nsNativeAppSupportBeOS, nsINativeAppSupport)
00255 
00256 
00257 void
00258 nsNativeAppSupportBeOS::HandleCommandLine(int32 argc, char **argv, PRUint32 aState)
00259 {
00260     nsresult rv;
00261     // Here we get stuck when starting from file-click or "OpenWith".
00262     // No cmdLine or any other service can be created
00263     // To workaround the problem, we store arguments if IsLaunching()
00264     // and using this after ::Enable() was called.
00265     nsCOMPtr<nsICommandLineRunner> cmdLine(do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
00266     if (!cmdLine)
00267     {
00268 #ifdef DC_PROGRAMNAME
00269         TRACE("Couldn't create command line!");
00270 #endif
00271         return;
00272     }
00273     // nsICommandLineRunner::Init() requires some folder to be provided
00274     // but that's unclear if we need it, so using 0 instead atm
00275     /*
00276     char wdir[MAXPATHLEN];
00277     nsCOMPtr<nsILocalFile> workingDir;
00278     NS_NewNativeLocalFile(nsDependentCString(getcwd((char *)wdir, MAXPATHLEN)),
00279                           PR_FALSE,
00280                           getter_AddRefs(workingDir));
00281     */
00282     
00283     // nsICommandLineRunner::Init() should be called from main mozilla thread
00284     // but we are at be_app thread. Using proxy to switch thread
00285     nsCOMPtr<nsICommandLineRunner> cmdLineProxy;
00286     rv = NS_GetProxyForObject(NS_UI_THREAD_EVENTQ, NS_GET_IID(nsICommandLineRunner), 
00287         cmdLine, PROXY_ASYNC | PROXY_ALWAYS, getter_AddRefs(cmdLineProxy));
00288     if (rv != NS_OK)
00289     {
00290 #ifdef DC_PROGRAMNAME
00291         TRACE("Couldn't get command line Proxy!");
00292 #endif
00293         return;
00294     }
00295     
00296     rv = cmdLine->Init(argc, argv, 0 , aState);
00297     if (rv != NS_OK)
00298     {
00299 #ifdef DC_PROGRAMNAME
00300         TRACE("Couldn't init command line!");
00301 #endif
00302         return;
00303     }
00304     
00305 #if 0 // TODO: try to use OpenURI here if there is navWin
00306     nsCOMPtr<nsIDOMWindowInternal> navWin;
00307     GetMostRecentWindow( NS_LITERAL_STRING( "navigator:browser" ).get(),
00308                          getter_AddRefs(navWin ));
00309     if (navWin)
00310     {
00311 # ifdef DC_PROGRAMNAME
00312         TRACE("GotNavWin!");
00313 # endif
00314         cmdLine->SetWindowContext(navWin);
00315     }
00316 #endif //0
00317 
00318     cmdLineProxy->Run();
00319 }
00320 
00321 NS_IMETHODIMP
00322 nsNativeAppSupportBeOS::Start(PRBool *aResult) 
00323 {
00324     NS_ENSURE_ARG(aResult);
00325     NS_ENSURE_TRUE(be_app == NULL, NS_ERROR_NOT_INITIALIZED);
00326     sem_id initsem = create_sem(0, "Mozilla BApplication init");
00327     if (initsem < B_OK)
00328         return NS_ERROR_FAILURE;
00329     thread_id tid = spawn_thread(nsBeOSApp::Main, "Mozilla XUL BApplication", B_NORMAL_PRIORITY, (void *)initsem);
00330 #ifdef DC_PROGRAMNAME
00331     TRACE("BeApp created");
00332 #endif
00333     *aResult = PR_TRUE;
00334     if (tid < B_OK || B_OK != resume_thread(tid))
00335         *aResult = PR_FALSE;
00336 
00337     if (B_OK != acquire_sem(initsem))
00338         *aResult = PR_FALSE;
00339     
00340     if (B_OK != delete_sem(initsem))
00341         *aResult = PR_FALSE;
00342     return *aResult == PR_TRUE ? NS_OK : NS_ERROR_FAILURE;
00343 }
00344 
00345 NS_IMETHODIMP
00346 nsNativeAppSupportBeOS::Stop(PRBool *aResult) 
00347 {
00348     NS_ENSURE_ARG(aResult);
00349     NS_ENSURE_TRUE(be_app, NS_ERROR_NOT_INITIALIZED);
00350 
00351     *aResult = PR_TRUE;
00352     return NS_OK;
00353 }
00354 
00355 NS_IMETHODIMP
00356 nsNativeAppSupportBeOS::Quit() 
00357 {
00358     if (be_app->Lock())
00359     {
00360         be_app->Quit();
00361         return NS_OK;
00362     }
00363     return NS_ERROR_FAILURE;
00364 }
00365 
00366 NS_IMETHODIMP
00367 nsNativeAppSupportBeOS::ReOpen()
00368 {
00369     return NS_ERROR_NOT_IMPLEMENTED;
00370 }
00371 
00372 NS_IMETHODIMP
00373 nsNativeAppSupportBeOS::Enable()
00374 {
00375     // Informing be_app that UI and services are ready to use.
00376     if (be_app)
00377     {
00378         be_app_messenger.SendMessage('enbl');
00379     }
00380     return NS_OK;
00381 }
00382 
00383 NS_IMETHODIMP
00384 nsNativeAppSupportBeOS::OnLastWindowClosing()
00385 {
00386     return NS_OK;
00387 }