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