summaryrefslogtreecommitdiffstats
path: root/dom/plugins/ipc/MiniShmParent.cpp
blob: eb0b16c8a24cc5c2c732dc8c17d21347cf9978e0 (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
/* -*- 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/. */

#include "MiniShmParent.h"

#include "base/scoped_handle.h"

#include <sstream>

namespace mozilla {
namespace plugins {

// static
const unsigned int MiniShmParent::kDefaultMiniShmSectionSize = 0x1000;

MiniShmParent::MiniShmParent()
  : mSectionSize(0),
    mParentEvent(nullptr),
    mParentGuard(nullptr),
    mChildEvent(nullptr),
    mChildGuard(nullptr),
    mRegWait(nullptr),
    mFileMapping(nullptr),
    mView(nullptr),
    mIsConnected(false),
    mTimeout(INFINITE)
{
}

MiniShmParent::~MiniShmParent()
{
  CleanUp();
}

void
MiniShmParent::CleanUp()
{
  if (mRegWait) {
    ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE);
    mRegWait = nullptr;
  }
  if (mParentEvent) {
    ::CloseHandle(mParentEvent);
    mParentEvent = nullptr;
  }
  if (mParentGuard) {
    ::CloseHandle(mParentGuard);
    mParentGuard = nullptr;
  }
  if (mChildEvent) {
    ::CloseHandle(mChildEvent);
    mChildEvent = nullptr;
  }
  if (mChildGuard) {
    ::CloseHandle(mChildGuard);
    mChildGuard = nullptr;
  }
  if (mView) {
    ::UnmapViewOfFile(mView);
    mView = nullptr;
  }
  if (mFileMapping) {
    ::CloseHandle(mFileMapping);
    mFileMapping = nullptr;
  }
}

nsresult
MiniShmParent::Init(MiniShmObserver* aObserver, const DWORD aTimeout,
                    const unsigned int aSectionSize)
{
  if (!aObserver || !aSectionSize || (aSectionSize % 0x1000) || !aTimeout) {
    return NS_ERROR_ILLEGAL_VALUE;
  }
  if (mFileMapping) {
    return NS_ERROR_ALREADY_INITIALIZED;
  }
  SECURITY_ATTRIBUTES securityAttributes = {sizeof(securityAttributes),
                                            nullptr,
                                            TRUE};
  ScopedHandle parentEvent(::CreateEvent(&securityAttributes,
                                         FALSE,
                                         FALSE,
                                         nullptr));
  if (!parentEvent.IsValid()) {
    return NS_ERROR_FAILURE;
  }
  ScopedHandle parentGuard(::CreateEvent(&securityAttributes,
                                         FALSE,
                                         TRUE,
                                         nullptr));
  if (!parentGuard.IsValid()) {
    return NS_ERROR_FAILURE;
  }
  ScopedHandle childEvent(::CreateEvent(&securityAttributes,
                                        FALSE,
                                        FALSE,
                                        nullptr));
  if (!childEvent.IsValid()) {
    return NS_ERROR_FAILURE;
  }
  ScopedHandle childGuard(::CreateEvent(&securityAttributes,
                                        FALSE,
                                        TRUE,
                                        nullptr));
  if (!childGuard.IsValid()) {
    return NS_ERROR_FAILURE;
  }
  ScopedHandle mapping(::CreateFileMapping(INVALID_HANDLE_VALUE,
                                           &securityAttributes,
                                           PAGE_READWRITE,
                                           0,
                                           aSectionSize,
                                           nullptr));
  if (!mapping.IsValid()) {
    return NS_ERROR_FAILURE;
  }
  ScopedMappedFileView view(::MapViewOfFile(mapping,
                                            FILE_MAP_WRITE,
                                            0, 0, 0));
  if (!view.IsValid()) {
    return NS_ERROR_FAILURE;
  }
  nsresult rv = SetView(view, aSectionSize, false);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = SetGuard(childGuard, aTimeout);
  NS_ENSURE_SUCCESS(rv, rv);

  MiniShmInit* initStruct = nullptr;
  rv = GetWritePtrInternal(initStruct);
  NS_ENSURE_SUCCESS(rv, rv);
  initStruct->mParentEvent = parentEvent;
  initStruct->mParentGuard = parentGuard;
  initStruct->mChildEvent = childEvent;
  initStruct->mChildGuard = childGuard;

  if (!::RegisterWaitForSingleObject(&mRegWait,
                                     parentEvent,
                                     &SOnEvent,
                                     this,
                                     INFINITE,
                                     WT_EXECUTEDEFAULT)) {
    return NS_ERROR_FAILURE;
  }

  mParentEvent = parentEvent.Take();
  mParentGuard = parentGuard.Take();
  mChildEvent = childEvent.Take();
  mChildGuard = childGuard.Take();
  mFileMapping = mapping.Take();
  mView = view.Take();
  mSectionSize = aSectionSize;
  SetObserver(aObserver);
  mTimeout = aTimeout;
  return NS_OK;
}

nsresult
MiniShmParent::GetCookie(std::wstring& cookie)
{
  if (!mFileMapping) {
    return NS_ERROR_NOT_INITIALIZED;
  }
  std::wostringstream oss;
  oss << mFileMapping;
  if (!oss) {
    return NS_ERROR_FAILURE;
  }
  cookie = oss.str();
  return NS_OK;
}

nsresult
MiniShmParent::Send()
{
  if (!mChildEvent) {
    return NS_ERROR_NOT_INITIALIZED;
  }
  if (!::SetEvent(mChildEvent)) {
    return NS_ERROR_FAILURE;
  }
  return NS_OK;
}

bool
MiniShmParent::IsConnected() const
{
  return mIsConnected;
}

void
MiniShmParent::OnEvent()
{
  if (mIsConnected) {
    MiniShmBase::OnEvent();
  } else {
    FinalizeConnection();
  }
  ::SetEvent(mParentGuard);
}

void
MiniShmParent::FinalizeConnection()
{
  const MiniShmInitComplete* initCompleteStruct = nullptr;
  nsresult rv = GetReadPtr(initCompleteStruct);
  mIsConnected = NS_SUCCEEDED(rv) && initCompleteStruct->mSucceeded;
  if (mIsConnected) {
    OnConnect();
  }
}

} // namespace plugins
} // namespace mozilla