Bug 567702. Factor out duplicated DOMWindowToWidget. r=roc
authorTimothy Nikkel <tnikkel@gmail.com>
Fri, 25 Jun 2010 16:51:17 -0500
changeset 46250 99f7e3994a54db21f0fb7d72de05a089b5e17894
parent 46249 eadd460405a2c3b6106603396d270ea3af4d2bbc
child 46251 375eee52bbdd72dac80ce120f2bdf42cea087b22
push idunknown
push userunknown
push dateunknown
reviewersroc
bugs567702
milestone1.9.3a6pre
Bug 567702. Factor out duplicated DOMWindowToWidget. r=roc
widget/src/Makefile.in
widget/src/gtk2/Makefile.in
widget/src/gtk2/nsPrintDialogGTK.cpp
widget/src/shared/Makefile.in
widget/src/shared/WidgetUtils.cpp
widget/src/shared/WidgetUtils.h
widget/src/xpwidgets/Makefile.in
widget/src/xpwidgets/nsBaseFilePicker.cpp
widget/src/xpwidgets/nsBaseFilePicker.h
--- a/widget/src/Makefile.in
+++ b/widget/src/Makefile.in
@@ -39,17 +39,17 @@ DEPTH		= ../..
 topsrcdir	= @top_srcdir@
 srcdir		= @srcdir@
 VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE  = widget
 
-DIRS		= xpwidgets
+DIRS		= shared xpwidgets
 
 ifneq (,$(filter beos os2 cocoa qt android,$(MOZ_WIDGET_TOOLKIT)))
 DIRS		+= $(MOZ_WIDGET_TOOLKIT)
 endif
 
 ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
 DIRS		+= windows build
 endif
--- a/widget/src/gtk2/Makefile.in
+++ b/widget/src/gtk2/Makefile.in
@@ -157,10 +157,11 @@ ifdef MOZ_ENABLE_POSTSCRIPT
 DEFINES         += -DUSE_POSTSCRIPT
 EXTRA_DSO_LDOPTS += -lgfxpsshar
 endif
 
 
 DEFINES		+=
 INCLUDES	+= \
 		-I$(srcdir)/../xpwidgets \
+		-I$(srcdir)/../shared \
 		-I$(topsrcdir)/other-licenses/atk-1.0 \
 		$(NULL)
--- a/widget/src/gtk2/nsPrintDialogGTK.cpp
+++ b/widget/src/gtk2/nsPrintDialogGTK.cpp
@@ -52,57 +52,24 @@
 #include "nsNetUtil.h"
 #include "nsIStringBundle.h"
 #include "nsIPrintSettingsService.h"
 #include "nsIDOMWindow.h"
 #include "nsPIDOMWindow.h"
 #include "nsIBaseWindow.h"
 #include "nsIDocShellTreeItem.h"
 #include "nsIDocShell.h"
+#include "WidgetUtils.h"
+
+using namespace mozilla::widget;
 
 static const char header_footer_tags[][4] =  {"", "&T", "&U", "&D", "&P", "&PT"};
 
 #define CUSTOM_VALUE_INDEX NS_ARRAY_LENGTH(header_footer_tags)
 
