ArgumentCodersGtk.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (C) 2011 Igalia S.L.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "ArgumentCodersGtk.h"
  27. #include "DataReference.h"
  28. #include "ShareableBitmap.h"
  29. #include "WebCoreArgumentCoders.h"
  30. #include <WebCore/DataObjectGtk.h>
  31. #include <WebCore/DragData.h>
  32. #include <WebCore/GraphicsContext.h>
  33. #include <WebCore/GtkVersioning.h>
  34. #include <WebCore/PlatformContextCairo.h>
  35. #include <wtf/gobject/GOwnPtr.h>
  36. using namespace WebCore;
  37. using namespace WebKit;
  38. namespace CoreIPC {
  39. static void encodeImage(ArgumentEncoder& encoder, const GdkPixbuf* pixbuf)
  40. {
  41. IntSize imageSize(gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf));
  42. RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(imageSize, ShareableBitmap::SupportsAlpha);
  43. OwnPtr<GraphicsContext> graphicsContext = bitmap->createGraphicsContext();
  44. cairo_t* cr = graphicsContext->platformContext()->cr();
  45. gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
  46. cairo_paint(cr);
  47. ShareableBitmap::Handle handle;
  48. bitmap->createHandle(handle);
  49. encoder << handle;
  50. }
  51. static bool decodeImage(ArgumentDecoder& decoder, GRefPtr<GdkPixbuf>& pixbuf)
  52. {
  53. ShareableBitmap::Handle handle;
  54. if (!decoder.decode(handle))
  55. return false;
  56. RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(handle);
  57. if (!bitmap)
  58. return false;
  59. RefPtr<Image> image = bitmap->createImage();
  60. if (!image)
  61. return false;
  62. RefPtr<cairo_surface_t> surface = image->nativeImageForCurrentFrame();
  63. if (!surface)
  64. return false;
  65. pixbuf = adoptGRef(gdk_pixbuf_get_from_surface(surface.get(), 0, 0, cairo_image_surface_get_width(surface.get()), cairo_image_surface_get_height(surface.get())));
  66. if (!pixbuf)
  67. return false;
  68. return true;
  69. }
  70. static void encodeDataObject(ArgumentEncoder& encoder, const DataObjectGtk* dataObject)
  71. {
  72. bool hasText = dataObject->hasText();
  73. encoder << hasText;
  74. if (hasText)
  75. encoder << dataObject->text();
  76. bool hasMarkup = dataObject->hasMarkup();
  77. encoder << hasMarkup;
  78. if (hasMarkup)
  79. encoder << dataObject->markup();
  80. bool hasURL = dataObject->hasURL();
  81. encoder << hasURL;
  82. if (hasURL)
  83. encoder << dataObject->url().string();
  84. bool hasURIList = dataObject->hasURIList();
  85. encoder << hasURIList;
  86. if (hasURIList)
  87. encoder << dataObject->uriList();
  88. bool hasImage = dataObject->hasImage();
  89. encoder << hasImage;
  90. if (hasImage)
  91. encodeImage(encoder, dataObject->image());
  92. }
  93. static bool decodeDataObject(ArgumentDecoder& decoder, RefPtr<DataObjectGtk>& dataObject)
  94. {
  95. RefPtr<DataObjectGtk> data = DataObjectGtk::create();
  96. bool hasText;
  97. if (!decoder.decode(hasText))
  98. return false;
  99. if (hasText) {
  100. String text;
  101. if (!decoder.decode(text))
  102. return false;
  103. data->setText(text);
  104. }
  105. bool hasMarkup;
  106. if (!decoder.decode(hasMarkup))
  107. return false;
  108. if (hasMarkup) {
  109. String markup;
  110. if (!decoder.decode(markup))
  111. return false;
  112. data->setMarkup(markup);
  113. }
  114. bool hasURL;
  115. if (!decoder.decode(hasURL))
  116. return false;
  117. if (hasURL) {
  118. String url;
  119. if (!decoder.decode(url))
  120. return false;
  121. data->setURL(KURL(KURL(), url), String());
  122. }
  123. bool hasURIList;
  124. if (!decoder.decode(hasURIList))
  125. return false;
  126. if (hasURIList) {
  127. String uriList;
  128. if (!decoder.decode(uriList))
  129. return false;
  130. data->setURIList(uriList);
  131. }
  132. bool hasImage;
  133. if (!decoder.decode(hasImage))
  134. return false;
  135. if (hasImage) {
  136. GRefPtr<GdkPixbuf> image;
  137. if (!decodeImage(decoder, image))
  138. return false;
  139. data->setImage(image.get());
  140. }
  141. dataObject = data;
  142. return true;
  143. }
  144. void ArgumentCoder<DragData>::encode(ArgumentEncoder& encoder, const DragData& dragData)
  145. {
  146. encoder << dragData.clientPosition();
  147. encoder << dragData.globalPosition();
  148. encoder << static_cast<uint64_t>(dragData.draggingSourceOperationMask());
  149. encoder << static_cast<uint64_t>(dragData.flags());
  150. DataObjectGtk* platformData = dragData.platformData();
  151. encoder << static_cast<bool>(platformData);
  152. if (platformData)
  153. encodeDataObject(encoder, platformData);
  154. }
  155. bool ArgumentCoder<DragData>::decode(ArgumentDecoder& decoder, DragData& dragData)
  156. {
  157. IntPoint clientPosition;
  158. if (!decoder.decode(clientPosition))
  159. return false;
  160. IntPoint globalPosition;
  161. if (!decoder.decode(globalPosition))
  162. return false;
  163. uint64_t sourceOperationMask;
  164. if (!decoder.decode(sourceOperationMask))
  165. return false;
  166. uint64_t flags;
  167. if (!decoder.decode(flags))
  168. return false;
  169. bool hasPlatformData;
  170. if (!decoder.decode(hasPlatformData))
  171. return false;
  172. RefPtr<DataObjectGtk> platformData;
  173. if (hasPlatformData) {
  174. if (!decodeDataObject(decoder, platformData))
  175. return false;
  176. }
  177. dragData = DragData(platformData.release().leakRef(), clientPosition, globalPosition, static_cast<DragOperation>(sourceOperationMask),
  178. static_cast<DragApplicationFlags>(flags));
  179. return true;
  180. }
  181. static void encodeGKeyFile(ArgumentEncoder& encoder, GKeyFile* keyFile)
  182. {
  183. gsize dataSize;
  184. GOwnPtr<char> data(g_key_file_to_data(keyFile, &dataSize, 0));
  185. encoder << DataReference(reinterpret_cast<uint8_t*>(data.get()), dataSize);
  186. }
  187. static bool decodeGKeyFile(ArgumentDecoder& decoder, GOwnPtr<GKeyFile>& keyFile)
  188. {
  189. DataReference dataReference;
  190. if (!decoder.decode(dataReference))
  191. return false;
  192. if (!dataReference.size())
  193. return true;
  194. keyFile.set(g_key_file_new());
  195. if (!g_key_file_load_from_data(keyFile.get(), reinterpret_cast<const gchar*>(dataReference.data()), dataReference.size(), G_KEY_FILE_NONE, 0)) {
  196. keyFile.clear();
  197. return false;
  198. }
  199. return true;
  200. }
  201. void encode(ArgumentEncoder& encoder, GtkPrintSettings* printSettings)
  202. {
  203. GOwnPtr<GKeyFile> keyFile(g_key_file_new());
  204. gtk_print_settings_to_key_file(printSettings, keyFile.get(), "Print Settings");
  205. encodeGKeyFile(encoder, keyFile.get());
  206. }
  207. bool decode(ArgumentDecoder& decoder, GRefPtr<GtkPrintSettings>& printSettings)
  208. {
  209. GOwnPtr<GKeyFile> keyFile;
  210. if (!decodeGKeyFile(decoder, keyFile))
  211. return false;
  212. printSettings = adoptGRef(gtk_print_settings_new());
  213. if (!keyFile)
  214. return true;
  215. if (!gtk_print_settings_load_key_file(printSettings.get(), keyFile.get(), "Print Settings", 0))
  216. printSettings = 0;
  217. return printSettings;
  218. }
  219. void encode(ArgumentEncoder& encoder, GtkPageSetup* pageSetup)
  220. {
  221. GOwnPtr<GKeyFile> keyFile(g_key_file_new());
  222. gtk_page_setup_to_key_file(pageSetup, keyFile.get(), "Page Setup");
  223. encodeGKeyFile(encoder, keyFile.get());
  224. }
  225. bool decode(ArgumentDecoder& decoder, GRefPtr<GtkPageSetup>& pageSetup)
  226. {
  227. GOwnPtr<GKeyFile> keyFile;
  228. if (!decodeGKeyFile(decoder, keyFile))
  229. return false;
  230. pageSetup = adoptGRef(gtk_page_setup_new());
  231. if (!keyFile)
  232. return true;
  233. if (!gtk_page_setup_load_key_file(pageSetup.get(), keyFile.get(), "Page Setup", 0))
  234. pageSetup = 0;
  235. return pageSetup;
  236. }
  237. }