summaryrefslogtreecommitdiffstats
path: root/dom/plugins/ipc/hangui/MiniShmBase.h
blob: 0ac8840dd6b3c6497fe328e18a9c98e164260fea (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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 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/. */

#ifndef mozilla_plugins_MiniShmBase_h
#define mozilla_plugins_MiniShmBase_h

#include "base/basictypes.h"

#include "nsDebug.h"

#include <windows.h>

namespace mozilla {
namespace plugins {

/**
 * This class is used to provide RAII semantics for mapped views.
 * @see ScopedHandle
 */
class ScopedMappedFileView
{
public:
  explicit
  ScopedMappedFileView(LPVOID aView)
    : mView(aView)
  {
  }

  ~ScopedMappedFileView()
  {
    Close();
  }

  void
  Close()
  {
    if (mView) {
      ::UnmapViewOfFile(mView);
      mView = nullptr;
    }
  }

  void
  Set(LPVOID aView)
  {
    Close();
    mView = aView;
  }

  LPVOID
  Get() const
  {
    return mView;
  }

  LPVOID
  Take()
  {
    LPVOID result = mView;
    mView = nullptr;
    return result;
  }

  operator LPVOID()
  {
    return mView;
  }

  bool
  IsValid() const
  {
    return (mView);
  }

private:
  DISALLOW_COPY_AND_ASSIGN(ScopedMappedFileView);

  LPVOID mView;
};

class MiniShmBase;

class MiniShmObserver
{
public:
  /**
   * This function is called whenever there is a new shared memory request.
   * @param aMiniShmObj MiniShmBase object that may be used to read and 
   *                    write from shared memory.
   */
  virtual void OnMiniShmEvent(MiniShmBase *aMiniShmObj) = 0;
  /**
   * This function is called once when a MiniShmParent and a MiniShmChild
   * object have successfully negotiated a connection.
   *
   * @param aMiniShmObj MiniShmBase object that may be used to read and 
   *                    write from shared memory.
   */
  virtual void OnMiniShmConnect(MiniShmBase *aMiniShmObj) { }
};

/**
 * Base class for MiniShm connections. This class defines the common 
 * interfaces and code between parent and child.
 */
class MiniShmBase
{
public:
  /**
   * Obtains a writable pointer into shared memory of type T.
   * typename T must be plain-old-data and contain an unsigned integral 
   * member T::identifier that uniquely identifies T with respect to 
   * other types used by the protocol being implemented.
   *
   * @param aPtr Pointer to receive the shared memory address.
   *             This value is set if and only if the function 
   *             succeeded.
   * @return NS_OK if and only if aPtr was successfully obtained.
   *         NS_ERROR_ILLEGAL_VALUE if type T is not valid for MiniShm.
   *         NS_ERROR_NOT_INITIALIZED if there is no valid MiniShm connection.
   *         NS_ERROR_NOT_AVAILABLE if the memory is not safe to write.
   */
  template<typename T> nsresult
  GetWritePtr(T*& aPtr)
  {
    if (!mWriteHeader || !mGuard) {
      return NS_ERROR_NOT_INITIALIZED;
    }
    if (sizeof(T) > mPayloadMaxLen ||
        T::identifier <= RESERVED_CODE_LAST) {
      return NS_ERROR_ILLEGAL_VALUE;
    }
    if (::WaitForSingleObject(mGuard, mTimeout) != WAIT_OBJECT_0) {
      return NS_ERROR_NOT_AVAILABLE;
    }
    mWriteHeader->mId = T::identifier;
    mWriteHeader->mPayloadLen = sizeof(T);
    aPtr = reinterpret_cast<T*>(mWriteHeader + 1);
    return NS_OK;
  }

  /**
   * Obtains a readable pointer into shared memory of type T.
   * typename T must be plain-old-data and contain an unsigned integral 
   * member T::identifier that uniquely identifies T with respect to 
   * other types used by the protocol being implemented.
   *
   * @param aPtr Pointer to receive the shared memory address.
   *             This value is set if and only if the function 
   *             succeeded.
   * @return NS_OK if and only if aPtr was successfully obtained.
   *         NS_ERROR_ILLEGAL_VALUE if type T is not valid for MiniShm or if
   *                                type T does not match the type of the data
   *                                stored in shared memory.
   *         NS_ERROR_NOT_INITIALIZED if there is no valid MiniShm connection.
   */
  template<typename T> nsresult
  GetReadPtr(const T*& aPtr)
  {
    if (!mReadHeader) {
      return NS_ERROR_NOT_INITIALIZED;
    }
    if (mReadHeader->mId != T::identifier ||
        sizeof(T) != mReadHeader->mPayloadLen) {
      return NS_ERROR_ILLEGAL_VALUE;
    }
    aPtr = reinterpret_cast<const T*>(mReadHeader + 1);
    return NS_OK;
  }

  /**
   * Fires the peer's event causing its request handler to execute.
   *
   * @return Should return NS_OK if the send was successful.
   */
  virtual nsresult
  Send() = 0;

protected:
  /**
   * MiniShm reserves some identifier codes for its own use. Any 
   * identifiers used by MiniShm protocol implementations must be 
   * greater than RESERVED_CODE_LAST.
   */
  enum ReservedCodes
  {
    RESERVED_CODE_INIT = 0,
    RESERVED_CODE_INIT_COMPLETE = 1,
    RESERVED_CODE_LAST = RESERVED_CODE_INIT_COMPLETE
  };

  struct MiniShmHeader
  {
    unsigned int  mId;
    unsigned int  mPayloadLen;
  };

  struct MiniShmInit
  {
    enum identifier_t
    {
      identifier = RESERVED_CODE_INIT
    };
    HANDLE    mParentEvent;
    HANDLE    mParentGuard;
    HANDLE    mChildEvent;
    HANDLE    mChildGuard;
  };

  struct MiniShmInitComplete
  {
    enum identifier_t
    {
      identifier = RESERVED_CODE_INIT_COMPLETE
    };
    bool      mSucceeded;
  };

  MiniShmBase()
    : mObserver(nullptr),
      mWriteHeader(nullptr),
      mReadHeader(nullptr),
      mPayloadMaxLen(0),
      mGuard(nullptr),
      mTimeout(INFINITE)
  {
  }
  virtual ~MiniShmBase()
  { }

  virtual void
  OnEvent()
  {
    if (mObserver) {
      mObserver->OnMiniShmEvent(this);
    }
  }

  virtual void
  OnConnect()
  {
    if (mObserver) {
      mObserver->OnMiniShmConnect(this);
    }
  }

  nsresult
  SetView(LPVOID aView, const unsigned int aSize, bool aIsChild)
  {
    if (!aView || aSize <= 2 * sizeof(MiniShmHeader)) {
      return NS_ERROR_ILLEGAL_VALUE;
    }
    // Divide the region into halves for parent and child
    if (aIsChild) {
      mReadHeader = static_cast<MiniShmHeader*>(aView);
      mWriteHeader = reinterpret_cast<MiniShmHeader*>(static_cast<char*>(aView)
                                                      + aSize / 2U);
    } else {
      mWriteHeader = static_cast<MiniShmHeader*>(aView);
      mReadHeader = reinterpret_cast<MiniShmHeader*>(static_cast<char*>(aView)
                                                     + aSize / 2U);
    }
    mPayloadMaxLen = aSize / 2U - sizeof(MiniShmHeader);
    return NS_OK;
  }

  nsresult
  SetGuard(HANDLE aGuard, DWORD aTimeout)
  {
    if (!aGuard || !aTimeout) {
      return NS_ERROR_ILLEGAL_VALUE;
    }
    mGuard = aGuard;
    mTimeout = aTimeout;
    return NS_OK;
  }

  inline void
  SetObserver(MiniShmObserver *aObserver) { mObserver = aObserver; }

  /**
   * Obtains a writable pointer into shared memory of type T. This version 
   * differs from GetWritePtr in that it allows typename T to be one of 
   * the private data structures declared in MiniShmBase.
   *
   * @param aPtr Pointer to receive the shared memory address.
   *             This value is set if and only if the function 
   *             succeeded.
   * @return NS_OK if and only if aPtr was successfully obtained.
   *         NS_ERROR_ILLEGAL_VALUE if type T not an internal MiniShm struct.
   *         NS_ERROR_NOT_INITIALIZED if there is no valid MiniShm connection.
   */
  template<typename T> nsresult
  GetWritePtrInternal(T*& aPtr)
  {
    if (!mWriteHeader) {
      return NS_ERROR_NOT_INITIALIZED;
    }
    if (sizeof(T) > mPayloadMaxLen ||
        T::identifier > RESERVED_CODE_LAST) {
      return NS_ERROR_ILLEGAL_VALUE;
    }
    mWriteHeader->mId = T::identifier;
    mWriteHeader->mPayloadLen = sizeof(T);
    aPtr = reinterpret_cast<T*>(mWriteHeader + 1);
    return NS_OK;
  }

  static VOID CALLBACK
  SOnEvent(PVOID aContext, BOOLEAN aIsTimer)
  {
    MiniShmBase* object = static_cast<MiniShmBase*>(aContext);
    object->OnEvent();
  }

private:
  MiniShmObserver*  mObserver;
  MiniShmHeader*    mWriteHeader;
  MiniShmHeader*    mReadHeader;
  unsigned int      mPayloadMaxLen;
  HANDLE            mGuard;
  DWORD             mTimeout;

  DISALLOW_COPY_AND_ASSIGN(MiniShmBase);
};

} // namespace plugins
} // namespace mozilla

#endif // mozilla_plugins_MiniShmBase_h