summaryrefslogtreecommitdiffstats
path: root/dom/ipc/BrowserBridgeChild.cpp
blob: 3a2f7d8f84c5289941a56f82e2e0414312a8378d (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
/* -*- 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/. */

#ifdef ACCESSIBILITY
#  include "mozilla/a11y/DocAccessible.h"
#  include "mozilla/a11y/DocManager.h"
#  include "mozilla/a11y/OuterDocAccessible.h"
#endif
#include "mozilla/dom/BrowserBridgeChild.h"
#include "mozilla/dom/BrowserBridgeHost.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/MozFrameLoaderOwnerBinding.h"
#include "mozilla/PresShell.h"
#include "nsFocusManager.h"
#include "nsFrameLoader.h"
#include "nsFrameLoaderOwner.h"
#include "nsObjectLoadingContent.h"
#include "nsQueryObject.h"
#include "nsSubDocumentFrame.h"
#include "nsView.h"

using namespace mozilla::ipc;

mozilla::LazyLogModule gBrowserChildFocusLog("BrowserChildFocus");

#define LOGBROWSERCHILDFOCUS(args) \
  MOZ_LOG(gBrowserChildFocusLog, mozilla::LogLevel::Debug, args)

namespace mozilla::dom {

BrowserBridgeChild::BrowserBridgeChild(BrowsingContext* aBrowsingContext,
                                       TabId aId, const LayersId& aLayersId)
    : mId{aId}, mLayersId{aLayersId}, mBrowsingContext(aBrowsingContext) {}

BrowserBridgeChild::~BrowserBridgeChild() {}

already_AddRefed<BrowserBridgeHost> BrowserBridgeChild::FinishInit(
    nsFrameLoader* aFrameLoader) {
  MOZ_DIAGNOSTIC_ASSERT(!mFrameLoader);
  mFrameLoader = aFrameLoader;

  RefPtr<Element> owner = mFrameLoader->GetOwnerContent();
  Document* doc = owner->OwnerDoc();
  doc->OOPChildLoadStarted(this);

#if defined(ACCESSIBILITY)
  if (a11y::DocAccessible* docAcc = a11y::GetExistingDocAccessible(doc)) {
    if (a11y::LocalAccessible* ownerAcc = docAcc->GetAccessible(owner)) {
      if (a11y::OuterDocAccessible* outerAcc = ownerAcc->AsOuterDoc()) {
        outerAcc->SendEmbedderAccessible(this);
      }
    }
  }
#endif  // defined(ACCESSIBILITY)

  return MakeAndAddRef<BrowserBridgeHost>(this);
}

nsILoadContext* BrowserBridgeChild::GetLoadContext() {
  return mBrowsingContext;
}

void BrowserBridgeChild::NavigateByKey(bool aForward,
                                       bool aForDocumentNavigation) {
  Unused << SendNavigateByKey(aForward, aForDocumentNavigation);
}

void BrowserBridgeChild::Activate(uint64_t aActionId) {
  LOGBROWSERCHILDFOCUS(
      ("BrowserBridgeChild::Activate actionid: %" PRIu64, aActionId));
  Unused << SendActivate(aActionId);
}

void BrowserBridgeChild::Deactivate(bool aWindowLowering, uint64_t aActionId) {
  Unused << SendDeactivate(aWindowLowering, aActionId);
}

/*static*/
BrowserBridgeChild* BrowserBridgeChild::GetFrom(nsFrameLoader* aFrameLoader) {
  if (!aFrameLoader) {
    return nullptr;
  }
  return aFrameLoader->GetBrowserBridgeChild();
}

/*static*/
BrowserBridgeChild* BrowserBridgeChild::GetFrom(nsIContent* aContent) {
  RefPtr<nsFrameLoaderOwner> loaderOwner = do_QueryObject(aContent);
  if (!loaderOwner) {
    return nullptr;
  }
  RefPtr<nsFrameLoader> frameLoader = loaderOwner->GetFrameLoader();
  return GetFrom(frameLoader);
}

mozilla::ipc::IPCResult BrowserBridgeChild::RecvRequestFocus(
    const bool& aCanRaise, const CallerType aCallerType) {
  // Adapted from BrowserParent
  RefPtr<Element> owner = mFrameLoader->GetOwnerContent();
  if (!owner) {
    return IPC_OK();
  }
  nsContentUtils::RequestFrameFocus(*owner, aCanRaise, aCallerType);
  return IPC_OK();
}

mozilla::ipc::IPCResult BrowserBridgeChild::RecvMoveFocus(
    const bool& aForward, const bool& aForDocumentNavigation) {
  // Adapted from BrowserParent
  RefPtr<nsFocusManager> fm = nsFocusManager::GetFocusManager();
  if (!fm) {
    return IPC_OK();
  }

  RefPtr<Element> owner = mFrameLoader->GetOwnerContent();
  if (!owner) {
    return IPC_OK();
  }

  RefPtr<Element> dummy;

  uint32_t type =
      aForward
          ? (aForDocumentNavigation
                 ? static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_FORWARDDOC)
                 : static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_FORWARD))
          : (aForDocumentNavigation
                 ? static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_BACKWARDDOC)
                 : static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_BACKWARD));
  fm->MoveFocus(nullptr, owner, type, nsIFocusManager::FLAG_BYKEY,
                getter_AddRefs(dummy));
  return IPC_OK();
}

