toolkit/components/remote/nsGTKRemoteService.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:expandtab:shiftwidth=2:tabstop=8:
     3  */
     4 /* This Source Code Form is subject to the terms of the Mozilla Public
     5  * License, v. 2.0. If a copy of the MPL was not distributed with this
     6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     8 #include "nsGTKRemoteService.h"
    10 #include <gtk/gtk.h>
    11 #include <gdk/gdk.h>
    12 #include <gdk/gdkx.h>
    14 #include "nsIBaseWindow.h"
    15 #include "nsIDocShell.h"
    16 #include "nsPIDOMWindow.h"
    17 #include "mozilla/ModuleUtils.h"
    18 #include "nsIServiceManager.h"
    19 #include "nsIWeakReference.h"
    20 #include "nsIWidget.h"
    21 #include "nsIAppShellService.h"
    22 #include "nsAppShellCID.h"
    24 #include "nsCOMPtr.h"
    26 #include "nsGTKToolkit.h"
    28 NS_IMPL_ISUPPORTS(nsGTKRemoteService,
    29                   nsIRemoteService,
    30                   nsIObserver)
    32 NS_IMETHODIMP
    33 nsGTKRemoteService::Startup(const char* aAppName, const char* aProfileName)
    34 {
    35   NS_ASSERTION(aAppName, "Don't pass a null appname!");
    36   sRemoteImplementation = this;
    38   if (mServerWindow) return NS_ERROR_ALREADY_INITIALIZED;
    40   XRemoteBaseStartup(aAppName, aProfileName);
    42   mServerWindow = gtk_invisible_new();
    43   gtk_widget_realize(mServerWindow);
    44   HandleCommandsFor(mServerWindow, nullptr);
    46   mWindows.EnumerateRead(StartupHandler, this);
    48   return NS_OK;
    49 }
    51 PLDHashOperator
    52 nsGTKRemoteService::StartupHandler(GtkWidget* aKey,
    53                                    nsIWeakReference* aData,
    54                                    void* aClosure)
    55 {
    56   GtkWidget* widget = (GtkWidget*) aKey;
    57   nsGTKRemoteService* aThis = (nsGTKRemoteService*) aClosure;
    59   aThis->HandleCommandsFor(widget, aData);
    60   return PL_DHASH_NEXT;
    61 }
    63 static nsIWidget* GetMainWidget(nsIDOMWindow* aWindow)
    64 {
    65   // get the native window for this instance
    66   nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
    67   NS_ENSURE_TRUE(window, nullptr);
    69   nsCOMPtr<nsIBaseWindow> baseWindow
    70     (do_QueryInterface(window->GetDocShell()));
    71   NS_ENSURE_TRUE(baseWindow, nullptr);
    73   nsCOMPtr<nsIWidget> mainWidget;
    74   baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
    75   return mainWidget;
    76 }
    78 NS_IMETHODIMP
    79 nsGTKRemoteService::RegisterWindow(nsIDOMWindow* aWindow)
    80 {
    81   nsIWidget* mainWidget = GetMainWidget(aWindow);
    82   NS_ENSURE_TRUE(mainWidget, NS_ERROR_FAILURE);
    84   GtkWidget* widget =
    85     (GtkWidget*) mainWidget->GetNativeData(NS_NATIVE_SHELLWIDGET);
    86   NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
    88   nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(aWindow);
    89   NS_ENSURE_TRUE(weak, NS_ERROR_FAILURE);
    91   mWindows.Put(widget, weak);
    93   // If Startup() has already been called, immediately register this window.
    94   if (mServerWindow) {
    95     HandleCommandsFor(widget, weak);
    96   }
    98   return NS_OK;
    99 }
   101 NS_IMETHODIMP
   102 nsGTKRemoteService::Shutdown()
   103 {
   104   if (!mServerWindow)
   105     return NS_ERROR_NOT_INITIALIZED;
   107   gtk_widget_destroy(mServerWindow);
   108   mServerWindow = nullptr;
   109   return NS_OK;
   110 }
   112 // Set desktop startup ID to the passed ID, if there is one, so that any created
   113 // windows get created with the right window manager metadata, and any windows
   114 // that get new tabs and are activated also get the right WM metadata.
   115 // The timestamp will be used if there is no desktop startup ID, or if we're
   116 // raising an existing window rather than showing a new window for the first time.
   117 void
   118 nsGTKRemoteService::SetDesktopStartupIDOrTimestamp(const nsACString& aDesktopStartupID,
   119                                                    uint32_t aTimestamp) {
   120   nsGTKToolkit* toolkit = nsGTKToolkit::GetToolkit();
   121   if (!toolkit)
   122     return;
   124   if (!aDesktopStartupID.IsEmpty()) {
   125     toolkit->SetDesktopStartupID(aDesktopStartupID);
   126   }
   128   toolkit->SetFocusTimestamp(aTimestamp);
   129 }
   132 void
   133 nsGTKRemoteService::HandleCommandsFor(GtkWidget* widget,
   134                                       nsIWeakReference* aWindow)
   135 {
   136   g_signal_connect(G_OBJECT(widget), "property_notify_event",
   137                    G_CALLBACK(HandlePropertyChange), aWindow);
   139   gtk_widget_add_events(widget, GDK_PROPERTY_CHANGE_MASK);
   141 #if (MOZ_WIDGET_GTK == 2)
   142   Window window = GDK_WINDOW_XWINDOW(widget->window);
   143 #else
   144   Window window = gdk_x11_window_get_xid(gtk_widget_get_window(widget));
   145 #endif
   146   nsXRemoteService::HandleCommandsFor(window);
   148 }
   150 gboolean
   151 nsGTKRemoteService::HandlePropertyChange(GtkWidget *aWidget,
   152                                          GdkEventProperty *pevent,
   153                                          nsIWeakReference *aThis)
   154 {
   155   if (pevent->state == GDK_PROPERTY_NEW_VALUE) {
   156     Atom changedAtom = gdk_x11_atom_to_xatom(pevent->atom);
   158 #if (MOZ_WIDGET_GTK == 2)
   159     XID window = GDK_WINDOW_XWINDOW(pevent->window);
   160 #else
   161     XID window = gdk_x11_window_get_xid(gtk_widget_get_window(aWidget));
   162 #endif
   163     return HandleNewProperty(window,
   164                              GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
   165                              pevent->time, changedAtom, aThis);
   166   }
   167   return FALSE;
   168 }
   171 // {C0773E90-5799-4eff-AD03-3EBCD85624AC}
   172 #define NS_REMOTESERVICE_CID \
   173   { 0xc0773e90, 0x5799, 0x4eff, { 0xad, 0x3, 0x3e, 0xbc, 0xd8, 0x56, 0x24, 0xac } }
   175 NS_GENERIC_FACTORY_CONSTRUCTOR(nsGTKRemoteService)
   176 NS_DEFINE_NAMED_CID(NS_REMOTESERVICE_CID);
   178 static const mozilla::Module::CIDEntry kRemoteCIDs[] = {
   179   { &kNS_REMOTESERVICE_CID, false, nullptr, nsGTKRemoteServiceConstructor },
   180   { nullptr }
   181 };
   183 static const mozilla::Module::ContractIDEntry kRemoteContracts[] = {
   184   { "@mozilla.org/toolkit/remote-service;1", &kNS_REMOTESERVICE_CID },
   185   { nullptr }
   186 };
   188 static const mozilla::Module kRemoteModule = {
   189   mozilla::Module::kVersion,
   190   kRemoteCIDs,
   191   kRemoteContracts
   192 };
   194 NSMODULE_DEFN(RemoteServiceModule) = &kRemoteModule;

mercurial