-// XXXdholbert Duplicated from widget/src/xpwidgets/nsBaseFilePicker.cpp
-// Needs to be unified in some generic utility class.
-static nsIWidget *
-DOMWindowToWidget(nsIDOMWindow *dw)
-{
-  nsCOMPtr<nsIWidget> widget;
-
-  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(dw);
-  if (window) {
-    nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(window->GetDocShell()));
-
-    while (!widget && baseWin) {
-      baseWin->GetParentWidget(getter_AddRefs(widget));
-      if (!widget) {
-        nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(baseWin));
-        if (!docShellAsItem)
-          return nsnull;
-
-        nsCOMPtr<nsIDocShellTreeItem> parent;
-        docShellAsItem->GetSameTypeParent(getter_AddRefs(parent));
-
-        window = do_GetInterface(parent);
-        if (!window)
-          return nsnull;
-
-        baseWin = do_QueryInterface(window->GetDocShell());
-      }
-    }
-  }
-
-  // This will return a pointer that we're about to release, but
-  // that's ok since the docshell (nsIBaseWindow) holds the widget
-  // alive.
-  return widget.get();
-}
-
 // XXXdholbert Duplicated from widget/src/gtk2/nsFilePicker.cpp
 // Needs to be unified in some generic utility class.
 static GtkWindow *
 get_gtk_window_for_nsiwidget(nsIWidget *widget)
 {
   // Get native GdkWindow
   GdkWindow *gdk_win = GDK_WINDOW(widget->GetNativeData(NS_NATIVE_WIDGET));
   if (!gdk_win)
@@ -225,17 +192,19 @@ class nsPrintDialogWidgetGTK {
      *   "Export" means to copy from GTK to NS
      */
     void ExportFramePrinting(nsIPrintSettings *aNS, GtkPrintSettings *aSettings);
     void ExportHeaderFooter(nsIPrintSettings *aNS);
 };
 
 nsPrintDialogWidgetGTK::nsPrintDialogWidgetGTK(nsIDOMWindow *aParent, nsIPrintSettings *aSettings)
 {
-  GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(DOMWindowToWidget(aParent));
+  nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
+  NS_ASSERTION(widget, "Need a widget for dialog to be modal.");
+  GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(widget);
   NS_ASSERTION(gtkParent, "Need a GTK window for dialog to be modal.");
 
   nsCOMPtr<nsIStringBundleService> bundleSvc = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
   bundleSvc->CreateBundle("chrome://global/locale/printdialog.properties", getter_AddRefs(printBundle));
 
   dialog = gtk_print_unix_dialog_new(GetUTF8FromBundle("printTitleGTK").get(), gtkParent);
 
   gtk_print_unix_dialog_set_manual_capabilities(GTK_PRINT_UNIX_DIALOG(dialog),
@@ -630,17 +599,19 @@ nsPrintDialogServiceGTK::Show(nsIDOMWind
 NS_IMETHODIMP
 nsPrintDialogServiceGTK::ShowPageSetup(nsIDOMWindow *aParent,
                                        nsIPrintSettings *aNSSettings)
 {
   NS_PRECONDITION(aParent, "aParent must not be null");
   NS_PRECONDITION(aNSSettings, "aSettings must not be null");
   NS_ENSURE_TRUE(aNSSettings, NS_ERROR_FAILURE);
 
-  GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(DOMWindowToWidget(aParent));
+  nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
+  NS_ASSERTION(widget, "Need a widget for dialog to be modal.");
+  GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(widget);
   NS_ASSERTION(gtkParent, "Need a GTK window for dialog to be modal.");
 
   nsCOMPtr<nsPrintSettingsGTK> aNSSettingsGTK(do_QueryInterface(aNSSettings));
   if (!aNSSettingsGTK)
     return NS_ERROR_FAILURE;
 
   // We need to init the prefs here because aNSSettings in its current form is a dummy in both uses of the word
   nsCOMPtr<nsIPrintSettingsService> psService = do_GetService("@mozilla.org/gfx/printsettings-service;1");
new file mode 100644
--- /dev/null
+++ b/widget/src/shared/Makefile.in
@@ -0,0 +1,64 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# Netscape Communications Corporation.
+# Portions created by the Initial Developer are Copyright (C) 1998
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH		= ../../..
+topsrcdir	= @top_srcdir@
+srcdir		= @srcdir@
+VPATH		= @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+MODULE		= widget
+LIBRARY_NAME	= widget_shared
+LIBXUL_LIBRARY  = 1
+
+
+DEFINES += \
+  -D_IMPL_NS_WIDGET \
+  $(NULL)
+
+CPPSRCS		= \
+		WidgetUtils.cpp \
+		$(NULL)
+
+# we don't want the shared lib, but we want to force the creation of a static lib.
+FORCE_STATIC_LIB = 1
+
+include $(topsrcdir)/config/config.mk
+include $(topsrcdir)/config/rules.mk
+
+CXXFLAGS	+= $(TK_CFLAGS)
new file mode 100644
--- /dev/null
+++ b/widget/src/shared/WidgetUtils.cpp
@@ -0,0 +1,84 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is the Mozilla browser.
+ *
+ * The Initial Developer of the Original Code is
+ * Netscape Communications Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 1999
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Stuart Parmenter <pavlov@netscape.com>
+ *   Mike Pinkerton   <pinkerton@netscape.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include "WidgetUtils.h"
+
+#include "nsIBaseWindow.h"
+#include "nsIDocShellTreeItem.h"
+#include "nsIDocShell.h"
+#include "nsIInterfaceRequestorUtils.h"
+
+namespace mozilla {
+namespace widget {
+
+//static
+already_AddRefed<nsIWidget>
+WidgetUtils::DOMWindowToWidget(nsIDOMWindow *aDOMWindow)
+{
+  nsCOMPtr<nsIWidget> widget;
+
+  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aDOMWindow);
+  if (window) {
+    nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(window->GetDocShell()));
+
+    while (!widget && baseWin) {
+      baseWin->GetParentWidget(getter_AddRefs(widget));
+      if (!widget) {
+        nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(baseWin));
+        if (!docShellAsItem)
+          return nsnull;
+
+        nsCOMPtr<nsIDocShellTreeItem> parent;
+        docShellAsItem->GetSameTypeParent(getter_AddRefs(parent));
+
+        window = do_GetInterface(parent);
+        if (!window)
+          return nsnull;
+
+        baseWin = do_QueryInterface(window->GetDocShell());
+      }
+    }
+  }
+
+  return widget.forget();
+}
+
+} // namespace widget
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/widget/src/shared/WidgetUtils.h
@@ -0,0 +1,66 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is the Mozilla browser.
+ *
+ * The Initial Developer of the Original Code is
+ * Netscape Communications Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 1999
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Stuart Parmenter <pavlov@netscape.com>
+ *   Mike Pinkerton   <pinkerton@netscape.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef __mozilla_widget_WidgetUtils_h__
+#define __mozilla_widget_WidgetUtils_h__
+
+#include "nsCOMPtr.h"
+#include "nsIWidget.h"
+#include "nsPIDOMWindow.h"
+#include "nsIDOMWindow.h"
+
+namespace mozilla {
+namespace widget {
+
+class WidgetUtils
+{
+public:
+
+  /**
+   * Starting at the docshell item for the passed in DOM window this looks up
+   * the docshell tree until it finds a docshell item that has a widget.
+   */
+  static already_AddRefed<nsIWidget> DOMWindowToWidget(nsIDOMWindow *aDOMWindow);
+};
+
+} // namespace widget
+} // namespace mozilla
+
+#endif
--- a/widget/src/xpwidgets/Makefile.in
+++ b/widget/src/xpwidgets/Makefile.in
@@ -77,18 +77,21 @@ endif
 ifneq (,$(filter beos qt gtk2 os2 cocoa windows,$(MOZ_WIDGET_TOOLKIT)))
 CPPSRCS += nsBaseFilePicker.cpp
 endif
 
 ifneq (,$(filter qt gtk2 windows cocoa,$(MOZ_WIDGET_TOOLKIT)))
 CPPSRCS += nsNativeTheme.cpp
 endif
 
+SHARED_LIBRARY_LIBS = ../shared/$(LIB_PREFIX)widget_shared.$(LIB_SUFFIX)
+
 LOCAL_INCLUDES	+= \
 		-I$(srcdir)/../$(MOZ_WIDGET_TOOLKIT) \
+		-I$(srcdir)/../shared \
 		-I$(srcdir) \
 		$(NULL)
 
 # we don't want the shared lib, but we want to force the creation of a static lib.
 FORCE_STATIC_LIB = 1
 
 include $(topsrcdir)/config/config.mk
 include $(topsrcdir)/ipc/chromium/chromium-config.mk
--- a/widget/src/xpwidgets/nsBaseFilePicker.cpp
+++ b/widget/src/xpwidgets/nsBaseFilePicker.cpp
@@ -50,75 +50,43 @@
 
 #include "nsIStringBundle.h"
 #include "nsXPIDLString.h"
 #include "nsIServiceManager.h"
 #include "nsCOMArray.h"
 #include "nsILocalFile.h"
 #include "nsEnumeratorUtils.h"
 #include "mozilla/Services.h"
+#include "WidgetUtils.h"
 
 #include "nsBaseFilePicker.h"
 
+using namespace mozilla::widget;
+
 #define FILEPICKER_TITLES "chrome://global/locale/filepicker.properties"
 #define FILEPICKER_FILTERS "chrome://global/content/filepicker.properties"
 
 nsBaseFilePicker::nsBaseFilePicker()
 {
 
 }
 
 nsBaseFilePicker::~nsBaseFilePicker()
 {
 
 }
 
-// XXXdholbert -- this function is duplicated in nsPrintDialogGTK.cpp
-// and needs to be unified in some generic utility class.
-nsIWidget *nsBaseFilePicker::DOMWindowToWidget(nsIDOMWindow *dw)
-{
-  nsCOMPtr<nsIWidget> widget;
-
-  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(dw);
-  if (window) {
-    nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(window->GetDocShell()));
-
-    while (!widget && baseWin) {
-      baseWin->GetParentWidget(getter_AddRefs(widget));
-      if (!widget) {
-        nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(baseWin));
-        if (!docShellAsItem)
-          return nsnull;
-
-        nsCOMPtr<nsIDocShellTreeItem> parent;
-        docShellAsItem->GetSameTypeParent(getter_AddRefs(parent));
-
-        window = do_GetInterface(parent);
-        if (!window)
-          return nsnull;
-
-        baseWin = do_QueryInterface(window->GetDocShell());
-      }
-    }
-  }
-
-  // This will return a pointer that we're about to release, but
-  // that's ok since the docshell (nsIBaseWindow) holds the widget
-  // alive.
-  return widget.get();
-}
-
 //-------------------------------------------------------------------------
 NS_IMETHODIMP nsBaseFilePicker::Init(nsIDOMWindow *aParent,
                                      const nsAString& aTitle,
                                      PRInt16 aMode)
 {
   NS_PRECONDITION(aParent, "Null parent passed to filepicker, no file "
                   "picker for you!");
-  nsIWidget *widget = DOMWindowToWidget(aParent);
+  nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
   NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
 
   InitNative(widget, aTitle, aMode);
 
   return NS_OK;
 }
 
 
--- a/widget/src/xpwidgets/nsBaseFilePicker.h
+++ b/widget/src/xpwidgets/nsBaseFilePicker.h
@@ -70,15 +70,14 @@ public:
   NS_IMETHOD SetDisplayDirectory(nsILocalFile * aDisplayDirectory);
 #endif
 
 protected:
 
   virtual void InitNative(nsIWidget *aParent, const nsAString& aTitle,
                           PRInt16 aMode) = 0;
 
-  nsIWidget *DOMWindowToWidget(nsIDOMWindow *dw);
 #ifdef BASEFILEPICKER_HAS_DISPLAYDIRECTORY 
   nsCOMPtr<nsILocalFile> mDisplayDirectory;
 #endif
 };
 
 #endif // nsBaseFilePicker_h__