summaryrefslogtreecommitdiffstats
path: root/dom/base/InProcessBrowserChildMessageManager.cpp
blob: 830c57591bc27fe9df6148438040e5a968be4853 (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
/* -*- 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/. */

#include "InProcessBrowserChildMessageManager.h"
#include "nsContentUtils.h"
#include "nsDocShell.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsFrameLoader.h"
#include "nsFrameLoaderOwner.h"
#include "nsQueryObject.h"
#include "xpcpublic.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/dom/ChromeMessageSender.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/MessageManagerBinding.h"
#include "mozilla/dom/SameProcessMessageQueue.h"
#include "mozilla/dom/ScriptLoader.h"
#include "mozilla/dom/WindowProxyHolder.h"
#include "mozilla/dom/JSActorService.h"
#include "mozilla/HoldDropJSObjects.h"

using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::dom::ipc;

/* static */
already_AddRefed<InProcessBrowserChildMessageManager>
InProcessBrowserChildMessageManager::Create(nsDocShell* aShell,
                                            nsIContent* aOwner,
                                            nsFrameMessageManager* aChrome) {
  RefPtr<InProcessBrowserChildMessageManager> mm =
      new InProcessBrowserChildMessageManager(aShell, aOwner, aChrome);

  NS_ENSURE_TRUE(mm->Init(), nullptr);

  if (XRE_IsParentProcess()) {
    RefPtr<JSActorService> wasvc = JSActorService::GetSingleton();
    wasvc->RegisterChromeEventTarget(mm);
  }

  return mm.forget();
}

bool InProcessBrowserChildMessageManager::DoSendBlockingMessage(
    const nsAString& aMessage, StructuredCloneData& aData,
    nsTArray<StructuredCloneData>* aRetVal) {
  SameProcessMessageQueue* queue = SameProcessMessageQueue::Get();
  queue->Flush();

  if (mChromeMessageManager) {
    RefPtr<nsFrameMessageManager> mm = mChromeMessageManager;
    RefPtr<nsFrameLoader> fl = GetFrameLoader();
    mm->ReceiveMessage(mOwner, fl, aMessage, true, &aData, aRetVal,
                       IgnoreErrors());
  }
  return true;
}

class nsAsyncMessageToParent : public nsSameProcessAsyncMessageBase,
                               public SameProcessMessageQueue::Runnable {
 public:
  explicit nsAsyncMessageToParent(
      InProcessBrowserChildMessageManager* aBrowserChild)
      : mBrowserChild(aBrowserChild) {}

  virtual nsresult HandleMessage() override {
    RefPtr<nsFrameLoader> fl = mBrowserChild->GetFrameLoader();
    ReceiveMessage(mBrowserChild->mOwner, fl,
                   mBrowserChild->mChromeMessageManager);
    return NS_OK;
  }
  RefPtr<InProcessBrowserChildMessageManager> mBrowserChild;
};

nsresult InProcessBrowserChildMessageManager::DoSendAsyncMessage(
    const nsAString& aMessage, StructuredCloneData& aData) {
  SameProcessMessageQueue* queue = SameProcessMessageQueue::Get();
  RefPtr<nsAsyncMessageToParent> ev = new nsAsyncMessageToParent(this);

  nsresult rv = ev->Init(aMessage, aData);
  if (NS_FAILED(rv)) {
    return rv;
  }

  queue->Push(ev);
  return NS_OK;
}

InProcessBrowserChildMessageManager::InProcessBrowserChildMessageManager(
    nsDocShell* aShell, nsIContent* aOwner, nsFrameMessageManager* aChrome)
    : ContentFrameMessageManager(new nsFrameMessageManager(this)),
      mDocShell(aShell),
      mLoadingScript(false),
      mPreventEventsEscaping(false),
      mOwner(aOwner),
      mChromeMessageManager(aChrome) {
  mozilla::HoldJSObjects(this);
}

InProcessBrowserChildMessageManager::~InProcessBrowserChildMessageManager() {
  if (XRE_IsParentProcess()) {
    JSActorService::UnregisterChromeEventTarget(this);
  }

  mozilla::DropJSObjects(this);
}

// This method isn't automatically forwarded safely because it's notxpcom, so
// the IDL binding doesn't know what value to return.
void InProcessBrowserChildMessageManager::MarkForCC() {
  MarkScopesForCC();
  MessageManagerGlobal::MarkForCC();
}

NS_IMPL_CYCLE_COLLECTION_CLASS(InProcessBrowserChildMessageManager)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
    InProcessBrowserChildMessageManager, DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageManager)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocShell)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(
    InProcessBrowserChildMessageManager, DOMEventTargetHelper)
  tmp->nsMessageManagerScriptExecutor::Trace(aCallbacks, aClosure);
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
    InProcessBrowserChildMessageManager, DOMEventTargetHelper)
  if (XRE_IsParentProcess()) {
    JSActorService::UnregisterChromeEventTarget(tmp);
  }

  NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessageManager)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocShell)
  tmp->nsMessageManagerScriptExecutor::Unlink();
  NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InProcessBrowserChildMessageManager)
  NS_INTERFACE_MAP_ENTRY(nsIMessageSender)
  NS_INTERFACE_MAP_ENTRY(nsIInProcessContentFrameMessageManager)
  NS_INTERFACE_MAP_ENTRY_CONCRETE(ContentFrameMessageManager)
  NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_ADDREF_INHERITED(InProcessBrowserChildMessageManager,
                         DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(InProcessBrowserChildMessageManager,
                          DOMEventTargetHelper)

JSObject* InProcessBrowserChildMessageManager::WrapObject(
    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return ContentFrameMessageManager_Binding::Wrap(aCx, this, aGivenProto);
}

void InProcessBrowserChildMessageManager::CacheFrameLoader(
    nsFrameLoader* aFrameLoader) {
  mFrameLoader = aFrameLoader;
}

Nullable<WindowProxyHolder> InProcessBrowserChildMessageManager::GetContent(
    ErrorResult& aError) {
  if (!mDocShell) {
    return nullptr;
  }
  return WindowProxyHolder(mDocShell->GetBrowsingContext());
}

already_AddRefed<nsIEventTarget>
InProcessBrowserChildMessageManager::GetTabEventTarget() {
  nsCOMPtr<nsIEventTarget> target = GetMainThreadSerialEventTarget();
  return target.forget();
}

void InProcessBrowserChildMessageManager::FireUnloadEvent() {
  // We're called from Document::MaybeInitializeFinalizeFrameLoaders, so it
  // should be safe to run script.
  MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());

  // Don't let the unload event propagate to chrome event handlers.
  mPreventEventsEscaping = true;
  DOMEventTargetHelper::DispatchTrustedEvent(u"unload"_ns);

  // Allow events fired during docshell destruction (pagehide, unload) to
  // propagate to the <browser> element since chrome code depends on this.
  mPreventEventsEscaping = false;
}

