dom/plugins/ipc/hangui/MiniShmChild.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/ipc/hangui/MiniShmChild.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,173 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     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 +#include "MiniShmChild.h"
    1.11 +
    1.12 +#include <limits>
    1.13 +#include <sstream>
    1.14 +
    1.15 +namespace mozilla {
    1.16 +namespace plugins {
    1.17 +
    1.18 +MiniShmChild::MiniShmChild()
    1.19 +  : mParentEvent(nullptr),
    1.20 +    mParentGuard(nullptr),
    1.21 +    mChildEvent(nullptr),
    1.22 +    mChildGuard(nullptr),
    1.23 +    mFileMapping(nullptr),
    1.24 +    mRegWait(nullptr),
    1.25 +    mView(nullptr),
    1.26 +    mTimeout(INFINITE)
    1.27 +{}
    1.28 +
    1.29 +MiniShmChild::~MiniShmChild()
    1.30 +{
    1.31 +  if (mRegWait) {
    1.32 +    ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE);
    1.33 +  }
    1.34 +  if (mParentGuard) {
    1.35 +    // Try to avoid shutting down while the parent's event handler is running.
    1.36 +    ::WaitForSingleObject(mParentGuard, mTimeout);
    1.37 +    ::CloseHandle(mParentGuard);
    1.38 +  }
    1.39 +  if (mParentEvent) {
    1.40 +    ::CloseHandle(mParentEvent);
    1.41 +  }
    1.42 +  if (mChildEvent) {
    1.43 +    ::CloseHandle(mChildEvent);
    1.44 +  }
    1.45 +  if (mChildGuard) {
    1.46 +    ::CloseHandle(mChildGuard);
    1.47 +  }
    1.48 +  if (mView) {
    1.49 +    ::UnmapViewOfFile(mView);
    1.50 +  }
    1.51 +  if (mFileMapping) {
    1.52 +    ::CloseHandle(mFileMapping);
    1.53 +  }
    1.54 +}
    1.55 +
    1.56 +nsresult
    1.57 +MiniShmChild::Init(MiniShmObserver* aObserver, const std::wstring& aCookie,
    1.58 +                   const DWORD aTimeout)
    1.59 +{
    1.60 +  if (aCookie.empty() || !aTimeout) {
    1.61 +    return NS_ERROR_ILLEGAL_VALUE;
    1.62 +  }
    1.63 +  if (mFileMapping) {
    1.64 +    return NS_ERROR_ALREADY_INITIALIZED;
    1.65 +  }
    1.66 +  std::wistringstream iss(aCookie);
    1.67 +  HANDLE mapHandle = nullptr;
    1.68 +  iss >> mapHandle;
    1.69 +  if (!iss) {
    1.70 +    return NS_ERROR_ILLEGAL_VALUE;
    1.71 +  }
    1.72 +  ScopedMappedFileView view(::MapViewOfFile(mapHandle,
    1.73 +                                            FILE_MAP_WRITE,
    1.74 +                                            0, 0, 0));
    1.75 +  if (!view.IsValid()) {
    1.76 +    return NS_ERROR_FAILURE;
    1.77 +  }
    1.78 +  MEMORY_BASIC_INFORMATION memInfo = {0};
    1.79 +  SIZE_T querySize = ::VirtualQuery(view, &memInfo, sizeof(memInfo));
    1.80 +  unsigned int mappingSize = 0;
    1.81 +  if (querySize) {
    1.82 +    if (memInfo.RegionSize <= std::numeric_limits<unsigned int>::max()) {
    1.83 +      mappingSize = static_cast<unsigned int>(memInfo.RegionSize);
    1.84 +    }
    1.85 +  }
    1.86 +  if (!querySize || !mappingSize) {
    1.87 +    return NS_ERROR_FAILURE;
    1.88 +  }
    1.89 +  nsresult rv = SetView(view, mappingSize, true);
    1.90 +  if (NS_FAILED(rv)) {
    1.91 +    return rv;
    1.92 +  }
    1.93 +
    1.94 +  const MiniShmInit* initStruct = nullptr;
    1.95 +  rv = GetReadPtr(initStruct);
    1.96 +  if (NS_FAILED(rv)) {
    1.97 +    return rv;
    1.98 +  }
    1.99 +  if (!initStruct->mParentEvent || !initStruct->mParentGuard ||
   1.100 +      !initStruct->mChildEvent || !initStruct->mChildGuard) {
   1.101 +    return NS_ERROR_FAILURE;
   1.102 +  }
   1.103 +  rv = SetGuard(initStruct->mParentGuard, aTimeout);
   1.104 +  if (NS_FAILED(rv)) {
   1.105 +    return rv;
   1.106 +  }
   1.107 +  if (!::RegisterWaitForSingleObject(&mRegWait,
   1.108 +                                     initStruct->mChildEvent,
   1.109 +                                     &SOnEvent,
   1.110 +                                     this,
   1.111 +                                     INFINITE,
   1.112 +                                     WT_EXECUTEDEFAULT)) {
   1.113 +    return NS_ERROR_FAILURE;
   1.114 +  }
   1.115 +
   1.116 +  MiniShmInitComplete* initCompleteStruct = nullptr;
   1.117 +  rv = GetWritePtrInternal(initCompleteStruct);
   1.118 +  if (NS_FAILED(rv)) {
   1.119 +    ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE);
   1.120 +    mRegWait = nullptr;
   1.121 +    return NS_ERROR_FAILURE;
   1.122 +  }
   1.123 +
   1.124 +  initCompleteStruct->mSucceeded = true;
   1.125 +
   1.126 +  // We must set the member variables before we signal the event
   1.127 +  mFileMapping = mapHandle;
   1.128 +  mView = view.Take();
   1.129 +  mParentEvent = initStruct->mParentEvent;
   1.130 +  mParentGuard = initStruct->mParentGuard;
   1.131 +  mChildEvent = initStruct->mChildEvent;
   1.132 +  mChildGuard = initStruct->mChildGuard;
   1.133 +  SetObserver(aObserver);
   1.134 +  mTimeout = aTimeout;
   1.135 +
   1.136 +  rv = Send();
   1.137 +  if (NS_FAILED(rv)) {
   1.138 +    initCompleteStruct->mSucceeded = false;
   1.139 +    mFileMapping = nullptr;
   1.140 +    view.Set(mView);
   1.141 +    mView = nullptr;
   1.142 +    mParentEvent = nullptr;
   1.143 +    mParentGuard = nullptr;
   1.144 +    mChildEvent = nullptr;
   1.145 +    mChildGuard = nullptr;
   1.146 +    ::UnregisterWaitEx(mRegWait, INVALID_HANDLE_VALUE);
   1.147 +    mRegWait = nullptr;
   1.148 +    return rv;
   1.149 +  }
   1.150 +
   1.151 +  OnConnect();
   1.152 +  return NS_OK;
   1.153 +}
   1.154 +
   1.155 +nsresult
   1.156 +MiniShmChild::Send()
   1.157 +{
   1.158 +  if (!mParentEvent) {
   1.159 +    return NS_ERROR_NOT_INITIALIZED;
   1.160 +  }
   1.161 +  if (!::SetEvent(mParentEvent)) {
   1.162 +    return NS_ERROR_FAILURE;
   1.163 +  }
   1.164 +  return NS_OK;
   1.165 +}
   1.166 +
   1.167 +void
   1.168 +MiniShmChild::OnEvent()
   1.169 +{
   1.170 +  MiniShmBase::OnEvent();
   1.171 +  ::SetEvent(mChildGuard);
   1.172 +}
   1.173 +
   1.174 +} // namespace plugins
   1.175 +} // namespace mozilla
   1.176 +

mercurial