TabParent.cpp 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272
  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 "TabParent.h"
  7. #include "AudioChannelService.h"
  8. #include "AppProcessChecker.h"
  9. #include "mozIApplication.h"
  10. #ifdef ACCESSIBILITY
  11. #include "mozilla/a11y/DocAccessibleParent.h"
  12. #include "nsAccessibilityService.h"
  13. #endif
  14. #include "mozilla/BrowserElementParent.h"
  15. #include "mozilla/dom/ContentBridgeParent.h"
  16. #include "mozilla/dom/ContentParent.h"
  17. #include "mozilla/dom/DataTransfer.h"
  18. #include "mozilla/dom/Event.h"
  19. #include "mozilla/dom/indexedDB/ActorsParent.h"
  20. #include "mozilla/plugins/PluginWidgetParent.h"
  21. #include "mozilla/EventStateManager.h"
  22. #include "mozilla/gfx/2D.h"
  23. #include "mozilla/gfx/DataSurfaceHelpers.h"
  24. #include "mozilla/gfx/GPUProcessManager.h"
  25. #include "mozilla/Hal.h"
  26. #include "mozilla/IMEStateManager.h"
  27. #include "mozilla/jsipc/CrossProcessObjectWrappers.h"
  28. #include "mozilla/layers/AsyncDragMetrics.h"
  29. #include "mozilla/layers/InputAPZContext.h"
  30. #include "mozilla/layout/RenderFrameParent.h"
  31. #include "mozilla/LookAndFeel.h"
  32. #include "mozilla/MouseEvents.h"
  33. #include "mozilla/net/NeckoChild.h"
  34. #include "mozilla/Preferences.h"
  35. #include "mozilla/TextEvents.h"
  36. #include "mozilla/TouchEvents.h"
  37. #include "mozilla/UniquePtr.h"
  38. #include "mozilla/Unused.h"
  39. #include "BlobParent.h"
  40. #include "nsCOMPtr.h"
  41. #include "nsContentAreaDragDrop.h"
  42. #include "nsContentUtils.h"
  43. #include "nsDebug.h"
  44. #include "nsFocusManager.h"
  45. #include "nsFrameLoader.h"
  46. #include "nsIBaseWindow.h"
  47. #include "nsIBrowser.h"
  48. #include "nsIContent.h"
  49. #include "nsIDocShell.h"
  50. #include "nsIDocShellTreeOwner.h"
  51. #include "nsIDOMElement.h"
  52. #include "nsIDOMEvent.h"
  53. #include "nsIDOMWindow.h"
  54. #include "nsIDOMWindowUtils.h"
  55. #include "nsIInterfaceRequestorUtils.h"
  56. #include "nsILoadInfo.h"
  57. #include "nsPrincipal.h"
  58. #include "nsIPromptFactory.h"
  59. #include "nsIURI.h"
  60. #include "nsIWindowWatcher.h"
  61. #include "nsIWebBrowserChrome.h"
  62. #include "nsIXULBrowserWindow.h"
  63. #include "nsIXULWindow.h"
  64. #include "nsIRemoteBrowser.h"
  65. #include "nsViewManager.h"
  66. #include "nsVariant.h"
  67. #include "nsIWidget.h"
  68. #ifndef XP_WIN
  69. #include "nsJARProtocolHandler.h"
  70. #endif
  71. #include "nsPIDOMWindow.h"
  72. #include "nsPresShell.h"
  73. #include "nsPrintfCString.h"
  74. #include "nsServiceManagerUtils.h"
  75. #include "nsThreadUtils.h"
  76. #include "PermissionMessageUtils.h"
  77. #include "StructuredCloneData.h"
  78. #include "ColorPickerParent.h"
  79. #include "FilePickerParent.h"
  80. #include "TabChild.h"
  81. #include "LoadContext.h"
  82. #include "nsNetCID.h"
  83. #include "nsIAuthInformation.h"
  84. #include "nsIAuthPromptCallback.h"
  85. #include "nsAuthInformationHolder.h"
  86. #include "nsICancelable.h"
  87. #include "gfxPrefs.h"
  88. #include "nsILoginManagerPrompter.h"
  89. #include "nsPIWindowRoot.h"
  90. #include "nsIAuthPrompt2.h"
  91. #include "gfxDrawable.h"
  92. #include "ImageOps.h"
  93. #include "UnitTransforms.h"
  94. #include <algorithm>
  95. #include "mozilla/WebBrowserPersistDocumentParent.h"
  96. #include "nsIGroupedSHistory.h"
  97. #include "PartialSHistory.h"
  98. #if defined(XP_WIN) && defined(ACCESSIBILITY)
  99. #include "mozilla/a11y/AccessibleWrap.h"
  100. #endif
  101. using namespace mozilla::dom;
  102. using namespace mozilla::ipc;
  103. using namespace mozilla::layers;
  104. using namespace mozilla::layout;
  105. using namespace mozilla::services;
  106. using namespace mozilla::widget;
  107. using namespace mozilla::jsipc;
  108. using namespace mozilla::gfx;
  109. using mozilla::Unused;
  110. // The flags passed by the webProgress notifications are 16 bits shifted
  111. // from the ones registered by webProgressListeners.
  112. #define NOTIFY_FLAG_SHIFT 16
  113. namespace mozilla {
  114. namespace dom {
  115. TabParent::LayerToTabParentTable* TabParent::sLayerToTabParentTable = nullptr;
  116. NS_IMPL_ISUPPORTS(TabParent,
  117. nsITabParent,
  118. nsIAuthPromptProvider,
  119. nsISecureBrowserUI,
  120. nsISupportsWeakReference,
  121. nsIWebBrowserPersistable)
  122. TabParent::TabParent(nsIContentParent* aManager,
  123. const TabId& aTabId,
  124. const TabContext& aContext,
  125. uint32_t aChromeFlags)
  126. : TabContext(aContext)
  127. , mFrameElement(nullptr)
  128. , mRect(0, 0, 0, 0)
  129. , mDimensions(0, 0)
  130. , mOrientation(0)
  131. , mDPI(0)
  132. , mRounding(0)
  133. , mDefaultScale(0)
  134. , mUpdatedDimensions(false)
  135. , mSizeMode(nsSizeMode_Normal)
  136. , mManager(aManager)
  137. , mDocShellIsActive(false)
  138. , mMarkedDestroying(false)
  139. , mIsDestroyed(false)
  140. , mChromeFlags(aChromeFlags)
  141. , mDragValid(false)
  142. , mInitedByParent(false)
  143. , mTabId(aTabId)
  144. , mCreatingWindow(false)
  145. , mCursor(nsCursor(-1))
  146. , mTabSetsCursor(false)
  147. , mHasContentOpener(false)
  148. #ifdef DEBUG
  149. , mActiveSupressDisplayportCount(0)
  150. #endif
  151. , mLayerTreeEpoch(0)
  152. , mPreserveLayers(false)
  153. {
  154. MOZ_ASSERT(aManager);
  155. }
  156. TabParent::~TabParent()
  157. {
  158. }
  159. TabParent*
  160. TabParent::GetTabParentFromLayersId(uint64_t aLayersId)
  161. {
  162. if (!sLayerToTabParentTable) {
  163. return nullptr;
  164. }
  165. return sLayerToTabParentTable->Get(aLayersId);
  166. }
  167. void
  168. TabParent::AddTabParentToTable(uint64_t aLayersId, TabParent* aTabParent)
  169. {
  170. if (!sLayerToTabParentTable) {
  171. sLayerToTabParentTable = new LayerToTabParentTable();
  172. }
  173. sLayerToTabParentTable->Put(aLayersId, aTabParent);
  174. }
  175. void
  176. TabParent::RemoveTabParentFromTable(uint64_t aLayersId)
  177. {
  178. if (!sLayerToTabParentTable) {
  179. return;
  180. }
  181. sLayerToTabParentTable->Remove(aLayersId);
  182. if (sLayerToTabParentTable->Count() == 0) {
  183. delete sLayerToTabParentTable;
  184. sLayerToTabParentTable = nullptr;
  185. }
  186. }
  187. void
  188. TabParent::CacheFrameLoader(nsFrameLoader* aFrameLoader)
  189. {
  190. mFrameLoader = aFrameLoader;
  191. }
  192. /**
  193. * Will return nullptr if there is no outer window available for the
  194. * document hosting the owner element of this TabParent. Also will return
  195. * nullptr if that outer window is in the process of closing.
  196. */
  197. already_AddRefed<nsPIDOMWindowOuter>
  198. TabParent::GetParentWindowOuter()
  199. {
  200. nsCOMPtr<nsIContent> frame = do_QueryInterface(GetOwnerElement());
  201. if (!frame) {
  202. return nullptr;
  203. }
  204. nsCOMPtr<nsPIDOMWindowOuter> parent = frame->OwnerDoc()->GetWindow();
  205. if (!parent || parent->Closed()) {
  206. return nullptr;
  207. }
  208. return parent.forget();
  209. }
  210. void
  211. TabParent::SetOwnerElement(Element* aElement)
  212. {
  213. // If we held previous content then unregister for its events.
  214. RemoveWindowListeners();
  215. // If we change top-level documents then we need to change our
  216. // registration with them.
  217. RefPtr<nsPIWindowRoot> curTopLevelWin, newTopLevelWin;
  218. if (mFrameElement) {
  219. curTopLevelWin = nsContentUtils::GetWindowRoot(mFrameElement->OwnerDoc());
  220. }
  221. if (aElement) {
  222. newTopLevelWin = nsContentUtils::GetWindowRoot(aElement->OwnerDoc());
  223. }
  224. bool isSameTopLevelWin = curTopLevelWin == newTopLevelWin;
  225. if (curTopLevelWin && !isSameTopLevelWin) {
  226. curTopLevelWin->RemoveBrowser(this);
  227. }
  228. // Update to the new content, and register to listen for events from it.
  229. mFrameElement = aElement;
  230. if (newTopLevelWin && !isSameTopLevelWin) {
  231. newTopLevelWin->AddBrowser(this);
  232. }
  233. if (mFrameElement) {
  234. bool useGlobalHistory =
  235. !mFrameElement->HasAttr(kNameSpaceID_None, nsGkAtoms::disableglobalhistory);
  236. Unused << SendSetUseGlobalHistory(useGlobalHistory);
  237. }
  238. #if defined(XP_WIN) && defined(ACCESSIBILITY)
  239. if (!mIsDestroyed) {
  240. uintptr_t newWindowHandle = 0;
  241. if (nsCOMPtr<nsIWidget> widget = GetWidget()) {
  242. newWindowHandle =
  243. reinterpret_cast<uintptr_t>(widget->GetNativeData(NS_NATIVE_WINDOW));
  244. }
  245. Unused << SendUpdateNativeWindowHandle(newWindowHandle);
  246. }
  247. #endif
  248. AddWindowListeners();
  249. TryCacheDPIAndScale();
  250. }
  251. void
  252. TabParent::AddWindowListeners()
  253. {
  254. if (mFrameElement && mFrameElement->OwnerDoc()) {
  255. if (nsCOMPtr<nsPIDOMWindowOuter> window = mFrameElement->OwnerDoc()->GetWindow()) {
  256. nsCOMPtr<EventTarget> eventTarget = window->GetTopWindowRoot();
  257. if (eventTarget) {
  258. eventTarget->AddEventListener(NS_LITERAL_STRING("MozUpdateWindowPos"),
  259. this, false, false);
  260. }
  261. }
  262. if (nsIPresShell* shell = mFrameElement->OwnerDoc()->GetShell()) {
  263. mPresShellWithRefreshListener = shell;
  264. shell->AddPostRefreshObserver(this);
  265. }
  266. RefPtr<AudioChannelService> acs = AudioChannelService::GetOrCreate();
  267. if (acs) {
  268. acs->RegisterTabParent(this);
  269. }
  270. }
  271. }
  272. void
  273. TabParent::RemoveWindowListeners()
  274. {
  275. if (mFrameElement && mFrameElement->OwnerDoc()->GetWindow()) {
  276. nsCOMPtr<nsPIDOMWindowOuter> window = mFrameElement->OwnerDoc()->GetWindow();
  277. nsCOMPtr<EventTarget> eventTarget = window->GetTopWindowRoot();
  278. if (eventTarget) {
  279. eventTarget->RemoveEventListener(NS_LITERAL_STRING("MozUpdateWindowPos"),
  280. this, false);
  281. }
  282. }
  283. if (mPresShellWithRefreshListener) {
  284. mPresShellWithRefreshListener->RemovePostRefreshObserver(this);
  285. mPresShellWithRefreshListener = nullptr;
  286. }
  287. RefPtr<AudioChannelService> acs = AudioChannelService::GetOrCreate();
  288. if (acs) {
  289. acs->UnregisterTabParent(this);
  290. }
  291. }
  292. void
  293. TabParent::DidRefresh()
  294. {
  295. if (mChromeOffset != -GetChildProcessOffset()) {
  296. UpdatePosition();
  297. }
  298. }
  299. void
  300. TabParent::GetAppType(nsAString& aOut)
  301. {
  302. aOut.Truncate();
  303. nsCOMPtr<Element> elem = do_QueryInterface(mFrameElement);
  304. if (!elem) {
  305. return;
  306. }
  307. elem->GetAttr(kNameSpaceID_None, nsGkAtoms::mozapptype, aOut);
  308. }
  309. bool
  310. TabParent::IsVisible() const
  311. {
  312. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
  313. if (!frameLoader) {
  314. return false;
  315. }
  316. return frameLoader->GetVisible();
  317. }
  318. void
  319. TabParent::DestroyInternal()
  320. {
  321. IMEStateManager::OnTabParentDestroying(this);
  322. RemoveWindowListeners();
  323. // If this fails, it's most likely due to a content-process crash,
  324. // and auto-cleanup will kick in. Otherwise, the child side will
  325. // destroy itself and send back __delete__().
  326. Unused << SendDestroy();
  327. if (RenderFrameParent* frame = GetRenderFrame()) {
  328. RemoveTabParentFromTable(frame->GetLayersId());
  329. frame->Destroy();
  330. }
  331. // Let all PluginWidgets know we are tearing down. Prevents
  332. // these objects from sending async events after the child side
  333. // is shut down.
  334. const ManagedContainer<PPluginWidgetParent>& kids =
  335. ManagedPPluginWidgetParent();
  336. for (auto iter = kids.ConstIter(); !iter.Done(); iter.Next()) {
  337. static_cast<mozilla::plugins::PluginWidgetParent*>(
  338. iter.Get()->GetKey())->ParentDestroy();
  339. }
  340. }
  341. void
  342. TabParent::Destroy()
  343. {
  344. // Aggressively release the window to avoid leaking the world in shutdown
  345. // corner cases.
  346. mBrowserDOMWindow = nullptr;
  347. if (mIsDestroyed) {
  348. return;
  349. }
  350. DestroyInternal();
  351. mIsDestroyed = true;
  352. if (XRE_IsParentProcess()) {
  353. ContentParent::NotifyTabDestroying(this->GetTabId(), Manager()->AsContentParent()->ChildID());
  354. } else {
  355. ContentParent::NotifyTabDestroying(this->GetTabId(), Manager()->ChildID());
  356. }
  357. mMarkedDestroying = true;
  358. }
  359. bool
  360. TabParent::RecvEnsureLayersConnected()
  361. {
  362. if (RenderFrameParent* frame = GetRenderFrame()) {
  363. frame->EnsureLayersConnected();
  364. }
  365. return true;
  366. }
  367. bool
  368. TabParent::Recv__delete__()
  369. {
  370. if (XRE_IsParentProcess()) {
  371. ContentParent::DeallocateTabId(mTabId,
  372. Manager()->AsContentParent()->ChildID(),
  373. mMarkedDestroying);
  374. }
  375. else {
  376. Manager()->AsContentBridgeParent()->NotifyTabDestroyed();
  377. ContentParent::DeallocateTabId(mTabId,
  378. Manager()->ChildID(),
  379. mMarkedDestroying);
  380. }
  381. return true;
  382. }
  383. void
  384. TabParent::ActorDestroy(ActorDestroyReason why)
  385. {
  386. // Even though TabParent::Destroy calls this, we need to do it here too in
  387. // case of a crash.
  388. IMEStateManager::OnTabParentDestroying(this);
  389. // Prevent executing ContentParent::NotifyTabDestroying in
  390. // TabParent::Destroy() called by frameLoader->DestroyComplete() below
  391. // when tab crashes in contentprocess because ContentParent::ActorDestroy()
  392. // in main process will be triggered before this function
  393. // and remove the process information that
  394. // ContentParent::NotifyTabDestroying need from mContentParentMap.
  395. // When tab crashes in content process,
  396. // there is no need to call ContentParent::NotifyTabDestroying
  397. // because the jobs in ContentParent::NotifyTabDestroying
  398. // will be done by ContentParent::ActorDestroy.
  399. if (XRE_IsContentProcess() && why == AbnormalShutdown && !mIsDestroyed) {
  400. DestroyInternal();
  401. mIsDestroyed = true;
  402. }
  403. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader(true);
  404. nsCOMPtr<nsIObserverService> os = services::GetObserverService();
  405. if (frameLoader) {
  406. nsCOMPtr<Element> frameElement(mFrameElement);
  407. ReceiveMessage(CHILD_PROCESS_SHUTDOWN_MESSAGE, false, nullptr, nullptr,
  408. nullptr);
  409. frameLoader->DestroyComplete();
  410. if (why == AbnormalShutdown && os) {
  411. os->NotifyObservers(NS_ISUPPORTS_CAST(nsIFrameLoader*, frameLoader),
  412. "oop-frameloader-crashed", nullptr);
  413. nsCOMPtr<nsIFrameLoaderOwner> owner = do_QueryInterface(frameElement);
  414. if (owner) {
  415. RefPtr<nsFrameLoader> currentFrameLoader = owner->GetFrameLoader();
  416. // It's possible that the frameloader owner has already moved on
  417. // and created a new frameloader. If so, we don't fire the event,
  418. // since the frameloader owner has clearly moved on.
  419. if (currentFrameLoader == frameLoader) {
  420. nsContentUtils::DispatchTrustedEvent(frameElement->OwnerDoc(), frameElement,
  421. NS_LITERAL_STRING("oop-browser-crashed"),
  422. true, true);
  423. }
  424. }
  425. }
  426. mFrameLoader = nullptr;
  427. }
  428. if (os) {
  429. os->NotifyObservers(NS_ISUPPORTS_CAST(nsITabParent*, this), "ipc:browser-destroyed", nullptr);
  430. }
  431. }
  432. bool
  433. TabParent::RecvMoveFocus(const bool& aForward, const bool& aForDocumentNavigation)
  434. {
  435. nsCOMPtr<nsIFocusManager> fm = do_GetService(FOCUSMANAGER_CONTRACTID);
  436. if (fm) {
  437. nsCOMPtr<nsIDOMElement> dummy;
  438. uint32_t type = aForward ?
  439. (aForDocumentNavigation ? static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_FORWARDDOC) :
  440. static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_FORWARD)) :
  441. (aForDocumentNavigation ? static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_BACKWARDDOC) :
  442. static_cast<uint32_t>(nsIFocusManager::MOVEFOCUS_BACKWARD));
  443. nsCOMPtr<nsIDOMElement> frame = do_QueryInterface(mFrameElement);
  444. fm->MoveFocus(nullptr, frame, type, nsIFocusManager::FLAG_BYKEY,
  445. getter_AddRefs(dummy));
  446. }
  447. return true;
  448. }
  449. bool
  450. TabParent::RecvSizeShellTo(const uint32_t& aFlags, const int32_t& aWidth, const int32_t& aHeight,
  451. const int32_t& aShellItemWidth, const int32_t& aShellItemHeight)
  452. {
  453. NS_ENSURE_TRUE(mFrameElement, true);
  454. nsCOMPtr<nsIDocShell> docShell = mFrameElement->OwnerDoc()->GetDocShell();
  455. NS_ENSURE_TRUE(docShell, true);
  456. nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
  457. nsresult rv = docShell->GetTreeOwner(getter_AddRefs(treeOwner));
  458. NS_ENSURE_SUCCESS(rv, true);
  459. int32_t width = aWidth;
  460. int32_t height = aHeight;
  461. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CX) {
  462. width = mDimensions.width;
  463. }
  464. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CY) {
  465. height = mDimensions.height;
  466. }
  467. nsCOMPtr<nsIXULWindow> xulWin(do_GetInterface(treeOwner));
  468. NS_ENSURE_TRUE(xulWin, true);
  469. xulWin->SizeShellToWithLimit(width, height, aShellItemWidth, aShellItemHeight);
  470. return true;
  471. }
  472. bool
  473. TabParent::RecvDropLinks(nsTArray<nsString>&& aLinks)
  474. {
  475. nsCOMPtr<nsIBrowser> browser = do_QueryInterface(mFrameElement);
  476. if (browser) {
  477. UniquePtr<const char16_t*[]> links;
  478. links = MakeUnique<const char16_t*[]>(aLinks.Length());
  479. for (uint32_t i = 0; i < aLinks.Length(); i++) {
  480. links[i] = aLinks[i].get();
  481. }
  482. browser->DropLinks(aLinks.Length(), links.get());
  483. }
  484. return true;
  485. }
  486. bool
  487. TabParent::RecvEvent(const RemoteDOMEvent& aEvent)
  488. {
  489. nsCOMPtr<nsIDOMEvent> event = do_QueryInterface(aEvent.mEvent);
  490. NS_ENSURE_TRUE(event, true);
  491. nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryInterface(mFrameElement);
  492. NS_ENSURE_TRUE(target, true);
  493. event->SetOwner(target);
  494. bool dummy;
  495. target->DispatchEvent(event, &dummy);
  496. return true;
  497. }
  498. TabParent* TabParent::sNextTabParent;
  499. /* static */ TabParent*
  500. TabParent::GetNextTabParent()
  501. {
  502. TabParent* result = sNextTabParent;
  503. sNextTabParent = nullptr;
  504. return result;
  505. }
  506. bool
  507. TabParent::SendLoadRemoteScript(const nsString& aURL,
  508. const bool& aRunInGlobalScope)
  509. {
  510. if (mCreatingWindow) {
  511. mDelayedFrameScripts.AppendElement(FrameScriptInfo(aURL, aRunInGlobalScope));
  512. return true;
  513. }
  514. MOZ_ASSERT(mDelayedFrameScripts.IsEmpty());
  515. return PBrowserParent::SendLoadRemoteScript(aURL, aRunInGlobalScope);
  516. }
  517. void
  518. TabParent::LoadURL(nsIURI* aURI)
  519. {
  520. MOZ_ASSERT(aURI);
  521. if (mIsDestroyed) {
  522. return;
  523. }
  524. nsCString spec;
  525. aURI->GetSpec(spec);
  526. if (mCreatingWindow) {
  527. // Don't send the message if the child wants to load its own URL.
  528. MOZ_ASSERT(mDelayedURL.IsEmpty());
  529. mDelayedURL = spec;
  530. return;
  531. }
  532. Unused << SendLoadURL(spec, GetShowInfo());
  533. }
  534. void
  535. TabParent::Show(const ScreenIntSize& size, bool aParentIsActive)
  536. {
  537. mDimensions = size;
  538. if (mIsDestroyed) {
  539. return;
  540. }
  541. TextureFactoryIdentifier textureFactoryIdentifier;
  542. uint64_t layersId = 0;
  543. bool success = false;
  544. RenderFrameParent* renderFrame = nullptr;
  545. if (IsInitedByParent()) {
  546. // If TabParent is initialized by parent side then the RenderFrame must also
  547. // be created here. If TabParent is initialized by child side,
  548. // child side will create RenderFrame.
  549. MOZ_ASSERT(!GetRenderFrame());
  550. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
  551. if (frameLoader) {
  552. renderFrame = new RenderFrameParent(frameLoader, &success);
  553. MOZ_ASSERT(success);
  554. layersId = renderFrame->GetLayersId();
  555. renderFrame->GetTextureFactoryIdentifier(&textureFactoryIdentifier);
  556. AddTabParentToTable(layersId, this);
  557. Unused << SendPRenderFrameConstructor(renderFrame);
  558. }
  559. } else {
  560. // Otherwise, the child should have constructed the RenderFrame,
  561. // and we should already know about it.
  562. MOZ_ASSERT(GetRenderFrame());
  563. }
  564. nsCOMPtr<nsISupports> container = mFrameElement->OwnerDoc()->GetContainer();
  565. nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container);
  566. nsCOMPtr<nsIWidget> mainWidget;
  567. baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
  568. mSizeMode = mainWidget ? mainWidget->SizeMode() : nsSizeMode_Normal;
  569. Unused << SendShow(size, GetShowInfo(), textureFactoryIdentifier,
  570. layersId, renderFrame, aParentIsActive, mSizeMode);
  571. }
  572. bool
  573. TabParent::RecvSetDimensions(const uint32_t& aFlags,
  574. const int32_t& aX, const int32_t& aY,
  575. const int32_t& aCx, const int32_t& aCy)
  576. {
  577. MOZ_ASSERT(!(aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER),
  578. "We should never see DIM_FLAGS_SIZE_INNER here!");
  579. NS_ENSURE_TRUE(mFrameElement, true);
  580. nsCOMPtr<nsIDocShell> docShell = mFrameElement->OwnerDoc()->GetDocShell();
  581. NS_ENSURE_TRUE(docShell, true);
  582. nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
  583. docShell->GetTreeOwner(getter_AddRefs(treeOwner));
  584. nsCOMPtr<nsIBaseWindow> treeOwnerAsWin = do_QueryInterface(treeOwner);
  585. NS_ENSURE_TRUE(treeOwnerAsWin, true);
  586. // We only care about the parameters that actually changed, see more
  587. // details in TabChild::SetDimensions.
  588. int32_t unused;
  589. int32_t x = aX;
  590. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_X) {
  591. treeOwnerAsWin->GetPosition(&x, &unused);
  592. }
  593. int32_t y = aY;
  594. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_Y) {
  595. treeOwnerAsWin->GetPosition(&unused, &y);
  596. }
  597. int32_t cx = aCx;
  598. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CX) {
  599. treeOwnerAsWin->GetSize(&cx, &unused);
  600. }
  601. int32_t cy = aCy;
  602. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_IGNORE_CY) {
  603. treeOwnerAsWin->GetSize(&unused, &cy);
  604. }
  605. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION &&
  606. aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
  607. treeOwnerAsWin->SetPositionAndSize(x, y, cx, cy,
  608. nsIBaseWindow::eRepaint);
  609. return true;
  610. }
  611. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
  612. treeOwnerAsWin->SetPosition(x, y);
  613. mUpdatedDimensions = false;
  614. UpdatePosition();
  615. return true;
  616. }
  617. if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
  618. treeOwnerAsWin->SetSize(cx, cy, true);
  619. return true;
  620. }
  621. MOZ_ASSERT(false, "Unknown flags!");
  622. return false;
  623. }
  624. nsresult
  625. TabParent::UpdatePosition()
  626. {
  627. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
  628. if (!frameLoader) {
  629. return NS_OK;
  630. }
  631. nsIntRect windowDims;
  632. NS_ENSURE_SUCCESS(frameLoader->GetWindowDimensions(windowDims), NS_ERROR_FAILURE);
  633. UpdateDimensions(windowDims, mDimensions);
  634. return NS_OK;
  635. }
  636. void
  637. TabParent::UpdateDimensions(const nsIntRect& rect, const ScreenIntSize& size)
  638. {
  639. if (mIsDestroyed) {
  640. return;
  641. }
  642. nsCOMPtr<nsIWidget> widget = GetWidget();
  643. if (!widget) {
  644. NS_WARNING("No widget found in TabParent::UpdateDimensions");
  645. return;
  646. }
  647. hal::ScreenConfiguration config;
  648. hal::GetCurrentScreenConfiguration(&config);
  649. ScreenOrientationInternal orientation = config.orientation();
  650. LayoutDeviceIntPoint clientOffset = widget->GetClientOffset();
  651. LayoutDeviceIntPoint chromeOffset = -GetChildProcessOffset();
  652. if (!mUpdatedDimensions || mOrientation != orientation ||
  653. mDimensions != size || !mRect.IsEqualEdges(rect) ||
  654. clientOffset != mClientOffset ||
  655. chromeOffset != mChromeOffset) {
  656. mUpdatedDimensions = true;
  657. mRect = rect;
  658. mDimensions = size;
  659. mOrientation = orientation;
  660. mClientOffset = clientOffset;
  661. mChromeOffset = chromeOffset;
  662. CSSToLayoutDeviceScale widgetScale = widget->GetDefaultScale();
  663. LayoutDeviceIntRect devicePixelRect =
  664. ViewAs<LayoutDevicePixel>(mRect,
  665. PixelCastJustification::LayoutDeviceIsScreenForTabDims);
  666. LayoutDeviceIntSize devicePixelSize =
  667. ViewAs<LayoutDevicePixel>(mDimensions,
  668. PixelCastJustification::LayoutDeviceIsScreenForTabDims);
  669. CSSRect unscaledRect = devicePixelRect / widgetScale;
  670. CSSSize unscaledSize = devicePixelSize / widgetScale;
  671. Unused << SendUpdateDimensions(unscaledRect, unscaledSize,
  672. orientation, clientOffset, chromeOffset);
  673. }
  674. }
  675. void
  676. TabParent::SizeModeChanged(const nsSizeMode& aSizeMode)
  677. {
  678. if (!mIsDestroyed && aSizeMode != mSizeMode) {
  679. mSizeMode = aSizeMode;
  680. Unused << SendSizeModeChanged(aSizeMode);
  681. }
  682. }
  683. void
  684. TabParent::UIResolutionChanged()
  685. {
  686. if (!mIsDestroyed) {
  687. // TryCacheDPIAndScale()'s cache is keyed off of
  688. // mDPI being greater than 0, so this invalidates it.
  689. mDPI = -1;
  690. TryCacheDPIAndScale();
  691. // If mDPI was set to -1 to invalidate it and then TryCacheDPIAndScale
  692. // fails to cache the values, then mDefaultScale.scale might be invalid.
  693. // We don't want to send that value to content. Just send -1 for it too in
  694. // that case.
  695. Unused << SendUIResolutionChanged(mDPI, mRounding,
  696. mDPI < 0 ? -1.0 : mDefaultScale.scale);
  697. }
  698. }
  699. void
  700. TabParent::ThemeChanged()
  701. {
  702. if (!mIsDestroyed) {
  703. // The theme has changed, and any cached values we had sent down
  704. // to the child have been invalidated. When this method is called,
  705. // LookAndFeel should have the up-to-date values, which we now
  706. // send down to the child. We do this for every remote tab for now,
  707. // but bug 1156934 has been filed to do it once per content process.
  708. Unused << SendThemeChanged(LookAndFeel::GetIntCache());
  709. }
  710. }
  711. void
  712. TabParent::HandleAccessKey(const WidgetKeyboardEvent& aEvent,
  713. nsTArray<uint32_t>& aCharCodes,
  714. const int32_t& aModifierMask)
  715. {
  716. if (!mIsDestroyed) {
  717. Unused << SendHandleAccessKey(aEvent, aCharCodes, aModifierMask);
  718. }
  719. }
  720. void
  721. TabParent::Activate()
  722. {
  723. if (!mIsDestroyed) {
  724. Unused << SendActivate();
  725. }
  726. }
  727. void
  728. TabParent::Deactivate()
  729. {
  730. if (!mIsDestroyed) {
  731. Unused << SendDeactivate();
  732. }
  733. }
  734. NS_IMETHODIMP
  735. TabParent::Init(mozIDOMWindowProxy *window)
  736. {
  737. return NS_OK;
  738. }
  739. NS_IMETHODIMP
  740. TabParent::GetState(uint32_t *aState)
  741. {
  742. NS_ENSURE_ARG(aState);
  743. NS_WARNING("SecurityState not valid here");
  744. *aState = 0;
  745. return NS_OK;
  746. }
  747. NS_IMETHODIMP
  748. TabParent::SetDocShell(nsIDocShell *aDocShell)
  749. {
  750. NS_ENSURE_ARG(aDocShell);
  751. NS_WARNING("No mDocShell member in TabParent so there is no docShell to set");
  752. return NS_OK;
  753. }
  754. a11y::PDocAccessibleParent*
  755. TabParent::AllocPDocAccessibleParent(PDocAccessibleParent* aParent,
  756. const uint64_t&, const uint32_t&,
  757. const IAccessibleHolder&)
  758. {
  759. #ifdef ACCESSIBILITY
  760. return new a11y::DocAccessibleParent();
  761. #else
  762. return nullptr;
  763. #endif
  764. }
  765. bool
  766. TabParent::DeallocPDocAccessibleParent(PDocAccessibleParent* aParent)
  767. {
  768. #ifdef ACCESSIBILITY
  769. delete static_cast<a11y::DocAccessibleParent*>(aParent);
  770. #endif
  771. return true;
  772. }
  773. bool
  774. TabParent::RecvPDocAccessibleConstructor(PDocAccessibleParent* aDoc,
  775. PDocAccessibleParent* aParentDoc,
  776. const uint64_t& aParentID,
  777. const uint32_t& aMsaaID,
  778. const IAccessibleHolder& aDocCOMProxy)
  779. {
  780. #ifdef ACCESSIBILITY
  781. auto doc = static_cast<a11y::DocAccessibleParent*>(aDoc);
  782. if (aParentDoc) {
  783. // A document should never directly be the parent of another document.
  784. // There should always be an outer doc accessible child of the outer
  785. // document containing the child.
  786. MOZ_ASSERT(aParentID);
  787. if (!aParentID) {
  788. return false;
  789. }
  790. auto parentDoc = static_cast<a11y::DocAccessibleParent*>(aParentDoc);
  791. bool added = parentDoc->AddChildDoc(doc, aParentID);
  792. #ifdef XP_WIN
  793. MOZ_ASSERT(aDocCOMProxy.IsNull());
  794. if (added) {
  795. a11y::WrapperFor(doc)->SetID(aMsaaID);
  796. }
  797. #endif
  798. return added;
  799. } else {
  800. // null aParentDoc means this document is at the top level in the child
  801. // process. That means it makes no sense to get an id for an accessible
  802. // that is its parent.
  803. MOZ_ASSERT(!aParentID);
  804. if (aParentID) {
  805. return false;
  806. }
  807. doc->SetTopLevel();
  808. a11y::DocManager::RemoteDocAdded(doc);
  809. #ifdef XP_WIN
  810. a11y::WrapperFor(doc)->SetID(aMsaaID);
  811. MOZ_ASSERT(!aDocCOMProxy.IsNull());
  812. RefPtr<IAccessible> proxy(aDocCOMProxy.Get());
  813. doc->SetCOMProxy(proxy);
  814. #endif
  815. }
  816. #endif
  817. return true;
  818. }
  819. a11y::DocAccessibleParent*
  820. TabParent::GetTopLevelDocAccessible() const
  821. {
  822. #ifdef ACCESSIBILITY
  823. // XXX Consider managing non top level PDocAccessibles with their parent
  824. // document accessible.
  825. const ManagedContainer<PDocAccessibleParent>& docs = ManagedPDocAccessibleParent();
  826. for (auto iter = docs.ConstIter(); !iter.Done(); iter.Next()) {
  827. auto doc = static_cast<a11y::DocAccessibleParent*>(iter.Get()->GetKey());
  828. if (!doc->ParentDoc()) {
  829. return doc;
  830. }
  831. }
  832. MOZ_ASSERT(docs.Count() == 0, "If there isn't a top level accessible doc "
  833. "there shouldn't be an accessible doc at all!");
  834. #endif
  835. return nullptr;
  836. }
  837. PFilePickerParent*
  838. TabParent::AllocPFilePickerParent(const nsString& aTitle, const int16_t& aMode)
  839. {
  840. return new FilePickerParent(aTitle, aMode);
  841. }
  842. bool
  843. TabParent::DeallocPFilePickerParent(PFilePickerParent* actor)
  844. {
  845. delete actor;
  846. return true;
  847. }
  848. auto
  849. TabParent::AllocPIndexedDBPermissionRequestParent(const Principal& aPrincipal)
  850. -> PIndexedDBPermissionRequestParent*
  851. {
  852. MOZ_ASSERT(NS_IsMainThread());
  853. nsCOMPtr<nsIPrincipal> principal(aPrincipal);
  854. if (!principal) {
  855. return nullptr;
  856. }
  857. nsCOMPtr<nsIContentParent> manager = Manager();
  858. if (manager->IsContentParent()) {
  859. if (NS_WARN_IF(!AssertAppPrincipal(manager->AsContentParent(),
  860. principal))) {
  861. return nullptr;
  862. }
  863. } else {
  864. MOZ_CRASH("Figure out security checks for bridged content!");
  865. }
  866. if (NS_WARN_IF(!mFrameElement)) {
  867. return nullptr;
  868. }
  869. return
  870. mozilla::dom::indexedDB::AllocPIndexedDBPermissionRequestParent(mFrameElement,
  871. principal);
  872. }
  873. bool
  874. TabParent::RecvPIndexedDBPermissionRequestConstructor(
  875. PIndexedDBPermissionRequestParent* aActor,
  876. const Principal& aPrincipal)
  877. {
  878. MOZ_ASSERT(NS_IsMainThread());
  879. MOZ_ASSERT(aActor);
  880. return
  881. mozilla::dom::indexedDB::RecvPIndexedDBPermissionRequestConstructor(aActor);
  882. }
  883. bool
  884. TabParent::DeallocPIndexedDBPermissionRequestParent(
  885. PIndexedDBPermissionRequestParent* aActor)
  886. {
  887. MOZ_ASSERT(NS_IsMainThread());
  888. MOZ_ASSERT(aActor);
  889. return
  890. mozilla::dom::indexedDB::DeallocPIndexedDBPermissionRequestParent(aActor);
  891. }
  892. void
  893. TabParent::SendMouseEvent(const nsAString& aType, float aX, float aY,
  894. int32_t aButton, int32_t aClickCount,
  895. int32_t aModifiers, bool aIgnoreRootScrollFrame)
  896. {
  897. if (!mIsDestroyed) {
  898. Unused << PBrowserParent::SendMouseEvent(nsString(aType), aX, aY,
  899. aButton, aClickCount,
  900. aModifiers, aIgnoreRootScrollFrame);
  901. }
  902. }
  903. void
  904. TabParent::SendKeyEvent(const nsAString& aType,
  905. int32_t aKeyCode,
  906. int32_t aCharCode,
  907. int32_t aModifiers,
  908. bool aPreventDefault)
  909. {
  910. if (!mIsDestroyed) {
  911. Unused << PBrowserParent::SendKeyEvent(nsString(aType), aKeyCode, aCharCode,
  912. aModifiers, aPreventDefault);
  913. }
  914. }
  915. bool TabParent::SendRealMouseEvent(WidgetMouseEvent& event)
  916. {
  917. if (mIsDestroyed) {
  918. return false;
  919. }
  920. event.mRefPoint += GetChildProcessOffset();
  921. nsCOMPtr<nsIWidget> widget = GetWidget();
  922. if (widget) {
  923. // When we mouseenter the tab, the tab's cursor should
  924. // become the current cursor. When we mouseexit, we stop.
  925. if (eMouseEnterIntoWidget == event.mMessage) {
  926. mTabSetsCursor = true;
  927. if (mCustomCursor) {
  928. widget->SetCursor(mCustomCursor, mCustomCursorHotspotX, mCustomCursorHotspotY);
  929. } else if (mCursor != nsCursor(-1)) {
  930. widget->SetCursor(mCursor);
  931. }
  932. } else if (eMouseExitFromWidget == event.mMessage) {
  933. mTabSetsCursor = false;
  934. }
  935. }
  936. ScrollableLayerGuid guid;
  937. uint64_t blockId;
  938. ApzAwareEventRoutingToChild(&guid, &blockId, nullptr);
  939. if (eMouseMove == event.mMessage) {
  940. if (event.mReason == WidgetMouseEvent::eSynthesized) {
  941. return SendSynthMouseMoveEvent(event, guid, blockId);
  942. } else {
  943. return SendRealMouseMoveEvent(event, guid, blockId);
  944. }
  945. }
  946. return SendRealMouseButtonEvent(event, guid, blockId);
  947. }
  948. LayoutDeviceToCSSScale
  949. TabParent::GetLayoutDeviceToCSSScale()
  950. {
  951. nsCOMPtr<nsIContent> content = do_QueryInterface(mFrameElement);
  952. nsIDocument* doc = (content ? content->OwnerDoc() : nullptr);
  953. nsIPresShell* shell = (doc ? doc->GetShell() : nullptr);
  954. nsPresContext* ctx = (shell ? shell->GetPresContext() : nullptr);
  955. return LayoutDeviceToCSSScale(ctx
  956. ? (float)ctx->AppUnitsPerDevPixel() / nsPresContext::AppUnitsPerCSSPixel()
  957. : 0.0f);
  958. }
  959. bool
  960. TabParent::SendRealDragEvent(WidgetDragEvent& event, uint32_t aDragAction,
  961. uint32_t aDropEffect)
  962. {
  963. if (mIsDestroyed) {
  964. return false;
  965. }
  966. event.mRefPoint += GetChildProcessOffset();
  967. return PBrowserParent::SendRealDragEvent(event, aDragAction, aDropEffect);
  968. }
  969. LayoutDevicePoint TabParent::AdjustTapToChildWidget(const LayoutDevicePoint& aPoint)
  970. {
  971. return aPoint + LayoutDevicePoint(GetChildProcessOffset());
  972. }
  973. bool TabParent::SendMouseWheelEvent(WidgetWheelEvent& event)
  974. {
  975. if (mIsDestroyed) {
  976. return false;
  977. }
  978. ScrollableLayerGuid guid;
  979. uint64_t blockId;
  980. ApzAwareEventRoutingToChild(&guid, &blockId, nullptr);
  981. event.mRefPoint += GetChildProcessOffset();
  982. return PBrowserParent::SendMouseWheelEvent(event, guid, blockId);
  983. }
  984. bool TabParent::RecvDispatchWheelEvent(const mozilla::WidgetWheelEvent& aEvent)
  985. {
  986. nsCOMPtr<nsIWidget> widget = GetWidget();
  987. if (!widget) {
  988. return true;
  989. }
  990. WidgetWheelEvent localEvent(aEvent);
  991. localEvent.mWidget = widget;
  992. localEvent.mRefPoint -= GetChildProcessOffset();
  993. widget->DispatchInputEvent(&localEvent);
  994. return true;
  995. }
  996. bool
  997. TabParent::RecvDispatchMouseEvent(const mozilla::WidgetMouseEvent& aEvent)
  998. {
  999. nsCOMPtr<nsIWidget> widget = GetWidget();
  1000. if (!widget) {
  1001. return true;
  1002. }
  1003. WidgetMouseEvent localEvent(aEvent);
  1004. localEvent.mWidget = widget;
  1005. localEvent.mRefPoint -= GetChildProcessOffset();
  1006. widget->DispatchInputEvent(&localEvent);
  1007. return true;
  1008. }
  1009. bool
  1010. TabParent::RecvDispatchKeyboardEvent(const mozilla::WidgetKeyboardEvent& aEvent)
  1011. {
  1012. nsCOMPtr<nsIWidget> widget = GetWidget();
  1013. if (!widget) {
  1014. return true;
  1015. }
  1016. WidgetKeyboardEvent localEvent(aEvent);
  1017. localEvent.mWidget = widget;
  1018. localEvent.mRefPoint -= GetChildProcessOffset();
  1019. widget->DispatchInputEvent(&localEvent);
  1020. return true;
  1021. }
  1022. static void
  1023. DoCommandCallback(mozilla::Command aCommand, void* aData)
  1024. {
  1025. static_cast<InfallibleTArray<mozilla::CommandInt>*>(aData)->AppendElement(aCommand);
  1026. }
  1027. bool
  1028. TabParent::RecvRequestNativeKeyBindings(const WidgetKeyboardEvent& aEvent,
  1029. MaybeNativeKeyBinding* aBindings)
  1030. {
  1031. AutoTArray<mozilla::CommandInt, 4> singleLine;
  1032. AutoTArray<mozilla::CommandInt, 4> multiLine;
  1033. AutoTArray<mozilla::CommandInt, 4> richText;
  1034. *aBindings = mozilla::void_t();
  1035. nsCOMPtr<nsIWidget> widget = GetWidget();
  1036. if (!widget) {
  1037. return true;
  1038. }
  1039. WidgetKeyboardEvent localEvent(aEvent);
  1040. if (NS_FAILED(widget->AttachNativeKeyEvent(localEvent))) {
  1041. return true;
  1042. }
  1043. widget->ExecuteNativeKeyBinding(nsIWidget::NativeKeyBindingsForSingleLineEditor,
  1044. localEvent, DoCommandCallback, &singleLine);
  1045. widget->ExecuteNativeKeyBinding(nsIWidget::NativeKeyBindingsForMultiLineEditor,
  1046. localEvent, DoCommandCallback, &multiLine);
  1047. widget->ExecuteNativeKeyBinding(nsIWidget::NativeKeyBindingsForRichTextEditor,
  1048. localEvent, DoCommandCallback, &richText);
  1049. if (!singleLine.IsEmpty() || !multiLine.IsEmpty() || !richText.IsEmpty()) {
  1050. *aBindings = NativeKeyBinding(singleLine, multiLine, richText);
  1051. }
  1052. return true;
  1053. }
  1054. class SynthesizedEventObserver : public nsIObserver
  1055. {
  1056. NS_DECL_ISUPPORTS
  1057. public:
  1058. SynthesizedEventObserver(TabParent* aTabParent, const uint64_t& aObserverId)
  1059. : mTabParent(aTabParent)
  1060. , mObserverId(aObserverId)
  1061. {
  1062. MOZ_ASSERT(mTabParent);
  1063. }
  1064. NS_IMETHOD Observe(nsISupports* aSubject,
  1065. const char* aTopic,
  1066. const char16_t* aData) override
  1067. {
  1068. if (!mTabParent || !mObserverId) {
  1069. // We already sent the notification, or we don't actually need to
  1070. // send any notification at all.
  1071. return NS_OK;
  1072. }
  1073. if (!mTabParent->SendNativeSynthesisResponse(mObserverId, nsCString(aTopic))) {
  1074. NS_WARNING("Unable to send native event synthesization response!");
  1075. }
  1076. // Null out tabparent to indicate we already sent the response
  1077. mTabParent = nullptr;
  1078. return NS_OK;
  1079. }
  1080. private:
  1081. virtual ~SynthesizedEventObserver() { }
  1082. RefPtr<TabParent> mTabParent;
  1083. uint64_t mObserverId;
  1084. };
  1085. NS_IMPL_ISUPPORTS(SynthesizedEventObserver, nsIObserver)
  1086. class MOZ_STACK_CLASS AutoSynthesizedEventResponder
  1087. {
  1088. public:
  1089. AutoSynthesizedEventResponder(TabParent* aTabParent,
  1090. const uint64_t& aObserverId,
  1091. const char* aTopic)
  1092. : mObserver(new SynthesizedEventObserver(aTabParent, aObserverId))
  1093. , mTopic(aTopic)
  1094. { }
  1095. ~AutoSynthesizedEventResponder()
  1096. {
  1097. // This may be a no-op if the observer already sent a response.
  1098. mObserver->Observe(nullptr, mTopic, nullptr);
  1099. }
  1100. nsIObserver* GetObserver()
  1101. {
  1102. return mObserver;
  1103. }
  1104. private:
  1105. nsCOMPtr<nsIObserver> mObserver;
  1106. const char* mTopic;
  1107. };
  1108. bool
  1109. TabParent::RecvSynthesizeNativeKeyEvent(const int32_t& aNativeKeyboardLayout,
  1110. const int32_t& aNativeKeyCode,
  1111. const uint32_t& aModifierFlags,
  1112. const nsString& aCharacters,
  1113. const nsString& aUnmodifiedCharacters,
  1114. const uint64_t& aObserverId)
  1115. {
  1116. AutoSynthesizedEventResponder responder(this, aObserverId, "keyevent");
  1117. nsCOMPtr<nsIWidget> widget = GetWidget();
  1118. if (widget) {
  1119. widget->SynthesizeNativeKeyEvent(aNativeKeyboardLayout, aNativeKeyCode,
  1120. aModifierFlags, aCharacters, aUnmodifiedCharacters,
  1121. responder.GetObserver());
  1122. }
  1123. return true;
  1124. }
  1125. bool
  1126. TabParent::RecvSynthesizeNativeMouseEvent(const LayoutDeviceIntPoint& aPoint,
  1127. const uint32_t& aNativeMessage,
  1128. const uint32_t& aModifierFlags,
  1129. const uint64_t& aObserverId)
  1130. {
  1131. AutoSynthesizedEventResponder responder(this, aObserverId, "mouseevent");
  1132. nsCOMPtr<nsIWidget> widget = GetWidget();
  1133. if (widget) {
  1134. widget->SynthesizeNativeMouseEvent(aPoint, aNativeMessage, aModifierFlags,
  1135. responder.GetObserver());
  1136. }
  1137. return true;
  1138. }
  1139. bool
  1140. TabParent::RecvSynthesizeNativeMouseMove(const LayoutDeviceIntPoint& aPoint,
  1141. const uint64_t& aObserverId)
  1142. {
  1143. AutoSynthesizedEventResponder responder(this, aObserverId, "mousemove");
  1144. nsCOMPtr<nsIWidget> widget = GetWidget();
  1145. if (widget) {
  1146. widget->SynthesizeNativeMouseMove(aPoint, responder.GetObserver());
  1147. }
  1148. return true;
  1149. }
  1150. bool
  1151. TabParent::RecvSynthesizeNativeMouseScrollEvent(const LayoutDeviceIntPoint& aPoint,
  1152. const uint32_t& aNativeMessage,
  1153. const double& aDeltaX,
  1154. const double& aDeltaY,
  1155. const double& aDeltaZ,
  1156. const uint32_t& aModifierFlags,
  1157. const uint32_t& aAdditionalFlags,
  1158. const uint64_t& aObserverId)
  1159. {
  1160. AutoSynthesizedEventResponder responder(this, aObserverId, "mousescrollevent");
  1161. nsCOMPtr<nsIWidget> widget = GetWidget();
  1162. if (widget) {
  1163. widget->SynthesizeNativeMouseScrollEvent(aPoint, aNativeMessage,
  1164. aDeltaX, aDeltaY, aDeltaZ, aModifierFlags, aAdditionalFlags,
  1165. responder.GetObserver());
  1166. }
  1167. return true;
  1168. }
  1169. bool
  1170. TabParent::RecvSynthesizeNativeTouchPoint(const uint32_t& aPointerId,
  1171. const TouchPointerState& aPointerState,
  1172. const LayoutDeviceIntPoint& aPoint,
  1173. const double& aPointerPressure,
  1174. const uint32_t& aPointerOrientation,
  1175. const uint64_t& aObserverId)
  1176. {
  1177. AutoSynthesizedEventResponder responder(this, aObserverId, "touchpoint");
  1178. nsCOMPtr<nsIWidget> widget = GetWidget();
  1179. if (widget) {
  1180. widget->SynthesizeNativeTouchPoint(aPointerId, aPointerState, aPoint,
  1181. aPointerPressure, aPointerOrientation, responder.GetObserver());
  1182. }
  1183. return true;
  1184. }
  1185. bool
  1186. TabParent::RecvSynthesizeNativeTouchTap(const LayoutDeviceIntPoint& aPoint,
  1187. const bool& aLongTap,
  1188. const uint64_t& aObserverId)
  1189. {
  1190. AutoSynthesizedEventResponder responder(this, aObserverId, "touchtap");
  1191. nsCOMPtr<nsIWidget> widget = GetWidget();
  1192. if (widget) {
  1193. widget->SynthesizeNativeTouchTap(aPoint, aLongTap, responder.GetObserver());
  1194. }
  1195. return true;
  1196. }
  1197. bool
  1198. TabParent::RecvClearNativeTouchSequence(const uint64_t& aObserverId)
  1199. {
  1200. AutoSynthesizedEventResponder responder(this, aObserverId, "cleartouch");
  1201. nsCOMPtr<nsIWidget> widget = GetWidget();
  1202. if (widget) {
  1203. widget->ClearNativeTouchSequence(responder.GetObserver());
  1204. }
  1205. return true;
  1206. }
  1207. bool TabParent::SendRealKeyEvent(WidgetKeyboardEvent& event)
  1208. {
  1209. if (mIsDestroyed) {
  1210. return false;
  1211. }
  1212. event.mRefPoint += GetChildProcessOffset();
  1213. MaybeNativeKeyBinding bindings;
  1214. bindings = void_t();
  1215. if (event.mMessage == eKeyPress) {
  1216. nsCOMPtr<nsIWidget> widget = GetWidget();
  1217. AutoTArray<mozilla::CommandInt, 4> singleLine;
  1218. AutoTArray<mozilla::CommandInt, 4> multiLine;
  1219. AutoTArray<mozilla::CommandInt, 4> richText;
  1220. widget->ExecuteNativeKeyBinding(nsIWidget::NativeKeyBindingsForSingleLineEditor,
  1221. event, DoCommandCallback, &singleLine);
  1222. widget->ExecuteNativeKeyBinding(nsIWidget::NativeKeyBindingsForMultiLineEditor,
  1223. event, DoCommandCallback, &multiLine);
  1224. widget->ExecuteNativeKeyBinding(nsIWidget::NativeKeyBindingsForRichTextEditor,
  1225. event, DoCommandCallback, &richText);
  1226. if (!singleLine.IsEmpty() || !multiLine.IsEmpty() || !richText.IsEmpty()) {
  1227. bindings = NativeKeyBinding(singleLine, multiLine, richText);
  1228. }
  1229. }
  1230. return PBrowserParent::SendRealKeyEvent(event, bindings);
  1231. }
  1232. bool TabParent::SendRealTouchEvent(WidgetTouchEvent& event)
  1233. {
  1234. if (mIsDestroyed) {
  1235. return false;
  1236. }
  1237. // PresShell::HandleEventInternal adds touches on touch end/cancel. This
  1238. // confuses remote content and the panning and zooming logic into thinking
  1239. // that the added touches are part of the touchend/cancel, when actually
  1240. // they're not.
  1241. if (event.mMessage == eTouchEnd || event.mMessage == eTouchCancel) {
  1242. for (int i = event.mTouches.Length() - 1; i >= 0; i--) {
  1243. if (!event.mTouches[i]->mChanged) {
  1244. event.mTouches.RemoveElementAt(i);
  1245. }
  1246. }
  1247. }
  1248. ScrollableLayerGuid guid;
  1249. uint64_t blockId;
  1250. nsEventStatus apzResponse;
  1251. ApzAwareEventRoutingToChild(&guid, &blockId, &apzResponse);
  1252. if (mIsDestroyed) {
  1253. return false;
  1254. }
  1255. LayoutDeviceIntPoint offset = GetChildProcessOffset();
  1256. for (uint32_t i = 0; i < event.mTouches.Length(); i++) {
  1257. event.mTouches[i]->mRefPoint += offset;
  1258. }
  1259. return (event.mMessage == eTouchMove) ?
  1260. PBrowserParent::SendRealTouchMoveEvent(event, guid, blockId, apzResponse) :
  1261. PBrowserParent::SendRealTouchEvent(event, guid, blockId, apzResponse);
  1262. }
  1263. bool
  1264. TabParent::SendHandleTap(TapType aType,
  1265. const LayoutDevicePoint& aPoint,
  1266. Modifiers aModifiers,
  1267. const ScrollableLayerGuid& aGuid,
  1268. uint64_t aInputBlockId)
  1269. {
  1270. if (mIsDestroyed) {
  1271. return false;
  1272. }
  1273. if ((aType == TapType::eSingleTap || aType == TapType::eSecondTap) &&
  1274. GetRenderFrame()) {
  1275. GetRenderFrame()->TakeFocusForClickFromTap();
  1276. }
  1277. LayoutDeviceIntPoint offset = GetChildProcessOffset();
  1278. return PBrowserParent::SendHandleTap(aType, aPoint + offset, aModifiers, aGuid,
  1279. aInputBlockId);
  1280. }
  1281. bool
  1282. TabParent::RecvSyncMessage(const nsString& aMessage,
  1283. const ClonedMessageData& aData,
  1284. InfallibleTArray<CpowEntry>&& aCpows,
  1285. const IPC::Principal& aPrincipal,
  1286. nsTArray<StructuredCloneData>* aRetVal)
  1287. {
  1288. // FIXME Permission check for TabParent in Content process
  1289. nsIPrincipal* principal = aPrincipal;
  1290. if (Manager()->IsContentParent()) {
  1291. ContentParent* parent = Manager()->AsContentParent();
  1292. if (!ContentParent::IgnoreIPCPrincipal() &&
  1293. parent && principal && !AssertAppPrincipal(parent, principal)) {
  1294. return false;
  1295. }
  1296. }
  1297. StructuredCloneData data;
  1298. ipc::UnpackClonedMessageDataForParent(aData, data);
  1299. CrossProcessCpowHolder cpows(Manager(), aCpows);
  1300. return ReceiveMessage(aMessage, true, &data, &cpows, aPrincipal, aRetVal);
  1301. }
  1302. bool
  1303. TabParent::RecvRpcMessage(const nsString& aMessage,
  1304. const ClonedMessageData& aData,
  1305. InfallibleTArray<CpowEntry>&& aCpows,
  1306. const IPC::Principal& aPrincipal,
  1307. nsTArray<StructuredCloneData>* aRetVal)
  1308. {
  1309. // FIXME Permission check for TabParent in Content process
  1310. nsIPrincipal* principal = aPrincipal;
  1311. if (Manager()->IsContentParent()) {
  1312. ContentParent* parent = Manager()->AsContentParent();
  1313. if (!ContentParent::IgnoreIPCPrincipal() &&
  1314. parent && principal && !AssertAppPrincipal(parent, principal)) {
  1315. return false;
  1316. }
  1317. }
  1318. StructuredCloneData data;
  1319. ipc::UnpackClonedMessageDataForParent(aData, data);
  1320. CrossProcessCpowHolder cpows(Manager(), aCpows);
  1321. return ReceiveMessage(aMessage, true, &data, &cpows, aPrincipal, aRetVal);
  1322. }
  1323. bool
  1324. TabParent::RecvAsyncMessage(const nsString& aMessage,
  1325. InfallibleTArray<CpowEntry>&& aCpows,
  1326. const IPC::Principal& aPrincipal,
  1327. const ClonedMessageData& aData)
  1328. {
  1329. // FIXME Permission check for TabParent in Content process
  1330. nsIPrincipal* principal = aPrincipal;
  1331. if (Manager()->IsContentParent()) {
  1332. ContentParent* parent = Manager()->AsContentParent();
  1333. if (!ContentParent::IgnoreIPCPrincipal() &&
  1334. parent && principal && !AssertAppPrincipal(parent, principal)) {
  1335. return false;
  1336. }
  1337. }
  1338. StructuredCloneData data;
  1339. ipc::UnpackClonedMessageDataForParent(aData, data);
  1340. CrossProcessCpowHolder cpows(Manager(), aCpows);
  1341. return ReceiveMessage(aMessage, false, &data, &cpows, aPrincipal, nullptr);
  1342. }
  1343. bool
  1344. TabParent::RecvSetCursor(const uint32_t& aCursor, const bool& aForce)
  1345. {
  1346. mCursor = static_cast<nsCursor>(aCursor);
  1347. mCustomCursor = nullptr;
  1348. nsCOMPtr<nsIWidget> widget = GetWidget();
  1349. if (widget) {
  1350. if (aForce) {
  1351. widget->ClearCachedCursor();
  1352. }
  1353. if (mTabSetsCursor) {
  1354. widget->SetCursor(mCursor);
  1355. }
  1356. }
  1357. return true;
  1358. }
  1359. bool
  1360. TabParent::RecvSetCustomCursor(const nsCString& aCursorData,
  1361. const uint32_t& aWidth,
  1362. const uint32_t& aHeight,
  1363. const uint32_t& aStride,
  1364. const uint8_t& aFormat,
  1365. const uint32_t& aHotspotX,
  1366. const uint32_t& aHotspotY,
  1367. const bool& aForce)
  1368. {
  1369. mCursor = nsCursor(-1);
  1370. nsCOMPtr<nsIWidget> widget = GetWidget();
  1371. if (widget) {
  1372. if (aForce) {
  1373. widget->ClearCachedCursor();
  1374. }
  1375. if (mTabSetsCursor) {
  1376. const gfx::IntSize size(aWidth, aHeight);
  1377. RefPtr<gfx::DataSourceSurface> customCursor =
  1378. gfx::CreateDataSourceSurfaceFromData(size,
  1379. static_cast<gfx::SurfaceFormat>(aFormat),
  1380. reinterpret_cast<const uint8_t*>(aCursorData.BeginReading()),
  1381. aStride);
  1382. RefPtr<gfxDrawable> drawable = new gfxSurfaceDrawable(customCursor, size);
  1383. nsCOMPtr<imgIContainer> cursorImage(image::ImageOps::CreateFromDrawable(drawable));
  1384. widget->SetCursor(cursorImage, aHotspotX, aHotspotY);
  1385. mCustomCursor = cursorImage;
  1386. mCustomCursorHotspotX = aHotspotX;
  1387. mCustomCursorHotspotY = aHotspotY;
  1388. }
  1389. }
  1390. return true;
  1391. }
  1392. nsIXULBrowserWindow*
  1393. TabParent::GetXULBrowserWindow()
  1394. {
  1395. if (!mFrameElement) {
  1396. return nullptr;
  1397. }
  1398. nsCOMPtr<nsIDocShell> docShell = mFrameElement->OwnerDoc()->GetDocShell();
  1399. if (!docShell) {
  1400. return nullptr;
  1401. }
  1402. nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
  1403. docShell->GetTreeOwner(getter_AddRefs(treeOwner));
  1404. if (!treeOwner) {
  1405. return nullptr;
  1406. }
  1407. nsCOMPtr<nsIXULWindow> window = do_GetInterface(treeOwner);
  1408. if (!window) {
  1409. return nullptr;
  1410. }
  1411. nsCOMPtr<nsIXULBrowserWindow> xulBrowserWindow;
  1412. window->GetXULBrowserWindow(getter_AddRefs(xulBrowserWindow));
  1413. return xulBrowserWindow;
  1414. }
  1415. bool
  1416. TabParent::RecvSetStatus(const uint32_t& aType, const nsString& aStatus)
  1417. {
  1418. nsCOMPtr<nsIXULBrowserWindow> xulBrowserWindow = GetXULBrowserWindow();
  1419. if (!xulBrowserWindow) {
  1420. return true;
  1421. }
  1422. switch (aType) {
  1423. case nsIWebBrowserChrome::STATUS_SCRIPT:
  1424. xulBrowserWindow->SetJSStatus(aStatus);
  1425. break;
  1426. case nsIWebBrowserChrome::STATUS_LINK:
  1427. xulBrowserWindow->SetOverLink(aStatus, nullptr);
  1428. break;
  1429. }
  1430. return true;
  1431. }
  1432. bool
  1433. TabParent::RecvShowTooltip(const uint32_t& aX, const uint32_t& aY, const nsString& aTooltip,
  1434. const nsString& aDirection)
  1435. {
  1436. nsCOMPtr<nsIXULBrowserWindow> xulBrowserWindow = GetXULBrowserWindow();
  1437. if (!xulBrowserWindow) {
  1438. return true;
  1439. }
  1440. xulBrowserWindow->ShowTooltip(aX, aY, aTooltip, aDirection);
  1441. return true;
  1442. }
  1443. bool
  1444. TabParent::RecvHideTooltip()
  1445. {
  1446. nsCOMPtr<nsIXULBrowserWindow> xulBrowserWindow = GetXULBrowserWindow();
  1447. if (!xulBrowserWindow) {
  1448. return true;
  1449. }
  1450. xulBrowserWindow->HideTooltip();
  1451. return true;
  1452. }
  1453. bool
  1454. TabParent::RecvNotifyIMEFocus(const ContentCache& aContentCache,
  1455. const IMENotification& aIMENotification,
  1456. nsIMEUpdatePreference* aPreference)
  1457. {
  1458. nsCOMPtr<nsIWidget> widget = GetWidget();
  1459. if (!widget) {
  1460. *aPreference = nsIMEUpdatePreference();
  1461. return true;
  1462. }
  1463. mContentCache.AssignContent(aContentCache, widget, &aIMENotification);
  1464. IMEStateManager::NotifyIME(aIMENotification, widget, true);
  1465. if (aIMENotification.mMessage == NOTIFY_IME_OF_FOCUS) {
  1466. *aPreference = widget->GetIMEUpdatePreference();
  1467. }
  1468. return true;
  1469. }
  1470. bool
  1471. TabParent::RecvNotifyIMETextChange(const ContentCache& aContentCache,
  1472. const IMENotification& aIMENotification)
  1473. {
  1474. nsCOMPtr<nsIWidget> widget = GetWidget();
  1475. if (!widget)
  1476. return true;
  1477. #ifdef DEBUG
  1478. nsIMEUpdatePreference updatePreference = widget->GetIMEUpdatePreference();
  1479. NS_ASSERTION(updatePreference.WantTextChange(),
  1480. "Don't call Send/RecvNotifyIMETextChange without NOTIFY_TEXT_CHANGE");
  1481. #endif
  1482. mContentCache.AssignContent(aContentCache, widget, &aIMENotification);
  1483. mContentCache.MaybeNotifyIME(widget, aIMENotification);
  1484. return true;
  1485. }
  1486. bool
  1487. TabParent::RecvNotifyIMECompositionUpdate(
  1488. const ContentCache& aContentCache,
  1489. const IMENotification& aIMENotification)
  1490. {
  1491. nsCOMPtr<nsIWidget> widget = GetWidget();
  1492. if (!widget) {
  1493. return true;
  1494. }
  1495. mContentCache.AssignContent(aContentCache, widget, &aIMENotification);
  1496. mContentCache.MaybeNotifyIME(widget, aIMENotification);
  1497. return true;
  1498. }
  1499. bool
  1500. TabParent::RecvNotifyIMESelection(const ContentCache& aContentCache,
  1501. const IMENotification& aIMENotification)
  1502. {
  1503. nsCOMPtr<nsIWidget> widget = GetWidget();
  1504. if (!widget)
  1505. return true;
  1506. mContentCache.AssignContent(aContentCache, widget, &aIMENotification);
  1507. mContentCache.MaybeNotifyIME(widget, aIMENotification);
  1508. return true;
  1509. }
  1510. bool
  1511. TabParent::RecvUpdateContentCache(const ContentCache& aContentCache)
  1512. {
  1513. nsCOMPtr<nsIWidget> widget = GetWidget();
  1514. if (!widget) {
  1515. return true;
  1516. }
  1517. mContentCache.AssignContent(aContentCache, widget);
  1518. return true;
  1519. }
  1520. bool
  1521. TabParent::RecvNotifyIMEMouseButtonEvent(
  1522. const IMENotification& aIMENotification,
  1523. bool* aConsumedByIME)
  1524. {
  1525. nsCOMPtr<nsIWidget> widget = GetWidget();
  1526. if (!widget) {
  1527. *aConsumedByIME = false;
  1528. return true;
  1529. }
  1530. nsresult rv = IMEStateManager::NotifyIME(aIMENotification, widget, true);
  1531. *aConsumedByIME = rv == NS_SUCCESS_EVENT_CONSUMED;
  1532. return true;
  1533. }
  1534. bool
  1535. TabParent::RecvNotifyIMEPositionChange(const ContentCache& aContentCache,
  1536. const IMENotification& aIMENotification)
  1537. {
  1538. nsCOMPtr<nsIWidget> widget = GetWidget();
  1539. if (!widget) {
  1540. return true;
  1541. }
  1542. mContentCache.AssignContent(aContentCache, widget, &aIMENotification);
  1543. mContentCache.MaybeNotifyIME(widget, aIMENotification);
  1544. return true;
  1545. }
  1546. bool
  1547. TabParent::RecvOnEventNeedingAckHandled(const EventMessage& aMessage)
  1548. {
  1549. // This is called when the child process receives WidgetCompositionEvent or
  1550. // WidgetSelectionEvent.
  1551. nsCOMPtr<nsIWidget> widget = GetWidget();
  1552. if (!widget) {
  1553. return true;
  1554. }
  1555. // While calling OnEventNeedingAckHandled(), TabParent *might* be destroyed
  1556. // since it may send notifications to IME.
  1557. RefPtr<TabParent> kungFuDeathGrip(this);
  1558. mContentCache.OnEventNeedingAckHandled(widget, aMessage);
  1559. return true;
  1560. }
  1561. void
  1562. TabParent::HandledWindowedPluginKeyEvent(const NativeEventData& aKeyEventData,
  1563. bool aIsConsumed)
  1564. {
  1565. DebugOnly<bool> ok =
  1566. SendHandledWindowedPluginKeyEvent(aKeyEventData, aIsConsumed);
  1567. NS_WARNING_ASSERTION(ok, "SendHandledWindowedPluginKeyEvent failed");
  1568. }
  1569. bool
  1570. TabParent::RecvOnWindowedPluginKeyEvent(const NativeEventData& aKeyEventData)
  1571. {
  1572. nsCOMPtr<nsIWidget> widget = GetWidget();
  1573. if (NS_WARN_IF(!widget)) {
  1574. // Notifies the plugin process of the key event being not consumed by us.
  1575. HandledWindowedPluginKeyEvent(aKeyEventData, false);
  1576. return true;
  1577. }
  1578. nsresult rv = widget->OnWindowedPluginKeyEvent(aKeyEventData, this);
  1579. if (NS_WARN_IF(NS_FAILED(rv))) {
  1580. // Notifies the plugin process of the key event being not consumed by us.
  1581. HandledWindowedPluginKeyEvent(aKeyEventData, false);
  1582. return true;
  1583. }
  1584. // If the key event is posted to another process, we need to wait a call
  1585. // of HandledWindowedPluginKeyEvent(). So, nothing to do here in this case.
  1586. if (rv == NS_SUCCESS_EVENT_HANDLED_ASYNCHRONOUSLY) {
  1587. return true;
  1588. }
  1589. // Otherwise, the key event is handled synchronously. Let's notify the
  1590. // plugin process of the key event's result.
  1591. bool consumed = (rv == NS_SUCCESS_EVENT_CONSUMED);
  1592. HandledWindowedPluginKeyEvent(aKeyEventData, consumed);
  1593. return true;
  1594. }
  1595. bool
  1596. TabParent::RecvRequestFocus(const bool& aCanRaise)
  1597. {
  1598. nsCOMPtr<nsIFocusManager> fm = nsFocusManager::GetFocusManager();
  1599. if (!fm) {
  1600. return true;
  1601. }
  1602. nsCOMPtr<nsIContent> content = do_QueryInterface(mFrameElement);
  1603. if (!content || !content->OwnerDoc()) {
  1604. return true;
  1605. }
  1606. uint32_t flags = nsIFocusManager::FLAG_NOSCROLL;
  1607. if (aCanRaise)
  1608. flags |= nsIFocusManager::FLAG_RAISE;
  1609. nsCOMPtr<nsIDOMElement> node = do_QueryInterface(mFrameElement);
  1610. fm->SetFocus(node, flags);
  1611. return true;
  1612. }
  1613. bool
  1614. TabParent::RecvEnableDisableCommands(const nsString& aAction,
  1615. nsTArray<nsCString>&& aEnabledCommands,
  1616. nsTArray<nsCString>&& aDisabledCommands)
  1617. {
  1618. nsCOMPtr<nsIRemoteBrowser> remoteBrowser = do_QueryInterface(mFrameElement);
  1619. if (remoteBrowser) {
  1620. UniquePtr<const char*[]> enabledCommands, disabledCommands;
  1621. if (aEnabledCommands.Length()) {
  1622. enabledCommands = MakeUnique<const char*[]>(aEnabledCommands.Length());
  1623. for (uint32_t c = 0; c < aEnabledCommands.Length(); c++) {
  1624. enabledCommands[c] = aEnabledCommands[c].get();
  1625. }
  1626. }
  1627. if (aDisabledCommands.Length()) {
  1628. disabledCommands = MakeUnique<const char*[]>(aDisabledCommands.Length());
  1629. for (uint32_t c = 0; c < aDisabledCommands.Length(); c++) {
  1630. disabledCommands[c] = aDisabledCommands[c].get();
  1631. }
  1632. }
  1633. remoteBrowser->EnableDisableCommands(aAction,
  1634. aEnabledCommands.Length(), enabledCommands.get(),
  1635. aDisabledCommands.Length(), disabledCommands.get());
  1636. }
  1637. return true;
  1638. }
  1639. NS_IMETHODIMP
  1640. TabParent::GetChildProcessOffset(int32_t* aOutCssX, int32_t* aOutCssY)
  1641. {
  1642. NS_ENSURE_ARG(aOutCssX);
  1643. NS_ENSURE_ARG(aOutCssY);
  1644. CSSPoint offset = LayoutDevicePoint(GetChildProcessOffset())
  1645. * GetLayoutDeviceToCSSScale();
  1646. *aOutCssX = offset.x;
  1647. *aOutCssY = offset.y;
  1648. return NS_OK;
  1649. }
  1650. LayoutDeviceIntPoint
  1651. TabParent::GetChildProcessOffset()
  1652. {
  1653. // The "toplevel widget" in child processes is always at position
  1654. // 0,0. Map the event coordinates to match that.
  1655. LayoutDeviceIntPoint offset(0, 0);
  1656. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
  1657. if (!frameLoader) {
  1658. return offset;
  1659. }
  1660. nsIFrame* targetFrame = frameLoader->GetPrimaryFrameOfOwningContent();
  1661. if (!targetFrame) {
  1662. return offset;
  1663. }
  1664. // Find out how far we're offset from the nearest widget.
  1665. nsCOMPtr<nsIWidget> widget = GetWidget();
  1666. if (!widget) {
  1667. return offset;
  1668. }
  1669. nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(widget,
  1670. LayoutDeviceIntPoint(0, 0),
  1671. targetFrame);
  1672. return LayoutDeviceIntPoint::FromAppUnitsToNearest(
  1673. pt, targetFrame->PresContext()->AppUnitsPerDevPixel());
  1674. }
  1675. bool
  1676. TabParent::RecvReplyKeyEvent(const WidgetKeyboardEvent& event)
  1677. {
  1678. NS_ENSURE_TRUE(mFrameElement, true);
  1679. WidgetKeyboardEvent localEvent(event);
  1680. // Mark the event as not to be dispatched to remote process again.
  1681. localEvent.StopCrossProcessForwarding();
  1682. // Here we convert the WidgetEvent that we received to an nsIDOMEvent
  1683. // to be able to dispatch it to the <browser> element as the target element.
  1684. nsIDocument* doc = mFrameElement->OwnerDoc();
  1685. nsIPresShell* presShell = doc->GetShell();
  1686. NS_ENSURE_TRUE(presShell, true);
  1687. nsPresContext* presContext = presShell->GetPresContext();
  1688. NS_ENSURE_TRUE(presContext, true);
  1689. AutoHandlingUserInputStatePusher userInpStatePusher(localEvent.IsTrusted(),
  1690. &localEvent, doc);
  1691. EventDispatcher::Dispatch(mFrameElement, presContext, &localEvent);
  1692. return true;
  1693. }
  1694. bool
  1695. TabParent::RecvDispatchAfterKeyboardEvent(const WidgetKeyboardEvent& aEvent)
  1696. {
  1697. NS_ENSURE_TRUE(mFrameElement, true);
  1698. WidgetKeyboardEvent localEvent(aEvent);
  1699. localEvent.mWidget = GetWidget();
  1700. nsIDocument* doc = mFrameElement->OwnerDoc();
  1701. nsCOMPtr<nsIPresShell> presShell = doc->GetShell();
  1702. NS_ENSURE_TRUE(presShell, true);
  1703. if (mFrameElement &&
  1704. PresShell::BeforeAfterKeyboardEventEnabled() &&
  1705. localEvent.mMessage != eKeyPress) {
  1706. presShell->DispatchAfterKeyboardEvent(mFrameElement, localEvent,
  1707. aEvent.DefaultPrevented());
  1708. }
  1709. return true;
  1710. }
  1711. bool
  1712. TabParent::RecvAccessKeyNotHandled(const WidgetKeyboardEvent& aEvent)
  1713. {
  1714. NS_ENSURE_TRUE(mFrameElement, true);
  1715. WidgetKeyboardEvent localEvent(aEvent);
  1716. localEvent.mMessage = eAccessKeyNotFound;
  1717. localEvent.mAccessKeyForwardedToChild = false;
  1718. // Here we convert the WidgetEvent that we received to an nsIDOMEvent
  1719. // to be able to dispatch it to the <browser> element as the target element.
  1720. nsIDocument* doc = mFrameElement->OwnerDoc();
  1721. nsIPresShell* presShell = doc->GetShell();
  1722. NS_ENSURE_TRUE(presShell, true);
  1723. if (presShell->CanDispatchEvent()) {
  1724. nsPresContext* presContext = presShell->GetPresContext();
  1725. NS_ENSURE_TRUE(presContext, true);
  1726. EventDispatcher::Dispatch(mFrameElement, presContext, &localEvent);
  1727. }
  1728. return true;
  1729. }
  1730. bool
  1731. TabParent::HandleQueryContentEvent(WidgetQueryContentEvent& aEvent)
  1732. {
  1733. nsCOMPtr<nsIWidget> widget = GetWidget();
  1734. if (!widget) {
  1735. return true;
  1736. }
  1737. if (NS_WARN_IF(!mContentCache.HandleQueryContentEvent(aEvent, widget)) ||
  1738. NS_WARN_IF(!aEvent.mSucceeded)) {
  1739. return true;
  1740. }
  1741. switch (aEvent.mMessage) {
  1742. case eQueryTextRect:
  1743. case eQueryCaretRect:
  1744. case eQueryEditorRect:
  1745. aEvent.mReply.mRect -= GetChildProcessOffset();
  1746. break;
  1747. default:
  1748. break;
  1749. }
  1750. return true;
  1751. }
  1752. bool
  1753. TabParent::SendCompositionEvent(WidgetCompositionEvent& event)
  1754. {
  1755. if (mIsDestroyed) {
  1756. return false;
  1757. }
  1758. if (!mContentCache.OnCompositionEvent(event)) {
  1759. return true;
  1760. }
  1761. return PBrowserParent::SendCompositionEvent(event);
  1762. }
  1763. bool
  1764. TabParent::SendSelectionEvent(WidgetSelectionEvent& event)
  1765. {
  1766. if (mIsDestroyed) {
  1767. return false;
  1768. }
  1769. nsCOMPtr<nsIWidget> widget = GetWidget();
  1770. if (!widget) {
  1771. return true;
  1772. }
  1773. mContentCache.OnSelectionEvent(event);
  1774. if (NS_WARN_IF(!PBrowserParent::SendSelectionEvent(event))) {
  1775. return false;
  1776. }
  1777. event.mSucceeded = true;
  1778. return true;
  1779. }
  1780. bool
  1781. TabParent::SendPasteTransferable(const IPCDataTransfer& aDataTransfer,
  1782. const bool& aIsPrivateData,
  1783. const IPC::Principal& aRequestingPrincipal)
  1784. {
  1785. return PBrowserParent::SendPasteTransferable(aDataTransfer,
  1786. aIsPrivateData,
  1787. aRequestingPrincipal);
  1788. }
  1789. /*static*/ TabParent*
  1790. TabParent::GetFrom(nsFrameLoader* aFrameLoader)
  1791. {
  1792. if (!aFrameLoader) {
  1793. return nullptr;
  1794. }
  1795. PBrowserParent* remoteBrowser = aFrameLoader->GetRemoteBrowser();
  1796. return static_cast<TabParent*>(remoteBrowser);
  1797. }
  1798. /*static*/ TabParent*
  1799. TabParent::GetFrom(nsIFrameLoader* aFrameLoader)
  1800. {
  1801. if (!aFrameLoader)
  1802. return nullptr;
  1803. return GetFrom(static_cast<nsFrameLoader*>(aFrameLoader));
  1804. }
  1805. /*static*/ TabParent*
  1806. TabParent::GetFrom(nsITabParent* aTabParent)
  1807. {
  1808. return static_cast<TabParent*>(aTabParent);
  1809. }
  1810. /*static*/ TabParent*
  1811. TabParent::GetFrom(PBrowserParent* aTabParent)
  1812. {
  1813. return static_cast<TabParent*>(aTabParent);
  1814. }
  1815. /*static*/ TabParent*
  1816. TabParent::GetFrom(nsIContent* aContent)
  1817. {
  1818. nsCOMPtr<nsIFrameLoaderOwner> loaderOwner = do_QueryInterface(aContent);
  1819. if (!loaderOwner) {
  1820. return nullptr;
  1821. }
  1822. RefPtr<nsFrameLoader> frameLoader = loaderOwner->GetFrameLoader();
  1823. return GetFrom(frameLoader);
  1824. }
  1825. /*static*/ TabId
  1826. TabParent::GetTabIdFrom(nsIDocShell *docShell)
  1827. {
  1828. nsCOMPtr<nsITabChild> tabChild(TabChild::GetFrom(docShell));
  1829. if (tabChild) {
  1830. return static_cast<TabChild*>(tabChild.get())->GetTabId();
  1831. }
  1832. return TabId(0);
  1833. }
  1834. RenderFrameParent*
  1835. TabParent::GetRenderFrame()
  1836. {
  1837. PRenderFrameParent* p = LoneManagedOrNullAsserts(ManagedPRenderFrameParent());
  1838. RenderFrameParent* frame = static_cast<RenderFrameParent*>(p);
  1839. return frame;
  1840. }
  1841. bool
  1842. TabParent::RecvRequestIMEToCommitComposition(const bool& aCancel,
  1843. bool* aIsCommitted,
  1844. nsString* aCommittedString)
  1845. {
  1846. nsCOMPtr<nsIWidget> widget = GetWidget();
  1847. if (!widget) {
  1848. *aIsCommitted = false;
  1849. return true;
  1850. }
  1851. *aIsCommitted =
  1852. mContentCache.RequestIMEToCommitComposition(widget, aCancel,
  1853. *aCommittedString);
  1854. return true;
  1855. }
  1856. bool
  1857. TabParent::RecvStartPluginIME(const WidgetKeyboardEvent& aKeyboardEvent,
  1858. const int32_t& aPanelX, const int32_t& aPanelY,
  1859. nsString* aCommitted)
  1860. {
  1861. nsCOMPtr<nsIWidget> widget = GetWidget();
  1862. if (!widget) {
  1863. return true;
  1864. }
  1865. widget->StartPluginIME(aKeyboardEvent,
  1866. (int32_t&)aPanelX,
  1867. (int32_t&)aPanelY,
  1868. *aCommitted);
  1869. return true;
  1870. }
  1871. bool
  1872. TabParent::RecvSetPluginFocused(const bool& aFocused)
  1873. {
  1874. nsCOMPtr<nsIWidget> widget = GetWidget();
  1875. if (!widget) {
  1876. return true;
  1877. }
  1878. widget->SetPluginFocused((bool&)aFocused);
  1879. return true;
  1880. }
  1881. bool
  1882. TabParent::RecvSetCandidateWindowForPlugin(
  1883. const CandidateWindowPosition& aPosition)
  1884. {
  1885. nsCOMPtr<nsIWidget> widget = GetWidget();
  1886. if (!widget) {
  1887. return true;
  1888. }
  1889. widget->SetCandidateWindowForPlugin(aPosition);
  1890. return true;
  1891. }
  1892. bool
  1893. TabParent::RecvDefaultProcOfPluginEvent(const WidgetPluginEvent& aEvent)
  1894. {
  1895. nsCOMPtr<nsIWidget> widget = GetWidget();
  1896. if (!widget) {
  1897. return true;
  1898. }
  1899. widget->DefaultProcOfPluginEvent(aEvent);
  1900. return true;
  1901. }
  1902. bool
  1903. TabParent::RecvGetInputContext(int32_t* aIMEEnabled,
  1904. int32_t* aIMEOpen)
  1905. {
  1906. nsCOMPtr<nsIWidget> widget = GetWidget();
  1907. if (!widget) {
  1908. *aIMEEnabled = IMEState::DISABLED;
  1909. *aIMEOpen = IMEState::OPEN_STATE_NOT_SUPPORTED;
  1910. return true;
  1911. }
  1912. InputContext context = widget->GetInputContext();
  1913. *aIMEEnabled = static_cast<int32_t>(context.mIMEState.mEnabled);
  1914. *aIMEOpen = static_cast<int32_t>(context.mIMEState.mOpen);
  1915. return true;
  1916. }
  1917. bool
  1918. TabParent::RecvSetInputContext(const int32_t& aIMEEnabled,
  1919. const int32_t& aIMEOpen,
  1920. const nsString& aType,
  1921. const nsString& aInputmode,
  1922. const nsString& aActionHint,
  1923. const int32_t& aCause,
  1924. const int32_t& aFocusChange)
  1925. {
  1926. InputContext context;
  1927. context.mIMEState.mEnabled = static_cast<IMEState::Enabled>(aIMEEnabled);
  1928. context.mIMEState.mOpen = static_cast<IMEState::Open>(aIMEOpen);
  1929. context.mHTMLInputType.Assign(aType);
  1930. context.mHTMLInputInputmode.Assign(aInputmode);
  1931. context.mActionHint.Assign(aActionHint);
  1932. context.mOrigin = InputContext::ORIGIN_CONTENT;
  1933. InputContextAction action(
  1934. static_cast<InputContextAction::Cause>(aCause),
  1935. static_cast<InputContextAction::FocusChange>(aFocusChange));
  1936. IMEStateManager::SetInputContextForChildProcess(this, context, action);
  1937. return true;
  1938. }
  1939. bool
  1940. TabParent::RecvIsParentWindowMainWidgetVisible(bool* aIsVisible)
  1941. {
  1942. nsCOMPtr<nsIContent> frame = do_QueryInterface(mFrameElement);
  1943. if (!frame)
  1944. return true;
  1945. nsCOMPtr<nsIDOMWindowUtils> windowUtils =
  1946. do_QueryInterface(frame->OwnerDoc()->GetWindow());
  1947. nsresult rv = windowUtils->GetIsParentWindowMainWidgetVisible(aIsVisible);
  1948. return NS_SUCCEEDED(rv);
  1949. }
  1950. bool
  1951. TabParent::RecvGetDPI(float* aValue)
  1952. {
  1953. TryCacheDPIAndScale();
  1954. MOZ_ASSERT(mDPI > 0,
  1955. "Must not ask for DPI before OwnerElement is received!");
  1956. *aValue = mDPI;
  1957. return true;
  1958. }
  1959. bool
  1960. TabParent::RecvGetDefaultScale(double* aValue)
  1961. {
  1962. TryCacheDPIAndScale();
  1963. MOZ_ASSERT(mDefaultScale.scale > 0,
  1964. "Must not ask for scale before OwnerElement is received!");
  1965. *aValue = mDefaultScale.scale;
  1966. return true;
  1967. }
  1968. bool
  1969. TabParent::RecvGetWidgetRounding(int32_t* aValue)
  1970. {
  1971. TryCacheDPIAndScale();
  1972. MOZ_ASSERT(mRounding > 0,
  1973. "Must not ask for rounding before OwnerElement is received!");
  1974. *aValue = mRounding;
  1975. return true;
  1976. }
  1977. bool
  1978. TabParent::RecvGetMaxTouchPoints(uint32_t* aTouchPoints)
  1979. {
  1980. nsCOMPtr<nsIWidget> widget = GetWidget();
  1981. if (widget) {
  1982. *aTouchPoints = widget->GetMaxTouchPoints();
  1983. } else {
  1984. *aTouchPoints = 0;
  1985. }
  1986. return true;
  1987. }
  1988. already_AddRefed<nsIWidget>
  1989. TabParent::GetTopLevelWidget()
  1990. {
  1991. nsCOMPtr<nsIContent> content = do_QueryInterface(mFrameElement);
  1992. if (content) {
  1993. nsIPresShell* shell = content->OwnerDoc()->GetShell();
  1994. if (shell) {
  1995. nsViewManager* vm = shell->GetViewManager();
  1996. nsCOMPtr<nsIWidget> widget;
  1997. vm->GetRootWidget(getter_AddRefs(widget));
  1998. return widget.forget();
  1999. }
  2000. }
  2001. return nullptr;
  2002. }
  2003. bool
  2004. TabParent::RecvGetWidgetNativeData(WindowsHandle* aValue)
  2005. {
  2006. *aValue = 0;
  2007. nsCOMPtr<nsIWidget> widget = GetTopLevelWidget();
  2008. if (widget) {
  2009. *aValue = reinterpret_cast<WindowsHandle>(
  2010. widget->GetNativeData(NS_NATIVE_SHAREABLE_WINDOW));
  2011. }
  2012. return true;
  2013. }
  2014. bool
  2015. TabParent::RecvSetNativeChildOfShareableWindow(const uintptr_t& aChildWindow)
  2016. {
  2017. #if defined(XP_WIN)
  2018. nsCOMPtr<nsIWidget> widget = GetTopLevelWidget();
  2019. if (widget) {
  2020. // Note that this call will probably cause a sync native message to the
  2021. // process that owns the child window.
  2022. widget->SetNativeData(NS_NATIVE_CHILD_OF_SHAREABLE_WINDOW, aChildWindow);
  2023. }
  2024. return true;
  2025. #else
  2026. NS_NOTREACHED(
  2027. "TabParent::RecvSetNativeChildOfShareableWindow not implemented!");
  2028. return false;
  2029. #endif
  2030. }
  2031. bool
  2032. TabParent::RecvDispatchFocusToTopLevelWindow()
  2033. {
  2034. nsCOMPtr<nsIWidget> widget = GetTopLevelWidget();
  2035. if (widget) {
  2036. widget->SetFocus(false);
  2037. }
  2038. return true;
  2039. }
  2040. bool
  2041. TabParent::ReceiveMessage(const nsString& aMessage,
  2042. bool aSync,
  2043. StructuredCloneData* aData,
  2044. CpowHolder* aCpows,
  2045. nsIPrincipal* aPrincipal,
  2046. nsTArray<StructuredCloneData>* aRetVal)
  2047. {
  2048. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader(true);
  2049. if (frameLoader && frameLoader->GetFrameMessageManager()) {
  2050. RefPtr<nsFrameMessageManager> manager =
  2051. frameLoader->GetFrameMessageManager();
  2052. manager->ReceiveMessage(mFrameElement,
  2053. frameLoader,
  2054. aMessage,
  2055. aSync,
  2056. aData,
  2057. aCpows,
  2058. aPrincipal,
  2059. aRetVal);
  2060. }
  2061. return true;
  2062. }
  2063. // nsIAuthPromptProvider
  2064. // This method is largely copied from nsDocShell::GetAuthPrompt
  2065. NS_IMETHODIMP
  2066. TabParent::GetAuthPrompt(uint32_t aPromptReason, const nsIID& iid,
  2067. void** aResult)
  2068. {
  2069. // we're either allowing auth, or it's a proxy request
  2070. nsresult rv;
  2071. nsCOMPtr<nsIPromptFactory> wwatch =
  2072. do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
  2073. NS_ENSURE_SUCCESS(rv, rv);
  2074. nsCOMPtr<nsPIDOMWindowOuter> window;
  2075. nsCOMPtr<nsIContent> frame = do_QueryInterface(mFrameElement);
  2076. if (frame)
  2077. window = frame->OwnerDoc()->GetWindow();
  2078. // Get an auth prompter for our window so that the parenting
  2079. // of the dialogs works as it should when using tabs.
  2080. nsCOMPtr<nsISupports> prompt;
  2081. rv = wwatch->GetPrompt(window, iid, getter_AddRefs(prompt));
  2082. NS_ENSURE_SUCCESS(rv, rv);
  2083. nsCOMPtr<nsILoginManagerPrompter> prompter = do_QueryInterface(prompt);
  2084. if (prompter) {
  2085. nsCOMPtr<nsIDOMElement> browser = do_QueryInterface(mFrameElement);
  2086. prompter->SetBrowser(browser);
  2087. }
  2088. *aResult = prompt.forget().take();
  2089. return NS_OK;
  2090. }
  2091. PColorPickerParent*
  2092. TabParent::AllocPColorPickerParent(const nsString& aTitle,
  2093. const nsString& aInitialColor)
  2094. {
  2095. return new ColorPickerParent(aTitle, aInitialColor);
  2096. }
  2097. bool
  2098. TabParent::DeallocPColorPickerParent(PColorPickerParent* actor)
  2099. {
  2100. delete actor;
  2101. return true;
  2102. }
  2103. PRenderFrameParent*
  2104. TabParent::AllocPRenderFrameParent()
  2105. {
  2106. MOZ_ASSERT(ManagedPRenderFrameParent().IsEmpty());
  2107. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
  2108. uint64_t layersId = 0;
  2109. bool success = false;
  2110. PRenderFrameParent* renderFrame =
  2111. new RenderFrameParent(frameLoader, &success);
  2112. if (success) {
  2113. RenderFrameParent* rfp = static_cast<RenderFrameParent*>(renderFrame);
  2114. layersId = rfp->GetLayersId();
  2115. AddTabParentToTable(layersId, this);
  2116. }
  2117. return renderFrame;
  2118. }
  2119. bool
  2120. TabParent::DeallocPRenderFrameParent(PRenderFrameParent* aFrame)
  2121. {
  2122. delete aFrame;
  2123. return true;
  2124. }
  2125. bool
  2126. TabParent::SetRenderFrame(PRenderFrameParent* aRFParent)
  2127. {
  2128. if (IsInitedByParent()) {
  2129. return false;
  2130. }
  2131. RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
  2132. if (!frameLoader) {
  2133. return false;
  2134. }
  2135. RenderFrameParent* renderFrame = static_cast<RenderFrameParent*>(aRFParent);
  2136. bool success = renderFrame->Init(frameLoader);
  2137. if (!success) {
  2138. return false;
  2139. }
  2140. uint64_t layersId = renderFrame->GetLayersId();
  2141. AddTabParentToTable(layersId, this);
  2142. return true;
  2143. }
  2144. bool
  2145. TabParent::GetRenderFrameInfo(TextureFactoryIdentifier* aTextureFactoryIdentifier,
  2146. uint64_t* aLayersId)
  2147. {
  2148. RenderFrameParent* rfp = GetRenderFrame();
  2149. if (!rfp) {
  2150. return false;
  2151. }
  2152. *aLayersId = rfp->GetLayersId();
  2153. rfp->GetTextureFactoryIdentifier(aTextureFactoryIdentifier);
  2154. return true;
  2155. }
  2156. bool
  2157. TabParent::RecvAudioChannelActivityNotification(const uint32_t& aAudioChannel,
  2158. const bool& aActive)
  2159. {
  2160. if (aAudioChannel >= NUMBER_OF_AUDIO_CHANNELS) {
  2161. return false;
  2162. }
  2163. nsCOMPtr<nsIObserverService> os = services::GetObserverService();
  2164. if (os) {
  2165. nsAutoCString topic;
  2166. topic.Assign("audiochannel-activity-");
  2167. topic.Append(AudioChannelService::GetAudioChannelTable()[aAudioChannel].tag);
  2168. os->NotifyObservers(NS_ISUPPORTS_CAST(nsITabParent*, this),
  2169. topic.get(),
  2170. aActive ? u"active" : u"inactive");
  2171. }
  2172. return true;
  2173. }
  2174. already_AddRefed<nsFrameLoader>
  2175. TabParent::GetFrameLoader(bool aUseCachedFrameLoaderAfterDestroy) const
  2176. {
  2177. if (mIsDestroyed && !aUseCachedFrameLoaderAfterDestroy) {
  2178. return nullptr;
  2179. }
  2180. if (mFrameLoader) {
  2181. RefPtr<nsFrameLoader> fl = mFrameLoader;
  2182. return fl.forget();
  2183. }
  2184. nsCOMPtr<nsIFrameLoaderOwner> frameLoaderOwner = do_QueryInterface(mFrameElement);
  2185. return frameLoaderOwner ? frameLoaderOwner->GetFrameLoader() : nullptr;
  2186. }
  2187. void
  2188. TabParent::TryCacheDPIAndScale()
  2189. {
  2190. if (mDPI > 0) {
  2191. return;
  2192. }
  2193. nsCOMPtr<nsIWidget> widget = GetWidget();
  2194. if (widget) {
  2195. mDPI = widget->GetDPI();
  2196. mRounding = widget->RoundsWidgetCoordinatesTo();
  2197. mDefaultScale = widget->GetDefaultScale();
  2198. }
  2199. }
  2200. already_AddRefed<nsIWidget>
  2201. TabParent::GetWidget() const
  2202. {
  2203. if (!mFrameElement) {
  2204. return nullptr;
  2205. }
  2206. nsCOMPtr<nsIWidget> widget = nsContentUtils::WidgetForDocument(mFrameElement->OwnerDoc());
  2207. return widget.forget();
  2208. }
  2209. void
  2210. TabParent::ApzAwareEventRoutingToChild(ScrollableLayerGuid* aOutTargetGuid,
  2211. uint64_t* aOutInputBlockId,
  2212. nsEventStatus* aOutApzResponse)
  2213. {
  2214. // Let the widget know that the event will be sent to the child process,
  2215. // which will (hopefully) send a confirmation notice back to APZ.
  2216. // Do this even if APZ is off since we need it for swipe gesture support on
  2217. // OS X without APZ.
  2218. InputAPZContext::SetRoutedToChildProcess();
  2219. if (AsyncPanZoomEnabled()) {
  2220. if (aOutTargetGuid) {
  2221. *aOutTargetGuid = InputAPZContext::GetTargetLayerGuid();
  2222. // There may be cases where the APZ hit-testing code came to a different
  2223. // conclusion than the main-thread hit-testing code as to where the event
  2224. // is destined. In such cases the layersId of the APZ result may not match
  2225. // the layersId of this renderframe. In such cases the main-thread hit-
  2226. // testing code "wins" so we need to update the guid to reflect this.
  2227. if (RenderFrameParent* rfp = GetRenderFrame()) {
  2228. if (aOutTargetGuid->mLayersId != rfp->GetLayersId()) {
  2229. *aOutTargetGuid = ScrollableLayerGuid(rfp->GetLayersId(), 0, FrameMetrics::NULL_SCROLL_ID);
  2230. }
  2231. }
  2232. }
  2233. if (aOutInputBlockId) {
  2234. *aOutInputBlockId = InputAPZContext::GetInputBlockId();
  2235. }
  2236. if (aOutApzResponse) {
  2237. *aOutApzResponse = InputAPZContext::GetApzResponse();
  2238. }
  2239. } else {
  2240. if (aOutInputBlockId) {
  2241. *aOutInputBlockId = 0;
  2242. }
  2243. if (aOutApzResponse) {
  2244. *aOutApzResponse = nsEventStatus_eIgnore;
  2245. }
  2246. }
  2247. }
  2248. bool
  2249. TabParent::RecvBrowserFrameOpenWindow(PBrowserParent* aOpener,
  2250. PRenderFrameParent* aRenderFrame,
  2251. const nsString& aURL,
  2252. const nsString& aName,
  2253. const nsString& aFeatures,
  2254. bool* aOutWindowOpened,
  2255. TextureFactoryIdentifier* aTextureFactoryIdentifier,
  2256. uint64_t* aLayersId)
  2257. {
  2258. BrowserElementParent::OpenWindowResult opened =
  2259. BrowserElementParent::OpenWindowOOP(TabParent::GetFrom(aOpener),
  2260. this, aRenderFrame, aURL, aName, aFeatures,
  2261. aTextureFactoryIdentifier, aLayersId);
  2262. *aOutWindowOpened = (opened == BrowserElementParent::OPEN_WINDOW_ADDED);
  2263. if (!*aOutWindowOpened) {
  2264. Destroy();
  2265. }
  2266. return true;
  2267. }
  2268. bool
  2269. TabParent::RecvRespondStartSwipeEvent(const uint64_t& aInputBlockId,
  2270. const bool& aStartSwipe)
  2271. {
  2272. if (nsCOMPtr<nsIWidget> widget = GetWidget()) {
  2273. widget->ReportSwipeStarted(aInputBlockId, aStartSwipe);
  2274. }
  2275. return true;
  2276. }
  2277. already_AddRefed<nsILoadContext>
  2278. TabParent::GetLoadContext()
  2279. {
  2280. nsCOMPtr<nsILoadContext> loadContext;
  2281. if (mLoadContext) {
  2282. loadContext = mLoadContext;
  2283. } else {
  2284. bool isPrivate = mChromeFlags & nsIWebBrowserChrome::CHROME_PRIVATE_WINDOW;
  2285. SetPrivateBrowsingAttributes(isPrivate);
  2286. loadContext = new LoadContext(GetOwnerElement(),
  2287. true /* aIsContent */,
  2288. isPrivate,
  2289. mChromeFlags & nsIWebBrowserChrome::CHROME_REMOTE_WINDOW,
  2290. OriginAttributesRef());
  2291. mLoadContext = loadContext;
  2292. }
  2293. return loadContext.forget();
  2294. }
  2295. NS_IMETHODIMP
  2296. TabParent::GetUseAsyncPanZoom(bool* useAsyncPanZoom)
  2297. {
  2298. *useAsyncPanZoom = AsyncPanZoomEnabled();
  2299. return NS_OK;
  2300. }
  2301. // defined in nsITabParent
  2302. NS_IMETHODIMP
  2303. TabParent::SetDocShellIsActive(bool isActive)
  2304. {
  2305. // Increment the epoch so that layer tree updates from previous
  2306. // SetDocShellIsActive requests are ignored.
  2307. mLayerTreeEpoch++;
  2308. // docshell is consider prerendered only if not active yet
  2309. mIsPrerendered &= !isActive;
  2310. mDocShellIsActive = isActive;
  2311. Unused << SendSetDocShellIsActive(isActive, mPreserveLayers, mLayerTreeEpoch);
  2312. // Ask the child to repaint using the PHangMonitor channel/thread (which may
  2313. // be less congested).
  2314. if (isActive) {
  2315. ContentParent* cp = Manager()->AsContentParent();
  2316. cp->ForceTabPaint(this, mLayerTreeEpoch);
  2317. }
  2318. return NS_OK;
  2319. }
  2320. NS_IMETHODIMP
  2321. TabParent::GetDocShellIsActive(bool* aIsActive)
  2322. {
  2323. *aIsActive = mDocShellIsActive;
  2324. return NS_OK;
  2325. }
  2326. NS_IMETHODIMP
  2327. TabParent::GetIsPrerendered(bool* aIsPrerendered)
  2328. {
  2329. *aIsPrerendered = mIsPrerendered;
  2330. return NS_OK;
  2331. }
  2332. NS_IMETHODIMP
  2333. TabParent::PreserveLayers(bool aPreserveLayers)
  2334. {
  2335. mPreserveLayers = aPreserveLayers;
  2336. return NS_OK;
  2337. }
  2338. NS_IMETHODIMP
  2339. TabParent::SuppressDisplayport(bool aEnabled)
  2340. {
  2341. if (IsDestroyed()) {
  2342. return NS_OK;
  2343. }
  2344. #ifdef DEBUG
  2345. if (aEnabled) {
  2346. mActiveSupressDisplayportCount++;
  2347. } else {
  2348. mActiveSupressDisplayportCount--;
  2349. }
  2350. MOZ_ASSERT(mActiveSupressDisplayportCount >= 0);
  2351. #endif
  2352. Unused << SendSuppressDisplayport(aEnabled);
  2353. return NS_OK;
  2354. }
  2355. NS_IMETHODIMP
  2356. TabParent::GetTabId(uint64_t* aId)
  2357. {
  2358. *aId = GetTabId();
  2359. return NS_OK;
  2360. }
  2361. NS_IMETHODIMP
  2362. TabParent::GetOsPid(int32_t* aId)
  2363. {
  2364. *aId = Manager()->Pid();
  2365. return NS_OK;
  2366. }
  2367. NS_IMETHODIMP
  2368. TabParent::GetHasContentOpener(bool* aResult)
  2369. {
  2370. *aResult = mHasContentOpener;
  2371. return NS_OK;
  2372. }
  2373. void
  2374. TabParent::SetHasContentOpener(bool aHasContentOpener)
  2375. {
  2376. mHasContentOpener = aHasContentOpener;
  2377. }
  2378. NS_IMETHODIMP
  2379. TabParent::NavigateByKey(bool aForward, bool aForDocumentNavigation)
  2380. {
  2381. Unused << SendNavigateByKey(aForward, aForDocumentNavigation);
  2382. return NS_OK;
  2383. }
  2384. class LayerTreeUpdateRunnable final
  2385. : public mozilla::Runnable
  2386. {
  2387. uint64_t mLayersId;
  2388. uint64_t mEpoch;
  2389. bool mActive;
  2390. public:
  2391. explicit LayerTreeUpdateRunnable(uint64_t aLayersId, uint64_t aEpoch, bool aActive)
  2392. : mLayersId(aLayersId), mEpoch(aEpoch), mActive(aActive)
  2393. {
  2394. MOZ_ASSERT(!NS_IsMainThread());
  2395. }
  2396. private:
  2397. NS_IMETHOD Run() override {
  2398. MOZ_ASSERT(NS_IsMainThread());
  2399. if (RefPtr<TabParent> tabParent = TabParent::GetTabParentFromLayersId(mLayersId)) {
  2400. tabParent->LayerTreeUpdate(mEpoch, mActive);
  2401. }
  2402. return NS_OK;
  2403. }
  2404. };
  2405. void
  2406. TabParent::LayerTreeUpdate(uint64_t aEpoch, bool aActive)
  2407. {
  2408. // Ignore updates from old epochs. They might tell us that layers are
  2409. // available when we've already sent a message to clear them. We can't trust
  2410. // the update in that case since layers could disappear anytime after that.
  2411. if (aEpoch != mLayerTreeEpoch || mIsDestroyed) {
  2412. return;
  2413. }
  2414. nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryInterface(mFrameElement);
  2415. if (!target) {
  2416. NS_WARNING("Could not locate target for layer tree message.");
  2417. return;
  2418. }
  2419. RefPtr<Event> event = NS_NewDOMEvent(mFrameElement, nullptr, nullptr);
  2420. if (aActive) {
  2421. event->InitEvent(NS_LITERAL_STRING("MozLayerTreeReady"), true, false);
  2422. } else {
  2423. event->InitEvent(NS_LITERAL_STRING("MozLayerTreeCleared"), true, false);
  2424. }
  2425. event->SetTrusted(true);
  2426. event->WidgetEventPtr()->mFlags.mOnlyChromeDispatch = true;
  2427. bool dummy;
  2428. mFrameElement->DispatchEvent(event, &dummy);
  2429. }
  2430. bool
  2431. TabParent::RecvForcePaintNoOp(const uint64_t& aLayerObserverEpoch)
  2432. {
  2433. // We sent a ForcePaint message when layers were already visible. In this
  2434. // case, we should act as if an update occurred even though we already have
  2435. // the layers.
  2436. LayerTreeUpdate(aLayerObserverEpoch, true);
  2437. return true;
  2438. }
  2439. bool
  2440. TabParent::RecvRemotePaintIsReady()
  2441. {
  2442. nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryInterface(mFrameElement);
  2443. if (!target) {
  2444. NS_WARNING("Could not locate target for MozAfterRemotePaint message.");
  2445. return true;
  2446. }
  2447. RefPtr<Event> event = NS_NewDOMEvent(mFrameElement, nullptr, nullptr);
  2448. event->InitEvent(NS_LITERAL_STRING("MozAfterRemotePaint"), false, false);
  2449. event->SetTrusted(true);
  2450. event->WidgetEventPtr()->mFlags.mOnlyChromeDispatch = true;
  2451. bool dummy;
  2452. mFrameElement->DispatchEvent(event, &dummy);
  2453. return true;
  2454. }
  2455. mozilla::plugins::PPluginWidgetParent*
  2456. TabParent::AllocPPluginWidgetParent()
  2457. {
  2458. return new mozilla::plugins::PluginWidgetParent();
  2459. }
  2460. bool
  2461. TabParent::DeallocPPluginWidgetParent(mozilla::plugins::PPluginWidgetParent* aActor)
  2462. {
  2463. delete aActor;
  2464. return true;
  2465. }
  2466. nsresult
  2467. TabParent::HandleEvent(nsIDOMEvent* aEvent)
  2468. {
  2469. nsAutoString eventType;
  2470. aEvent->GetType(eventType);
  2471. if (eventType.EqualsLiteral("MozUpdateWindowPos") && !mIsDestroyed) {
  2472. // This event is sent when the widget moved. Therefore we only update
  2473. // the position.
  2474. return UpdatePosition();
  2475. }
  2476. return NS_OK;
  2477. }
  2478. class FakeChannel final : public nsIChannel,
  2479. public nsIAuthPromptCallback,
  2480. public nsIInterfaceRequestor,
  2481. public nsILoadContext
  2482. {
  2483. public:
  2484. FakeChannel(const nsCString& aUri, uint64_t aCallbackId, Element* aElement)
  2485. : mCallbackId(aCallbackId)
  2486. , mElement(aElement)
  2487. {
  2488. NS_NewURI(getter_AddRefs(mUri), aUri);
  2489. }
  2490. NS_DECL_ISUPPORTS
  2491. #define NO_IMPL override { return NS_ERROR_NOT_IMPLEMENTED; }
  2492. NS_IMETHOD GetName(nsACString&) NO_IMPL
  2493. NS_IMETHOD IsPending(bool*) NO_IMPL
  2494. NS_IMETHOD GetStatus(nsresult*) NO_IMPL
  2495. NS_IMETHOD Cancel(nsresult) NO_IMPL
  2496. NS_IMETHOD Suspend() NO_IMPL
  2497. NS_IMETHOD Resume() NO_IMPL
  2498. NS_IMETHOD GetLoadGroup(nsILoadGroup**) NO_IMPL
  2499. NS_IMETHOD SetLoadGroup(nsILoadGroup*) NO_IMPL
  2500. NS_IMETHOD SetLoadFlags(nsLoadFlags) NO_IMPL
  2501. NS_IMETHOD GetLoadFlags(nsLoadFlags*) NO_IMPL
  2502. NS_IMETHOD GetOriginalURI(nsIURI**) NO_IMPL
  2503. NS_IMETHOD SetOriginalURI(nsIURI*) NO_IMPL
  2504. NS_IMETHOD GetURI(nsIURI** aUri) override
  2505. {
  2506. nsCOMPtr<nsIURI> copy = mUri;
  2507. copy.forget(aUri);
  2508. return NS_OK;
  2509. }
  2510. NS_IMETHOD GetOwner(nsISupports**) NO_IMPL
  2511. NS_IMETHOD SetOwner(nsISupports*) NO_IMPL
  2512. NS_IMETHOD GetLoadInfo(nsILoadInfo** aLoadInfo) override
  2513. {
  2514. nsCOMPtr<nsILoadInfo> copy = mLoadInfo;
  2515. copy.forget(aLoadInfo);
  2516. return NS_OK;
  2517. }
  2518. NS_IMETHOD SetLoadInfo(nsILoadInfo* aLoadInfo) override
  2519. {
  2520. mLoadInfo = aLoadInfo;
  2521. return NS_OK;
  2522. }
  2523. NS_IMETHOD GetNotificationCallbacks(nsIInterfaceRequestor** aRequestor) override
  2524. {
  2525. NS_ADDREF(*aRequestor = this);
  2526. return NS_OK;
  2527. }
  2528. NS_IMETHOD SetNotificationCallbacks(nsIInterfaceRequestor*) NO_IMPL
  2529. NS_IMETHOD GetSecurityInfo(nsISupports**) NO_IMPL
  2530. NS_IMETHOD GetContentType(nsACString&) NO_IMPL
  2531. NS_IMETHOD SetContentType(const nsACString&) NO_IMPL
  2532. NS_IMETHOD GetContentCharset(nsACString&) NO_IMPL
  2533. NS_IMETHOD SetContentCharset(const nsACString&) NO_IMPL
  2534. NS_IMETHOD GetContentLength(int64_t*) NO_IMPL
  2535. NS_IMETHOD SetContentLength(int64_t) NO_IMPL
  2536. NS_IMETHOD Open(nsIInputStream**) NO_IMPL
  2537. NS_IMETHOD Open2(nsIInputStream**) NO_IMPL
  2538. NS_IMETHOD AsyncOpen(nsIStreamListener*, nsISupports*) NO_IMPL
  2539. NS_IMETHOD AsyncOpen2(nsIStreamListener*) NO_IMPL
  2540. NS_IMETHOD GetContentDisposition(uint32_t*) NO_IMPL
  2541. NS_IMETHOD SetContentDisposition(uint32_t) NO_IMPL
  2542. NS_IMETHOD GetContentDispositionFilename(nsAString&) NO_IMPL
  2543. NS_IMETHOD SetContentDispositionFilename(const nsAString&) NO_IMPL
  2544. NS_IMETHOD GetContentDispositionHeader(nsACString&) NO_IMPL
  2545. NS_IMETHOD OnAuthAvailable(nsISupports *aContext, nsIAuthInformation *aAuthInfo) override;
  2546. NS_IMETHOD OnAuthCancelled(nsISupports *aContext, bool userCancel) override;
  2547. NS_IMETHOD GetInterface(const nsIID & uuid, void **result) override
  2548. {
  2549. return QueryInterface(uuid, result);
  2550. }
  2551. NS_IMETHOD GetAssociatedWindow(mozIDOMWindowProxy**) NO_IMPL
  2552. NS_IMETHOD GetTopWindow(mozIDOMWindowProxy**) NO_IMPL
  2553. NS_IMETHOD GetTopFrameElement(nsIDOMElement** aElement) override
  2554. {
  2555. nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(mElement);
  2556. elem.forget(aElement);
  2557. return NS_OK;
  2558. }
  2559. NS_IMETHOD GetNestedFrameId(uint64_t*) NO_IMPL
  2560. NS_IMETHOD GetIsContent(bool*) NO_IMPL
  2561. NS_IMETHOD GetUsePrivateBrowsing(bool*) NO_IMPL
  2562. NS_IMETHOD SetUsePrivateBrowsing(bool) NO_IMPL
  2563. NS_IMETHOD SetPrivateBrowsing(bool) NO_IMPL
  2564. NS_IMETHOD GetIsInIsolatedMozBrowserElement(bool*) NO_IMPL
  2565. NS_IMETHOD GetAppId(uint32_t*) NO_IMPL
  2566. NS_IMETHOD GetOriginAttributes(JS::MutableHandleValue) NO_IMPL
  2567. NS_IMETHOD GetUseRemoteTabs(bool*) NO_IMPL
  2568. NS_IMETHOD SetRemoteTabs(bool) NO_IMPL
  2569. NS_IMETHOD IsTrackingProtectionOn(bool*) NO_IMPL
  2570. #undef NO_IMPL
  2571. protected:
  2572. ~FakeChannel() {}
  2573. nsCOMPtr<nsIURI> mUri;
  2574. uint64_t mCallbackId;
  2575. nsCOMPtr<Element> mElement;
  2576. nsCOMPtr<nsILoadInfo> mLoadInfo;
  2577. };
  2578. NS_IMPL_ISUPPORTS(FakeChannel, nsIChannel, nsIAuthPromptCallback,
  2579. nsIRequest, nsIInterfaceRequestor, nsILoadContext);
  2580. bool
  2581. TabParent::RecvAsyncAuthPrompt(const nsCString& aUri,
  2582. const nsString& aRealm,
  2583. const uint64_t& aCallbackId)
  2584. {
  2585. nsCOMPtr<nsIAuthPrompt2> authPrompt;
  2586. GetAuthPrompt(nsIAuthPromptProvider::PROMPT_NORMAL,
  2587. NS_GET_IID(nsIAuthPrompt2),
  2588. getter_AddRefs(authPrompt));
  2589. RefPtr<FakeChannel> channel = new FakeChannel(aUri, aCallbackId, mFrameElement);
  2590. uint32_t promptFlags = nsIAuthInformation::AUTH_HOST;
  2591. RefPtr<nsAuthInformationHolder> holder =
  2592. new nsAuthInformationHolder(promptFlags, aRealm,
  2593. EmptyCString());
  2594. uint32_t level = nsIAuthPrompt2::LEVEL_NONE;
  2595. nsCOMPtr<nsICancelable> dummy;
  2596. nsresult rv =
  2597. authPrompt->AsyncPromptAuth(channel, channel, nullptr,
  2598. level, holder, getter_AddRefs(dummy));
  2599. return rv == NS_OK;
  2600. }
  2601. bool
  2602. TabParent::RecvInvokeDragSession(nsTArray<IPCDataTransfer>&& aTransfers,
  2603. const uint32_t& aAction,
  2604. const OptionalShmem& aVisualDnDData,
  2605. const uint32_t& aStride, const uint8_t& aFormat,
  2606. const LayoutDeviceIntRect& aDragRect)
  2607. {
  2608. mInitialDataTransferItems.Clear();
  2609. nsIPresShell* shell = mFrameElement->OwnerDoc()->GetShell();
  2610. if (!shell) {
  2611. if (Manager()->IsContentParent()) {
  2612. Unused << Manager()->AsContentParent()->SendEndDragSession(true, true,
  2613. LayoutDeviceIntPoint());
  2614. }
  2615. return true;
  2616. }
  2617. EventStateManager* esm = shell->GetPresContext()->EventStateManager();
  2618. for (uint32_t i = 0; i < aTransfers.Length(); ++i) {
  2619. mInitialDataTransferItems.AppendElement(mozilla::Move(aTransfers[i].items()));
  2620. }
  2621. if (Manager()->IsContentParent()) {
  2622. nsCOMPtr<nsIDragService> dragService =
  2623. do_GetService("@mozilla.org/widget/dragservice;1");
  2624. if (dragService) {
  2625. dragService->MaybeAddChildProcess(Manager()->AsContentParent());
  2626. }
  2627. }
  2628. if (aVisualDnDData.type() == OptionalShmem::Tvoid_t ||
  2629. !aVisualDnDData.get_Shmem().IsReadable() ||
  2630. aVisualDnDData.get_Shmem().Size<char>() < aDragRect.height * aStride) {
  2631. mDnDVisualization = nullptr;
  2632. } else {
  2633. mDnDVisualization =
  2634. gfx::CreateDataSourceSurfaceFromData(gfx::IntSize(aDragRect.width, aDragRect.height),
  2635. static_cast<gfx::SurfaceFormat>(aFormat),
  2636. aVisualDnDData.get_Shmem().get<uint8_t>(),
  2637. aStride);
  2638. }
  2639. mDragValid = true;
  2640. mDragRect = aDragRect;
  2641. esm->BeginTrackingRemoteDragGesture(mFrameElement);
  2642. if (aVisualDnDData.type() == OptionalShmem::TShmem) {
  2643. Unused << DeallocShmem(aVisualDnDData);
  2644. }
  2645. return true;
  2646. }
  2647. void
  2648. TabParent::AddInitialDnDDataTo(DataTransfer* aDataTransfer)
  2649. {
  2650. for (uint32_t i = 0; i < mInitialDataTransferItems.Length(); ++i) {
  2651. nsTArray<IPCDataTransferItem>& itemArray = mInitialDataTransferItems[i];
  2652. for (auto& item : itemArray) {
  2653. RefPtr<nsVariantCC> variant = new nsVariantCC();
  2654. // Special case kFilePromiseMime so that we get the right
  2655. // nsIFlavorDataProvider for it.
  2656. if (item.flavor().EqualsLiteral(kFilePromiseMime)) {
  2657. RefPtr<nsISupports> flavorDataProvider =
  2658. new nsContentAreaDragDropDataProvider();
  2659. variant->SetAsISupports(flavorDataProvider);
  2660. } else if (item.data().type() == IPCDataTransferData::TnsString) {
  2661. variant->SetAsAString(item.data().get_nsString());
  2662. } else if (item.data().type() == IPCDataTransferData::TPBlobParent) {
  2663. auto* parent = static_cast<BlobParent*>(item.data().get_PBlobParent());
  2664. RefPtr<BlobImpl> impl = parent->GetBlobImpl();
  2665. variant->SetAsISupports(impl);
  2666. } else if (item.data().type() == IPCDataTransferData::TShmem) {
  2667. if (nsContentUtils::IsFlavorImage(item.flavor())) {
  2668. // An image! Get the imgIContainer for it and set it in the variant.
  2669. nsCOMPtr<imgIContainer> imageContainer;
  2670. nsresult rv =
  2671. nsContentUtils::DataTransferItemToImage(item,
  2672. getter_AddRefs(imageContainer));
  2673. if (NS_FAILED(rv)) {
  2674. continue;
  2675. }
  2676. variant->SetAsISupports(imageContainer);
  2677. } else {
  2678. Shmem data = item.data().get_Shmem();
  2679. variant->SetAsACString(nsDependentCString(data.get<char>(), data.Size<char>()));
  2680. }
  2681. mozilla::Unused << DeallocShmem(item.data().get_Shmem());
  2682. }
  2683. // Using system principal here, since once the data is on parent process
  2684. // side, it can be handled as being from browser chrome or OS.
  2685. // We set aHidden to false, as we don't need to worry about hiding data
  2686. // from content in the parent process where there is no content.
  2687. // XXX: Nested Content Processes may change this
  2688. aDataTransfer->SetDataWithPrincipalFromOtherProcess(NS_ConvertUTF8toUTF16(item.flavor()),
  2689. variant, i,
  2690. nsContentUtils::GetSystemPrincipal(),
  2691. /* aHidden = */ false);
  2692. }
  2693. }
  2694. mInitialDataTransferItems.Clear();
  2695. }
  2696. bool
  2697. TabParent::TakeDragVisualization(RefPtr<mozilla::gfx::SourceSurface>& aSurface,
  2698. LayoutDeviceIntRect* aDragRect)
  2699. {
  2700. if (!mDragValid)
  2701. return false;
  2702. aSurface = mDnDVisualization.forget();
  2703. *aDragRect = mDragRect;
  2704. mDragValid = false;
  2705. return true;
  2706. }
  2707. bool
  2708. TabParent::AsyncPanZoomEnabled() const
  2709. {
  2710. nsCOMPtr<nsIWidget> widget = GetWidget();
  2711. return widget && widget->AsyncPanZoomEnabled();
  2712. }
  2713. NS_IMETHODIMP
  2714. TabParent::StartPersistence(uint64_t aOuterWindowID,
  2715. nsIWebBrowserPersistDocumentReceiver* aRecv)
  2716. {
  2717. nsCOMPtr<nsIContentParent> manager = Manager();
  2718. if (!manager->IsContentParent()) {
  2719. return NS_ERROR_UNEXPECTED;
  2720. }
  2721. auto* actor = new WebBrowserPersistDocumentParent();
  2722. actor->SetOnReady(aRecv);
  2723. return manager->AsContentParent()
  2724. ->SendPWebBrowserPersistDocumentConstructor(actor, this, aOuterWindowID)
  2725. ? NS_OK : NS_ERROR_FAILURE;
  2726. // (The actor will be destroyed on constructor failure.)
  2727. }
  2728. ShowInfo
  2729. TabParent::GetShowInfo()
  2730. {
  2731. TryCacheDPIAndScale();
  2732. if (mFrameElement) {
  2733. nsAutoString name;
  2734. mFrameElement->GetAttr(kNameSpaceID_None, nsGkAtoms::name, name);
  2735. bool allowFullscreen =
  2736. mFrameElement->HasAttr(kNameSpaceID_None, nsGkAtoms::allowfullscreen) ||
  2737. mFrameElement->HasAttr(kNameSpaceID_None, nsGkAtoms::mozallowfullscreen);
  2738. bool isPrivate = mFrameElement->HasAttr(kNameSpaceID_None, nsGkAtoms::mozprivatebrowsing);
  2739. bool isTransparent =
  2740. nsContentUtils::IsChromeDoc(mFrameElement->OwnerDoc()) &&
  2741. mFrameElement->HasAttr(kNameSpaceID_None, nsGkAtoms::transparent);
  2742. return ShowInfo(name, allowFullscreen, isPrivate, false,
  2743. isTransparent, mDPI, mRounding, mDefaultScale.scale);
  2744. }
  2745. return ShowInfo(EmptyString(), false, false, false,
  2746. false, mDPI, mRounding, mDefaultScale.scale);
  2747. }
  2748. void
  2749. TabParent::AudioChannelChangeNotification(nsPIDOMWindowOuter* aWindow,
  2750. AudioChannel aAudioChannel,
  2751. float aVolume,
  2752. bool aMuted)
  2753. {
  2754. if (!mFrameElement || !mFrameElement->OwnerDoc()) {
  2755. return;
  2756. }
  2757. nsCOMPtr<nsPIDOMWindowOuter> window = mFrameElement->OwnerDoc()->GetWindow();
  2758. while (window) {
  2759. if (window == aWindow) {
  2760. Unused << SendAudioChannelChangeNotification(static_cast<uint32_t>(aAudioChannel),
  2761. aVolume, aMuted);
  2762. break;
  2763. }
  2764. nsCOMPtr<nsPIDOMWindowOuter> win = window->GetScriptableParentOrNull();
  2765. if (!win) {
  2766. break;
  2767. }
  2768. window = win;
  2769. }
  2770. }
  2771. bool
  2772. TabParent::RecvGetTabCount(uint32_t* aValue)
  2773. {
  2774. *aValue = 0;
  2775. nsCOMPtr<nsIXULBrowserWindow> xulBrowserWindow = GetXULBrowserWindow();
  2776. NS_ENSURE_TRUE(xulBrowserWindow, true);
  2777. uint32_t tabCount;
  2778. nsresult rv = xulBrowserWindow->GetTabCount(&tabCount);
  2779. NS_ENSURE_SUCCESS(rv, true);
  2780. *aValue = tabCount;
  2781. return true;
  2782. }
  2783. bool
  2784. TabParent::RecvLookUpDictionary(const nsString& aText,
  2785. nsTArray<FontRange>&& aFontRangeArray,
  2786. const bool& aIsVertical,
  2787. const LayoutDeviceIntPoint& aPoint)
  2788. {
  2789. nsCOMPtr<nsIWidget> widget = GetWidget();
  2790. if (!widget) {
  2791. return true;
  2792. }
  2793. widget->LookUpDictionary(aText, aFontRangeArray, aIsVertical,
  2794. aPoint - GetChildProcessOffset());
  2795. return true;
  2796. }
  2797. bool
  2798. TabParent::RecvNotifySessionHistoryChange(const uint32_t& aCount)
  2799. {
  2800. RefPtr<nsFrameLoader> frameLoader(GetFrameLoader());
  2801. if (!frameLoader) {
  2802. // FrameLoader can be nullptr if the it is destroying.
  2803. // In this case session history change can simply be ignored.
  2804. return true;
  2805. }
  2806. nsCOMPtr<nsIPartialSHistory> partialHistory;
  2807. frameLoader->GetPartialSessionHistory(getter_AddRefs(partialHistory));
  2808. if (!partialHistory) {
  2809. // PartialSHistory is not enabled
  2810. return true;
  2811. }
  2812. partialHistory->OnSessionHistoryChange(aCount);
  2813. return true;
  2814. }
  2815. bool
  2816. TabParent::RecvRequestCrossBrowserNavigation(const uint32_t& aGlobalIndex)
  2817. {
  2818. RefPtr<nsFrameLoader> frameLoader(GetFrameLoader());
  2819. if (!frameLoader) {
  2820. // FrameLoader can be nullptr if the it is destroying.
  2821. // In this case we can ignore the request.
  2822. return true;
  2823. }
  2824. return NS_SUCCEEDED(frameLoader->RequestGroupedHistoryNavigation(aGlobalIndex));
  2825. }
  2826. NS_IMETHODIMP
  2827. FakeChannel::OnAuthAvailable(nsISupports *aContext, nsIAuthInformation *aAuthInfo)
  2828. {
  2829. nsAuthInformationHolder* holder =
  2830. static_cast<nsAuthInformationHolder*>(aAuthInfo);
  2831. if (!net::gNeckoChild->SendOnAuthAvailable(mCallbackId,
  2832. holder->User(),
  2833. holder->Password(),
  2834. holder->Domain())) {
  2835. return NS_ERROR_FAILURE;
  2836. }
  2837. return NS_OK;
  2838. }
  2839. NS_IMETHODIMP
  2840. FakeChannel::OnAuthCancelled(nsISupports *aContext, bool userCancel)
  2841. {
  2842. if (!net::gNeckoChild->SendOnAuthCancelled(mCallbackId, userCancel)) {
  2843. return NS_ERROR_FAILURE;
  2844. }
  2845. return NS_OK;
  2846. }
  2847. } // namespace dom
  2848. } // namespace mozilla