mozilla::ipc::IPCResult BrowserBridgeChild::RecvMaybeFireEmbedderLoadEvents(
    EmbedderElementEventType aFireEventAtEmbeddingElement) {
  RefPtr<Element> owner = mFrameLoader->GetOwnerContent();
  if (!owner) {
    return IPC_OK();
  }

  if (aFireEventAtEmbeddingElement == EmbedderElementEventType::LoadEvent) {
    nsEventStatus status = nsEventStatus_eIgnore;
    WidgetEvent event(/* aIsTrusted = */ true, eLoad);
    event.mFlags.mBubbles = false;
    event.mFlags.mCancelable = false;
    EventDispatcher::Dispatch(owner, nullptr, &event, nullptr, &status);
  } else if (aFireEventAtEmbeddingElement ==
             EmbedderElementEventType::ErrorEvent) {
    mFrameLoader->FireErrorEvent();
  }

  UnblockOwnerDocsLoadEvent();

  return IPC_OK();
}

mozilla::ipc::IPCResult BrowserBridgeChild::RecvScrollRectIntoView(
    const nsRect& aRect, const ScrollAxis& aVertical,
    const ScrollAxis& aHorizontal, const ScrollFlags& aScrollFlags,
    const int32_t& aAppUnitsPerDevPixel) {
  RefPtr<Element> owner = mFrameLoader->GetOwnerContent();
  if (!owner) {
    return IPC_OK();
  }

  nsIFrame* frame = owner->GetPrimaryFrame();
  if (!frame) {
    return IPC_OK();
  }

  nsSubDocumentFrame* subdocumentFrame = do_QueryFrame(frame);
  if (!subdocumentFrame) {
    return IPC_OK();
  }

  nsPoint extraOffset = subdocumentFrame->GetExtraOffset();

  int32_t parentAPD = frame->PresContext()->AppUnitsPerDevPixel();
  nsRect rect =
      aRect.ScaleToOtherAppUnitsRoundOut(aAppUnitsPerDevPixel, parentAPD);
  rect += extraOffset;
  RefPtr<PresShell> presShell = frame->PresShell();
  presShell->ScrollFrameIntoView(frame, Some(rect), aVertical, aHorizontal,
                                 aScrollFlags);
  return IPC_OK();
}

mozilla::ipc::IPCResult BrowserBridgeChild::RecvSubFrameCrashed() {
  if (RefPtr<nsFrameLoaderOwner> frameLoaderOwner =
          do_QueryObject(mFrameLoader->GetOwnerContent())) {
    frameLoaderOwner->SubframeCrashed();
  }
  return IPC_OK();
}

void BrowserBridgeChild::ActorDestroy(ActorDestroyReason aWhy) {
  if (mFrameLoader) {
    mFrameLoader->DestroyComplete();
  }

  if (!mBrowsingContext) {
    // This BBC was never valid, skip teardown.
    return;
  }

  // Ensure we unblock our document's 'load' event (in case the OOP-iframe has
  // been removed before it finished loading, or its subprocess crashed):
  UnblockOwnerDocsLoadEvent();
}

void BrowserBridgeChild::UnblockOwnerDocsLoadEvent() {
  if (!mHadInitialLoad) {
    mHadInitialLoad = true;

    if (Document* doc = mBrowsingContext->GetParent()->GetExtantDocument()) {
      doc->OOPChildLoadDone(this);
    }
  }
}

mozilla::ipc::IPCResult BrowserBridgeChild::RecvIntrinsicSizeOrRatioChanged(
    const Maybe<IntrinsicSize>& aIntrinsicSize,
    const Maybe<AspectRatio>& aIntrinsicRatio) {
  if (RefPtr<Element> owner = mFrameLoader->GetOwnerContent()) {
    if (nsCOMPtr<nsIObjectLoadingContent> olc = do_QueryInterface(owner)) {
      static_cast<nsObjectLoadingContent*>(olc.get())
          ->SubdocumentIntrinsicSizeOrRatioChanged(aIntrinsicSize,
                                                   aIntrinsicRatio);
    }
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult BrowserBridgeChild::RecvImageLoadComplete(
    const nsresult& aResult) {
  if (RefPtr<Element> owner = mFrameLoader->GetOwnerContent()) {
    if (nsCOMPtr<nsIObjectLoadingContent> olc = do_QueryInterface(owner)) {
      static_cast<nsObjectLoadingContent*>(olc.get())
          ->SubdocumentImageLoadComplete(aResult);
    }
  }
  return IPC_OK();
}

}  // namespace mozilla::dom