summaryrefslogtreecommitdiffstats
path: root/dom/base/InProcessBrowserChildMessageManager.h
blob: 4064a6d65b14ac093e0af81c01d8c6dd098f7b70 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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 nsInProcessBrowserChildGlobal_h
#define nsInProcessBrowserChildGlobal_h

#include "mozilla/Attributes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/ContentFrameMessageManager.h"
#include "mozilla/dom/MessageManagerCallback.h"
#include "nsCOMPtr.h"
#include "nsIScriptContext.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsDocShell.h"
#include "nsCOMArray.h"
#include "nsWeakReference.h"

class nsFrameMessageManager;

namespace mozilla {
class EventChainPreVisitor;

namespace dom {

/**
 * This class implements a ContentFrameMessageManager for use by frame loaders
 * in the parent process. It is bound to a DocShell rather than a BrowserChild,
 * and does not use any IPC infrastructure for its message passing.
 */

class InProcessBrowserChildMessageManager final
    : public ContentFrameMessageManager,
      public nsMessageManagerScriptExecutor,
      public nsIInProcessContentFrameMessageManager,
      public nsSupportsWeakReference,
      public mozilla::dom::ipc::MessageManagerCallback {
  using StructuredCloneData = mozilla::dom::ipc::StructuredCloneData;

 private:
  InProcessBrowserChildMessageManager(nsDocShell* aShell, nsIContent* aOwner,
                                      nsFrameMessageManager* aChrome);

 public:
  static already_AddRefed<InProcessBrowserChildMessageManager> Create(
      nsDocShell* aShell, nsIContent* aOwner, nsFrameMessageManager* aChrome);

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
      InProcessBrowserChildMessageManager, DOMEventTargetHelper)

  void MarkForCC();

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

  Nullable<WindowProxyHolder> GetContent(ErrorResult& aError) override;
  virtual already_AddRefed<nsIDocShell> GetDocShell(
      ErrorResult& aError) override {
    return do_AddRef(mDocShell);
  }
  virtual already_AddRefed<nsIEventTarget> GetTabEventTarget() override;

  NS_FORWARD_SAFE_NSIMESSAGESENDER(mMessageManager)

  NS_DECL_NSIINPROCESSCONTENTFRAMEMESSAGEMANAGER

  void CacheFrameLoader(nsFrameLoader* aFrameLoader);

  /**
   * MessageManagerCallback methods that we override.
   */
  virtual bool DoSendBlockingMessage(
      const nsAString& aMessage, StructuredCloneData& aData,
      nsTArray<StructuredCloneData>* aRetVal) override;
  virtual nsresult DoSendAsyncMessage(const nsAString& aMessage,
                                      StructuredCloneData& aData) override;

  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;

  void LoadFrameScript(const nsAString& aURL, bool aRunInGlobalScope);
  void FireUnloadEvent();
  void DisconnectEventListeners();
  void Disconnect();
  void SendMessageToParent(const nsString& aMessage, bool aSync,
                           const nsString& aJSON,
                           nsTArray<nsString>* aJSONRetVal);
  nsFrameMessageManager* GetInnerManager() {
    return static_cast<nsFrameMessageManager*>(mMessageManager.get());
  }

  void SetOwner(nsIContent* aOwner) { mOwner = aOwner; }
  nsFrameMessageManager* GetChromeMessageManager() {
    return mChromeMessageManager;
  }
  void SetChromeMessageManager(nsFrameMessageManager* aParent) {
    mChromeMessageManager = aParent;
  }

  already_AddRefed<nsFrameLoader> GetFrameLoader();

 protected:
  virtual ~InProcessBrowserChildMessageManager();

  RefPtr<nsDocShell> mDocShell;
  bool mLoadingScript;

  bool mPreventEventsEscaping;

  // We keep a strong reference to the frameloader after we've started
  // teardown. This allows us to dispatch message manager messages during this
  // time.
  RefPtr<nsFrameLoader> mFrameLoader;

 public:
  nsIContent* mOwner;
  nsFrameMessageManager* mChromeMessageManager;
};

}  // namespace dom
}  // namespace mozilla

#endif