void InProcessBrowserChildMessageManager::DisconnectEventListeners() {
  if (mDocShell) {
    if (nsCOMPtr<nsPIDOMWindowOuter> win = mDocShell->GetWindow()) {
      win->SetChromeEventHandler(win->GetChromeEventHandler());
    }
  }
  if (mListenerManager) {
    mListenerManager->Disconnect();
  }

  mDocShell = nullptr;
}

void InProcessBrowserChildMessageManager::Disconnect() {
  mChromeMessageManager = nullptr;
  mOwner = nullptr;
  if (mMessageManager) {
    static_cast<nsFrameMessageManager*>(mMessageManager.get())->Disconnect();
    mMessageManager = nullptr;
  }
}

NS_IMETHODIMP_(nsIContent*)
InProcessBrowserChildMessageManager::GetOwnerContent() { return mOwner; }

void InProcessBrowserChildMessageManager::GetEventTargetParent(
    EventChainPreVisitor& aVisitor) {
  aVisitor.mForceContentDispatch = true;
  aVisitor.mCanHandle = true;

  if (mPreventEventsEscaping) {
    aVisitor.SetParentTarget(nullptr, false);
    return;
  }

  aVisitor.SetParentTarget(mOwner, false);
}

class nsAsyncScriptLoad : public Runnable {
 public:
  nsAsyncScriptLoad(InProcessBrowserChildMessageManager* aBrowserChild,
                    const nsAString& aURL, bool aRunInGlobalScope)
      : mozilla::Runnable("nsAsyncScriptLoad"),
        mBrowserChild(aBrowserChild),
        mURL(aURL),
        mRunInGlobalScope(aRunInGlobalScope) {}

  NS_IMETHOD Run() override {
    mBrowserChild->LoadFrameScript(mURL, mRunInGlobalScope);
    return NS_OK;
  }
  RefPtr<InProcessBrowserChildMessageManager> mBrowserChild;
  nsString mURL;
  bool mRunInGlobalScope;
};

void InProcessBrowserChildMessageManager::LoadFrameScript(
    const nsAString& aURL, bool aRunInGlobalScope) {
  if (!nsContentUtils::IsSafeToRunScript()) {
    nsContentUtils::AddScriptRunner(
        new nsAsyncScriptLoad(this, aURL, aRunInGlobalScope));
    return;
  }
  bool tmp = mLoadingScript;
  mLoadingScript = true;
  JS::Rooted<JSObject*> mm(mozilla::dom::RootingCx(), GetOrCreateWrapper());
  LoadScriptInternal(mm, aURL, !aRunInGlobalScope);
  mLoadingScript = tmp;
}

already_AddRefed<nsFrameLoader>
InProcessBrowserChildMessageManager::GetFrameLoader() {
  RefPtr<nsFrameLoaderOwner> owner = do_QueryObject(mOwner);
  RefPtr<nsFrameLoader> fl = owner ? owner->GetFrameLoader() : nullptr;
  if (!fl) {
    fl = mFrameLoader;
  }
  return fl.forget();
}