1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_plugins_MiniShmParent_h
8 #define mozilla_plugins_MiniShmParent_h
9 
10 #include "MiniShmBase.h"
11 
12 #include <string>
13 
14 namespace mozilla {
15 namespace plugins {
16 
17 /**
18  * This class provides a lightweight shared memory interface for a parent
19  * process in Win32.
20  * This code assumes that there is a parent-child relationship between
21  * processes, as it creates inheritable handles.
22  * Note that this class is *not* an IPDL actor.
23  *
24  * @see MiniShmChild
25  */
26 class MiniShmParent : public MiniShmBase {
27  public:
28   MiniShmParent();
29   virtual ~MiniShmParent();
30 
31   static const unsigned int kDefaultMiniShmSectionSize;
32 
33   /**
34    * Initialize shared memory on the parent side.
35    *
36    * @param aObserver A MiniShmObserver object to receive event notifications.
37    * @param aTimeout Timeout in milliseconds.
38    * @param aSectionSize Desired size of the shared memory section. This is
39    *                     expected to be a multiple of 0x1000 (4KiB).
40    * @return nsresult error code
41    */
42   nsresult Init(MiniShmObserver* aObserver, const DWORD aTimeout,
43                 const unsigned int aSectionSize = kDefaultMiniShmSectionSize);
44 
45   /**
46    * Destroys the shared memory section. Useful to explicitly release
47    * resources if it is known that they won't be needed again.
48    */
49   void CleanUp();
50 
51   /**
52    * Provides a cookie string that should be passed to MiniShmChild
53    * during its initialization.
54    *
55    * @param aCookie A std::wstring variable to receive the cookie.
56    * @return nsresult error code
57    */
58   nsresult GetCookie(std::wstring& aCookie);
59 
60   virtual nsresult Send() override;
61 
62   bool IsConnected() const;
63 
64  protected:
65   void OnEvent() override;
66 
67  private:
68   void FinalizeConnection();
69 
70   unsigned int mSectionSize;
71   HANDLE mParentEvent;
72   HANDLE mParentGuard;
73   HANDLE mChildEvent;
74   HANDLE mChildGuard;
75   HANDLE mRegWait;
76   HANDLE mFileMapping;
77   LPVOID mView;
78   bool mIsConnected;
79   DWORD mTimeout;
80 
81   DISALLOW_COPY_AND_ASSIGN(MiniShmParent);
82 };
83 
84 }  // namespace plugins
85 }  // namespace mozilla
86 
87 #endif  // mozilla_plugins_MiniShmParent_h
88