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