dom/plugins/ipc/PluginMessageUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/ipc/PluginMessageUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,915 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=4 ts=4 et :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef DOM_PLUGINS_PLUGINMESSAGEUTILS_H
    1.11 +#define DOM_PLUGINS_PLUGINMESSAGEUTILS_H
    1.12 +
    1.13 +#include "ipc/IPCMessageUtils.h"
    1.14 +#include "base/message_loop.h"
    1.15 +
    1.16 +#include "mozilla/ipc/MessageChannel.h"
    1.17 +#include "mozilla/ipc/CrossProcessMutex.h"
    1.18 +#include "gfxipc/ShadowLayerUtils.h"
    1.19 +
    1.20 +#include "npapi.h"
    1.21 +#include "npruntime.h"
    1.22 +#include "npfunctions.h"
    1.23 +#include "nsAutoPtr.h"
    1.24 +#include "nsString.h"
    1.25 +#include "nsTArray.h"
    1.26 +#include "prlog.h"
    1.27 +#include "nsHashKeys.h"
    1.28 +#ifdef MOZ_CRASHREPORTER
    1.29 +#  include "nsExceptionHandler.h"
    1.30 +#endif
    1.31 +#ifdef XP_MACOSX
    1.32 +#include "PluginInterposeOSX.h"
    1.33 +#else
    1.34 +namespace mac_plugin_interposing { class NSCursorInfo { }; }
    1.35 +#endif
    1.36 +using mac_plugin_interposing::NSCursorInfo;
    1.37 +
    1.38 +namespace mozilla {
    1.39 +namespace plugins {
    1.40 +
    1.41 +using layers::SurfaceDescriptorX11;
    1.42 +
    1.43 +enum ScriptableObjectType
    1.44 +{
    1.45 +  LocalObject,
    1.46 +  Proxy
    1.47 +};
    1.48 +
    1.49 +mozilla::ipc::RacyInterruptPolicy
    1.50 +MediateRace(const mozilla::ipc::MessageChannel::Message& parent,
    1.51 +            const mozilla::ipc::MessageChannel::Message& child);
    1.52 +
    1.53 +std::string
    1.54 +MungePluginDsoPath(const std::string& path);
    1.55 +std::string
    1.56 +UnmungePluginDsoPath(const std::string& munged);
    1.57 +
    1.58 +extern PRLogModuleInfo* GetPluginLog();
    1.59 +
    1.60 +const uint32_t kAllowAsyncDrawing = 0x1;
    1.61 +
    1.62 +inline bool IsDrawingModelAsync(int16_t aModel) {
    1.63 +  return aModel == NPDrawingModelAsyncBitmapSurface
    1.64 +#ifdef XP_WIN
    1.65 +         || aModel == NPDrawingModelAsyncWindowsDXGISurface
    1.66 +#endif
    1.67 +         ;
    1.68 +}
    1.69 +
    1.70 +#if defined(_MSC_VER)
    1.71 +#define FULLFUNCTION __FUNCSIG__
    1.72 +#elif defined(__GNUC__)
    1.73 +#define FULLFUNCTION __PRETTY_FUNCTION__
    1.74 +#else
    1.75 +#define FULLFUNCTION __FUNCTION__
    1.76 +#endif
    1.77 +
    1.78 +#define PLUGIN_LOG_DEBUG(args) PR_LOG(GetPluginLog(), PR_LOG_DEBUG, args)
    1.79 +#define PLUGIN_LOG_DEBUG_FUNCTION PR_LOG(GetPluginLog(), PR_LOG_DEBUG, ("%s", FULLFUNCTION))
    1.80 +#define PLUGIN_LOG_DEBUG_METHOD PR_LOG(GetPluginLog(), PR_LOG_DEBUG, ("%s [%p]", FULLFUNCTION, (void*) this))
    1.81 +
    1.82 +/**
    1.83 + * This is NPByteRange without the linked list.
    1.84 + */
    1.85 +struct IPCByteRange
    1.86 +{
    1.87 +  int32_t offset;
    1.88 +  uint32_t length;
    1.89 +};  
    1.90 +
    1.91 +typedef std::vector<IPCByteRange> IPCByteRanges;
    1.92 +
    1.93 +typedef nsCString Buffer;
    1.94 +
    1.95 +struct NPRemoteWindow
    1.96 +{
    1.97 +  NPRemoteWindow();
    1.98 +  uint64_t window;
    1.99 +  int32_t x;
   1.100 +  int32_t y;
   1.101 +  uint32_t width;
   1.102 +  uint32_t height;
   1.103 +  NPRect clipRect;
   1.104 +  NPWindowType type;
   1.105 +#if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
   1.106 +  VisualID visualID;
   1.107 +  Colormap colormap;
   1.108 +#endif /* XP_UNIX */
   1.109 +#if defined(XP_WIN)
   1.110 +  base::SharedMemoryHandle surfaceHandle;
   1.111 +#endif
   1.112 +#if defined(XP_MACOSX)
   1.113 +  double contentsScaleFactor;
   1.114 +#endif
   1.115 +};
   1.116 +
   1.117 +#ifdef XP_WIN
   1.118 +typedef HWND NativeWindowHandle;
   1.119 +#elif defined(MOZ_X11)
   1.120 +typedef XID NativeWindowHandle;
   1.121 +#elif defined(XP_MACOSX) || defined(ANDROID) || defined(MOZ_WIDGET_QT)
   1.122 +typedef intptr_t NativeWindowHandle; // never actually used, will always be 0
   1.123 +#else
   1.124 +#error Need NativeWindowHandle for this platform
   1.125 +#endif
   1.126 +
   1.127 +#ifdef XP_WIN
   1.128 +typedef base::SharedMemoryHandle WindowsSharedMemoryHandle;
   1.129 +typedef HANDLE DXGISharedSurfaceHandle;
   1.130 +#else
   1.131 +typedef mozilla::null_t WindowsSharedMemoryHandle;
   1.132 +typedef mozilla::null_t DXGISharedSurfaceHandle;
   1.133 +#endif
   1.134 +
   1.135 +// XXX maybe not the best place for these. better one?
   1.136 +
   1.137 +#define VARSTR(v_)  case v_: return #v_
   1.138 +inline const char* const
   1.139 +NPPVariableToString(NPPVariable aVar)
   1.140 +{
   1.141 +    switch (aVar) {
   1.142 +        VARSTR(NPPVpluginNameString);
   1.143 +        VARSTR(NPPVpluginDescriptionString);
   1.144 +        VARSTR(NPPVpluginWindowBool);
   1.145 +        VARSTR(NPPVpluginTransparentBool);
   1.146 +        VARSTR(NPPVjavaClass);
   1.147 +        VARSTR(NPPVpluginWindowSize);
   1.148 +        VARSTR(NPPVpluginTimerInterval);
   1.149 +
   1.150 +        VARSTR(NPPVpluginScriptableInstance);
   1.151 +        VARSTR(NPPVpluginScriptableIID);
   1.152 +
   1.153 +        VARSTR(NPPVjavascriptPushCallerBool);
   1.154 +
   1.155 +        VARSTR(NPPVpluginKeepLibraryInMemory);
   1.156 +        VARSTR(NPPVpluginNeedsXEmbed);
   1.157 +
   1.158 +        VARSTR(NPPVpluginScriptableNPObject);
   1.159 +
   1.160 +        VARSTR(NPPVformValue);
   1.161 +  
   1.162 +        VARSTR(NPPVpluginUrlRequestsDisplayedBool);
   1.163 +  
   1.164 +        VARSTR(NPPVpluginWantsAllNetworkStreams);
   1.165 +
   1.166 +#ifdef XP_MACOSX
   1.167 +        VARSTR(NPPVpluginDrawingModel);
   1.168 +        VARSTR(NPPVpluginEventModel);
   1.169 +#endif
   1.170 +
   1.171 +    default: return "???";
   1.172 +    }
   1.173 +}
   1.174 +
   1.175 +inline const char*
   1.176 +NPNVariableToString(NPNVariable aVar)
   1.177 +{
   1.178 +    switch(aVar) {
   1.179 +        VARSTR(NPNVxDisplay);
   1.180 +        VARSTR(NPNVxtAppContext);
   1.181 +        VARSTR(NPNVnetscapeWindow);
   1.182 +        VARSTR(NPNVjavascriptEnabledBool);
   1.183 +        VARSTR(NPNVasdEnabledBool);
   1.184 +        VARSTR(NPNVisOfflineBool);
   1.185 +
   1.186 +        VARSTR(NPNVserviceManager);
   1.187 +        VARSTR(NPNVDOMElement);
   1.188 +        VARSTR(NPNVDOMWindow);
   1.189 +        VARSTR(NPNVToolkit);
   1.190 +        VARSTR(NPNVSupportsXEmbedBool);
   1.191 +
   1.192 +        VARSTR(NPNVWindowNPObject);
   1.193 +
   1.194 +        VARSTR(NPNVPluginElementNPObject);
   1.195 +
   1.196 +        VARSTR(NPNVSupportsWindowless);
   1.197 +
   1.198 +        VARSTR(NPNVprivateModeBool);
   1.199 +        VARSTR(NPNVdocumentOrigin);
   1.200 +
   1.201 +    default: return "???";
   1.202 +    }
   1.203 +}
   1.204 +#undef VARSTR
   1.205 +
   1.206 +inline bool IsPluginThread()
   1.207 +{
   1.208 +  MessageLoop* loop = MessageLoop::current();
   1.209 +  if (!loop)
   1.210 +      return false;
   1.211 +  return (loop->type() == MessageLoop::TYPE_UI);
   1.212 +}
   1.213 +
   1.214 +inline void AssertPluginThread()
   1.215 +{
   1.216 +  NS_ASSERTION(IsPluginThread(), "Should be on the plugin's main thread!");
   1.217 +}
   1.218 +
   1.219 +#define ENSURE_PLUGIN_THREAD(retval) \
   1.220 +  PR_BEGIN_MACRO \
   1.221 +    if (!IsPluginThread()) { \
   1.222 +      NS_WARNING("Not running on the plugin's main thread!"); \
   1.223 +      return (retval); \
   1.224 +    } \
   1.225 +  PR_END_MACRO
   1.226 +
   1.227 +#define ENSURE_PLUGIN_THREAD_VOID() \
   1.228 +  PR_BEGIN_MACRO \
   1.229 +    if (!IsPluginThread()) { \
   1.230 +      NS_WARNING("Not running on the plugin's main thread!"); \
   1.231 +      return; \
   1.232 +    } \
   1.233 +  PR_END_MACRO
   1.234 +
   1.235 +void DeferNPObjectLastRelease(const NPNetscapeFuncs* f, NPObject* o);
   1.236 +void DeferNPVariantLastRelease(const NPNetscapeFuncs* f, NPVariant* v);
   1.237 +
   1.238 +// in NPAPI, char* == nullptr is sometimes meaningful.  the following is
   1.239 +// helper code for dealing with nullable nsCString's
   1.240 +inline nsCString
   1.241 +NullableString(const char* aString)
   1.242 +{
   1.243 +    if (!aString) {
   1.244 +        nsCString str;
   1.245 +        str.SetIsVoid(true);
   1.246 +        return str;
   1.247 +    }
   1.248 +    return nsCString(aString);
   1.249 +}
   1.250 +
   1.251 +inline const char*
   1.252 +NullableStringGet(const nsCString& str)
   1.253 +{
   1.254 +  if (str.IsVoid())
   1.255 +    return nullptr;
   1.256 +
   1.257 +  return str.get();
   1.258 +}
   1.259 +
   1.260 +struct DeletingObjectEntry : public nsPtrHashKey<NPObject>
   1.261 +{
   1.262 +  DeletingObjectEntry(const NPObject* key)
   1.263 +    : nsPtrHashKey<NPObject>(key)
   1.264 +    , mDeleted(false)
   1.265 +  { }
   1.266 +
   1.267 +  bool mDeleted;
   1.268 +};
   1.269 +
   1.270 +#ifdef XP_WIN
   1.271 +// The private event used for double-pass widgetless plugin rendering.
   1.272 +UINT DoublePassRenderingEvent();
   1.273 +#endif
   1.274 +
   1.275 +} /* namespace plugins */
   1.276 +
   1.277 +} /* namespace mozilla */
   1.278 +
   1.279 +namespace IPC {
   1.280 +
   1.281 +template <>
   1.282 +struct ParamTraits<NPRect>
   1.283 +{
   1.284 +  typedef NPRect paramType;
   1.285 +
   1.286 +  static void Write(Message* aMsg, const paramType& aParam)
   1.287 +  {
   1.288 +    WriteParam(aMsg, aParam.top);
   1.289 +    WriteParam(aMsg, aParam.left);
   1.290 +    WriteParam(aMsg, aParam.bottom);
   1.291 +    WriteParam(aMsg, aParam.right);
   1.292 +  }
   1.293 +
   1.294 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.295 +  {
   1.296 +    uint16_t top, left, bottom, right;
   1.297 +    if (ReadParam(aMsg, aIter, &top) &&
   1.298 +        ReadParam(aMsg, aIter, &left) &&
   1.299 +        ReadParam(aMsg, aIter, &bottom) &&
   1.300 +        ReadParam(aMsg, aIter, &right)) {
   1.301 +      aResult->top = top;
   1.302 +      aResult->left = left;
   1.303 +      aResult->bottom = bottom;
   1.304 +      aResult->right = right;
   1.305 +      return true;
   1.306 +    }
   1.307 +    return false;
   1.308 +  }
   1.309 +
   1.310 +  static void Log(const paramType& aParam, std::wstring* aLog)
   1.311 +  {
   1.312 +    aLog->append(StringPrintf(L"[%u, %u, %u, %u]", aParam.top, aParam.left,
   1.313 +                              aParam.bottom, aParam.right));
   1.314 +  }
   1.315 +};
   1.316 +
   1.317 +template <>
   1.318 +struct ParamTraits<NPWindowType>
   1.319 +{
   1.320 +  typedef NPWindowType paramType;
   1.321 +
   1.322 +  static void Write(Message* aMsg, const paramType& aParam)
   1.323 +  {
   1.324 +    aMsg->WriteInt16(int16_t(aParam));
   1.325 +  }
   1.326 +
   1.327 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.328 +  {
   1.329 +    int16_t result;
   1.330 +    if (aMsg->ReadInt16(aIter, &result)) {
   1.331 +      *aResult = paramType(result);
   1.332 +      return true;
   1.333 +    }
   1.334 +    return false;
   1.335 +  }
   1.336 +
   1.337 +  static void Log(const paramType& aParam, std::wstring* aLog)
   1.338 +  {
   1.339 +    aLog->append(StringPrintf(L"%d", int16_t(aParam)));
   1.340 +  }
   1.341 +};
   1.342 +
   1.343 +template <>
   1.344 +struct ParamTraits<NPImageFormat>
   1.345 +{
   1.346 +  typedef NPImageFormat paramType;
   1.347 +
   1.348 +  static void Write(Message* aMsg, const paramType& aParam)
   1.349 +  {
   1.350 +    aMsg->WriteInt16(int16_t(aParam));
   1.351 +  }
   1.352 +
   1.353 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.354 +  {
   1.355 +    int16_t result;
   1.356 +    if (aMsg->ReadInt16(aIter, &result)) {
   1.357 +      *aResult = paramType(result);
   1.358 +      return true;
   1.359 +    }
   1.360 +    return false;
   1.361 +  }
   1.362 +
   1.363 +  static void Log(const paramType& aParam, std::wstring* aLog)
   1.364 +  {
   1.365 +    aLog->append(StringPrintf(L"%d", int16_t(aParam)));
   1.366 +  }
   1.367 +};
   1.368 +
   1.369 +template <>
   1.370 +struct ParamTraits<mozilla::plugins::NPRemoteWindow>
   1.371 +{
   1.372 +  typedef mozilla::plugins::NPRemoteWindow paramType;
   1.373 +
   1.374 +  static void Write(Message* aMsg, const paramType& aParam)
   1.375 +  {
   1.376 +    aMsg->WriteUInt64(aParam.window);
   1.377 +    WriteParam(aMsg, aParam.x);
   1.378 +    WriteParam(aMsg, aParam.y);
   1.379 +    WriteParam(aMsg, aParam.width);
   1.380 +    WriteParam(aMsg, aParam.height);
   1.381 +    WriteParam(aMsg, aParam.clipRect);
   1.382 +    WriteParam(aMsg, aParam.type);
   1.383 +#if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
   1.384 +    aMsg->WriteULong(aParam.visualID);
   1.385 +    aMsg->WriteULong(aParam.colormap);
   1.386 +#endif
   1.387 +#if defined(XP_WIN)
   1.388 +    WriteParam(aMsg, aParam.surfaceHandle);
   1.389 +#endif
   1.390 +#if defined(XP_MACOSX)
   1.391 +    aMsg->WriteDouble(aParam.contentsScaleFactor);
   1.392 +#endif
   1.393 +  }
   1.394 +
   1.395 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.396 +  {
   1.397 +    uint64_t window;
   1.398 +    int32_t x, y;
   1.399 +    uint32_t width, height;
   1.400 +    NPRect clipRect;
   1.401 +    NPWindowType type;
   1.402 +    if (!(aMsg->ReadUInt64(aIter, &window) &&
   1.403 +          ReadParam(aMsg, aIter, &x) &&
   1.404 +          ReadParam(aMsg, aIter, &y) &&
   1.405 +          ReadParam(aMsg, aIter, &width) &&
   1.406 +          ReadParam(aMsg, aIter, &height) &&
   1.407 +          ReadParam(aMsg, aIter, &clipRect) &&
   1.408 +          ReadParam(aMsg, aIter, &type)))
   1.409 +      return false;
   1.410 +
   1.411 +#if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
   1.412 +    unsigned long visualID;
   1.413 +    unsigned long colormap;
   1.414 +    if (!(aMsg->ReadULong(aIter, &visualID) &&
   1.415 +          aMsg->ReadULong(aIter, &colormap)))
   1.416 +      return false;
   1.417 +#endif
   1.418 +
   1.419 +#if defined(XP_WIN)
   1.420 +    base::SharedMemoryHandle surfaceHandle;
   1.421 +    if (!ReadParam(aMsg, aIter, &surfaceHandle))
   1.422 +      return false;
   1.423 +#endif
   1.424 +
   1.425 +#if defined(XP_MACOSX)
   1.426 +    double contentsScaleFactor;
   1.427 +    if (!aMsg->ReadDouble(aIter, &contentsScaleFactor))
   1.428 +      return false;
   1.429 +#endif
   1.430 +
   1.431 +    aResult->window = window;
   1.432 +    aResult->x = x;
   1.433 +    aResult->y = y;
   1.434 +    aResult->width = width;
   1.435 +    aResult->height = height;
   1.436 +    aResult->clipRect = clipRect;
   1.437 +    aResult->type = type;
   1.438 +#if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
   1.439 +    aResult->visualID = visualID;
   1.440 +    aResult->colormap = colormap;
   1.441 +#endif
   1.442 +#if defined(XP_WIN)
   1.443 +    aResult->surfaceHandle = surfaceHandle;
   1.444 +#endif
   1.445 +#if defined(XP_MACOSX)
   1.446 +    aResult->contentsScaleFactor = contentsScaleFactor;
   1.447 +#endif
   1.448 +    return true;
   1.449 +  }
   1.450 +
   1.451 +  static void Log(const paramType& aParam, std::wstring* aLog)
   1.452 +  {
   1.453 +    aLog->append(StringPrintf(L"[%u, %d, %d, %u, %u, %d",
   1.454 +                              (unsigned long)aParam.window,
   1.455 +                              aParam.x, aParam.y, aParam.width,
   1.456 +                              aParam.height, (long)aParam.type));
   1.457 +  }
   1.458 +};
   1.459 +
   1.460 +template <>
   1.461 +struct ParamTraits<NPString>
   1.462 +{
   1.463 +  typedef NPString paramType;
   1.464 +
   1.465 +  static void Write(Message* aMsg, const paramType& aParam)
   1.466 +  {
   1.467 +    WriteParam(aMsg, aParam.UTF8Length);
   1.468 +    aMsg->WriteBytes(aParam.UTF8Characters,
   1.469 +                     aParam.UTF8Length * sizeof(NPUTF8));
   1.470 +  }
   1.471 +
   1.472 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.473 +  {
   1.474 +    if (ReadParam(aMsg, aIter, &aResult->UTF8Length)) {
   1.475 +      int byteCount = aResult->UTF8Length * sizeof(NPUTF8);
   1.476 +      if (!byteCount) {
   1.477 +        aResult->UTF8Characters = "\0";
   1.478 +        return true;
   1.479 +      }
   1.480 +
   1.481 +      const char* messageBuffer = nullptr;
   1.482 +      nsAutoArrayPtr<char> newBuffer(new char[byteCount]);
   1.483 +      if (newBuffer && aMsg->ReadBytes(aIter, &messageBuffer, byteCount )) {
   1.484 +        memcpy((void*)messageBuffer, newBuffer.get(), byteCount);
   1.485 +        aResult->UTF8Characters = newBuffer.forget();
   1.486 +        return true;
   1.487 +      }
   1.488 +    }
   1.489 +    return false;
   1.490 +  }
   1.491 +
   1.492 +  static void Log(const paramType& aParam, std::wstring* aLog)
   1.493 +  {
   1.494 +    aLog->append(StringPrintf(L"%s", aParam.UTF8Characters));
   1.495 +  }
   1.496 +};
   1.497 +
   1.498 +#ifdef XP_MACOSX
   1.499 +template <>
   1.500 +struct ParamTraits<NPNSString*>
   1.501 +{
   1.502 +  typedef NPNSString* paramType;
   1.503 +
   1.504 +  // Empty string writes a length of 0 and no buffer.
   1.505 +  // We don't write a nullptr terminating character in buffers.
   1.506 +  static void Write(Message* aMsg, const paramType& aParam)
   1.507 +  {
   1.508 +    CFStringRef cfString = (CFStringRef)aParam;
   1.509 +
   1.510 +    // Write true if we have a string, false represents nullptr.
   1.511 +    aMsg->WriteBool(!!cfString);
   1.512 +    if (!cfString) {
   1.513 +      return;
   1.514 +    }
   1.515 +
   1.516 +    long length = ::CFStringGetLength(cfString);
   1.517 +    WriteParam(aMsg, length);
   1.518 +    if (length == 0) {
   1.519 +      return;
   1.520 +    }
   1.521 +
   1.522 +    // Attempt to get characters without any allocation/conversion.
   1.523 +    if (::CFStringGetCharactersPtr(cfString)) {
   1.524 +      aMsg->WriteBytes(::CFStringGetCharactersPtr(cfString), length * sizeof(UniChar));
   1.525 +    } else {
   1.526 +      UniChar *buffer = (UniChar*)moz_xmalloc(length * sizeof(UniChar));
   1.527 +      ::CFStringGetCharacters(cfString, ::CFRangeMake(0, length), buffer);
   1.528 +      aMsg->WriteBytes(buffer, length * sizeof(UniChar));
   1.529 +      free(buffer);
   1.530 +    }
   1.531 +  }
   1.532 +
   1.533 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.534 +  {
   1.535 +    bool haveString = false;
   1.536 +    if (!aMsg->ReadBool(aIter, &haveString)) {
   1.537 +      return false;
   1.538 +    }
   1.539 +    if (!haveString) {
   1.540 +      *aResult = nullptr;
   1.541 +      return true;
   1.542 +    }
   1.543 +
   1.544 +    long length;
   1.545 +    if (!ReadParam(aMsg, aIter, &length)) {
   1.546 +      return false;
   1.547 +    }
   1.548 +
   1.549 +    UniChar* buffer = nullptr;
   1.550 +    if (length != 0) {
   1.551 +      if (!aMsg->ReadBytes(aIter, (const char**)&buffer, length * sizeof(UniChar)) ||
   1.552 +          !buffer) {
   1.553 +        return false;
   1.554 +      }
   1.555 +    }
   1.556 +
   1.557 +    *aResult = (NPNSString*)::CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8*)buffer,
   1.558 +                                                      length * sizeof(UniChar),
   1.559 +                                                      kCFStringEncodingUTF16, false);
   1.560 +    if (!*aResult) {
   1.561 +      return false;
   1.562 +    }
   1.563 +
   1.564 +    return true;
   1.565 +  }
   1.566 +};
   1.567 +#endif
   1.568 +
   1.569 +#ifdef XP_MACOSX
   1.570 +template <>
   1.571 +struct ParamTraits<NSCursorInfo>
   1.572 +{
   1.573 +  typedef NSCursorInfo paramType;
   1.574 +
   1.575 +  static void Write(Message* aMsg, const paramType& aParam)
   1.576 +  {
   1.577 +    NSCursorInfo::Type type = aParam.GetType();
   1.578 +
   1.579 +    aMsg->WriteInt(type);
   1.580 +
   1.581 +    nsPoint hotSpot = aParam.GetHotSpot();
   1.582 +    WriteParam(aMsg, hotSpot.x);
   1.583 +    WriteParam(aMsg, hotSpot.y);
   1.584 +
   1.585 +    uint32_t dataLength = aParam.GetCustomImageDataLength();
   1.586 +    WriteParam(aMsg, dataLength);
   1.587 +    if (dataLength == 0) {
   1.588 +      return;
   1.589 +    }
   1.590 +
   1.591 +    uint8_t* buffer = (uint8_t*)moz_xmalloc(dataLength);
   1.592 +    memcpy(buffer, aParam.GetCustomImageData(), dataLength);
   1.593 +    aMsg->WriteBytes(buffer, dataLength);
   1.594 +    free(buffer);
   1.595 +  }
   1.596 +
   1.597 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.598 +  {
   1.599 +    NSCursorInfo::Type type;
   1.600 +    if (!aMsg->ReadInt(aIter, (int*)&type)) {
   1.601 +      return false;
   1.602 +    }
   1.603 +
   1.604 +    nscoord hotSpotX, hotSpotY;
   1.605 +    if (!ReadParam(aMsg, aIter, &hotSpotX) ||
   1.606 +        !ReadParam(aMsg, aIter, &hotSpotY)) {
   1.607 +      return false;
   1.608 +    }
   1.609 +
   1.610 +    uint32_t dataLength;
   1.611 +    if (!ReadParam(aMsg, aIter, &dataLength)) {
   1.612 +      return false;
   1.613 +    }
   1.614 +
   1.615 +    uint8_t* data = nullptr;
   1.616 +    if (dataLength != 0) {
   1.617 +      if (!aMsg->ReadBytes(aIter, (const char**)&data, dataLength) || !data) {
   1.618 +        return false;
   1.619 +      }
   1.620 +    }
   1.621 +
   1.622 +    aResult->SetType(type);
   1.623 +    aResult->SetHotSpot(nsPoint(hotSpotX, hotSpotY));
   1.624 +    aResult->SetCustomImageData(data, dataLength);
   1.625 +
   1.626 +    return true;
   1.627 +  }
   1.628 +
   1.629 +  static void Log(const paramType& aParam, std::wstring* aLog)
   1.630 +  {
   1.631 +    const char* typeName = aParam.GetTypeName();
   1.632 +    nsPoint hotSpot = aParam.GetHotSpot();
   1.633 +    int hotSpotX, hotSpotY;
   1.634 +#ifdef NS_COORD_IS_FLOAT
   1.635 +    hotSpotX = rint(hotSpot.x);
   1.636 +    hotSpotY = rint(hotSpot.y);
   1.637 +#else
   1.638 +    hotSpotX = hotSpot.x;
   1.639 +    hotSpotY = hotSpot.y;
   1.640 +#endif
   1.641 +    uint32_t dataLength = aParam.GetCustomImageDataLength();
   1.642 +    uint8_t* data = aParam.GetCustomImageData();
   1.643 +
   1.644 +    aLog->append(StringPrintf(L"[%s, (%i %i), %u, %p]",
   1.645 +                              typeName, hotSpotX, hotSpotY, dataLength, data));
   1.646 +  }
   1.647 +};
   1.648 +#else
   1.649 +template<>
   1.650 +struct ParamTraits<NSCursorInfo>
   1.651 +{
   1.652 +  typedef NSCursorInfo paramType;
   1.653 +  static void Write(Message* aMsg, const paramType& aParam) {
   1.654 +    NS_RUNTIMEABORT("NSCursorInfo isn't meaningful on this platform");
   1.655 +  }
   1.656 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult) {
   1.657 +    NS_RUNTIMEABORT("NSCursorInfo isn't meaningful on this platform");
   1.658 +    return false;
   1.659 +  }
   1.660 +};
   1.661 +#endif // #ifdef XP_MACOSX
   1.662 +
   1.663 +template <>
   1.664 +struct ParamTraits<NPVariant>
   1.665 +{
   1.666 +  typedef NPVariant paramType;
   1.667 +
   1.668 +  static void Write(Message* aMsg, const paramType& aParam)
   1.669 +  {
   1.670 +    if (NPVARIANT_IS_VOID(aParam)) {
   1.671 +      aMsg->WriteInt(0);
   1.672 +      return;
   1.673 +    }
   1.674 +
   1.675 +    if (NPVARIANT_IS_NULL(aParam)) {
   1.676 +      aMsg->WriteInt(1);
   1.677 +      return;
   1.678 +    }
   1.679 +
   1.680 +    if (NPVARIANT_IS_BOOLEAN(aParam)) {
   1.681 +      aMsg->WriteInt(2);
   1.682 +      WriteParam(aMsg, NPVARIANT_TO_BOOLEAN(aParam));
   1.683 +      return;
   1.684 +    }
   1.685 +
   1.686 +    if (NPVARIANT_IS_INT32(aParam)) {
   1.687 +      aMsg->WriteInt(3);
   1.688 +      WriteParam(aMsg, NPVARIANT_TO_INT32(aParam));
   1.689 +      return;
   1.690 +    }
   1.691 +
   1.692 +    if (NPVARIANT_IS_DOUBLE(aParam)) {
   1.693 +      aMsg->WriteInt(4);
   1.694 +      WriteParam(aMsg, NPVARIANT_TO_DOUBLE(aParam));
   1.695 +      return;
   1.696 +    }
   1.697 +
   1.698 +    if (NPVARIANT_IS_STRING(aParam)) {
   1.699 +      aMsg->WriteInt(5);
   1.700 +      WriteParam(aMsg, NPVARIANT_TO_STRING(aParam));
   1.701 +      return;
   1.702 +    }
   1.703 +
   1.704 +    NS_ERROR("Unsupported type!");
   1.705 +  }
   1.706 +
   1.707 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.708 +  {
   1.709 +    int type;
   1.710 +    if (!aMsg->ReadInt(aIter, &type)) {
   1.711 +      return false;
   1.712 +    }
   1.713 +
   1.714 +    switch (type) {
   1.715 +      case 0:
   1.716 +        VOID_TO_NPVARIANT(*aResult);
   1.717 +        return true;
   1.718 +
   1.719 +      case 1:
   1.720 +        NULL_TO_NPVARIANT(*aResult);
   1.721 +        return true;
   1.722 +
   1.723 +      case 2: {
   1.724 +        bool value;
   1.725 +        if (ReadParam(aMsg, aIter, &value)) {
   1.726 +          BOOLEAN_TO_NPVARIANT(value, *aResult);
   1.727 +          return true;
   1.728 +        }
   1.729 +      } break;
   1.730 +
   1.731 +      case 3: {
   1.732 +        int32_t value;
   1.733 +        if (ReadParam(aMsg, aIter, &value)) {
   1.734 +          INT32_TO_NPVARIANT(value, *aResult);
   1.735 +          return true;
   1.736 +        }
   1.737 +      } break;
   1.738 +
   1.739 +      case 4: {
   1.740 +        double value;
   1.741 +        if (ReadParam(aMsg, aIter, &value)) {
   1.742 +          DOUBLE_TO_NPVARIANT(value, *aResult);
   1.743 +          return true;
   1.744 +        }
   1.745 +      } break;
   1.746 +
   1.747 +      case 5: {
   1.748 +        NPString value;
   1.749 +        if (ReadParam(aMsg, aIter, &value)) {
   1.750 +          STRINGN_TO_NPVARIANT(value.UTF8Characters, value.UTF8Length,
   1.751 +                               *aResult);
   1.752 +          return true;
   1.753 +        }
   1.754 +      } break;
   1.755 +
   1.756 +      default:
   1.757 +        NS_ERROR("Unsupported type!");
   1.758 +    }
   1.759 +
   1.760 +    return false;
   1.761 +  }
   1.762 +
   1.763 +  static void Log(const paramType& aParam, std::wstring* aLog)
   1.764 +  {
   1.765 +    if (NPVARIANT_IS_VOID(aParam)) {
   1.766 +      aLog->append(L"[void]");
   1.767 +      return;
   1.768 +    }
   1.769 +
   1.770 +    if (NPVARIANT_IS_NULL(aParam)) {
   1.771 +      aLog->append(L"[null]");
   1.772 +      return;
   1.773 +    }
   1.774 +
   1.775 +    if (NPVARIANT_IS_BOOLEAN(aParam)) {
   1.776 +      LogParam(NPVARIANT_TO_BOOLEAN(aParam), aLog);
   1.777 +      return;
   1.778 +    }
   1.779 +
   1.780 +    if (NPVARIANT_IS_INT32(aParam)) {
   1.781 +      LogParam(NPVARIANT_TO_INT32(aParam), aLog);
   1.782 +      return;
   1.783 +    }
   1.784 +
   1.785 +    if (NPVARIANT_IS_DOUBLE(aParam)) {
   1.786 +      LogParam(NPVARIANT_TO_DOUBLE(aParam), aLog);
   1.787 +      return;
   1.788 +    }
   1.789 +
   1.790 +    if (NPVARIANT_IS_STRING(aParam)) {
   1.791 +      LogParam(NPVARIANT_TO_STRING(aParam), aLog);
   1.792 +      return;
   1.793 +    }
   1.794 +
   1.795 +    NS_ERROR("Unsupported type!");
   1.796 +  }
   1.797 +};
   1.798 +
   1.799 +template <>
   1.800 +struct ParamTraits<mozilla::plugins::IPCByteRange>
   1.801 +{
   1.802 +  typedef mozilla::plugins::IPCByteRange paramType;
   1.803 +
   1.804 +  static void Write(Message* aMsg, const paramType& aParam)
   1.805 +  {
   1.806 +    WriteParam(aMsg, aParam.offset);
   1.807 +    WriteParam(aMsg, aParam.length);
   1.808 +  }
   1.809 +
   1.810 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.811 +  {
   1.812 +    paramType p;
   1.813 +    if (ReadParam(aMsg, aIter, &p.offset) &&
   1.814 +        ReadParam(aMsg, aIter, &p.length)) {
   1.815 +      *aResult = p;
   1.816 +      return true;
   1.817 +    }
   1.818 +    return false;
   1.819 +  }
   1.820 +};
   1.821 +
   1.822 +template <>
   1.823 +struct ParamTraits<NPNVariable>
   1.824 +{
   1.825 +  typedef NPNVariable paramType;
   1.826 +
   1.827 +  static void Write(Message* aMsg, const paramType& aParam)
   1.828 +  {
   1.829 +    WriteParam(aMsg, int(aParam));
   1.830 +  }
   1.831 +
   1.832 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.833 +  {
   1.834 +    int intval;
   1.835 +    if (ReadParam(aMsg, aIter, &intval)) {
   1.836 +      *aResult = paramType(intval);
   1.837 +      return true;
   1.838 +    }
   1.839 +    return false;
   1.840 +  }
   1.841 +};
   1.842 +
   1.843 +template<>
   1.844 +struct ParamTraits<NPNURLVariable>
   1.845 +{
   1.846 +  typedef NPNURLVariable paramType;
   1.847 +
   1.848 +  static void Write(Message* aMsg, const paramType& aParam)
   1.849 +  {
   1.850 +    WriteParam(aMsg, int(aParam));
   1.851 +  }
   1.852 +
   1.853 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.854 +  {
   1.855 +    int intval;
   1.856 +    if (ReadParam(aMsg, aIter, &intval)) {
   1.857 +      switch (intval) {
   1.858 +      case NPNURLVCookie:
   1.859 +      case NPNURLVProxy:
   1.860 +        *aResult = paramType(intval);
   1.861 +        return true;
   1.862 +      }
   1.863 +    }
   1.864 +    return false;
   1.865 +  }
   1.866 +};
   1.867 +
   1.868 +  
   1.869 +template<>
   1.870 +struct ParamTraits<NPCoordinateSpace>
   1.871 +{
   1.872 +  typedef NPCoordinateSpace paramType;
   1.873 +
   1.874 +  static void Write(Message* aMsg, const paramType& aParam)
   1.875 +  {
   1.876 +    WriteParam(aMsg, int32_t(aParam));
   1.877 +  }
   1.878 +
   1.879 +  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
   1.880 +  {
   1.881 +    int32_t intval;
   1.882 +    if (ReadParam(aMsg, aIter, &intval)) {
   1.883 +      switch (intval) {
   1.884 +      case NPCoordinateSpacePlugin:
   1.885 +      case NPCoordinateSpaceWindow:
   1.886 +      case NPCoordinateSpaceFlippedWindow:
   1.887 +      case NPCoordinateSpaceScreen:
   1.888 +      case NPCoordinateSpaceFlippedScreen:
   1.889 +        *aResult = paramType(intval);
   1.890 +        return true;
   1.891 +      }
   1.892 +    }
   1.893 +    return false;
   1.894 +  }
   1.895 +};
   1.896 +
   1.897 +} /* namespace IPC */
   1.898 +
   1.899 +
   1.900 +// Serializing NPEvents is completely platform-specific and can be rather
   1.901 +// intricate depending on the platform.  So for readability we split it
   1.902 +// into separate files and have the only macro crud live here.
   1.903 +// 
   1.904 +// NB: these guards are based on those where struct NPEvent is defined
   1.905 +// in npapi.h.  They should be kept in sync.
   1.906 +#if defined(XP_MACOSX)
   1.907 +#  include "mozilla/plugins/NPEventOSX.h"
   1.908 +#elif defined(XP_WIN)
   1.909 +#  include "mozilla/plugins/NPEventWindows.h"
   1.910 +#elif defined(ANDROID)
   1.911 +#  include "mozilla/plugins/NPEventAndroid.h"
   1.912 +#elif defined(XP_UNIX)
   1.913 +#  include "mozilla/plugins/NPEventUnix.h"
   1.914 +#else
   1.915 +#  error Unsupported platform
   1.916 +#endif
   1.917 +
   1.918 +#endif /* DOM_PLUGINS_PLUGINMESSAGEUTILS_H */

mercurial