Bug 1090448 - Add GTK-specific PrintData fields and serialization / deserialization. r=karlt.
authorMike Conley <mconley@mozilla.com>
Thu, 05 Mar 2015 11:17:18 -0500
changeset 232487 fc5e797ee40a092dec3978568efa32bf19194bcf
parent 232486 0c609cf348148d4d8155904d33331411d18f6311
child 232488 fb3cbae160dfdfecd904b77711074097e393bc30
push id28384
push usercbook@mozilla.com
push dateMon, 09 Mar 2015 12:46:25 +0000
treeherdermozilla-central@23f1f0369df5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt
bugs1090448
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1090448 - Add GTK-specific PrintData fields and serialization / deserialization. r=karlt.
embedding/components/printingui/ipc/PPrintingTypes.ipdlh
widget/gtk/nsPrintOptionsGTK.cpp
widget/gtk/nsPrintOptionsGTK.h
widget/gtk/nsPrintSettingsGTK.cpp
--- a/embedding/components/printingui/ipc/PPrintingTypes.ipdlh
+++ b/embedding/components/printingui/ipc/PPrintingTypes.ipdlh
@@ -1,16 +1,21 @@
 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
 /* 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/. */
 
 namespace mozilla {
 namespace embedding {
 
+struct CStringKeyValue {
+  nsCString key;
+  nsCString value;
+};
+
 struct PrintData {
   int32_t startPageRange;
   int32_t endPageRange;
   double edgeTop;
   double edgeLeft;
   double edgeBottom;
   double edgeRight;
   double marginTop;
@@ -71,15 +76,25 @@ struct PrintData {
   /* Windows-specific things */
   nsString driverName;
   nsString deviceName;
   bool isFramesetDocument;
   bool isFramesetFrameSelected;
   bool isIFrameSelected;
   bool isRangeSelection;
 
-  /* TODO: OS X specific things - specifically, an array of names for the
+  /**
+   * GTK-specific things. Some of these might look like dupes of the
+   * information we're already passing, but the generalized settings that
+   * we hold in nsIPrintSettings don't map perfectly to GTK's GtkPrintSettings,
+   * so there are some nuances. GtkPrintSettings, for example, stores both an
+   * internal name for paper size, as well as the display name.
+   */
+  CStringKeyValue[] GTKPrintSettings;
+
+  /**
+   * TODO: OS X specific things - specifically, an array of names for the
    * document to be supplied by nsIWebBrowserPrint::enumerateDocumentNames
    */
 };
 
 } // namespace embedding
 } // namespace mozilla
--- a/widget/gtk/nsPrintOptionsGTK.cpp
+++ b/widget/gtk/nsPrintOptionsGTK.cpp
@@ -1,12 +1,13 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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/. */
+
 #include "nsPrintOptionsGTK.h"
 #include "nsPrintSettingsGTK.h"
 
 using namespace mozilla::embedding;
 
 /** ---------------------------------------------------
  *  See documentation in nsPrintOptionsWin.h
  *	@update 6/21/00 dwc
@@ -19,42 +20,84 @@ nsPrintOptionsGTK::nsPrintOptionsGTK()
 /** ---------------------------------------------------
  *  See documentation in nsPrintOptionsImpl.h
  *	@update 6/21/00 dwc
  */
 nsPrintOptionsGTK::~nsPrintOptionsGTK()
 {
 }
 
+static void
+serialize_gtk_printsettings_to_printdata(const gchar *key,
+                                         const gchar *value,
+                                         gpointer aData)
+{
+  PrintData* data = (PrintData*)aData;
+  CStringKeyValue pair;
+  pair.key() = key;
+  pair.value() = value;
+  data->GTKPrintSettings().AppendElement(pair);
+}
+
 NS_IMETHODIMP
 nsPrintOptionsGTK::SerializeToPrintData(nsIPrintSettings* aSettings,
                                         nsIWebBrowserPrint* aWBP,
                                         PrintData* data)
 {
   nsresult rv = nsPrintOptions::SerializeToPrintData(aSettings, aWBP, data);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  nsCOMPtr<nsPrintSettingsGTK> settingsGTK(do_QueryInterface(aSettings));
+  NS_ENSURE_STATE(settingsGTK);
+
+  GtkPrintSettings* gtkPrintSettings = settingsGTK->GetGtkPrintSettings();
+  NS_ENSURE_STATE(gtkPrintSettings);
+
+  gtk_print_settings_foreach(
+    gtkPrintSettings,
+    serialize_gtk_printsettings_to_printdata,
+    data);
+
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPrintOptionsGTK::DeserializeToPrintSettings(const PrintData& data,
                                               nsIPrintSettings* settings)
 {
+  nsCOMPtr<nsPrintSettingsGTK> settingsGTK(do_QueryInterface(settings));
+  NS_ENSURE_STATE(settingsGTK);
+
   nsresult rv = nsPrintOptions::DeserializeToPrintSettings(data, settings);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  // Instead of re-using the GtkPrintSettings that nsIPrintSettings is
+  // wrapping, we'll create a new one to deserialize to and replace it
+  // within nsIPrintSettings.
+  GtkPrintSettings* newGtkPrintSettings = gtk_print_settings_new();
+
+  for (uint32_t i = 0; i < data.GTKPrintSettings().Length(); ++i) {
+    CStringKeyValue pair = data.GTKPrintSettings()[i];
+    gtk_print_settings_set(newGtkPrintSettings,
+                           pair.key().get(),
+                           pair.value().get());
+  }
+
+  settingsGTK->SetGtkPrintSettings(newGtkPrintSettings);
+
+  // nsPrintSettingsGTK is holding a reference to newGtkPrintSettings
+  g_object_unref(newGtkPrintSettings);
+  newGtkPrintSettings = nullptr;
   return NS_OK;
 }
 
 /* nsIPrintSettings CreatePrintSettings (); */
 nsresult nsPrintOptionsGTK::_CreatePrintSettings(nsIPrintSettings **_retval)
 {
   *_retval = nullptr;
   nsPrintSettingsGTK* printSettings = new nsPrintSettingsGTK(); // does not initially ref count
   NS_ENSURE_TRUE(printSettings, NS_ERROR_OUT_OF_MEMORY);
 
   NS_ADDREF(*_retval = printSettings); // ref count
 
   return NS_OK;
 }
 
-
--- a/widget/gtk/nsPrintOptionsGTK.h
+++ b/widget/gtk/nsPrintOptionsGTK.h
@@ -28,14 +28,13 @@ public:
 
   NS_IMETHODIMP SerializeToPrintData(nsIPrintSettings* aSettings,
                                      nsIWebBrowserPrint* aWBP,
                                      mozilla::embedding::PrintData* data);
   NS_IMETHODIMP DeserializeToPrintSettings(const mozilla::embedding::PrintData& data,
                                            nsIPrintSettings* settings);
 
   virtual nsresult _CreatePrintSettings(nsIPrintSettings **_retval);
-
 };
 
 
 
 #endif /* nsPrintOptions_h__ */
--- a/widget/gtk/nsPrintSettingsGTK.cpp
+++ b/widget/gtk/nsPrintSettingsGTK.cpp
@@ -753,19 +753,21 @@ nsPrintSettingsGTK::SetResolution(int32_
 {
   gtk_print_settings_set_resolution(mPrintSettings, aResolution);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPrintSettingsGTK::GetDuplex(int32_t *aDuplex)
 {
-  if (!gtk_print_settings_has_key(mPrintSettings, GTK_PRINT_SETTINGS_DUPLEX))
-    return NS_ERROR_FAILURE;
-  *aDuplex = gtk_print_settings_get_duplex(mPrintSettings);
+  if (!gtk_print_settings_has_key(mPrintSettings, GTK_PRINT_SETTINGS_DUPLEX)) {
+    *aDuplex = GTK_PRINT_DUPLEX_SIMPLEX;
+  } else {
+    *aDuplex = gtk_print_settings_get_duplex(mPrintSettings);
+  }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPrintSettingsGTK::SetDuplex(int32_t aDuplex)
 {
   MOZ_ASSERT(aDuplex >= GTK_PRINT_DUPLEX_SIMPLEX &&
              aDuplex <= GTK_PRINT_DUPLEX_VERTICAL,