TabChild.cpp 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "base/basictypes.h"
  6. #include "TabChild.h"
  7. #include "gfxPrefs.h"
  8. #ifdef ACCESSIBILITY
  9. #include "mozilla/a11y/DocAccessibleChild.h"
  10. #endif
  11. #include "Layers.h"
  12. #include "ContentChild.h"
  13. #include "TabParent.h"
  14. #include "mozilla/Preferences.h"
  15. #include "mozilla/BrowserElementParent.h"
  16. #include "mozilla/ClearOnShutdown.h"
  17. #include "mozilla/EventListenerManager.h"
  18. #include "mozilla/dom/indexedDB/PIndexedDBPermissionRequestChild.h"
  19. #include "mozilla/plugins/PluginWidgetChild.h"
  20. #include "mozilla/IMEStateManager.h"
  21. #include "mozilla/ipc/URIUtils.h"
  22. #include "mozilla/layers/APZChild.h"
  23. #include "mozilla/layers/APZCCallbackHelper.h"
  24. #include "mozilla/layers/APZCTreeManager.h"
  25. #include "mozilla/layers/APZEventState.h"
  26. #include "mozilla/layers/ContentProcessController.h"
  27. #include "mozilla/layers/CompositorBridgeChild.h"
  28. #include "mozilla/layers/DoubleTapToZoom.h"
  29. #include "mozilla/layers/ImageBridgeChild.h"
  30. #include "mozilla/layers/InputAPZContext.h"
  31. #include "mozilla/layers/ShadowLayers.h"
  32. #include "mozilla/layout/RenderFrameChild.h"
  33. #include "mozilla/layout/RenderFrameParent.h"
  34. #include "mozilla/LookAndFeel.h"
  35. #include "mozilla/MouseEvents.h"
  36. #include "mozilla/Move.h"
  37. #include "mozilla/ProcessHangMonitor.h"
  38. #include "mozilla/ScopeExit.h"
  39. #include "mozilla/Services.h"
  40. #include "mozilla/StaticPtr.h"
  41. #include "mozilla/TextEvents.h"
  42. #include "mozilla/TouchEvents.h"
  43. #include "mozilla/Unused.h"
  44. #include "mozIApplication.h"
  45. #include "nsContentUtils.h"
  46. #include "nsCSSFrameConstructor.h"
  47. #include "nsDocShell.h"
  48. #include "nsEmbedCID.h"
  49. #include "nsGlobalWindow.h"
  50. #include <algorithm>
  51. #include "nsFilePickerProxy.h"
  52. #include "mozilla/dom/Element.h"
  53. #include "nsGlobalWindow.h"
  54. #include "nsIBaseWindow.h"
  55. #include "nsIBrowserDOMWindow.h"
  56. #include "nsIDocumentInlines.h"
  57. #include "nsIDocShellTreeOwner.h"
  58. #include "nsIDOMChromeWindow.h"
  59. #include "nsIDOMDocument.h"
  60. #include "nsIDOMEvent.h"
  61. #include "nsIDOMWindow.h"
  62. #include "nsIDOMWindowUtils.h"
  63. #include "nsFocusManager.h"
  64. #include "EventStateManager.h"
  65. #include "nsIDocShell.h"
  66. #include "nsIFrame.h"
  67. #include "nsIURI.h"
  68. #include "nsIURIFixup.h"
  69. #include "nsCDefaultURIFixup.h"
  70. #include "nsISupportsPrimitives.h"
  71. #include "nsIWebBrowser.h"
  72. #include "nsIWebBrowserFocus.h"
  73. #include "nsIWebBrowserSetup.h"
  74. #include "nsIWebProgress.h"
  75. #include "nsIXULRuntime.h"
  76. #include "nsPIDOMWindow.h"
  77. #include "nsPIWindowRoot.h"
  78. #include "nsLayoutUtils.h"
  79. #include "nsPrintfCString.h"
  80. #include "nsThreadUtils.h"
  81. #include "nsViewManager.h"
  82. #include "nsWeakReference.h"
  83. #include "nsWindowWatcher.h"
  84. #include "PermissionMessageUtils.h"
  85. #include "PuppetWidget.h"
  86. #include "StructuredCloneData.h"
  87. #include "nsViewportInfo.h"
  88. #include "nsILoadContext.h"
  89. #include "ipc/nsGUIEventIPC.h"
  90. #include "mozilla/gfx/Matrix.h"
  91. #include "UnitTransforms.h"
  92. #include "ClientLayerManager.h"
  93. #include "LayersLogging.h"
  94. #include "nsDOMClassInfoID.h"
  95. #include "nsColorPickerProxy.h"
  96. #include "nsContentPermissionHelper.h"
  97. #include "nsPresShell.h"
  98. #include "nsIAppsService.h"
  99. #include "nsNetUtil.h"
  100. #include "nsIPermissionManager.h"
  101. #include "nsIURILoader.h"
  102. #include "nsIScriptError.h"
  103. #include "mozilla/EventForwards.h"
  104. #include "nsDeviceContext.h"
  105. #include "nsSandboxFlags.h"
  106. #include "FrameLayerBuilder.h"
  107. #include "nsICommandParams.h"
  108. #include "nsISHistory.h"
  109. #include "nsQueryObject.h"
  110. #include "GroupedSHistory.h"
  111. #include "nsIHttpChannel.h"
  112. #include "mozilla/dom/DocGroup.h"
  113. #ifdef NS_PRINTING
  114. #include "nsIPrintSession.h"
  115. #include "nsIPrintSettings.h"
  116. #include "nsIPrintSettingsService.h"
  117. #include "nsIWebBrowserPrint.h"
  118. #endif
  119. #define BROWSER_ELEMENT_CHILD_SCRIPT \
  120. NS_LITERAL_STRING("chrome://global/content/BrowserElementChild.js")
  121. #define TABC_LOG(...)
  122. // #define TABC_LOG(...) printf_stderr("TABC: " __VA_ARGS__)
  123. using namespace mozilla;
  124. using namespace mozilla::dom;
  125. using namespace mozilla::dom::ipc;
  126. using namespace mozilla::dom::workers;
  127. using namespace mozilla::ipc;
  128. using namespace mozilla::layers;
  129. using namespace mozilla::layout;
  130. using namespace mozilla::docshell;
  131. using namespace mozilla::widget;
  132. using namespace mozilla::jsipc;
  133. using mozilla::layers::GeckoContentController;
  134. NS_IMPL_ISUPPORTS(ContentListener, nsIDOMEventListener)
  135. NS_IMPL_ISUPPORTS(TabChildSHistoryListener,
  136. nsISHistoryListener,
  137. nsIPartialSHistoryListener,
  138. nsISupportsWeakReference)
  139. static const CSSSize kDefaultViewportSize(980, 480);
  140. static const char BEFORE_FIRST_PAINT[] = "before-first-paint";
  141. typedef nsDataHashtable<nsUint64HashKey, TabChild*> TabChildMap;
  142. static TabChildMap* sTabChildren;
  143. TabChildBase::TabChildBase()
  144. : mTabChildGlobal(nullptr)
  145. {
  146. mozilla::HoldJSObjects(this);
  147. }
  148. TabChildBase::~TabChildBase()
  149. {
  150. mAnonymousGlobalScopes.Clear();
  151. mozilla::DropJSObjects(this);
  152. }
  153. NS_IMPL_CYCLE_COLLECTION_CLASS(TabChildBase)
  154. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(TabChildBase)
  155. NS_IMPL_CYCLE_COLLECTION_UNLINK(mTabChildGlobal)
  156. NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
  157. NS_IMPL_CYCLE_COLLECTION_UNLINK(mAnonymousGlobalScopes)
  158. NS_IMPL_CYCLE_COLLECTION_UNLINK(mWebBrowserChrome)
  159. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  160. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(TabChildBase)
  161. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTabChildGlobal)
  162. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
  163. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWebBrowserChrome)
  164. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  165. NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(TabChildBase)
  166. tmp->nsMessageManagerScriptExecutor::Trace(aCallbacks, aClosure);
  167. NS_IMPL_CYCLE_COLLECTION_TRACE_END
  168. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TabChildBase)
  169. NS_INTERFACE_MAP_ENTRY(nsISupports)
  170. NS_INTERFACE_MAP_END
  171. NS_IMPL_CYCLE_COLLECTING_ADDREF(TabChildBase)
  172. NS_IMPL_CYCLE_COLLECTING_RELEASE(TabChildBase)
  173. already_AddRefed<nsIDocument>
  174. TabChildBase::GetDocument() const
  175. {
  176. nsCOMPtr<nsIDOMDocument> domDoc;
  177. WebNavigation()->GetDocument(getter_AddRefs(domDoc));
  178. nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDoc));
  179. return doc.forget();
  180. }
  181. already_AddRefed<nsIPresShell>
  182. TabChildBase::GetPresShell() const
  183. {
  184. nsCOMPtr<nsIPresShell> result;
  185. if (nsCOMPtr<nsIDocument> doc = GetDocument()) {
  186. result = doc->GetShell();
  187. }
  188. return result.forget();
  189. }
  190. void
  191. TabChildBase::DispatchMessageManagerMessage(const nsAString& aMessageName,
  192. const nsAString& aJSONData)
  193. {
  194. AutoSafeJSContext cx;
  195. JS::Rooted<JS::Value> json(cx, JS::NullValue());
  196. StructuredCloneData data;
  197. if (JS_ParseJSON(cx,
  198. static_cast<const char16_t*>(aJSONData.BeginReading()),
  199. aJSONData.Length(),
  200. &json)) {
  201. ErrorResult rv;
  202. data.Write(cx, json, rv);
  203. if (NS_WARN_IF(rv.Failed())) {
  204. rv.SuppressException();
  205. return;
  206. }
  207. }
  208. nsCOMPtr<nsIXPConnectJSObjectHolder> kungFuDeathGrip(GetGlobal());
  209. // Let the BrowserElementScrolling helper (if it exists) for this
  210. // content manipulate the frame state.
  211. RefPtr<nsFrameMessageManager> mm =
  212. static_cast<nsFrameMessageManager*>(mTabChildGlobal->mMessageManager.get());
  213. mm->ReceiveMessage(static_cast<EventTarget*>(mTabChildGlobal), nullptr,
  214. aMessageName, false, &data, nullptr, nullptr, nullptr);
  215. }
  216. bool
  217. TabChildBase::UpdateFrameHandler(const FrameMetrics& aFrameMetrics)
  218. {
  219. MOZ_ASSERT(aFrameMetrics.GetScrollId() != FrameMetrics::NULL_SCROLL_ID);
  220. if (aFrameMetrics.IsRootContent()) {
  221. if (nsCOMPtr<nsIPresShell> shell = GetPresShell()) {
  222. // Guard against stale updates (updates meant for a pres shell which
  223. // has since been torn down and destroyed).
  224. if (aFrameMetrics.GetPresShellId() == shell->GetPresShellId()) {
  225. ProcessUpdateFrame(aFrameMetrics);
  226. return true;
  227. }
  228. }
  229. } else {
  230. // aFrameMetrics.mIsRoot is false, so we are trying to update a subframe.
  231. // This requires special handling.
  232. FrameMetrics newSubFrameMetrics(aFrameMetrics);
  233. APZCCallbackHelper::UpdateSubFrame(newSubFrameMetrics);
  234. return true;
  235. }
  236. return true;
  237. }
  238. void
  239. TabChildBase::ProcessUpdateFrame(const FrameMetrics& aFrameMetrics)
  240. {
  241. if (!mGlobal || !mTabChildGlobal) {
  242. return;
  243. }
  244. FrameMetrics newMetrics = aFrameMetrics;
  245. APZCCallbackHelper::UpdateRootFrame(newMetrics);
  246. }
  247. NS_IMETHODIMP
  248. ContentListener::HandleEvent(nsIDOMEvent* aEvent)
  249. {
  250. RemoteDOMEvent remoteEvent;
  251. remoteEvent.mEvent = do_QueryInterface(aEvent);
  252. NS_ENSURE_STATE(remoteEvent.mEvent);
  253. mTabChild->SendEvent(remoteEvent);
  254. return NS_OK;
  255. }
  256. class TabChild::DelayedDeleteRunnable final
  257. : public Runnable
  258. {
  259. RefPtr<TabChild> mTabChild;
  260. public:
  261. explicit DelayedDeleteRunnable(TabChild* aTabChild)
  262. : mTabChild(aTabChild)
  263. {
  264. MOZ_ASSERT(NS_IsMainThread());
  265. MOZ_ASSERT(aTabChild);
  266. }
  267. private:
  268. ~DelayedDeleteRunnable()
  269. {
  270. MOZ_ASSERT(NS_IsMainThread());
  271. MOZ_ASSERT(!mTabChild);
  272. }
  273. NS_IMETHOD
  274. Run() override
  275. {
  276. MOZ_ASSERT(NS_IsMainThread());
  277. MOZ_ASSERT(mTabChild);
  278. // Check in case ActorDestroy was called after RecvDestroy message.
  279. if (mTabChild->IPCOpen()) {
  280. Unused << PBrowserChild::Send__delete__(mTabChild);
  281. }
  282. mTabChild = nullptr;
  283. return NS_OK;
  284. }
  285. };
  286. namespace {
  287. StaticRefPtr<TabChild> sPreallocatedTab;
  288. std::map<TabId, RefPtr<TabChild>>&
  289. NestedTabChildMap()
  290. {
  291. MOZ_ASSERT(NS_IsMainThread());
  292. static std::map<TabId, RefPtr<TabChild>> sNestedTabChildMap;
  293. return sNestedTabChildMap;
  294. }
  295. } // namespace
  296. already_AddRefed<TabChild>
  297. TabChild::FindTabChild(const TabId& aTabId)
  298. {
  299. auto iter = NestedTabChildMap().find(aTabId);
  300. if (iter == NestedTabChildMap().end()) {
  301. return nullptr;
  302. }
  303. RefPtr<TabChild> tabChild = iter->second;
  304. return tabChild.forget();
  305. }
  306. static void
  307. PreloadSlowThingsPostFork(void* aUnused)
  308. {
  309. nsCOMPtr<nsIObserverService> observerService =
  310. mozilla::services::GetObserverService();
  311. observerService->NotifyObservers(nullptr, "preload-postfork", nullptr);
  312. MOZ_ASSERT(sPreallocatedTab);
  313. // Initialize initial reflow of the PresShell has to happen after fork
  314. // because about:blank content viewer is created in the above observer
  315. // notification.
  316. nsCOMPtr<nsIDocShell> docShell =
  317. do_GetInterface(sPreallocatedTab->WebNavigation());
  318. if (nsIPresShell* presShell = docShell->GetPresShell()) {
  319. // Initialize and do an initial reflow of the about:blank
  320. // PresShell to let it preload some things for us.
  321. presShell->Initialize(0, 0);
  322. nsIDocument* doc = presShell->GetDocument();
  323. doc->FlushPendingNotifications(Flush_Layout);
  324. // ... but after it's done, make sure it doesn't do any more
  325. // work.
  326. presShell->MakeZombie();
  327. }
  328. }
  329. static bool sPreloaded = false;
  330. /*static*/ void
  331. TabChild::PreloadSlowThings()
  332. {
  333. if (sPreloaded) {
  334. // If we are alredy initialized in Nuwa, don't redo preloading.
  335. return;
  336. }
  337. sPreloaded = true;
  338. // Pass nullptr to aManager since at this point the TabChild is
  339. // not connected to any manager. Any attempt to use the TabChild
  340. // in IPC will crash.
  341. RefPtr<TabChild> tab(new TabChild(nullptr,
  342. TabId(0),
  343. TabContext(), /* chromeFlags */ 0));
  344. if (!NS_SUCCEEDED(tab->Init()) ||
  345. !tab->InitTabChildGlobal(DONT_LOAD_SCRIPTS)) {
  346. return;
  347. }
  348. // Just load and compile these scripts, but don't run them.
  349. tab->TryCacheLoadAndCompileScript(BROWSER_ELEMENT_CHILD_SCRIPT, true);
  350. // Load, compile, and run these scripts.
  351. tab->RecvLoadRemoteScript(
  352. NS_LITERAL_STRING("chrome://global/content/preload.js"),
  353. true);
  354. sPreallocatedTab = tab;
  355. ClearOnShutdown(&sPreallocatedTab);
  356. PreloadSlowThingsPostFork(nullptr);
  357. }
  358. /*static*/ already_AddRefed<TabChild>
  359. TabChild::Create(nsIContentChild* aManager,
  360. const TabId& aTabId,
  361. const TabContext &aContext,
  362. uint32_t aChromeFlags)
  363. {
  364. if (sPreallocatedTab &&
  365. sPreallocatedTab->mChromeFlags == aChromeFlags &&
  366. aContext.IsMozBrowserOrApp()) {
  367. RefPtr<TabChild> child = sPreallocatedTab.get();
  368. sPreallocatedTab = nullptr;
  369. MOZ_ASSERT(!child->mTriedBrowserInit);
  370. child->mManager = aManager;
  371. child->SetTabId(aTabId);
  372. child->SetTabContext(aContext);
  373. child->NotifyTabContextUpdated(true);
  374. return child.forget();
  375. }
  376. RefPtr<TabChild> iframe = new TabChild(aManager, aTabId,
  377. aContext, aChromeFlags);
  378. return NS_SUCCEEDED(iframe->Init()) ? iframe.forget() : nullptr;
  379. }
  380. TabChild::TabChild(nsIContentChild* aManager,
  381. const TabId& aTabId,
  382. const TabContext& aContext,
  383. uint32_t aChromeFlags)
  384. : TabContext(aContext)
  385. , mRemoteFrame(nullptr)
  386. , mManager(aManager)
  387. , mChromeFlags(aChromeFlags)
  388. , mActiveSuppressDisplayport(0)
  389. , mLayersId(0)
  390. , mDidFakeShow(false)
  391. , mNotified(false)
  392. , mTriedBrowserInit(false)
  393. , mOrientation(eScreenOrientation_PortraitPrimary)
  394. , mIgnoreKeyPressEvent(false)
  395. , mHasValidInnerSize(false)
  396. , mDestroyed(false)
  397. , mUniqueId(aTabId)
  398. , mDPI(0)
  399. , mRounding(0)
  400. , mDefaultScale(0)
  401. , mIsTransparent(false)
  402. , mIPCOpen(false)
  403. , mParentIsActive(false)
  404. , mDidSetRealShowInfo(false)
  405. , mDidLoadURLInit(false)
  406. , mIsFreshProcess(false)
  407. , mLayerObserverEpoch(0)
  408. #if defined(XP_WIN) && defined(ACCESSIBILITY)
  409. , mNativeWindowHandle(0)
  410. #endif
  411. {
  412. // In the general case having the TabParent tell us if APZ is enabled or not
  413. // doesn't really work because the TabParent itself may not have a reference
  414. // to the owning widget during initialization. Instead we assume that this
  415. // TabChild corresponds to a widget type that would have APZ enabled, and just
  416. // check the other conditions necessary for enabling APZ.
  417. mAsyncPanZoomEnabled = gfxPlatform::AsyncPanZoomEnabled();
  418. nsWeakPtr weakPtrThis(do_GetWeakReference(static_cast<nsITabChild*>(this))); // for capture by the lambda
  419. mSetAllowedTouchBehaviorCallback = [weakPtrThis](uint64_t aInputBlockId,
  420. const nsTArray<TouchBehaviorFlags>& aFlags)
  421. {
  422. if (nsCOMPtr<nsITabChild> tabChild = do_QueryReferent(weakPtrThis)) {
  423. static_cast<TabChild*>(tabChild.get())->SetAllowedTouchBehavior(aInputBlockId, aFlags);
  424. }
  425. };
  426. // preloaded TabChild should not be added to child map
  427. if (mUniqueId) {
  428. MOZ_ASSERT(NestedTabChildMap().find(mUniqueId) == NestedTabChildMap().end());
  429. NestedTabChildMap()[mUniqueId] = this;
  430. }
  431. nsCOMPtr<nsIObserverService> observerService =
  432. mozilla::services::GetObserverService();
  433. if (observerService) {
  434. const nsAttrValue::EnumTable* table =
  435. AudioChannelService::GetAudioChannelTable();
  436. nsAutoCString topic;
  437. for (uint32_t i = 0; table[i].tag; ++i) {
  438. topic.Assign("audiochannel-activity-");
  439. topic.Append(table[i].tag);
  440. observerService->AddObserver(this, topic.get(), false);
  441. }
  442. }
  443. for (uint32_t idx = 0; idx < NUMBER_OF_AUDIO_CHANNELS; idx++) {
  444. mAudioChannelsActive.AppendElement(false);
  445. }
  446. }
  447. NS_IMETHODIMP
  448. TabChild::Observe(nsISupports *aSubject,
  449. const char *aTopic,
  450. const char16_t *aData)
  451. {
  452. if (!strcmp(aTopic, BEFORE_FIRST_PAINT)) {
  453. if (AsyncPanZoomEnabled()) {
  454. nsCOMPtr<nsIDocument> subject(do_QueryInterface(aSubject));
  455. nsCOMPtr<nsIDocument> doc(GetDocument());
  456. if (SameCOMIdentity(subject, doc)) {
  457. nsCOMPtr<nsIPresShell> shell(doc->GetShell());
  458. if (shell) {
  459. shell->SetIsFirstPaint(true);
  460. }
  461. APZCCallbackHelper::InitializeRootDisplayport(shell);
  462. }
  463. }
  464. }
  465. const nsAttrValue::EnumTable* table =
  466. AudioChannelService::GetAudioChannelTable();
  467. nsAutoCString topic;
  468. int16_t audioChannel = -1;
  469. for (uint32_t i = 0; table[i].tag; ++i) {
  470. topic.Assign("audiochannel-activity-");
  471. topic.Append(table[i].tag);
  472. if (topic.Equals(aTopic)) {
  473. audioChannel = table[i].value;
  474. break;
  475. }
  476. }
  477. if (audioChannel != -1 && mIPCOpen) {
  478. // If the subject is not a wrapper, it is sent by the TabParent and we
  479. // should ignore it.
  480. nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
  481. if (!wrapper) {
  482. return NS_OK;
  483. }
  484. // We must have a window in order to compare the windowID contained into the
  485. // wrapper.
  486. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  487. if (!window) {
  488. return NS_OK;
  489. }
  490. uint64_t windowID = 0;
  491. nsresult rv = wrapper->GetData(&windowID);
  492. if (NS_WARN_IF(NS_FAILED(rv))) {
  493. return rv;
  494. }
  495. // In theory a tabChild should contain just 1 top window, but let's double
  496. // check it comparing the windowID.
  497. if (window->WindowID() != windowID) {
  498. MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
  499. ("TabChild, Observe, different windowID, owner ID = %lld, "
  500. "ID from wrapper = %lld", window->WindowID(), windowID));
  501. return NS_OK;
  502. }
  503. nsAutoString activeStr(aData);
  504. bool active = activeStr.EqualsLiteral("active");
  505. if (active != mAudioChannelsActive[audioChannel]) {
  506. mAudioChannelsActive[audioChannel] = active;
  507. Unused << SendAudioChannelActivityNotification(audioChannel, active);
  508. }
  509. }
  510. return NS_OK;
  511. }
  512. void
  513. TabChild::ContentReceivedInputBlock(const ScrollableLayerGuid& aGuid,
  514. uint64_t aInputBlockId,
  515. bool aPreventDefault) const
  516. {
  517. if (mApzcTreeManager) {
  518. mApzcTreeManager->ContentReceivedInputBlock(aInputBlockId, aPreventDefault);
  519. }
  520. }
  521. void
  522. TabChild::SetTargetAPZC(uint64_t aInputBlockId,
  523. const nsTArray<ScrollableLayerGuid>& aTargets) const
  524. {
  525. if (mApzcTreeManager) {
  526. mApzcTreeManager->SetTargetAPZC(aInputBlockId, aTargets);
  527. }
  528. }
  529. void
  530. TabChild::SetAllowedTouchBehavior(uint64_t aInputBlockId,
  531. const nsTArray<TouchBehaviorFlags>& aTargets) const
  532. {
  533. if (mApzcTreeManager) {
  534. mApzcTreeManager->SetAllowedTouchBehavior(aInputBlockId, aTargets);
  535. }
  536. }
  537. bool
  538. TabChild::DoUpdateZoomConstraints(const uint32_t& aPresShellId,
  539. const ViewID& aViewId,
  540. const Maybe<ZoomConstraints>& aConstraints)
  541. {
  542. if (sPreallocatedTab == this) {
  543. // If we're the preallocated tab, bail out because doing IPC will crash.
  544. // Once we get used for something we'll get another zoom constraints update
  545. // and all will be well.
  546. return true;
  547. }
  548. if (!mApzcTreeManager) {
  549. return false;
  550. }
  551. ScrollableLayerGuid guid = ScrollableLayerGuid(mLayersId, aPresShellId, aViewId);
  552. mApzcTreeManager->UpdateZoomConstraints(guid, aConstraints);
  553. return true;
  554. }
  555. nsresult
  556. TabChild::Init()
  557. {
  558. nsCOMPtr<nsIWebBrowser> webBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID);
  559. if (!webBrowser) {
  560. NS_ERROR("Couldn't create a nsWebBrowser?");
  561. return NS_ERROR_FAILURE;
  562. }
  563. webBrowser->SetContainerWindow(this);
  564. webBrowser->SetOriginAttributes(OriginAttributesRef());
  565. mWebNav = do_QueryInterface(webBrowser);
  566. NS_ASSERTION(mWebNav, "nsWebBrowser doesn't implement nsIWebNavigation?");
  567. nsCOMPtr<nsIDocShellTreeItem> docShellItem(do_QueryInterface(WebNavigation()));
  568. docShellItem->SetItemType(nsIDocShellTreeItem::typeContentWrapper);
  569. nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(WebNavigation());
  570. if (!baseWindow) {
  571. NS_ERROR("mWebNav doesn't QI to nsIBaseWindow");
  572. return NS_ERROR_FAILURE;
  573. }
  574. nsCOMPtr<nsIWidget> widget = nsIWidget::CreatePuppetWidget(this);
  575. mPuppetWidget = static_cast<PuppetWidget*>(widget.get());
  576. if (!mPuppetWidget) {
  577. NS_ERROR("couldn't create fake widget");
  578. return NS_ERROR_FAILURE;
  579. }
  580. mPuppetWidget->InfallibleCreate(
  581. nullptr, 0, // no parents
  582. LayoutDeviceIntRect(0, 0, 0, 0),
  583. nullptr // HandleWidgetEvent
  584. );
  585. baseWindow->InitWindow(0, mPuppetWidget, 0, 0, 0, 0);
  586. baseWindow->Create();
  587. // Set the tab context attributes then pass to docShell
  588. NotifyTabContextUpdated(false);
  589. // IPC uses a WebBrowser object for which DNS prefetching is turned off
  590. // by default. But here we really want it, so enable it explicitly
  591. nsCOMPtr<nsIWebBrowserSetup> webBrowserSetup =
  592. do_QueryInterface(baseWindow);
  593. if (webBrowserSetup) {
  594. webBrowserSetup->SetProperty(nsIWebBrowserSetup::SETUP_ALLOW_DNS_PREFETCH,
  595. true);
  596. } else {
  597. NS_WARNING("baseWindow doesn't QI to nsIWebBrowserSetup, skipping "
  598. "DNS prefetching enable step.");
  599. }
  600. nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
  601. MOZ_ASSERT(docShell);
  602. docShell->SetAffectPrivateSessionLifetime(
  603. mChromeFlags & nsIWebBrowserChrome::CHROME_PRIVATE_LIFETIME);
  604. nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(WebNavigation());
  605. MOZ_ASSERT(loadContext);
  606. loadContext->SetPrivateBrowsing(OriginAttributesRef().mPrivateBrowsingId > 0);
  607. loadContext->SetRemoteTabs(
  608. mChromeFlags & nsIWebBrowserChrome::CHROME_REMOTE_WINDOW);
  609. // Few lines before, baseWindow->Create() will end up creating a new
  610. // window root in nsGlobalWindow::SetDocShell.
  611. // Then this chrome event handler, will be inherited to inner windows.
  612. // We want to also set it to the docshell so that inner windows
  613. // and any code that has access to the docshell
  614. // can all listen to the same chrome event handler.
  615. // XXX: ideally, we would set a chrome event handler earlier,
  616. // and all windows, even the root one, will use the docshell one.
  617. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  618. NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
  619. nsCOMPtr<EventTarget> chromeHandler =
  620. do_QueryInterface(window->GetChromeEventHandler());
  621. docShell->SetChromeEventHandler(chromeHandler);
  622. if (window->GetCurrentInnerWindow()) {
  623. window->SetKeyboardIndicators(ShowAccelerators(), ShowFocusRings());
  624. } else {
  625. // Skip ShouldShowFocusRing check if no inner window is available
  626. window->SetInitialKeyboardIndicators(ShowAccelerators(), ShowFocusRings());
  627. }
  628. // Set prerender flag if necessary.
  629. if (mIsPrerendered) {
  630. docShell->SetIsPrerendered();
  631. }
  632. nsContentUtils::SetScrollbarsVisibility(window->GetDocShell(),
  633. !!(mChromeFlags & nsIWebBrowserChrome::CHROME_SCROLLBARS));
  634. nsWeakPtr weakPtrThis = do_GetWeakReference(static_cast<nsITabChild*>(this)); // for capture by the lambda
  635. ContentReceivedInputBlockCallback callback(
  636. [weakPtrThis](const ScrollableLayerGuid& aGuid,
  637. uint64_t aInputBlockId,
  638. bool aPreventDefault)
  639. {
  640. if (nsCOMPtr<nsITabChild> tabChild = do_QueryReferent(weakPtrThis)) {
  641. static_cast<TabChild*>(tabChild.get())->ContentReceivedInputBlock(aGuid, aInputBlockId, aPreventDefault);
  642. }
  643. });
  644. mAPZEventState = new APZEventState(mPuppetWidget, Move(callback));
  645. mIPCOpen = true;
  646. if (GroupedSHistory::GroupedHistoryEnabled()) {
  647. // Set session history listener.
  648. nsCOMPtr<nsISHistory> shistory;
  649. mWebNav->GetSessionHistory(getter_AddRefs(shistory));
  650. if (!shistory) {
  651. return NS_ERROR_FAILURE;
  652. }
  653. mHistoryListener = new TabChildSHistoryListener(this);
  654. nsCOMPtr<nsISHistoryListener> listener(do_QueryObject(mHistoryListener));
  655. shistory->AddSHistoryListener(listener);
  656. nsCOMPtr<nsIPartialSHistoryListener> partialListener(do_QueryObject(mHistoryListener));
  657. shistory->SetPartialSHistoryListener(partialListener);
  658. }
  659. return NS_OK;
  660. }
  661. void
  662. TabChild::NotifyTabContextUpdated(bool aIsPreallocated)
  663. {
  664. nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
  665. MOZ_ASSERT(docShell);
  666. if (!docShell) {
  667. return;
  668. }
  669. UpdateFrameType();
  670. if (aIsPreallocated) {
  671. nsDocShell::Cast(docShell)->SetOriginAttributes(OriginAttributesRef());
  672. }
  673. }
  674. void
  675. TabChild::UpdateFrameType()
  676. {
  677. nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
  678. MOZ_ASSERT(docShell);
  679. // TODO: Bug 1252794 - remove frameType from nsIDocShell.idl
  680. docShell->SetFrameType(IsMozBrowserElement() ? nsIDocShell::FRAME_TYPE_BROWSER :
  681. HasOwnApp() ? nsIDocShell::FRAME_TYPE_APP :
  682. nsIDocShell::FRAME_TYPE_REGULAR);
  683. }
  684. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TabChild)
  685. NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome)
  686. NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome2)
  687. NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow)
  688. NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChromeFocus)
  689. NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
  690. NS_INTERFACE_MAP_ENTRY(nsIWindowProvider)
  691. NS_INTERFACE_MAP_ENTRY(nsITabChild)
  692. NS_INTERFACE_MAP_ENTRY(nsIObserver)
  693. NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
  694. NS_INTERFACE_MAP_ENTRY(nsITooltipListener)
  695. NS_INTERFACE_MAP_END_INHERITING(TabChildBase)
  696. NS_IMPL_ADDREF_INHERITED(TabChild, TabChildBase);
  697. NS_IMPL_RELEASE_INHERITED(TabChild, TabChildBase);
  698. NS_IMETHODIMP
  699. TabChild::SetStatus(uint32_t aStatusType, const char16_t* aStatus)
  700. {
  701. return SetStatusWithContext(aStatusType,
  702. aStatus ? static_cast<const nsString &>(nsDependentString(aStatus))
  703. : EmptyString(),
  704. nullptr);
  705. }
  706. NS_IMETHODIMP
  707. TabChild::GetWebBrowser(nsIWebBrowser** aWebBrowser)
  708. {
  709. NS_WARNING("TabChild::GetWebBrowser not supported in TabChild");
  710. return NS_ERROR_NOT_IMPLEMENTED;
  711. }
  712. NS_IMETHODIMP
  713. TabChild::SetWebBrowser(nsIWebBrowser* aWebBrowser)
  714. {
  715. NS_WARNING("TabChild::SetWebBrowser not supported in TabChild");
  716. return NS_ERROR_NOT_IMPLEMENTED;
  717. }
  718. NS_IMETHODIMP
  719. TabChild::GetChromeFlags(uint32_t* aChromeFlags)
  720. {
  721. *aChromeFlags = mChromeFlags;
  722. return NS_OK;
  723. }
  724. NS_IMETHODIMP
  725. TabChild::SetChromeFlags(uint32_t aChromeFlags)
  726. {
  727. NS_WARNING("trying to SetChromeFlags from content process?");
  728. return NS_ERROR_NOT_IMPLEMENTED;
  729. }
  730. NS_IMETHODIMP
  731. TabChild::DestroyBrowserWindow()
  732. {
  733. NS_WARNING("TabChild::DestroyBrowserWindow not supported in TabChild");
  734. return NS_ERROR_NOT_IMPLEMENTED;
  735. }
  736. NS_IMETHODIMP
  737. TabChild::RemoteSizeShellTo(int32_t aWidth, int32_t aHeight,
  738. int32_t aShellItemWidth, int32_t aShellItemHeight)
  739. {
  740. nsCOMPtr<nsIDocShell> ourDocShell = do_GetInterface(WebNavigation());
  741. nsCOMPtr<nsIBaseWindow> docShellAsWin(do_QueryInterface(ourDocShell));
  742. int32_t width, height;
  743. docShellAsWin->GetSize(&width, &height);
  744. uint32_t flags = 0;
  745. if (width == aWidth) {
  746. flags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CX;
  747. }
  748. if (height == aHeight) {
  749. flags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CY;
  750. }
  751. bool sent = SendSizeShellTo(flags, aWidth, aHeight, aShellItemWidth, aShellItemHeight);
  752. return sent ? NS_OK : NS_ERROR_FAILURE;
  753. }
  754. NS_IMETHODIMP
  755. TabChild::RemoteDropLinks(uint32_t aLinksCount, nsIDroppedLinkItem** aLinks)
  756. {
  757. nsTArray<nsString> linksArray;
  758. nsresult rv = NS_OK;
  759. for (uint32_t i = 0; i < aLinksCount; i++) {
  760. nsString tmp;
  761. rv = aLinks[i]->GetUrl(tmp);
  762. if (NS_FAILED(rv)) {
  763. return rv;
  764. }
  765. linksArray.AppendElement(tmp);
  766. rv = aLinks[i]->GetName(tmp);
  767. if (NS_FAILED(rv)) {
  768. return rv;
  769. }
  770. linksArray.AppendElement(tmp);
  771. rv = aLinks[i]->GetType(tmp);
  772. if (NS_FAILED(rv)) {
  773. return rv;
  774. }
  775. linksArray.AppendElement(tmp);
  776. }
  777. bool sent = SendDropLinks(linksArray);
  778. return sent ? NS_OK : NS_ERROR_FAILURE;
  779. }
  780. NS_IMETHODIMP
  781. TabChild::SizeBrowserTo(int32_t aWidth, int32_t aHeight)
  782. {
  783. NS_WARNING("TabChild::SizeBrowserTo not supported in TabChild");
  784. return NS_ERROR_NOT_IMPLEMENTED;
  785. }
  786. NS_IMETHODIMP
  787. TabChild::ShowAsModal()
  788. {
  789. NS_WARNING("TabChild::ShowAsModal not supported in TabChild");
  790. return NS_ERROR_NOT_IMPLEMENTED;
  791. }
  792. NS_IMETHODIMP
  793. TabChild::IsWindowModal(bool* aRetVal)
  794. {
  795. *aRetVal = false;
  796. return NS_OK;
  797. }
  798. NS_IMETHODIMP
  799. TabChild::ExitModalEventLoop(nsresult aStatus)
  800. {
  801. NS_WARNING("TabChild::ExitModalEventLoop not supported in TabChild");
  802. return NS_ERROR_NOT_IMPLEMENTED;
  803. }
  804. NS_IMETHODIMP
  805. TabChild::SetStatusWithContext(uint32_t aStatusType,
  806. const nsAString& aStatusText,
  807. nsISupports* aStatusContext)
  808. {
  809. // We can only send the status after the ipc machinery is set up,
  810. // mRemoteFrame is a good indicator.
  811. if (mRemoteFrame)
  812. SendSetStatus(aStatusType, nsString(aStatusText));
  813. return NS_OK;
  814. }
  815. NS_IMETHODIMP
  816. TabChild::SetDimensions(uint32_t aFlags, int32_t aX, int32_t aY,
  817. int32_t aCx, int32_t aCy)
  818. {
  819. // The parent is in charge of the dimension changes. If JS code wants to
  820. // change the dimensions (moveTo, screenX, etc.) we send a message to the
  821. // parent about the new requested dimension, the parent does the resize/move
  822. // then send a message to the child to update itself. For APIs like screenX
  823. // this function is called with the current value for the non-changed values.
  824. // In a series of calls like window.screenX = 10; window.screenY = 10; for
  825. // the second call, since screenX is not yet updated we might accidentally
  826. // reset back screenX to it's old value. To avoid this if a parameter did not
  827. // change we want the parent to ignore its value.
  828. int32_t x, y, cx, cy;
  829. GetDimensions(aFlags, &x, &y, &cx, &cy);
  830. if (x == aX) {
  831. aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_X;
  832. }
  833. if (y == aY) {
  834. aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_Y;
  835. }
  836. if (cx == aCx) {
  837. aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CX;
  838. }
  839. if (cy == aCy) {
  840. aFlags |= nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CY;
  841. }
  842. Unused << SendSetDimensions(aFlags, aX, aY, aCx, aCy);
  843. return NS_OK;
  844. }
  845. NS_IMETHODIMP
  846. TabChild::GetDimensions(uint32_t aFlags, int32_t* aX,
  847. int32_t* aY, int32_t* aCx, int32_t* aCy)
  848. {
  849. ScreenIntRect rect = GetOuterRect();
  850. if (aX) {
  851. *aX = rect.x;
  852. }
  853. if (aY) {
  854. *aY = rect.y;
  855. }
  856. if (aCx) {
  857. *aCx = rect.width;
  858. }
  859. if (aCy) {
  860. *aCy = rect.height;
  861. }
  862. return NS_OK;
  863. }
  864. NS_IMETHODIMP
  865. TabChild::SetFocus()
  866. {
  867. return NS_ERROR_NOT_IMPLEMENTED;
  868. }
  869. NS_IMETHODIMP
  870. TabChild::GetVisibility(bool* aVisibility)
  871. {
  872. *aVisibility = true;
  873. return NS_OK;
  874. }
  875. NS_IMETHODIMP
  876. TabChild::SetVisibility(bool aVisibility)
  877. {
  878. // should the platform support this? Bug 666365
  879. return NS_OK;
  880. }
  881. NS_IMETHODIMP
  882. TabChild::GetTitle(char16_t** aTitle)
  883. {
  884. NS_WARNING("TabChild::GetTitle not supported in TabChild");
  885. return NS_ERROR_NOT_IMPLEMENTED;
  886. }
  887. NS_IMETHODIMP
  888. TabChild::SetTitle(const char16_t* aTitle)
  889. {
  890. // JavaScript sends the "DOMTitleChanged" event to the parent
  891. // via the message manager.
  892. return NS_OK;
  893. }
  894. NS_IMETHODIMP
  895. TabChild::GetSiteWindow(void** aSiteWindow)
  896. {
  897. NS_WARNING("TabChild::GetSiteWindow not supported in TabChild");
  898. return NS_ERROR_NOT_IMPLEMENTED;
  899. }
  900. NS_IMETHODIMP
  901. TabChild::Blur()
  902. {
  903. return NS_ERROR_NOT_IMPLEMENTED;
  904. }
  905. NS_IMETHODIMP
  906. TabChild::FocusNextElement(bool aForDocumentNavigation)
  907. {
  908. SendMoveFocus(true, aForDocumentNavigation);
  909. return NS_OK;
  910. }
  911. NS_IMETHODIMP
  912. TabChild::FocusPrevElement(bool aForDocumentNavigation)
  913. {
  914. SendMoveFocus(false, aForDocumentNavigation);
  915. return NS_OK;
  916. }
  917. NS_IMETHODIMP
  918. TabChild::GetInterface(const nsIID & aIID, void **aSink)
  919. {
  920. if (aIID.Equals(NS_GET_IID(nsIWebBrowserChrome3))) {
  921. NS_IF_ADDREF(((nsISupports *) (*aSink = mWebBrowserChrome)));
  922. return NS_OK;
  923. }
  924. // XXXbz should we restrict the set of interfaces we hand out here?
  925. // See bug 537429
  926. return QueryInterface(aIID, aSink);
  927. }
  928. NS_IMETHODIMP
  929. TabChild::ProvideWindow(mozIDOMWindowProxy* aParent,
  930. uint32_t aChromeFlags,
  931. bool aCalledFromJS,
  932. bool aPositionSpecified, bool aSizeSpecified,
  933. nsIURI* aURI, const nsAString& aName,
  934. const nsACString& aFeatures, bool aForceNoOpener,
  935. bool* aWindowIsNew, mozIDOMWindowProxy** aReturn)
  936. {
  937. *aReturn = nullptr;
  938. // If aParent is inside an <iframe mozbrowser> or <iframe mozapp> and this
  939. // isn't a request to open a modal-type window, we're going to create a new
  940. // <iframe mozbrowser/mozapp> and return its window here.
  941. nsCOMPtr<nsIDocShell> docshell = do_GetInterface(aParent);
  942. bool iframeMoz = (docshell && docshell->GetIsInMozBrowserOrApp() &&
  943. !(aChromeFlags & (nsIWebBrowserChrome::CHROME_MODAL |
  944. nsIWebBrowserChrome::CHROME_OPENAS_DIALOG |
  945. nsIWebBrowserChrome::CHROME_OPENAS_CHROME)));
  946. if (!iframeMoz) {
  947. int32_t openLocation =
  948. nsWindowWatcher::GetWindowOpenLocation(nsPIDOMWindowOuter::From(aParent),
  949. aChromeFlags, aCalledFromJS,
  950. aPositionSpecified, aSizeSpecified);
  951. // If it turns out we're opening in the current browser, just hand over the
  952. // current browser's docshell.
  953. if (openLocation == nsIBrowserDOMWindow::OPEN_CURRENTWINDOW) {
  954. nsCOMPtr<nsIWebBrowser> browser = do_GetInterface(WebNavigation());
  955. *aWindowIsNew = false;
  956. return browser->GetContentDOMWindow(aReturn);
  957. }
  958. }
  959. // Note that ProvideWindowCommon may return NS_ERROR_ABORT if the
  960. // open window call was canceled. It's important that we pass this error
  961. // code back to our caller.
  962. ContentChild* cc = ContentChild::GetSingleton();
  963. return cc->ProvideWindowCommon(this,
  964. aParent,
  965. iframeMoz,
  966. aChromeFlags,
  967. aCalledFromJS,
  968. aPositionSpecified,
  969. aSizeSpecified,
  970. aURI,
  971. aName,
  972. aFeatures,
  973. aForceNoOpener,
  974. aWindowIsNew,
  975. aReturn);
  976. }
  977. void
  978. TabChild::DestroyWindow()
  979. {
  980. nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(WebNavigation());
  981. if (baseWindow)
  982. baseWindow->Destroy();
  983. // NB: the order of mPuppetWidget->Destroy() and mRemoteFrame->Destroy()
  984. // is important: we want to kill off remote layers before their
  985. // frames
  986. if (mPuppetWidget) {
  987. mPuppetWidget->Destroy();
  988. }
  989. if (mRemoteFrame) {
  990. mRemoteFrame->Destroy();
  991. mRemoteFrame = nullptr;
  992. }
  993. if (mLayersId != 0) {
  994. MOZ_ASSERT(sTabChildren);
  995. sTabChildren->Remove(mLayersId);
  996. if (!sTabChildren->Count()) {
  997. delete sTabChildren;
  998. sTabChildren = nullptr;
  999. }
  1000. mLayersId = 0;
  1001. }
  1002. }
  1003. void
  1004. TabChild::ActorDestroy(ActorDestroyReason why)
  1005. {
  1006. mIPCOpen = false;
  1007. DestroyWindow();
  1008. if (mTabChildGlobal) {
  1009. // We should have a message manager if the global is alive, but it
  1010. // seems sometimes we don't. Assert in aurora/nightly, but don't
  1011. // crash in release builds.
  1012. MOZ_DIAGNOSTIC_ASSERT(mTabChildGlobal->mMessageManager);
  1013. if (mTabChildGlobal->mMessageManager) {
  1014. // The messageManager relays messages via the TabChild which
  1015. // no longer exists.
  1016. static_cast<nsFrameMessageManager*>
  1017. (mTabChildGlobal->mMessageManager.get())->Disconnect();
  1018. mTabChildGlobal->mMessageManager = nullptr;
  1019. }
  1020. }
  1021. CompositorBridgeChild* compositorChild = static_cast<CompositorBridgeChild*>(CompositorBridgeChild::Get());
  1022. compositorChild->CancelNotifyAfterRemotePaint(this);
  1023. if (GetTabId() != 0) {
  1024. NestedTabChildMap().erase(GetTabId());
  1025. }
  1026. }
  1027. TabChild::~TabChild()
  1028. {
  1029. DestroyWindow();
  1030. nsCOMPtr<nsIWebBrowser> webBrowser = do_QueryInterface(WebNavigation());
  1031. if (webBrowser) {
  1032. webBrowser->SetContainerWindow(nullptr);
  1033. }
  1034. if (mHistoryListener) {
  1035. mHistoryListener->ClearTabChild();
  1036. }
  1037. }
  1038. void
  1039. TabChild::SetProcessNameToAppName()
  1040. {
  1041. nsCOMPtr<mozIApplication> app = GetOwnApp();
  1042. if (!app) {
  1043. return;
  1044. }
  1045. nsAutoString appName;
  1046. nsresult rv = app->GetName(appName);
  1047. if (NS_FAILED(rv)) {
  1048. NS_WARNING("Failed to retrieve app name");
  1049. return;
  1050. }
  1051. ContentChild::GetSingleton()->SetProcessName(appName, true);
  1052. }
  1053. bool
  1054. TabChild::RecvLoadURL(const nsCString& aURI,
  1055. const ShowInfo& aInfo)
  1056. {
  1057. if (!mDidLoadURLInit) {
  1058. mDidLoadURLInit = true;
  1059. if (!InitTabChildGlobal()) {
  1060. return false;
  1061. }
  1062. ApplyShowInfo(aInfo);
  1063. SetProcessNameToAppName();
  1064. }
  1065. nsresult rv =
  1066. WebNavigation()->LoadURI(NS_ConvertUTF8toUTF16(aURI).get(),
  1067. nsIWebNavigation::LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP |
  1068. nsIWebNavigation::LOAD_FLAGS_DISALLOW_INHERIT_PRINCIPAL,
  1069. nullptr, nullptr, nullptr);
  1070. if (NS_FAILED(rv)) {
  1071. NS_WARNING("WebNavigation()->LoadURI failed. Eating exception, what else can I do?");
  1072. }
  1073. return true;
  1074. }
  1075. void
  1076. TabChild::DoFakeShow(const TextureFactoryIdentifier& aTextureFactoryIdentifier,
  1077. const uint64_t& aLayersId,
  1078. PRenderFrameChild* aRenderFrame, const ShowInfo& aShowInfo)
  1079. {
  1080. RecvShow(ScreenIntSize(0, 0), aShowInfo, aTextureFactoryIdentifier,
  1081. aLayersId, aRenderFrame, mParentIsActive, nsSizeMode_Normal);
  1082. mDidFakeShow = true;
  1083. }
  1084. void
  1085. TabChild::ApplyShowInfo(const ShowInfo& aInfo)
  1086. {
  1087. if (mDidSetRealShowInfo) {
  1088. return;
  1089. }
  1090. if (!aInfo.fakeShowInfo()) {
  1091. // Once we've got one ShowInfo from parent, no need to update the values
  1092. // anymore.
  1093. mDidSetRealShowInfo = true;
  1094. }
  1095. nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
  1096. if (docShell) {
  1097. nsCOMPtr<nsIDocShellTreeItem> item = do_GetInterface(docShell);
  1098. if (IsMozBrowserOrApp()) {
  1099. // B2G allows window.name to be set by changing the name attribute on the
  1100. // <iframe mozbrowser> element. window.open calls cause this attribute to
  1101. // be set to the correct value. A normal <xul:browser> element has no such
  1102. // attribute. The data we get here comes from reading the attribute, so we
  1103. // shouldn't trust it for <xul:browser> elements.
  1104. item->SetName(aInfo.name());
  1105. }
  1106. docShell->SetFullscreenAllowed(aInfo.fullscreenAllowed());
  1107. if (aInfo.isPrivate()) {
  1108. nsCOMPtr<nsILoadContext> context = do_GetInterface(docShell);
  1109. // No need to re-set private browsing mode.
  1110. if (!context->UsePrivateBrowsing()) {
  1111. if (docShell->GetHasLoadedNonBlankURI()) {
  1112. nsContentUtils::ReportToConsoleNonLocalized(
  1113. NS_LITERAL_STRING("We should not switch to Private Browsing after loading a document."),
  1114. nsIScriptError::warningFlag,
  1115. NS_LITERAL_CSTRING("mozprivatebrowsing"),
  1116. nullptr);
  1117. } else {
  1118. DocShellOriginAttributes attrs(nsDocShell::Cast(docShell)->GetOriginAttributes());
  1119. attrs.SyncAttributesWithPrivateBrowsing(true);
  1120. nsDocShell::Cast(docShell)->SetOriginAttributes(attrs);
  1121. }
  1122. }
  1123. }
  1124. }
  1125. mDPI = aInfo.dpi();
  1126. mRounding = aInfo.widgetRounding();
  1127. mDefaultScale = aInfo.defaultScale();
  1128. mIsTransparent = aInfo.isTransparent();
  1129. }
  1130. bool
  1131. TabChild::RecvShow(const ScreenIntSize& aSize,
  1132. const ShowInfo& aInfo,
  1133. const TextureFactoryIdentifier& aTextureFactoryIdentifier,
  1134. const uint64_t& aLayersId,
  1135. PRenderFrameChild* aRenderFrame,
  1136. const bool& aParentIsActive,
  1137. const nsSizeMode& aSizeMode)
  1138. {
  1139. MOZ_ASSERT((!mDidFakeShow && aRenderFrame) || (mDidFakeShow && !aRenderFrame));
  1140. mPuppetWidget->SetSizeMode(aSizeMode);
  1141. if (mDidFakeShow) {
  1142. ApplyShowInfo(aInfo);
  1143. RecvParentActivated(aParentIsActive);
  1144. return true;
  1145. }
  1146. nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(WebNavigation());
  1147. if (!baseWindow) {
  1148. NS_ERROR("WebNavigation() doesn't QI to nsIBaseWindow");
  1149. return false;
  1150. }
  1151. if (!InitRenderingState(aTextureFactoryIdentifier, aLayersId, aRenderFrame)) {
  1152. // We can fail to initialize our widget if the <browser
  1153. // remote> has already been destroyed, and we couldn't hook
  1154. // into the parent-process's layer system. That's not a fatal
  1155. // error.
  1156. return true;
  1157. }
  1158. baseWindow->SetVisibility(true);
  1159. bool res = InitTabChildGlobal();
  1160. ApplyShowInfo(aInfo);
  1161. RecvParentActivated(aParentIsActive);
  1162. return res;
  1163. }
  1164. bool
  1165. TabChild::RecvUpdateDimensions(const CSSRect& rect, const CSSSize& size,
  1166. const ScreenOrientationInternal& orientation,
  1167. const LayoutDeviceIntPoint& clientOffset,
  1168. const LayoutDeviceIntPoint& chromeDisp)
  1169. {
  1170. if (!mRemoteFrame) {
  1171. return true;
  1172. }
  1173. mUnscaledOuterRect = rect;
  1174. mClientOffset = clientOffset;
  1175. mChromeDisp = chromeDisp;
  1176. mOrientation = orientation;
  1177. SetUnscaledInnerSize(size);
  1178. if (!mHasValidInnerSize && size.width != 0 && size.height != 0) {
  1179. mHasValidInnerSize = true;
  1180. }
  1181. ScreenIntSize screenSize = GetInnerSize();
  1182. ScreenIntRect screenRect = GetOuterRect();
  1183. // Set the size on the document viewer before we update the widget and
  1184. // trigger a reflow. Otherwise the MobileViewportManager reads the stale
  1185. // size from the content viewer when it computes a new CSS viewport.
  1186. nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(WebNavigation());
  1187. baseWin->SetPositionAndSize(0, 0, screenSize.width, screenSize.height,
  1188. nsIBaseWindow::eRepaint);
  1189. mPuppetWidget->Resize(screenRect.x + clientOffset.x + chromeDisp.x,
  1190. screenRect.y + clientOffset.y + chromeDisp.y,
  1191. screenSize.width, screenSize.height, true);
  1192. return true;
  1193. }
  1194. bool
  1195. TabChild::RecvSizeModeChanged(const nsSizeMode& aSizeMode)
  1196. {
  1197. mPuppetWidget->SetSizeMode(aSizeMode);
  1198. if (!mPuppetWidget->IsVisible()) {
  1199. return true;
  1200. }
  1201. nsCOMPtr<nsIDocument> document(GetDocument());
  1202. nsCOMPtr<nsIPresShell> presShell = document->GetShell();
  1203. if (presShell) {
  1204. nsPresContext* presContext = presShell->GetPresContext();
  1205. if (presContext) {
  1206. presContext->SizeModeChanged(aSizeMode);
  1207. }
  1208. }
  1209. return true;
  1210. }
  1211. bool
  1212. TabChild::UpdateFrame(const FrameMetrics& aFrameMetrics)
  1213. {
  1214. return TabChildBase::UpdateFrameHandler(aFrameMetrics);
  1215. }
  1216. bool
  1217. TabChild::RecvSuppressDisplayport(const bool& aEnabled)
  1218. {
  1219. if (aEnabled) {
  1220. mActiveSuppressDisplayport++;
  1221. } else {
  1222. mActiveSuppressDisplayport--;
  1223. }
  1224. MOZ_ASSERT(mActiveSuppressDisplayport >= 0);
  1225. APZCCallbackHelper::SuppressDisplayport(aEnabled, GetPresShell());
  1226. return true;
  1227. }
  1228. void
  1229. TabChild::HandleDoubleTap(const CSSPoint& aPoint, const Modifiers& aModifiers,
  1230. const ScrollableLayerGuid& aGuid)
  1231. {
  1232. TABC_LOG("Handling double tap at %s with %p %p\n",
  1233. Stringify(aPoint).c_str(), mGlobal.get(), mTabChildGlobal.get());
  1234. if (!mGlobal || !mTabChildGlobal) {
  1235. return;
  1236. }
  1237. // Note: there is nothing to do with the modifiers here, as we are not
  1238. // synthesizing any sort of mouse event.
  1239. nsCOMPtr<nsIDocument> document = GetDocument();
  1240. CSSRect zoomToRect = CalculateRectToZoomTo(document, aPoint);
  1241. // The double-tap can be dispatched by any scroll frame (so |aGuid| could be
  1242. // the guid of any scroll frame), but the zoom-to-rect operation must be
  1243. // performed by the root content scroll frame, so query its identifiers
  1244. // for the SendZoomToRect() call rather than using the ones from |aGuid|.
  1245. uint32_t presShellId;
  1246. ViewID viewId;
  1247. if (APZCCallbackHelper::GetOrCreateScrollIdentifiers(
  1248. document->GetDocumentElement(), &presShellId, &viewId) && mApzcTreeManager) {
  1249. ScrollableLayerGuid guid(mLayersId, presShellId, viewId);
  1250. mApzcTreeManager->ZoomToRect(guid, zoomToRect, DEFAULT_BEHAVIOR);
  1251. }
  1252. }
  1253. bool
  1254. TabChild::RecvHandleTap(const GeckoContentController::TapType& aType,
  1255. const LayoutDevicePoint& aPoint,
  1256. const Modifiers& aModifiers,
  1257. const ScrollableLayerGuid& aGuid,
  1258. const uint64_t& aInputBlockId)
  1259. {
  1260. nsCOMPtr<nsIPresShell> presShell = GetPresShell();
  1261. if (!presShell) {
  1262. return true;
  1263. }
  1264. if (!presShell->GetPresContext()) {
  1265. return true;
  1266. }
  1267. CSSToLayoutDeviceScale scale(presShell->GetPresContext()->CSSToDevPixelScale());
  1268. CSSPoint point = APZCCallbackHelper::ApplyCallbackTransform(aPoint / scale, aGuid);
  1269. switch (aType) {
  1270. case GeckoContentController::TapType::eSingleTap:
  1271. if (mGlobal && mTabChildGlobal) {
  1272. mAPZEventState->ProcessSingleTap(point, scale, aModifiers, aGuid, 1);
  1273. }
  1274. break;
  1275. case GeckoContentController::TapType::eDoubleTap:
  1276. HandleDoubleTap(point, aModifiers, aGuid);
  1277. break;
  1278. case GeckoContentController::TapType::eSecondTap:
  1279. if (mGlobal && mTabChildGlobal) {
  1280. mAPZEventState->ProcessSingleTap(point, scale, aModifiers, aGuid, 2);
  1281. }
  1282. break;
  1283. case GeckoContentController::TapType::eLongTap:
  1284. if (mGlobal && mTabChildGlobal) {
  1285. mAPZEventState->ProcessLongTap(presShell, point, scale, aModifiers, aGuid,
  1286. aInputBlockId);
  1287. }
  1288. break;
  1289. case GeckoContentController::TapType::eLongTapUp:
  1290. if (mGlobal && mTabChildGlobal) {
  1291. mAPZEventState->ProcessLongTapUp(presShell, point, scale, aModifiers);
  1292. }
  1293. break;
  1294. case GeckoContentController::TapType::eSentinel:
  1295. // Should never happen, but we need to handle this case to make the compiler
  1296. // happy.
  1297. MOZ_ASSERT(false);
  1298. break;
  1299. }
  1300. return true;
  1301. }
  1302. bool
  1303. TabChild::NotifyAPZStateChange(const ViewID& aViewId,
  1304. const layers::GeckoContentController::APZStateChange& aChange,
  1305. const int& aArg)
  1306. {
  1307. mAPZEventState->ProcessAPZStateChange(aViewId, aChange, aArg);
  1308. if (aChange == layers::GeckoContentController::APZStateChange::eTransformEnd) {
  1309. // This is used by tests to determine when the APZ is done doing whatever
  1310. // it's doing. XXX generify this as needed when writing additional tests.
  1311. nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
  1312. observerService->NotifyObservers(nullptr, "APZ:TransformEnd", nullptr);
  1313. }
  1314. return true;
  1315. }
  1316. void
  1317. TabChild::StartScrollbarDrag(const layers::AsyncDragMetrics& aDragMetrics)
  1318. {
  1319. ScrollableLayerGuid guid(mLayersId, aDragMetrics.mPresShellId,
  1320. aDragMetrics.mViewId);
  1321. if (mApzcTreeManager) {
  1322. mApzcTreeManager->StartScrollbarDrag(guid, aDragMetrics);
  1323. }
  1324. }
  1325. void
  1326. TabChild::ZoomToRect(const uint32_t& aPresShellId,
  1327. const FrameMetrics::ViewID& aViewId,
  1328. const CSSRect& aRect,
  1329. const uint32_t& aFlags)
  1330. {
  1331. ScrollableLayerGuid guid(mLayersId, aPresShellId, aViewId);
  1332. if (mApzcTreeManager) {
  1333. mApzcTreeManager->ZoomToRect(guid, aRect, aFlags);
  1334. }
  1335. }
  1336. bool
  1337. TabChild::RecvActivate()
  1338. {
  1339. nsCOMPtr<nsIWebBrowserFocus> browser = do_QueryInterface(WebNavigation());
  1340. browser->Activate();
  1341. return true;
  1342. }
  1343. bool TabChild::RecvDeactivate()
  1344. {
  1345. nsCOMPtr<nsIWebBrowserFocus> browser = do_QueryInterface(WebNavigation());
  1346. browser->Deactivate();
  1347. return true;
  1348. }
  1349. bool TabChild::RecvParentActivated(const bool& aActivated)
  1350. {
  1351. mParentIsActive = aActivated;
  1352. nsFocusManager* fm = nsFocusManager::GetFocusManager();
  1353. NS_ENSURE_TRUE(fm, true);
  1354. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  1355. fm->ParentActivated(window, aActivated);
  1356. return true;
  1357. }
  1358. bool TabChild::RecvSetKeyboardIndicators(const UIStateChangeType& aShowAccelerators,
  1359. const UIStateChangeType& aShowFocusRings)
  1360. {
  1361. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  1362. NS_ENSURE_TRUE(window, true);
  1363. window->SetKeyboardIndicators(aShowAccelerators, aShowFocusRings);
  1364. return true;
  1365. }
  1366. bool
  1367. TabChild::RecvStopIMEStateManagement()
  1368. {
  1369. IMEStateManager::StopIMEStateManagement();
  1370. return true;
  1371. }
  1372. bool
  1373. TabChild::RecvMenuKeyboardListenerInstalled(const bool& aInstalled)
  1374. {
  1375. IMEStateManager::OnInstalledMenuKeyboardListener(aInstalled);
  1376. return true;
  1377. }
  1378. bool
  1379. TabChild::RecvNotifyAttachGroupedSessionHistory(const uint32_t& aOffset)
  1380. {
  1381. // nsISHistory uses int32_t
  1382. if (NS_WARN_IF(aOffset > INT32_MAX)) {
  1383. return false;
  1384. }
  1385. nsCOMPtr<nsISHistory> shistory;
  1386. mWebNav->GetSessionHistory(getter_AddRefs(shistory));
  1387. NS_ENSURE_TRUE(shistory, false);
  1388. return NS_SUCCEEDED(shistory->OnAttachGroupedSessionHistory(aOffset));
  1389. }
  1390. bool
  1391. TabChild::RecvNotifyPartialSessionHistoryActive(const uint32_t& aGlobalLength,
  1392. const uint32_t& aTargetLocalIndex)
  1393. {
  1394. // nsISHistory uses int32_t
  1395. if (NS_WARN_IF(aGlobalLength > INT32_MAX || aTargetLocalIndex > INT32_MAX)) {
  1396. return false;
  1397. }
  1398. nsCOMPtr<nsISHistory> shistory;
  1399. mWebNav->GetSessionHistory(getter_AddRefs(shistory));
  1400. NS_ENSURE_TRUE(shistory, false);
  1401. return NS_SUCCEEDED(shistory->OnPartialSessionHistoryActive(aGlobalLength,
  1402. aTargetLocalIndex));
  1403. }
  1404. bool
  1405. TabChild::RecvNotifyPartialSessionHistoryDeactive()
  1406. {
  1407. nsCOMPtr<nsISHistory> shistory;
  1408. mWebNav->GetSessionHistory(getter_AddRefs(shistory));
  1409. NS_ENSURE_TRUE(shistory, false);
  1410. return NS_SUCCEEDED(shistory->OnPartialSessionHistoryDeactive());
  1411. }
  1412. bool
  1413. TabChild::RecvMouseEvent(const nsString& aType,
  1414. const float& aX,
  1415. const float& aY,
  1416. const int32_t& aButton,
  1417. const int32_t& aClickCount,
  1418. const int32_t& aModifiers,
  1419. const bool& aIgnoreRootScrollFrame)
  1420. {
  1421. APZCCallbackHelper::DispatchMouseEvent(GetPresShell(), aType, CSSPoint(aX, aY),
  1422. aButton, aClickCount, aModifiers, aIgnoreRootScrollFrame, nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN);
  1423. return true;
  1424. }
  1425. bool
  1426. TabChild::RecvRealMouseMoveEvent(const WidgetMouseEvent& aEvent,
  1427. const ScrollableLayerGuid& aGuid,
  1428. const uint64_t& aInputBlockId)
  1429. {
  1430. return RecvRealMouseButtonEvent(aEvent, aGuid, aInputBlockId);
  1431. }
  1432. bool
  1433. TabChild::RecvSynthMouseMoveEvent(const WidgetMouseEvent& aEvent,
  1434. const ScrollableLayerGuid& aGuid,
  1435. const uint64_t& aInputBlockId)
  1436. {
  1437. return RecvRealMouseButtonEvent(aEvent, aGuid, aInputBlockId);
  1438. }
  1439. bool
  1440. TabChild::RecvRealMouseButtonEvent(const WidgetMouseEvent& aEvent,
  1441. const ScrollableLayerGuid& aGuid,
  1442. const uint64_t& aInputBlockId)
  1443. {
  1444. // Mouse events like eMouseEnterIntoWidget, that are created in the parent
  1445. // process EventStateManager code, have an input block id which they get from
  1446. // the InputAPZContext in the parent process stack. However, they did not
  1447. // actually go through the APZ code and so their mHandledByAPZ flag is false.
  1448. // Since thos events didn't go through APZ, we don't need to send notifications
  1449. // for them.
  1450. if (aInputBlockId && aEvent.mFlags.mHandledByAPZ) {
  1451. nsCOMPtr<nsIDocument> document(GetDocument());
  1452. APZCCallbackHelper::SendSetTargetAPZCNotification(
  1453. mPuppetWidget, document, aEvent, aGuid, aInputBlockId);
  1454. }
  1455. nsEventStatus unused;
  1456. InputAPZContext context(aGuid, aInputBlockId, unused);
  1457. WidgetMouseEvent localEvent(aEvent);
  1458. localEvent.mWidget = mPuppetWidget;
  1459. APZCCallbackHelper::ApplyCallbackTransform(localEvent, aGuid,
  1460. mPuppetWidget->GetDefaultScale());
  1461. APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1462. if (aInputBlockId && aEvent.mFlags.mHandledByAPZ) {
  1463. mAPZEventState->ProcessMouseEvent(aEvent, aGuid, aInputBlockId);
  1464. }
  1465. return true;
  1466. }
  1467. bool
  1468. TabChild::RecvMouseWheelEvent(const WidgetWheelEvent& aEvent,
  1469. const ScrollableLayerGuid& aGuid,
  1470. const uint64_t& aInputBlockId)
  1471. {
  1472. if (aInputBlockId && aEvent.mFlags.mHandledByAPZ) {
  1473. nsCOMPtr<nsIDocument> document(GetDocument());
  1474. APZCCallbackHelper::SendSetTargetAPZCNotification(
  1475. mPuppetWidget, document, aEvent, aGuid, aInputBlockId);
  1476. }
  1477. WidgetWheelEvent localEvent(aEvent);
  1478. localEvent.mWidget = mPuppetWidget;
  1479. APZCCallbackHelper::ApplyCallbackTransform(localEvent, aGuid,
  1480. mPuppetWidget->GetDefaultScale());
  1481. APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1482. if (localEvent.mCanTriggerSwipe) {
  1483. SendRespondStartSwipeEvent(aInputBlockId, localEvent.TriggersSwipe());
  1484. }
  1485. if (aInputBlockId && aEvent.mFlags.mHandledByAPZ) {
  1486. mAPZEventState->ProcessWheelEvent(localEvent, aGuid, aInputBlockId);
  1487. }
  1488. return true;
  1489. }
  1490. bool
  1491. TabChild::RecvRealTouchEvent(const WidgetTouchEvent& aEvent,
  1492. const ScrollableLayerGuid& aGuid,
  1493. const uint64_t& aInputBlockId,
  1494. const nsEventStatus& aApzResponse)
  1495. {
  1496. TABC_LOG("Receiving touch event of type %d\n", aEvent.mMessage);
  1497. WidgetTouchEvent localEvent(aEvent);
  1498. localEvent.mWidget = mPuppetWidget;
  1499. APZCCallbackHelper::ApplyCallbackTransform(localEvent, aGuid,
  1500. mPuppetWidget->GetDefaultScale());
  1501. if (localEvent.mMessage == eTouchStart && AsyncPanZoomEnabled()) {
  1502. nsCOMPtr<nsIDocument> document = GetDocument();
  1503. if (gfxPrefs::TouchActionEnabled()) {
  1504. APZCCallbackHelper::SendSetAllowedTouchBehaviorNotification(mPuppetWidget,
  1505. document, localEvent, aInputBlockId, mSetAllowedTouchBehaviorCallback);
  1506. }
  1507. APZCCallbackHelper::SendSetTargetAPZCNotification(mPuppetWidget, document,
  1508. localEvent, aGuid, aInputBlockId);
  1509. }
  1510. // Dispatch event to content (potentially a long-running operation)
  1511. nsEventStatus status = APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1512. if (!AsyncPanZoomEnabled()) {
  1513. // We shouldn't have any e10s platforms that have touch events enabled
  1514. // without APZ.
  1515. MOZ_ASSERT(false);
  1516. return true;
  1517. }
  1518. mAPZEventState->ProcessTouchEvent(localEvent, aGuid, aInputBlockId,
  1519. aApzResponse, status);
  1520. return true;
  1521. }
  1522. bool
  1523. TabChild::RecvRealTouchMoveEvent(const WidgetTouchEvent& aEvent,
  1524. const ScrollableLayerGuid& aGuid,
  1525. const uint64_t& aInputBlockId,
  1526. const nsEventStatus& aApzResponse)
  1527. {
  1528. return RecvRealTouchEvent(aEvent, aGuid, aInputBlockId, aApzResponse);
  1529. }
  1530. bool
  1531. TabChild::RecvRealDragEvent(const WidgetDragEvent& aEvent,
  1532. const uint32_t& aDragAction,
  1533. const uint32_t& aDropEffect)
  1534. {
  1535. WidgetDragEvent localEvent(aEvent);
  1536. localEvent.mWidget = mPuppetWidget;
  1537. nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
  1538. if (dragSession) {
  1539. dragSession->SetDragAction(aDragAction);
  1540. nsCOMPtr<nsIDOMDataTransfer> initialDataTransfer;
  1541. dragSession->GetDataTransfer(getter_AddRefs(initialDataTransfer));
  1542. if (initialDataTransfer) {
  1543. initialDataTransfer->SetDropEffectInt(aDropEffect);
  1544. }
  1545. }
  1546. if (aEvent.mMessage == eDrop) {
  1547. bool canDrop = true;
  1548. if (!dragSession || NS_FAILED(dragSession->GetCanDrop(&canDrop)) ||
  1549. !canDrop) {
  1550. localEvent.mMessage = eDragExit;
  1551. }
  1552. } else if (aEvent.mMessage == eDragOver) {
  1553. nsCOMPtr<nsIDragService> dragService =
  1554. do_GetService("@mozilla.org/widget/dragservice;1");
  1555. if (dragService) {
  1556. // This will dispatch 'drag' event at the source if the
  1557. // drag transaction started in this process.
  1558. dragService->FireDragEventAtSource(eDrag);
  1559. }
  1560. }
  1561. APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1562. return true;
  1563. }
  1564. bool
  1565. TabChild::RecvPluginEvent(const WidgetPluginEvent& aEvent)
  1566. {
  1567. WidgetPluginEvent localEvent(aEvent);
  1568. localEvent.mWidget = mPuppetWidget;
  1569. nsEventStatus status = APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1570. if (status != nsEventStatus_eConsumeNoDefault) {
  1571. // If not consumed, we should call default action
  1572. SendDefaultProcOfPluginEvent(aEvent);
  1573. }
  1574. return true;
  1575. }
  1576. void
  1577. TabChild::RequestNativeKeyBindings(AutoCacheNativeKeyCommands* aAutoCache,
  1578. const WidgetKeyboardEvent* aEvent)
  1579. {
  1580. MaybeNativeKeyBinding maybeBindings;
  1581. if (!SendRequestNativeKeyBindings(*aEvent, &maybeBindings)) {
  1582. return;
  1583. }
  1584. if (maybeBindings.type() == MaybeNativeKeyBinding::TNativeKeyBinding) {
  1585. const NativeKeyBinding& bindings = maybeBindings;
  1586. aAutoCache->Cache(bindings.singleLineCommands(),
  1587. bindings.multiLineCommands(),
  1588. bindings.richTextCommands());
  1589. } else {
  1590. aAutoCache->CacheNoCommands();
  1591. }
  1592. }
  1593. bool
  1594. TabChild::RecvNativeSynthesisResponse(const uint64_t& aObserverId,
  1595. const nsCString& aResponse)
  1596. {
  1597. mozilla::widget::AutoObserverNotifier::NotifySavedObserver(aObserverId, aResponse.get());
  1598. return true;
  1599. }
  1600. bool
  1601. TabChild::RecvRealKeyEvent(const WidgetKeyboardEvent& event,
  1602. const MaybeNativeKeyBinding& aBindings)
  1603. {
  1604. AutoCacheNativeKeyCommands autoCache(mPuppetWidget);
  1605. if (event.mMessage == eKeyPress) {
  1606. // If content code called preventDefault() on a keydown event, then we don't
  1607. // want to process any following keypress events.
  1608. if (mIgnoreKeyPressEvent) {
  1609. return true;
  1610. }
  1611. if (aBindings.type() == MaybeNativeKeyBinding::TNativeKeyBinding) {
  1612. const NativeKeyBinding& bindings = aBindings;
  1613. autoCache.Cache(bindings.singleLineCommands(),
  1614. bindings.multiLineCommands(),
  1615. bindings.richTextCommands());
  1616. } else {
  1617. autoCache.CacheNoCommands();
  1618. }
  1619. }
  1620. WidgetKeyboardEvent localEvent(event);
  1621. localEvent.mWidget = mPuppetWidget;
  1622. nsEventStatus status = APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1623. if (event.mMessage == eKeyDown) {
  1624. mIgnoreKeyPressEvent = status == nsEventStatus_eConsumeNoDefault;
  1625. }
  1626. if (localEvent.mFlags.mIsSuppressedOrDelayed) {
  1627. localEvent.PreventDefault();
  1628. }
  1629. // If a response is desired from the content process, resend the key event.
  1630. // If mAccessKeyForwardedToChild is set, then don't resend the key event yet
  1631. // as RecvHandleAccessKey will do this.
  1632. if (localEvent.mFlags.mWantReplyFromContentProcess) {
  1633. SendReplyKeyEvent(localEvent);
  1634. }
  1635. if (localEvent.mAccessKeyForwardedToChild) {
  1636. SendAccessKeyNotHandled(localEvent);
  1637. }
  1638. if (PresShell::BeforeAfterKeyboardEventEnabled()) {
  1639. SendDispatchAfterKeyboardEvent(localEvent);
  1640. }
  1641. return true;
  1642. }
  1643. bool
  1644. TabChild::RecvKeyEvent(const nsString& aType,
  1645. const int32_t& aKeyCode,
  1646. const int32_t& aCharCode,
  1647. const int32_t& aModifiers,
  1648. const bool& aPreventDefault)
  1649. {
  1650. bool ignored = false;
  1651. nsContentUtils::SendKeyEvent(mPuppetWidget, aType, aKeyCode, aCharCode,
  1652. aModifiers, aPreventDefault, &ignored);
  1653. return true;
  1654. }
  1655. bool
  1656. TabChild::RecvCompositionEvent(const WidgetCompositionEvent& event)
  1657. {
  1658. WidgetCompositionEvent localEvent(event);
  1659. localEvent.mWidget = mPuppetWidget;
  1660. APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1661. Unused << SendOnEventNeedingAckHandled(event.mMessage);
  1662. return true;
  1663. }
  1664. bool
  1665. TabChild::RecvSelectionEvent(const WidgetSelectionEvent& event)
  1666. {
  1667. WidgetSelectionEvent localEvent(event);
  1668. localEvent.mWidget = mPuppetWidget;
  1669. APZCCallbackHelper::DispatchWidgetEvent(localEvent);
  1670. Unused << SendOnEventNeedingAckHandled(event.mMessage);
  1671. return true;
  1672. }
  1673. bool
  1674. TabChild::RecvPasteTransferable(const IPCDataTransfer& aDataTransfer,
  1675. const bool& aIsPrivateData,
  1676. const IPC::Principal& aRequestingPrincipal)
  1677. {
  1678. nsresult rv;
  1679. nsCOMPtr<nsITransferable> trans =
  1680. do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
  1681. NS_ENSURE_SUCCESS(rv, true);
  1682. trans->Init(nullptr);
  1683. rv = nsContentUtils::IPCTransferableToTransferable(aDataTransfer,
  1684. aIsPrivateData,
  1685. aRequestingPrincipal,
  1686. trans, nullptr, this);
  1687. NS_ENSURE_SUCCESS(rv, true);
  1688. nsCOMPtr<nsIDocShell> ourDocShell = do_GetInterface(WebNavigation());
  1689. if (NS_WARN_IF(!ourDocShell)) {
  1690. return true;
  1691. }
  1692. nsCOMPtr<nsICommandParams> params = do_CreateInstance("@mozilla.org/embedcomp/command-params;1", &rv);
  1693. NS_ENSURE_SUCCESS(rv, true);
  1694. rv = params->SetISupportsValue("transferable", trans);
  1695. NS_ENSURE_SUCCESS(rv, true);
  1696. ourDocShell->DoCommandWithParams("cmd_pasteTransferable", params);
  1697. return true;
  1698. }
  1699. a11y::PDocAccessibleChild*
  1700. TabChild::AllocPDocAccessibleChild(PDocAccessibleChild*, const uint64_t&,
  1701. const uint32_t&, const IAccessibleHolder&)
  1702. {
  1703. MOZ_ASSERT(false, "should never call this!");
  1704. return nullptr;
  1705. }
  1706. bool
  1707. TabChild::DeallocPDocAccessibleChild(a11y::PDocAccessibleChild* aChild)
  1708. {
  1709. #ifdef ACCESSIBILITY
  1710. delete static_cast<mozilla::a11y::DocAccessibleChild*>(aChild);
  1711. #endif
  1712. return true;
  1713. }
  1714. PColorPickerChild*
  1715. TabChild::AllocPColorPickerChild(const nsString&, const nsString&)
  1716. {
  1717. NS_RUNTIMEABORT("unused");
  1718. return nullptr;
  1719. }
  1720. bool
  1721. TabChild::DeallocPColorPickerChild(PColorPickerChild* aColorPicker)
  1722. {
  1723. nsColorPickerProxy* picker = static_cast<nsColorPickerProxy*>(aColorPicker);
  1724. NS_RELEASE(picker);
  1725. return true;
  1726. }
  1727. PFilePickerChild*
  1728. TabChild::AllocPFilePickerChild(const nsString&, const int16_t&)
  1729. {
  1730. NS_RUNTIMEABORT("unused");
  1731. return nullptr;
  1732. }
  1733. bool
  1734. TabChild::DeallocPFilePickerChild(PFilePickerChild* actor)
  1735. {
  1736. nsFilePickerProxy* filePicker = static_cast<nsFilePickerProxy*>(actor);
  1737. NS_RELEASE(filePicker);
  1738. return true;
  1739. }
  1740. auto
  1741. TabChild::AllocPIndexedDBPermissionRequestChild(const Principal& aPrincipal)
  1742. -> PIndexedDBPermissionRequestChild*
  1743. {
  1744. MOZ_CRASH("PIndexedDBPermissionRequestChild actors should always be created "
  1745. "manually!");
  1746. }
  1747. bool
  1748. TabChild::DeallocPIndexedDBPermissionRequestChild(
  1749. PIndexedDBPermissionRequestChild* aActor)
  1750. {
  1751. MOZ_ASSERT(aActor);
  1752. delete aActor;
  1753. return true;
  1754. }
  1755. bool
  1756. TabChild::RecvActivateFrameEvent(const nsString& aType, const bool& capture)
  1757. {
  1758. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  1759. NS_ENSURE_TRUE(window, true);
  1760. nsCOMPtr<EventTarget> chromeHandler =
  1761. do_QueryInterface(window->GetChromeEventHandler());
  1762. NS_ENSURE_TRUE(chromeHandler, true);
  1763. RefPtr<ContentListener> listener = new ContentListener(this);
  1764. chromeHandler->AddEventListener(aType, listener, capture);
  1765. return true;
  1766. }
  1767. bool
  1768. TabChild::RecvLoadRemoteScript(const nsString& aURL, const bool& aRunInGlobalScope)
  1769. {
  1770. if (!mGlobal && !InitTabChildGlobal())
  1771. // This can happen if we're half-destroyed. It's not a fatal
  1772. // error.
  1773. return true;
  1774. LoadScriptInternal(aURL, aRunInGlobalScope);
  1775. return true;
  1776. }
  1777. bool
  1778. TabChild::RecvAsyncMessage(const nsString& aMessage,
  1779. InfallibleTArray<CpowEntry>&& aCpows,
  1780. const IPC::Principal& aPrincipal,
  1781. const ClonedMessageData& aData)
  1782. {
  1783. if (!mTabChildGlobal) {
  1784. return true;
  1785. }
  1786. // We should have a message manager if the global is alive, but it
  1787. // seems sometimes we don't. Assert in aurora/nightly, but don't
  1788. // crash in release builds.
  1789. MOZ_DIAGNOSTIC_ASSERT(mTabChildGlobal->mMessageManager);
  1790. if (!mTabChildGlobal->mMessageManager) {
  1791. return true;
  1792. }
  1793. nsCOMPtr<nsIXPConnectJSObjectHolder> kungFuDeathGrip(GetGlobal());
  1794. StructuredCloneData data;
  1795. UnpackClonedMessageDataForChild(aData, data);
  1796. RefPtr<nsFrameMessageManager> mm =
  1797. static_cast<nsFrameMessageManager*>(mTabChildGlobal->mMessageManager.get());
  1798. CrossProcessCpowHolder cpows(Manager(), aCpows);
  1799. mm->ReceiveMessage(static_cast<EventTarget*>(mTabChildGlobal), nullptr,
  1800. aMessage, false, &data, &cpows, aPrincipal, nullptr);
  1801. return true;
  1802. }
  1803. bool
  1804. TabChild::RecvSwappedWithOtherRemoteLoader(const IPCTabContext& aContext)
  1805. {
  1806. nsCOMPtr<nsIDocShell> ourDocShell = do_GetInterface(WebNavigation());
  1807. if (NS_WARN_IF(!ourDocShell)) {
  1808. return true;
  1809. }
  1810. nsCOMPtr<nsPIDOMWindowOuter> ourWindow = ourDocShell->GetWindow();
  1811. if (NS_WARN_IF(!ourWindow)) {
  1812. return true;
  1813. }
  1814. RefPtr<nsDocShell> docShell = static_cast<nsDocShell*>(ourDocShell.get());
  1815. nsCOMPtr<EventTarget> ourEventTarget = ourWindow->GetParentTarget();
  1816. docShell->SetInFrameSwap(true);
  1817. nsContentUtils::FirePageShowEvent(ourDocShell, ourEventTarget, false);
  1818. nsContentUtils::FirePageHideEvent(ourDocShell, ourEventTarget);
  1819. // Owner content type may have changed, so store the possibly updated context
  1820. // and notify others.
  1821. MaybeInvalidTabContext maybeContext(aContext);
  1822. if (!maybeContext.IsValid()) {
  1823. NS_ERROR(nsPrintfCString("Received an invalid TabContext from "
  1824. "the parent process. (%s)",
  1825. maybeContext.GetInvalidReason()).get());
  1826. MOZ_CRASH("Invalid TabContext received from the parent process.");
  1827. }
  1828. if (!UpdateTabContextAfterSwap(maybeContext.GetTabContext())) {
  1829. MOZ_CRASH("Update to TabContext after swap was denied.");
  1830. }
  1831. // Since mIsMozBrowserElement may change in UpdateTabContextAfterSwap, so we
  1832. // call UpdateFrameType here to make sure the frameType on the docshell is
  1833. // correct.
  1834. UpdateFrameType();
  1835. // Ignore previous value of mTriedBrowserInit since owner content has changed.
  1836. mTriedBrowserInit = true;
  1837. // Initialize the child side of the browser element machinery, if appropriate.
  1838. if (IsMozBrowserOrApp()) {
  1839. RecvLoadRemoteScript(BROWSER_ELEMENT_CHILD_SCRIPT, true);
  1840. }
  1841. nsContentUtils::FirePageShowEvent(ourDocShell, ourEventTarget, true);
  1842. docShell->SetInFrameSwap(false);
  1843. return true;
  1844. }
  1845. bool
  1846. TabChild::RecvHandleAccessKey(const WidgetKeyboardEvent& aEvent,
  1847. nsTArray<uint32_t>&& aCharCodes,
  1848. const int32_t& aModifierMask)
  1849. {
  1850. nsCOMPtr<nsIDocument> document(GetDocument());
  1851. nsCOMPtr<nsIPresShell> presShell = document->GetShell();
  1852. if (presShell) {
  1853. nsPresContext* pc = presShell->GetPresContext();
  1854. if (pc) {
  1855. if (!pc->EventStateManager()->
  1856. HandleAccessKey(&(const_cast<WidgetKeyboardEvent&>(aEvent)),
  1857. pc, aCharCodes,
  1858. aModifierMask, true)) {
  1859. // If no accesskey was found, inform the parent so that accesskeys on
  1860. // menus can be handled.
  1861. WidgetKeyboardEvent localEvent(aEvent);
  1862. localEvent.mWidget = mPuppetWidget;
  1863. SendAccessKeyNotHandled(localEvent);
  1864. }
  1865. }
  1866. }
  1867. return true;
  1868. }
  1869. bool
  1870. TabChild::RecvAudioChannelChangeNotification(const uint32_t& aAudioChannel,
  1871. const float& aVolume,
  1872. const bool& aMuted)
  1873. {
  1874. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  1875. if (window) {
  1876. RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
  1877. MOZ_ASSERT(service);
  1878. service->SetAudioChannelVolume(window,
  1879. static_cast<AudioChannel>(aAudioChannel),
  1880. aVolume);
  1881. service->SetAudioChannelMuted(window,
  1882. static_cast<AudioChannel>(aAudioChannel),
  1883. aMuted);
  1884. }
  1885. return true;
  1886. }
  1887. bool
  1888. TabChild::RecvSetUseGlobalHistory(const bool& aUse)
  1889. {
  1890. nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
  1891. MOZ_ASSERT(docShell);
  1892. nsresult rv = docShell->SetUseGlobalHistory(aUse);
  1893. if (NS_FAILED(rv)) {
  1894. NS_WARNING("Failed to set UseGlobalHistory on TabChild docShell");
  1895. }
  1896. return true;
  1897. }
  1898. bool
  1899. TabChild::RecvPrint(const uint64_t& aOuterWindowID, const PrintData& aPrintData)
  1900. {
  1901. #ifdef NS_PRINTING
  1902. nsGlobalWindow* outerWindow =
  1903. nsGlobalWindow::GetOuterWindowWithId(aOuterWindowID);
  1904. if (NS_WARN_IF(!outerWindow)) {
  1905. return true;
  1906. }
  1907. nsCOMPtr<nsIWebBrowserPrint> webBrowserPrint =
  1908. do_GetInterface(outerWindow->AsOuter());
  1909. if (NS_WARN_IF(!webBrowserPrint)) {
  1910. return true;
  1911. }
  1912. nsCOMPtr<nsIPrintSettingsService> printSettingsSvc =
  1913. do_GetService("@mozilla.org/gfx/printsettings-service;1");
  1914. if (NS_WARN_IF(!printSettingsSvc)) {
  1915. return true;
  1916. }
  1917. nsCOMPtr<nsIPrintSettings> printSettings;
  1918. nsresult rv =
  1919. printSettingsSvc->GetNewPrintSettings(getter_AddRefs(printSettings));
  1920. if (NS_WARN_IF(NS_FAILED(rv))) {
  1921. return true;
  1922. }
  1923. nsCOMPtr<nsIPrintSession> printSession =
  1924. do_CreateInstance("@mozilla.org/gfx/printsession;1", &rv);
  1925. if (NS_WARN_IF(NS_FAILED(rv))) {
  1926. return true;
  1927. }
  1928. printSettings->SetPrintSession(printSession);
  1929. printSettingsSvc->DeserializeToPrintSettings(aPrintData, printSettings);
  1930. rv = webBrowserPrint->Print(printSettings, nullptr);
  1931. if (NS_WARN_IF(NS_FAILED(rv))) {
  1932. return true;
  1933. }
  1934. #endif
  1935. return true;
  1936. }
  1937. bool
  1938. TabChild::RecvUpdateNativeWindowHandle(const uintptr_t& aNewHandle)
  1939. {
  1940. #if defined(XP_WIN) && defined(ACCESSIBILITY)
  1941. mNativeWindowHandle = aNewHandle;
  1942. return true;
  1943. #else
  1944. return false;
  1945. #endif
  1946. }
  1947. bool
  1948. TabChild::RecvDestroy()
  1949. {
  1950. MOZ_ASSERT(mDestroyed == false);
  1951. mDestroyed = true;
  1952. nsTArray<PContentPermissionRequestChild*> childArray =
  1953. nsContentPermissionUtils::GetContentPermissionRequestChildById(GetTabId());
  1954. // Need to close undeleted ContentPermissionRequestChilds before tab is closed.
  1955. for (auto& permissionRequestChild : childArray) {
  1956. auto child = static_cast<RemotePermissionRequest*>(permissionRequestChild);
  1957. child->Destroy();
  1958. }
  1959. while (mActiveSuppressDisplayport > 0) {
  1960. APZCCallbackHelper::SuppressDisplayport(false, nullptr);
  1961. mActiveSuppressDisplayport--;
  1962. }
  1963. if (mTabChildGlobal) {
  1964. // Message handlers are called from the event loop, so it better be safe to
  1965. // run script.
  1966. MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
  1967. mTabChildGlobal->DispatchTrustedEvent(NS_LITERAL_STRING("unload"));
  1968. }
  1969. nsCOMPtr<nsIObserverService> observerService =
  1970. mozilla::services::GetObserverService();
  1971. observerService->RemoveObserver(this, BEFORE_FIRST_PAINT);
  1972. const nsAttrValue::EnumTable* table =
  1973. AudioChannelService::GetAudioChannelTable();
  1974. nsAutoCString topic;
  1975. for (uint32_t i = 0; table[i].tag; ++i) {
  1976. topic.Assign("audiochannel-activity-");
  1977. topic.Append(table[i].tag);
  1978. observerService->RemoveObserver(this, topic.get());
  1979. }
  1980. // XXX what other code in ~TabChild() should we be running here?
  1981. DestroyWindow();
  1982. // Bounce through the event loop once to allow any delayed teardown runnables
  1983. // that were just generated to have a chance to run.
  1984. nsCOMPtr<nsIRunnable> deleteRunnable = new DelayedDeleteRunnable(this);
  1985. MOZ_ALWAYS_SUCCEEDS(NS_DispatchToCurrentThread(deleteRunnable));
  1986. return true;
  1987. }
  1988. bool
  1989. TabChild::RecvSetDocShellIsActive(const bool& aIsActive,
  1990. const bool& aPreserveLayers,
  1991. const uint64_t& aLayerObserverEpoch)
  1992. {
  1993. // Since SetDocShellIsActive requests come in from both the hang monitor
  1994. // channel and the PContent channel, we have an ordering problem. This code
  1995. // ensures that we respect the order in which the requests were made and
  1996. // ignore stale requests.
  1997. if (mLayerObserverEpoch >= aLayerObserverEpoch) {
  1998. return true;
  1999. }
  2000. mLayerObserverEpoch = aLayerObserverEpoch;
  2001. MOZ_ASSERT(mPuppetWidget);
  2002. MOZ_ASSERT(mPuppetWidget->GetLayerManager());
  2003. MOZ_ASSERT(mPuppetWidget->GetLayerManager()->GetBackendType() ==
  2004. LayersBackend::LAYERS_CLIENT);
  2005. auto clearForcePaint = MakeScopeExit([&] {
  2006. // We might force a paint, or we might already have painted and this is a
  2007. // no-op. In either case, once we exit this scope, we need to alert the
  2008. // ProcessHangMonitor that we've finished responding to what might have
  2009. // been a request to force paint. This is so that the BackgroundHangMonitor
  2010. // for force painting can be made to wait again.
  2011. if (aIsActive) {
  2012. ProcessHangMonitor::ClearForcePaint();
  2013. }
  2014. });
  2015. // We send the current layer observer epoch to the compositor so that
  2016. // TabParent knows whether a layer update notification corresponds to the
  2017. // latest SetDocShellIsActive request that was made.
  2018. if (ClientLayerManager* clm = mPuppetWidget->GetLayerManager()->AsClientLayerManager()) {
  2019. clm->SetLayerObserverEpoch(aLayerObserverEpoch);
  2020. }
  2021. // docshell is consider prerendered only if not active yet
  2022. mIsPrerendered &= !aIsActive;
  2023. nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
  2024. if (docShell) {
  2025. bool wasActive;
  2026. docShell->GetIsActive(&wasActive);
  2027. if (aIsActive && wasActive) {
  2028. // This request is a no-op. In this case, we still want a MozLayerTreeReady
  2029. // notification to fire in the parent (so that it knows that the child has
  2030. // updated its epoch). ForcePaintNoOp does that.
  2031. if (IPCOpen()) {
  2032. Unused << SendForcePaintNoOp(aLayerObserverEpoch);
  2033. return true;
  2034. }
  2035. }
  2036. docShell->SetIsActive(aIsActive);
  2037. }
  2038. if (aIsActive) {
  2039. MakeVisible();
  2040. // We don't use TabChildBase::GetPresShell() here because that would create
  2041. // a content viewer if one doesn't exist yet. Creating a content viewer can
  2042. // cause JS to run, which we want to avoid. nsIDocShell::GetPresShell
  2043. // returns null if no content viewer exists yet.
  2044. if (nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell()) {
  2045. if (nsIFrame* root = presShell->FrameConstructor()->GetRootFrame()) {
  2046. FrameLayerBuilder::InvalidateAllLayersForFrame(
  2047. nsLayoutUtils::GetDisplayRootFrame(root));
  2048. root->SchedulePaint();
  2049. }
  2050. // If we need to repaint, let's do that right away. No sense waiting until
  2051. // we get back to the event loop again. We suppress the display port so that
  2052. // we only paint what's visible. This ensures that the tab we're switching
  2053. // to paints as quickly as possible.
  2054. APZCCallbackHelper::SuppressDisplayport(true, presShell);
  2055. if (nsContentUtils::IsSafeToRunScript()) {
  2056. WebWidget()->PaintNowIfNeeded();
  2057. } else {
  2058. RefPtr<nsViewManager> vm = presShell->GetViewManager();
  2059. if (nsView* view = vm->GetRootView()) {
  2060. presShell->Paint(view, view->GetBounds(),
  2061. nsIPresShell::PAINT_LAYERS);
  2062. }
  2063. }
  2064. APZCCallbackHelper::SuppressDisplayport(false, presShell);
  2065. }
  2066. } else if (!aPreserveLayers) {
  2067. MakeHidden();
  2068. }
  2069. return true;
  2070. }
  2071. bool
  2072. TabChild::RecvNavigateByKey(const bool& aForward, const bool& aForDocumentNavigation)
  2073. {
  2074. nsIFocusManager* fm = nsFocusManager::GetFocusManager();
  2075. if (fm) {
  2076. nsCOMPtr<nsIDOMElement> result;
  2077. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  2078. // Move to the first or last document.
  2079. uint32_t type = aForward ?
  2080. (aForDocumentNavigation ? static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_FIRSTDOC) :
  2081. static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_ROOT)) :
  2082. (aForDocumentNavigation ? static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_LASTDOC) :
  2083. static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_LAST));
  2084. fm->MoveFocus(window, nullptr, type,
  2085. nsIFocusManager::FLAG_BYKEY, getter_AddRefs(result));
  2086. // No valid root element was found, so move to the first focusable element.
  2087. if (!result && aForward && !aForDocumentNavigation) {
  2088. fm->MoveFocus(window, nullptr, nsIFocusManager::MOVEFOCUS_FIRST,
  2089. nsIFocusManager::FLAG_BYKEY, getter_AddRefs(result));
  2090. }
  2091. SendRequestFocus(false);
  2092. }
  2093. return true;
  2094. }
  2095. bool
  2096. TabChild::RecvHandledWindowedPluginKeyEvent(
  2097. const NativeEventData& aKeyEventData,
  2098. const bool& aIsConsumed)
  2099. {
  2100. if (NS_WARN_IF(!mPuppetWidget)) {
  2101. return true;
  2102. }
  2103. mPuppetWidget->HandledWindowedPluginKeyEvent(aKeyEventData, aIsConsumed);
  2104. return true;
  2105. }
  2106. PRenderFrameChild*
  2107. TabChild::AllocPRenderFrameChild()
  2108. {
  2109. return new RenderFrameChild();
  2110. }
  2111. bool
  2112. TabChild::DeallocPRenderFrameChild(PRenderFrameChild* aFrame)
  2113. {
  2114. delete aFrame;
  2115. return true;
  2116. }
  2117. bool
  2118. TabChild::InitTabChildGlobal(FrameScriptLoading aScriptLoading)
  2119. {
  2120. if (!mGlobal && !mTabChildGlobal) {
  2121. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(WebNavigation());
  2122. NS_ENSURE_TRUE(window, false);
  2123. nsCOMPtr<EventTarget> chromeHandler =
  2124. do_QueryInterface(window->GetChromeEventHandler());
  2125. NS_ENSURE_TRUE(chromeHandler, false);
  2126. RefPtr<TabChildGlobal> scope = new TabChildGlobal(this);
  2127. mTabChildGlobal = scope;
  2128. nsISupports* scopeSupports = NS_ISUPPORTS_CAST(EventTarget*, scope);
  2129. NS_NAMED_LITERAL_CSTRING(globalId, "outOfProcessTabChildGlobal");
  2130. NS_ENSURE_TRUE(InitChildGlobalInternal(scopeSupports, globalId), false);
  2131. scope->Init();
  2132. nsCOMPtr<nsPIWindowRoot> root = do_QueryInterface(chromeHandler);
  2133. NS_ENSURE_TRUE(root, false);
  2134. root->SetParentTarget(scope);
  2135. }
  2136. if (aScriptLoading != DONT_LOAD_SCRIPTS && !mTriedBrowserInit) {
  2137. mTriedBrowserInit = true;
  2138. // Initialize the child side of the browser element machinery,
  2139. // if appropriate.
  2140. if (IsMozBrowserOrApp()) {
  2141. RecvLoadRemoteScript(BROWSER_ELEMENT_CHILD_SCRIPT, true);
  2142. }
  2143. }
  2144. return true;
  2145. }
  2146. bool
  2147. TabChild::InitRenderingState(const TextureFactoryIdentifier& aTextureFactoryIdentifier,
  2148. const uint64_t& aLayersId,
  2149. PRenderFrameChild* aRenderFrame)
  2150. {
  2151. mPuppetWidget->InitIMEState();
  2152. RenderFrameChild* remoteFrame = static_cast<RenderFrameChild*>(aRenderFrame);
  2153. if (!remoteFrame) {
  2154. NS_WARNING("failed to construct RenderFrame");
  2155. return false;
  2156. }
  2157. MOZ_ASSERT(aLayersId != 0);
  2158. mTextureFactoryIdentifier = aTextureFactoryIdentifier;
  2159. // Pushing layers transactions directly to a separate
  2160. // compositor context.
  2161. PCompositorBridgeChild* compositorChild = CompositorBridgeChild::Get();
  2162. if (!compositorChild) {
  2163. NS_WARNING("failed to get CompositorBridgeChild instance");
  2164. PRenderFrameChild::Send__delete__(remoteFrame);
  2165. return false;
  2166. }
  2167. ShadowLayerForwarder* lf =
  2168. mPuppetWidget->GetLayerManager(
  2169. nullptr, mTextureFactoryIdentifier.mParentBackend)
  2170. ->AsShadowForwarder();
  2171. // As long as we are creating a ClientLayerManager for the puppet widget,
  2172. // lf must be non-null here.
  2173. MOZ_ASSERT(lf);
  2174. if (lf) {
  2175. nsTArray<LayersBackend> backends;
  2176. backends.AppendElement(mTextureFactoryIdentifier.mParentBackend);
  2177. bool success;
  2178. PLayerTransactionChild* shadowManager =
  2179. compositorChild->SendPLayerTransactionConstructor(backends,
  2180. aLayersId, &mTextureFactoryIdentifier, &success);
  2181. if (!success) {
  2182. NS_WARNING("failed to properly allocate layer transaction");
  2183. PRenderFrameChild::Send__delete__(remoteFrame);
  2184. return false;
  2185. }
  2186. if (!shadowManager) {
  2187. NS_WARNING("failed to construct LayersChild");
  2188. // This results in |remoteFrame| being deleted.
  2189. PRenderFrameChild::Send__delete__(remoteFrame);
  2190. return false;
  2191. }
  2192. lf->SetShadowManager(shadowManager);
  2193. lf->IdentifyTextureHost(mTextureFactoryIdentifier);
  2194. ImageBridgeChild::IdentifyCompositorTextureHost(mTextureFactoryIdentifier);
  2195. }
  2196. mRemoteFrame = remoteFrame;
  2197. if (aLayersId != 0) {
  2198. if (!sTabChildren) {
  2199. sTabChildren = new TabChildMap;
  2200. }
  2201. MOZ_ASSERT(!sTabChildren->Get(aLayersId));
  2202. sTabChildren->Put(aLayersId, this);
  2203. mLayersId = aLayersId;
  2204. }
  2205. mApzcTreeManager = CompositorBridgeChild::Get()->GetAPZCTreeManager(mLayersId);
  2206. nsCOMPtr<nsIObserverService> observerService =
  2207. mozilla::services::GetObserverService();
  2208. if (observerService) {
  2209. observerService->AddObserver(this,
  2210. BEFORE_FIRST_PAINT,
  2211. false);
  2212. }
  2213. return true;
  2214. }
  2215. void
  2216. TabChild::GetDPI(float* aDPI)
  2217. {
  2218. *aDPI = -1.0;
  2219. if (!mRemoteFrame) {
  2220. return;
  2221. }
  2222. if (mDPI > 0) {
  2223. *aDPI = mDPI;
  2224. return;
  2225. }
  2226. // Fallback to a sync call if needed.
  2227. SendGetDPI(aDPI);
  2228. }
  2229. void
  2230. TabChild::GetDefaultScale(double* aScale)
  2231. {
  2232. *aScale = -1.0;
  2233. if (!mRemoteFrame) {
  2234. return;
  2235. }
  2236. if (mDefaultScale > 0) {
  2237. *aScale = mDefaultScale;
  2238. return;
  2239. }
  2240. // Fallback to a sync call if needed.
  2241. SendGetDefaultScale(aScale);
  2242. }
  2243. void
  2244. TabChild::GetWidgetRounding(int32_t* aRounding)
  2245. {
  2246. *aRounding = 1;
  2247. if (!mRemoteFrame) {
  2248. return;
  2249. }
  2250. if (mRounding > 0) {
  2251. *aRounding = mRounding;
  2252. return;
  2253. }
  2254. // Fallback to a sync call if needed.
  2255. SendGetWidgetRounding(aRounding);
  2256. }
  2257. void
  2258. TabChild::GetMaxTouchPoints(uint32_t* aTouchPoints)
  2259. {
  2260. // Fallback to a sync call.
  2261. SendGetMaxTouchPoints(aTouchPoints);
  2262. }
  2263. void
  2264. TabChild::NotifyPainted()
  2265. {
  2266. if (!mNotified) {
  2267. mRemoteFrame->SendNotifyCompositorTransaction();
  2268. mNotified = true;
  2269. }
  2270. }
  2271. void
  2272. TabChild::MakeVisible()
  2273. {
  2274. if (mPuppetWidget && mPuppetWidget->IsVisible()) {
  2275. return;
  2276. }
  2277. if (mPuppetWidget) {
  2278. mPuppetWidget->Show(true);
  2279. }
  2280. }
  2281. void
  2282. TabChild::MakeHidden()
  2283. {
  2284. if (mPuppetWidget && !mPuppetWidget->IsVisible()) {
  2285. return;
  2286. }
  2287. CompositorBridgeChild* compositor = CompositorBridgeChild::Get();
  2288. // Clear cached resources directly. This avoids one extra IPC
  2289. // round-trip from CompositorBridgeChild to CompositorBridgeParent.
  2290. compositor->RecvClearCachedResources(mLayersId);
  2291. if (mPuppetWidget) {
  2292. mPuppetWidget->Show(false);
  2293. }
  2294. }
  2295. NS_IMETHODIMP
  2296. TabChild::GetMessageManager(nsIContentFrameMessageManager** aResult)
  2297. {
  2298. if (mTabChildGlobal) {
  2299. NS_ADDREF(*aResult = mTabChildGlobal);
  2300. return NS_OK;
  2301. }
  2302. *aResult = nullptr;
  2303. return NS_ERROR_FAILURE;
  2304. }
  2305. NS_IMETHODIMP
  2306. TabChild::GetWebBrowserChrome(nsIWebBrowserChrome3** aWebBrowserChrome)
  2307. {
  2308. NS_IF_ADDREF(*aWebBrowserChrome = mWebBrowserChrome);
  2309. return NS_OK;
  2310. }
  2311. NS_IMETHODIMP
  2312. TabChild::SetWebBrowserChrome(nsIWebBrowserChrome3* aWebBrowserChrome)
  2313. {
  2314. mWebBrowserChrome = aWebBrowserChrome;
  2315. return NS_OK;
  2316. }
  2317. void
  2318. TabChild::SendRequestFocus(bool aCanFocus)
  2319. {
  2320. PBrowserChild::SendRequestFocus(aCanFocus);
  2321. }
  2322. void
  2323. TabChild::SendGetTabCount(uint32_t* tabCount)
  2324. {
  2325. PBrowserChild::SendGetTabCount(tabCount);
  2326. }
  2327. void
  2328. TabChild::EnableDisableCommands(const nsAString& aAction,
  2329. nsTArray<nsCString>& aEnabledCommands,
  2330. nsTArray<nsCString>& aDisabledCommands)
  2331. {
  2332. PBrowserChild::SendEnableDisableCommands(PromiseFlatString(aAction),
  2333. aEnabledCommands, aDisabledCommands);
  2334. }
  2335. NS_IMETHODIMP
  2336. TabChild::GetTabId(uint64_t* aId)
  2337. {
  2338. *aId = GetTabId();
  2339. return NS_OK;
  2340. }
  2341. void
  2342. TabChild::SetTabId(const TabId& aTabId)
  2343. {
  2344. MOZ_ASSERT(mUniqueId == 0);
  2345. mUniqueId = aTabId;
  2346. NestedTabChildMap()[mUniqueId] = this;
  2347. }
  2348. bool
  2349. TabChild::DoSendBlockingMessage(JSContext* aCx,
  2350. const nsAString& aMessage,
  2351. StructuredCloneData& aData,
  2352. JS::Handle<JSObject *> aCpows,
  2353. nsIPrincipal* aPrincipal,
  2354. nsTArray<StructuredCloneData>* aRetVal,
  2355. bool aIsSync)
  2356. {
  2357. ClonedMessageData data;
  2358. if (!BuildClonedMessageDataForChild(Manager(), aData, data)) {
  2359. return false;
  2360. }
  2361. InfallibleTArray<CpowEntry> cpows;
  2362. if (aCpows && !Manager()->GetCPOWManager()->Wrap(aCx, aCpows, &cpows)) {
  2363. return false;
  2364. }
  2365. if (aIsSync) {
  2366. return SendSyncMessage(PromiseFlatString(aMessage), data, cpows,
  2367. Principal(aPrincipal), aRetVal);
  2368. }
  2369. return SendRpcMessage(PromiseFlatString(aMessage), data, cpows,
  2370. Principal(aPrincipal), aRetVal);
  2371. }
  2372. nsresult
  2373. TabChild::DoSendAsyncMessage(JSContext* aCx,
  2374. const nsAString& aMessage,
  2375. StructuredCloneData& aData,
  2376. JS::Handle<JSObject *> aCpows,
  2377. nsIPrincipal* aPrincipal)
  2378. {
  2379. ClonedMessageData data;
  2380. if (!BuildClonedMessageDataForChild(Manager(), aData, data)) {
  2381. return NS_ERROR_DOM_DATA_CLONE_ERR;
  2382. }
  2383. InfallibleTArray<CpowEntry> cpows;
  2384. if (aCpows && !Manager()->GetCPOWManager()->Wrap(aCx, aCpows, &cpows)) {
  2385. return NS_ERROR_UNEXPECTED;
  2386. }
  2387. if (!SendAsyncMessage(PromiseFlatString(aMessage), cpows,
  2388. Principal(aPrincipal), data)) {
  2389. return NS_ERROR_UNEXPECTED;
  2390. }
  2391. return NS_OK;
  2392. }
  2393. /* static */ nsTArray<RefPtr<TabChild>>
  2394. TabChild::GetAll()
  2395. {
  2396. nsTArray<RefPtr<TabChild>> list;
  2397. if (!sTabChildren) {
  2398. return list;
  2399. }
  2400. for (auto iter = sTabChildren->Iter(); !iter.Done(); iter.Next()) {
  2401. list.AppendElement(iter.Data());
  2402. }
  2403. return list;
  2404. }
  2405. TabChild*
  2406. TabChild::GetFrom(nsIPresShell* aPresShell)
  2407. {
  2408. nsIDocument* doc = aPresShell->GetDocument();
  2409. if (!doc) {
  2410. return nullptr;
  2411. }
  2412. nsCOMPtr<nsIDocShell> docShell(doc->GetDocShell());
  2413. return GetFrom(docShell);
  2414. }
  2415. TabChild*
  2416. TabChild::GetFrom(uint64_t aLayersId)
  2417. {
  2418. if (!sTabChildren) {
  2419. return nullptr;
  2420. }
  2421. return sTabChildren->Get(aLayersId);
  2422. }
  2423. void
  2424. TabChild::DidComposite(uint64_t aTransactionId,
  2425. const TimeStamp& aCompositeStart,
  2426. const TimeStamp& aCompositeEnd)
  2427. {
  2428. MOZ_ASSERT(mPuppetWidget);
  2429. MOZ_ASSERT(mPuppetWidget->GetLayerManager());
  2430. MOZ_ASSERT(mPuppetWidget->GetLayerManager()->GetBackendType() ==
  2431. LayersBackend::LAYERS_CLIENT);
  2432. RefPtr<ClientLayerManager> manager = mPuppetWidget->GetLayerManager()->AsClientLayerManager();
  2433. manager->DidComposite(aTransactionId, aCompositeStart, aCompositeEnd);
  2434. }
  2435. void
  2436. TabChild::DidRequestComposite(const TimeStamp& aCompositeReqStart,
  2437. const TimeStamp& aCompositeReqEnd)
  2438. {
  2439. nsCOMPtr<nsIDocShell> docShellComPtr = do_GetInterface(WebNavigation());
  2440. if (!docShellComPtr) {
  2441. return;
  2442. }
  2443. nsDocShell* docShell = static_cast<nsDocShell*>(docShellComPtr.get());
  2444. RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
  2445. if (timelines && timelines->HasConsumer(docShell)) {
  2446. // Since we're assuming that it's impossible for content JS to directly
  2447. // trigger a synchronous paint, we can avoid capturing a stack trace here,
  2448. // which means we won't run into JS engine reentrancy issues like bug
  2449. // 1310014.
  2450. timelines->AddMarkerForDocShell(docShell,
  2451. "CompositeForwardTransaction", aCompositeReqStart,
  2452. MarkerTracingType::START, MarkerStackRequest::NO_STACK);
  2453. timelines->AddMarkerForDocShell(docShell,
  2454. "CompositeForwardTransaction", aCompositeReqEnd,
  2455. MarkerTracingType::END, MarkerStackRequest::NO_STACK);
  2456. }
  2457. }
  2458. void
  2459. TabChild::ClearCachedResources()
  2460. {
  2461. MOZ_ASSERT(mPuppetWidget);
  2462. MOZ_ASSERT(mPuppetWidget->GetLayerManager());
  2463. MOZ_ASSERT(mPuppetWidget->GetLayerManager()->GetBackendType() ==
  2464. LayersBackend::LAYERS_CLIENT);
  2465. ClientLayerManager *manager = mPuppetWidget->GetLayerManager()->AsClientLayerManager();
  2466. manager->ClearCachedResources();
  2467. }
  2468. void
  2469. TabChild::InvalidateLayers()
  2470. {
  2471. MOZ_ASSERT(mPuppetWidget);
  2472. MOZ_ASSERT(mPuppetWidget->GetLayerManager());
  2473. MOZ_ASSERT(mPuppetWidget->GetLayerManager()->GetBackendType() ==
  2474. LayersBackend::LAYERS_CLIENT);
  2475. RefPtr<LayerManager> lm = mPuppetWidget->GetLayerManager();
  2476. FrameLayerBuilder::InvalidateAllLayers(lm);
  2477. }
  2478. void
  2479. TabChild::ReinitRendering()
  2480. {
  2481. MOZ_ASSERT(mLayersId);
  2482. // Before we establish a new PLayerTransaction, we must connect our layer tree
  2483. // id, CompositorBridge, and the widget compositor all together again.
  2484. // Normally this happens in TabParent before TabChild is given rendering
  2485. // information.
  2486. //
  2487. // In this case, we will send a sync message to our TabParent, which in turn
  2488. // will send a sync message to the Compositor of the widget owning this tab.
  2489. // This guarantees the correct association is in place before our
  2490. // PLayerTransaction constructor message arrives on the cross-process
  2491. // compositor bridge.
  2492. SendEnsureLayersConnected();
  2493. RefPtr<CompositorBridgeChild> cb = CompositorBridgeChild::Get();
  2494. bool success;
  2495. nsTArray<LayersBackend> ignored;
  2496. PLayerTransactionChild* shadowManager =
  2497. cb->SendPLayerTransactionConstructor(ignored, LayersId(), &mTextureFactoryIdentifier, &success);
  2498. if (!success) {
  2499. NS_WARNING("failed to re-allocate layer transaction");
  2500. return;
  2501. }
  2502. if (!shadowManager) {
  2503. NS_WARNING("failed to re-construct LayersChild");
  2504. return;
  2505. }
  2506. RefPtr<LayerManager> lm = mPuppetWidget->RecreateLayerManager(shadowManager);
  2507. ShadowLayerForwarder* lf = lm->AsShadowForwarder();
  2508. lf->IdentifyTextureHost(mTextureFactoryIdentifier);
  2509. mApzcTreeManager = CompositorBridgeChild::Get()->GetAPZCTreeManager(mLayersId);
  2510. if (mApzcTreeManager) {
  2511. APZChild* apz = ContentProcessController::Create(mUniqueId);
  2512. CompositorBridgeChild::Get()->SendPAPZConstructor(apz, mLayersId);
  2513. }
  2514. nsCOMPtr<nsIDocument> doc(GetDocument());
  2515. doc->NotifyLayerManagerRecreated();
  2516. }
  2517. void
  2518. TabChild::CompositorUpdated(const TextureFactoryIdentifier& aNewIdentifier)
  2519. {
  2520. gfxPlatform::GetPlatform()->CompositorUpdated();
  2521. RefPtr<LayerManager> lm = mPuppetWidget->GetLayerManager();
  2522. ClientLayerManager* clm = lm->AsClientLayerManager();
  2523. MOZ_ASSERT(clm);
  2524. mTextureFactoryIdentifier = aNewIdentifier;
  2525. clm->UpdateTextureFactoryIdentifier(aNewIdentifier);
  2526. FrameLayerBuilder::InvalidateAllLayers(clm);
  2527. }
  2528. NS_IMETHODIMP
  2529. TabChild::OnShowTooltip(int32_t aXCoords, int32_t aYCoords, const char16_t *aTipText,
  2530. const char16_t *aTipDir)
  2531. {
  2532. nsString str(aTipText);
  2533. nsString dir(aTipDir);
  2534. SendShowTooltip(aXCoords, aYCoords, str, dir);
  2535. return NS_OK;
  2536. }
  2537. NS_IMETHODIMP
  2538. TabChild::OnHideTooltip()
  2539. {
  2540. SendHideTooltip();
  2541. return NS_OK;
  2542. }
  2543. bool
  2544. TabChild::RecvRequestNotifyAfterRemotePaint()
  2545. {
  2546. // Get the CompositorBridgeChild instance for this content thread.
  2547. CompositorBridgeChild* compositor = CompositorBridgeChild::Get();
  2548. // Tell the CompositorBridgeChild that, when it gets a RemotePaintIsReady
  2549. // message that it should forward it us so that we can bounce it to our
  2550. // RenderFrameParent.
  2551. compositor->RequestNotifyAfterRemotePaint(this);
  2552. return true;
  2553. }
  2554. bool
  2555. TabChild::RecvUIResolutionChanged(const float& aDpi,
  2556. const int32_t& aRounding,
  2557. const double& aScale)
  2558. {
  2559. ScreenIntSize oldScreenSize = GetInnerSize();
  2560. mDPI = 0;
  2561. mRounding = 0;
  2562. mDefaultScale = 0;
  2563. static_cast<PuppetWidget*>(mPuppetWidget.get())->UpdateBackingScaleCache(aDpi, aRounding, aScale);
  2564. nsCOMPtr<nsIDocument> document(GetDocument());
  2565. nsCOMPtr<nsIPresShell> presShell = document->GetShell();
  2566. if (presShell) {
  2567. RefPtr<nsPresContext> presContext = presShell->GetPresContext();
  2568. if (presContext) {
  2569. presContext->UIResolutionChangedSync();
  2570. }
  2571. }
  2572. ScreenIntSize screenSize = GetInnerSize();
  2573. if (mHasValidInnerSize && oldScreenSize != screenSize) {
  2574. ScreenIntRect screenRect = GetOuterRect();
  2575. mPuppetWidget->Resize(screenRect.x + mClientOffset.x + mChromeDisp.x,
  2576. screenRect.y + mClientOffset.y + mChromeDisp.y,
  2577. screenSize.width, screenSize.height, true);
  2578. nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(WebNavigation());
  2579. baseWin->SetPositionAndSize(0, 0, screenSize.width, screenSize.height,
  2580. nsIBaseWindow::eRepaint);
  2581. }
  2582. return true;
  2583. }
  2584. bool
  2585. TabChild::RecvThemeChanged(nsTArray<LookAndFeelInt>&& aLookAndFeelIntCache)
  2586. {
  2587. LookAndFeel::SetIntCache(aLookAndFeelIntCache);
  2588. nsCOMPtr<nsIDocument> document(GetDocument());
  2589. nsCOMPtr<nsIPresShell> presShell = document->GetShell();
  2590. if (presShell) {
  2591. RefPtr<nsPresContext> presContext = presShell->GetPresContext();
  2592. if (presContext) {
  2593. presContext->ThemeChanged();
  2594. }
  2595. }
  2596. return true;
  2597. }
  2598. bool
  2599. TabChild::RecvSetFreshProcess()
  2600. {
  2601. mIsFreshProcess = true;
  2602. return true;
  2603. }
  2604. mozilla::plugins::PPluginWidgetChild*
  2605. TabChild::AllocPPluginWidgetChild()
  2606. {
  2607. return new mozilla::plugins::PluginWidgetChild();
  2608. }
  2609. bool
  2610. TabChild::DeallocPPluginWidgetChild(mozilla::plugins::PPluginWidgetChild* aActor)
  2611. {
  2612. delete aActor;
  2613. return true;
  2614. }
  2615. nsresult
  2616. TabChild::CreatePluginWidget(nsIWidget* aParent, nsIWidget** aOut)
  2617. {
  2618. *aOut = nullptr;
  2619. mozilla::plugins::PluginWidgetChild* child =
  2620. static_cast<mozilla::plugins::PluginWidgetChild*>(SendPPluginWidgetConstructor());
  2621. if (!child) {
  2622. NS_ERROR("couldn't create PluginWidgetChild");
  2623. return NS_ERROR_UNEXPECTED;
  2624. }
  2625. nsCOMPtr<nsIWidget> pluginWidget = nsIWidget::CreatePluginProxyWidget(this, child);
  2626. if (!pluginWidget) {
  2627. NS_ERROR("couldn't create PluginWidgetProxy");
  2628. return NS_ERROR_UNEXPECTED;
  2629. }
  2630. nsWidgetInitData initData;
  2631. initData.mWindowType = eWindowType_plugin_ipc_content;
  2632. initData.mUnicode = false;
  2633. initData.clipChildren = true;
  2634. initData.clipSiblings = true;
  2635. nsresult rv = pluginWidget->Create(aParent, nullptr,
  2636. LayoutDeviceIntRect(0, 0, 0, 0),
  2637. &initData);
  2638. if (NS_FAILED(rv)) {
  2639. NS_WARNING("Creating native plugin widget on the chrome side failed.");
  2640. }
  2641. pluginWidget.forget(aOut);
  2642. return rv;
  2643. }
  2644. ScreenIntSize
  2645. TabChild::GetInnerSize()
  2646. {
  2647. LayoutDeviceIntSize innerSize =
  2648. RoundedToInt(mUnscaledInnerSize * mPuppetWidget->GetDefaultScale());
  2649. return ViewAs<ScreenPixel>(innerSize, PixelCastJustification::LayoutDeviceIsScreenForTabDims);
  2650. };
  2651. ScreenIntRect
  2652. TabChild::GetOuterRect()
  2653. {
  2654. LayoutDeviceIntRect outerRect =
  2655. RoundedToInt(mUnscaledOuterRect * mPuppetWidget->GetDefaultScale());
  2656. return ViewAs<ScreenPixel>(outerRect, PixelCastJustification::LayoutDeviceIsScreenForTabDims);
  2657. }
  2658. void
  2659. TabChild::ForcePaint(uint64_t aLayerObserverEpoch)
  2660. {
  2661. if (!IPCOpen()) {
  2662. // Don't bother doing anything now. Better to wait until we receive the
  2663. // message on the PContent channel.
  2664. return;
  2665. }
  2666. nsAutoScriptBlocker scriptBlocker;
  2667. RecvSetDocShellIsActive(true, false, aLayerObserverEpoch);
  2668. }
  2669. /*******************************************************************************
  2670. * nsISHistoryListener
  2671. ******************************************************************************/
  2672. NS_IMETHODIMP
  2673. TabChildSHistoryListener::OnHistoryNewEntry(nsIURI *aNewURI, int32_t aOldIndex)
  2674. {
  2675. return NS_ERROR_NOT_IMPLEMENTED;
  2676. }
  2677. NS_IMETHODIMP
  2678. TabChildSHistoryListener::OnHistoryGoBack(nsIURI *aBackURI, bool *_retval)
  2679. {
  2680. return NS_ERROR_NOT_IMPLEMENTED;
  2681. }
  2682. NS_IMETHODIMP
  2683. TabChildSHistoryListener::OnHistoryGoForward(nsIURI *aForwardURI, bool *_retval)
  2684. {
  2685. return NS_ERROR_NOT_IMPLEMENTED;
  2686. }
  2687. NS_IMETHODIMP
  2688. TabChildSHistoryListener::OnHistoryReload(nsIURI *aReloadURI, uint32_t aReloadFlags, bool *_retval)
  2689. {
  2690. return NS_ERROR_NOT_IMPLEMENTED;
  2691. }
  2692. NS_IMETHODIMP
  2693. TabChildSHistoryListener::OnHistoryGotoIndex(int32_t aIndex, nsIURI *aGotoURI, bool *_retval)
  2694. {
  2695. return NS_ERROR_NOT_IMPLEMENTED;
  2696. }
  2697. NS_IMETHODIMP
  2698. TabChildSHistoryListener::OnHistoryPurge(int32_t aNumEntries, bool *_retval)
  2699. {
  2700. return NS_ERROR_NOT_IMPLEMENTED;
  2701. }
  2702. NS_IMETHODIMP
  2703. TabChildSHistoryListener::OnHistoryReplaceEntry(int32_t aIndex)
  2704. {
  2705. return NS_ERROR_NOT_IMPLEMENTED;
  2706. }
  2707. NS_IMETHODIMP
  2708. TabChildSHistoryListener::OnLengthChange(int32_t aCount)
  2709. {
  2710. RefPtr<TabChild> tabChild(mTabChild);
  2711. if (!tabChild) {
  2712. return NS_ERROR_FAILURE;
  2713. }
  2714. if (aCount < 0) {
  2715. return NS_ERROR_FAILURE;
  2716. }
  2717. return tabChild->SendNotifySessionHistoryChange(aCount) ?
  2718. NS_OK : NS_ERROR_FAILURE;
  2719. }
  2720. NS_IMETHODIMP
  2721. TabChildSHistoryListener::OnRequestCrossBrowserNavigation(uint32_t aIndex)
  2722. {
  2723. RefPtr<TabChild> tabChild(mTabChild);
  2724. if (!tabChild) {
  2725. return NS_ERROR_FAILURE;
  2726. }
  2727. return tabChild->SendRequestCrossBrowserNavigation(aIndex) ?
  2728. NS_OK : NS_ERROR_FAILURE;
  2729. }
  2730. TabChildGlobal::TabChildGlobal(TabChildBase* aTabChild)
  2731. : mTabChild(aTabChild)
  2732. {
  2733. SetIsNotDOMBinding();
  2734. }
  2735. TabChildGlobal::~TabChildGlobal()
  2736. {
  2737. }
  2738. void
  2739. TabChildGlobal::Init()
  2740. {
  2741. NS_ASSERTION(!mMessageManager, "Re-initializing?!?");
  2742. mMessageManager = new nsFrameMessageManager(mTabChild,
  2743. nullptr,
  2744. MM_CHILD);
  2745. }
  2746. NS_IMPL_CYCLE_COLLECTION_CLASS(TabChildGlobal)
  2747. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(TabChildGlobal,
  2748. DOMEventTargetHelper)
  2749. NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessageManager);
  2750. NS_IMPL_CYCLE_COLLECTION_UNLINK(mTabChild);
  2751. tmp->UnlinkHostObjectURIs();
  2752. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  2753. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(TabChildGlobal,
  2754. DOMEventTargetHelper)
  2755. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageManager)
  2756. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTabChild)
  2757. tmp->TraverseHostObjectURIs(cb);
  2758. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  2759. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TabChildGlobal)
  2760. NS_INTERFACE_MAP_ENTRY(nsIMessageListenerManager)
  2761. NS_INTERFACE_MAP_ENTRY(nsIMessageSender)
  2762. NS_INTERFACE_MAP_ENTRY(nsISyncMessageSender)
  2763. NS_INTERFACE_MAP_ENTRY(nsIContentFrameMessageManager)
  2764. NS_INTERFACE_MAP_ENTRY(nsIScriptObjectPrincipal)
  2765. NS_INTERFACE_MAP_ENTRY(nsIGlobalObject)
  2766. NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
  2767. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(ContentFrameMessageManager)
  2768. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  2769. NS_IMPL_ADDREF_INHERITED(TabChildGlobal, DOMEventTargetHelper)
  2770. NS_IMPL_RELEASE_INHERITED(TabChildGlobal, DOMEventTargetHelper)
  2771. // This method isn't automatically forwarded safely because it's notxpcom, so
  2772. // the IDL binding doesn't know what value to return.
  2773. NS_IMETHODIMP_(bool)
  2774. TabChildGlobal::MarkForCC()
  2775. {
  2776. if (mTabChild) {
  2777. mTabChild->MarkScopesForCC();
  2778. }
  2779. EventListenerManager* elm = GetExistingListenerManager();
  2780. if (elm) {
  2781. elm->MarkForCC();
  2782. }
  2783. return mMessageManager ? mMessageManager->MarkForCC() : false;
  2784. }
  2785. NS_IMETHODIMP
  2786. TabChildGlobal::GetContent(mozIDOMWindowProxy** aContent)
  2787. {
  2788. *aContent = nullptr;
  2789. if (!mTabChild)
  2790. return NS_ERROR_NULL_POINTER;
  2791. nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(mTabChild->WebNavigation());
  2792. window.forget(aContent);
  2793. return NS_OK;
  2794. }
  2795. NS_IMETHODIMP
  2796. TabChildGlobal::GetDocShell(nsIDocShell** aDocShell)
  2797. {
  2798. *aDocShell = nullptr;
  2799. if (!mTabChild)
  2800. return NS_ERROR_NULL_POINTER;
  2801. nsCOMPtr<nsIDocShell> docShell = do_GetInterface(mTabChild->WebNavigation());
  2802. docShell.swap(*aDocShell);
  2803. return NS_OK;
  2804. }
  2805. nsIPrincipal*
  2806. TabChildGlobal::GetPrincipal()
  2807. {
  2808. if (!mTabChild)
  2809. return nullptr;
  2810. return mTabChild->GetPrincipal();
  2811. }
  2812. JSObject*
  2813. TabChildGlobal::GetGlobalJSObject()
  2814. {
  2815. NS_ENSURE_TRUE(mTabChild, nullptr);
  2816. nsCOMPtr<nsIXPConnectJSObjectHolder> ref = mTabChild->GetGlobal();
  2817. NS_ENSURE_TRUE(ref, nullptr);
  2818. return ref->GetJSObject();
  2819. }