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