summaryrefslogtreecommitdiffstats
path: root/dom/plugins/ipc/PluginMessageUtils.cpp
blob: 4d639b6f391ba93b9a51df991b72574ce6906d23 (plain)
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
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8; -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* 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 "PluginMessageUtils.h"
#include "nsThreadUtils.h"

#include "PluginInstanceParent.h"
#include "PluginInstanceChild.h"
#include "PluginScriptableObjectParent.h"
#include "PluginScriptableObjectChild.h"

using std::string;

using mozilla::ipc::MessageChannel;

namespace {

class DeferNPObjectReleaseRunnable : public mozilla::Runnable {
 public:
  DeferNPObjectReleaseRunnable(const NPNetscapeFuncs* f, NPObject* o)
      : Runnable("DeferNPObjectReleaseRunnable"), mFuncs(f), mObject(o) {
    NS_ASSERTION(o, "no release null objects");
  }

  NS_IMETHOD Run() override;

 private:
  const NPNetscapeFuncs* mFuncs;
  NPObject* mObject;
};

NS_IMETHODIMP
DeferNPObjectReleaseRunnable::Run() {
  mFuncs->releaseobject(mObject);
  return NS_OK;
}

}  // namespace

namespace mozilla::plugins {

NPRemoteWindow::NPRemoteWindow()
    : window(0),
      x(0),
      y(0),
      width(0),
      height(0),
      type(NPWindowTypeDrawable)
#if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
      ,
      visualID(0),
      colormap(0)
#endif /* XP_UNIX */
#if defined(XP_MACOSX)
      ,
      contentsScaleFactor(1.0)
#endif
{
  clipRect.top = 0;
  clipRect.left = 0;
  clipRect.bottom = 0;
  clipRect.right = 0;
}

ipc::RacyInterruptPolicy MediateRace(const MessageChannel::MessageInfo& parent,
                                     const MessageChannel::MessageInfo& child) {
  switch (parent.type()) {
    case PPluginInstance::Msg_Paint__ID:
    case PPluginInstance::Msg_NPP_SetWindow__ID:
    case PPluginInstance::Msg_NPP_HandleEvent_Shmem__ID:
    case PPluginInstance::Msg_NPP_HandleEvent_IOSurface__ID:
      // our code relies on the frame list not changing during paints and
      // reflows
      return ipc::RIPParentWins;

    default:
      return ipc::RIPChildWins;
  }
}

#if defined(OS_LINUX) || defined(OS_SOLARIS)
static string ReplaceAll(const string& haystack, const string& needle,
                         const string& with) {
  string munged = haystack;
  string::size_type i = 0;

  while (string::npos != (i = munged.find(needle, i))) {
    munged.replace(i, needle.length(), with);
    i += with.length();
  }

  return munged;
}
#endif

string MungePluginDsoPath(const string& path) {
#if defined(OS_LINUX) || defined(OS_SOLARIS)
  // https://bugzilla.mozilla.org/show_bug.cgi?id=519601
  return ReplaceAll(path, "netscape", "netsc@pe");
#else
  return path;
#endif
}

string UnmungePluginDsoPath(const string& munged) {
#if defined(OS_LINUX) || defined(OS_SOLARIS)
  return ReplaceAll(munged, "netsc@pe", "netscape");
#else
  return munged;
#endif
}

LogModule* GetPluginLog() {
  static LazyLogModule sLog("IPCPlugins");
  return sLog;
}

void DeferNPObjectLastRelease(const NPNetscapeFuncs* f, NPObject* o) {
  if (!o) return;

  if (o->referenceCount > 1) {
    f->releaseobject(o);
    return;
  }

  NS_DispatchToCurrentThread(new DeferNPObjectReleaseRunnable(f, o));
}

void DeferNPVariantLastRelease(const NPNetscapeFuncs* f, NPVariant* v) {
  if (!NPVARIANT_IS_OBJECT(*v)) {
    f->releasevariantvalue(v);
    return;
  }
  DeferNPObjectLastRelease(f, v->value.objectValue);
  VOID_TO_NPVARIANT(*v);
}

}  // namespace mozilla::plugins