michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsNativeAppSupportBase.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsISupportsPrimitives.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsIAppStartup.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "prlink.h" michael@0: #include "nsXREDirProvider.h" michael@0: #include "nsReadableUtils.h" michael@0: michael@0: #include "nsIFile.h" michael@0: #include "nsDirectoryServiceDefs.h" michael@0: #include "nsICommandLineRunner.h" michael@0: #include "nsIWindowMediator.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIBaseWindow.h" michael@0: #include "nsIWidget.h" michael@0: #include "nsIWritablePropertyBag2.h" michael@0: #include "nsIPrefService.h" michael@0: #include "mozilla/Services.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef MOZ_X11 michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef MOZ_ENABLE_DBUS michael@0: #include michael@0: #endif michael@0: michael@0: #define MIN_GTK_MAJOR_VERSION 2 michael@0: #define MIN_GTK_MINOR_VERSION 10 michael@0: #define UNSUPPORTED_GTK_MSG "We're sorry, this application requires a version of the GTK+ library that is not installed on your computer.\n\n\ michael@0: You have GTK+ %d.%d.\nThis application requires GTK+ %d.%d or newer.\n\n\ michael@0: Please upgrade your GTK+ library if you wish to use this application." michael@0: michael@0: typedef struct _GnomeProgram GnomeProgram; michael@0: typedef struct _GnomeModuleInfo GnomeModuleInfo; michael@0: typedef struct _GnomeClient GnomeClient; michael@0: michael@0: typedef enum { michael@0: GNOME_SAVE_GLOBAL, michael@0: GNOME_SAVE_LOCAL, michael@0: GNOME_SAVE_BOTH michael@0: } GnomeSaveStyle; michael@0: michael@0: typedef enum { michael@0: GNOME_INTERACT_NONE, michael@0: GNOME_INTERACT_ERRORS, michael@0: GNOME_INTERACT_ANY michael@0: } GnomeInteractStyle; michael@0: michael@0: typedef enum { michael@0: GNOME_DIALOG_ERROR, michael@0: GNOME_DIALOG_NORMAL michael@0: } GnomeDialogType; michael@0: michael@0: typedef GnomeProgram * (*_gnome_program_init_fn)(const char *, const char *, michael@0: const GnomeModuleInfo *, int, michael@0: char **, const char *, ...); michael@0: typedef GnomeProgram * (*_gnome_program_get_fn)(void); michael@0: typedef const GnomeModuleInfo * (*_libgnomeui_module_info_get_fn)(); michael@0: typedef GnomeClient * (*_gnome_master_client_fn)(void); michael@0: typedef void (*_gnome_client_set_restart_command_fn)(GnomeClient*, gint, gchar*[]); michael@0: michael@0: static _gnome_client_set_restart_command_fn gnome_client_set_restart_command; michael@0: michael@0: gboolean save_yourself_cb(GnomeClient *client, gint phase, michael@0: GnomeSaveStyle style, gboolean shutdown, michael@0: GnomeInteractStyle interact, gboolean fast, michael@0: gpointer user_data) michael@0: { michael@0: nsCOMPtr obsServ = michael@0: mozilla::services::GetObserverService(); michael@0: michael@0: nsCOMPtr didSaveSession = michael@0: do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID); michael@0: michael@0: if (!obsServ || !didSaveSession) michael@0: return TRUE; // OOM michael@0: michael@0: // Notify observers to save the session state michael@0: didSaveSession->SetData(false); michael@0: obsServ->NotifyObservers(didSaveSession, "session-save", nullptr); michael@0: michael@0: bool status; michael@0: didSaveSession->GetData(&status); michael@0: michael@0: // If there was no session saved and the save_yourself request is michael@0: // caused by upcoming shutdown we like to prepare for it michael@0: if (!status && shutdown) { michael@0: nsCOMPtr cancelQuit = michael@0: do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID); michael@0: michael@0: cancelQuit->SetData(false); michael@0: obsServ->NotifyObservers(cancelQuit, "quit-application-requested", nullptr); michael@0: michael@0: bool abortQuit; michael@0: cancelQuit->GetData(&abortQuit); michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: void die_cb(GnomeClient *client, gpointer user_data) michael@0: { michael@0: nsCOMPtr appService = michael@0: do_GetService("@mozilla.org/toolkit/app-startup;1"); michael@0: michael@0: if (appService) michael@0: appService->Quit(nsIAppStartup::eForceQuit); michael@0: } michael@0: michael@0: class nsNativeAppSupportUnix : public nsNativeAppSupportBase michael@0: { michael@0: public: michael@0: NS_IMETHOD Start(bool* aRetVal); michael@0: NS_IMETHOD Stop(bool *aResult); michael@0: NS_IMETHOD Enable(); michael@0: michael@0: private: michael@0: }; michael@0: michael@0: NS_IMETHODIMP michael@0: nsNativeAppSupportUnix::Start(bool *aRetVal) michael@0: { michael@0: NS_ASSERTION(gAppData, "gAppData must not be null."); michael@0: michael@0: // The dbus library is used by both nsWifiScannerDBus and BluetoothDBusService, michael@0: // from diffrent threads. This could lead to race conditions if the dbus is not michael@0: // initialized before making any other library calls. michael@0: #ifdef MOZ_ENABLE_DBUS michael@0: dbus_threads_init_default(); michael@0: #endif michael@0: michael@0: #if (MOZ_WIDGET_GTK == 2) michael@0: if (gtk_major_version < MIN_GTK_MAJOR_VERSION || michael@0: (gtk_major_version == MIN_GTK_MAJOR_VERSION && gtk_minor_version < MIN_GTK_MINOR_VERSION)) { michael@0: GtkWidget* versionErrDialog = gtk_message_dialog_new(nullptr, michael@0: GtkDialogFlags(GTK_DIALOG_MODAL | michael@0: GTK_DIALOG_DESTROY_WITH_PARENT), michael@0: GTK_MESSAGE_ERROR, michael@0: GTK_BUTTONS_OK, michael@0: UNSUPPORTED_GTK_MSG, michael@0: gtk_major_version, michael@0: gtk_minor_version, michael@0: MIN_GTK_MAJOR_VERSION, michael@0: MIN_GTK_MINOR_VERSION); michael@0: gtk_dialog_run(GTK_DIALOG(versionErrDialog)); michael@0: gtk_widget_destroy(versionErrDialog); michael@0: exit(0); michael@0: } michael@0: #endif michael@0: michael@0: *aRetVal = true; michael@0: michael@0: #if defined(MOZ_X11) && (MOZ_WIDGET_GTK == 2) michael@0: michael@0: PRLibrary *gnomeuiLib = PR_LoadLibrary("libgnomeui-2.so.0"); michael@0: if (!gnomeuiLib) michael@0: return NS_OK; michael@0: michael@0: PRLibrary *gnomeLib = PR_LoadLibrary("libgnome-2.so.0"); michael@0: if (!gnomeLib) { michael@0: PR_UnloadLibrary(gnomeuiLib); michael@0: return NS_OK; michael@0: } michael@0: michael@0: _gnome_program_init_fn gnome_program_init = michael@0: (_gnome_program_init_fn)PR_FindFunctionSymbol(gnomeLib, "gnome_program_init"); michael@0: _gnome_program_get_fn gnome_program_get = michael@0: (_gnome_program_get_fn)PR_FindFunctionSymbol(gnomeLib, "gnome_program_get"); michael@0: _libgnomeui_module_info_get_fn libgnomeui_module_info_get = (_libgnomeui_module_info_get_fn)PR_FindFunctionSymbol(gnomeuiLib, "libgnomeui_module_info_get"); michael@0: if (!gnome_program_init || !gnome_program_get || !libgnomeui_module_info_get) { michael@0: PR_UnloadLibrary(gnomeuiLib); michael@0: PR_UnloadLibrary(gnomeLib); michael@0: return NS_OK; michael@0: } michael@0: michael@0: #endif /* MOZ_X11 && (MOZ_WIDGET_GTK == 2) */ michael@0: michael@0: #ifdef ACCESSIBILITY michael@0: // We will load gail, atk-bridge by ourself later michael@0: // We can't run atk-bridge init here, because gail get the control michael@0: // Set GNOME_ACCESSIBILITY to 0 can avoid this michael@0: static const char *accEnv = "GNOME_ACCESSIBILITY"; michael@0: const char *accOldValue = getenv(accEnv); michael@0: setenv(accEnv, "0", 1); michael@0: #endif michael@0: michael@0: #if defined(MOZ_X11) && (MOZ_WIDGET_GTK == 2) michael@0: if (!gnome_program_get()) { michael@0: gnome_program_init("Gecko", "1.0", libgnomeui_module_info_get(), michael@0: gArgc, gArgv, nullptr); michael@0: } michael@0: #endif /* MOZ_X11 && (MOZ_WIDGET_GTK == 2) */ michael@0: michael@0: #ifdef ACCESSIBILITY michael@0: if (accOldValue) { michael@0: setenv(accEnv, accOldValue, 1); michael@0: } else { michael@0: unsetenv(accEnv); michael@0: } michael@0: #endif michael@0: michael@0: // Careful! These libraries cannot be unloaded after this point because michael@0: // gnome_program_init causes atexit handlers to be registered. Strange michael@0: // crashes will occur if these libraries are unloaded. michael@0: michael@0: // TODO GTK3 - see Bug 694570 - Stop using libgnome and libgnomeui on Linux michael@0: #if defined(MOZ_X11) && (MOZ_WIDGET_GTK == 2) michael@0: gnome_client_set_restart_command = (_gnome_client_set_restart_command_fn) michael@0: PR_FindFunctionSymbol(gnomeuiLib, "gnome_client_set_restart_command"); michael@0: michael@0: _gnome_master_client_fn gnome_master_client = (_gnome_master_client_fn) michael@0: PR_FindFunctionSymbol(gnomeuiLib, "gnome_master_client"); michael@0: michael@0: GnomeClient *client = gnome_master_client(); michael@0: g_signal_connect(client, "save-yourself", G_CALLBACK(save_yourself_cb), nullptr); michael@0: g_signal_connect(client, "die", G_CALLBACK(die_cb), nullptr); michael@0: michael@0: // Set the correct/requested restart command in any case. michael@0: michael@0: // Is there a request to suppress default binary launcher? michael@0: nsAutoCString path; michael@0: char* argv1 = getenv("MOZ_APP_LAUNCHER"); michael@0: michael@0: if(!argv1) { michael@0: // Tell the desktop the command for restarting us so that we can be part of XSMP session restore michael@0: NS_ASSERTION(gDirServiceProvider, "gDirServiceProvider is NULL! This shouldn't happen!"); michael@0: nsCOMPtr executablePath; michael@0: nsresult rv; michael@0: michael@0: bool dummy; michael@0: rv = gDirServiceProvider->GetFile(XRE_EXECUTABLE_FILE, &dummy, getter_AddRefs(executablePath)); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // Strip off the -bin suffix to get the shell script we should run; this is what Breakpad does michael@0: nsAutoCString leafName; michael@0: rv = executablePath->GetNativeLeafName(leafName); michael@0: if (NS_SUCCEEDED(rv) && StringEndsWith(leafName, NS_LITERAL_CSTRING("-bin"))) { michael@0: leafName.SetLength(leafName.Length() - strlen("-bin")); michael@0: executablePath->SetNativeLeafName(leafName); michael@0: } michael@0: michael@0: executablePath->GetNativePath(path); michael@0: argv1 = (char*)(path.get()); michael@0: } michael@0: } michael@0: michael@0: if (argv1) { michael@0: gnome_client_set_restart_command(client, 1, &argv1); michael@0: } michael@0: #endif /* MOZ_X11 && (MOZ_WIDGET_GTK == 2) */ michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNativeAppSupportUnix::Stop(bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG(aResult); michael@0: *aResult = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNativeAppSupportUnix::Enable() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: NS_CreateNativeAppSupport(nsINativeAppSupport **aResult) michael@0: { michael@0: nsNativeAppSupportBase* native = new nsNativeAppSupportUnix(); michael@0: if (!native) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: *aResult = native; michael@0: NS_ADDREF(*aResult); michael@0: michael@0: return NS_OK; michael@0: }