nsDocShellTreeOwner.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  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. // Local Includes
  6. #include "nsDocShellTreeOwner.h"
  7. #include "nsWebBrowser.h"
  8. // Helper Classes
  9. #include "nsStyleCoord.h"
  10. #include "nsSize.h"
  11. #include "mozilla/ReflowInput.h"
  12. #include "nsIServiceManager.h"
  13. #include "nsComponentManagerUtils.h"
  14. #include "nsXPIDLString.h"
  15. #include "nsIAtom.h"
  16. #include "nsReadableUtils.h"
  17. #include "nsUnicharUtils.h"
  18. #include "nsISimpleEnumerator.h"
  19. #include "mozilla/LookAndFeel.h"
  20. // Interfaces needed to be included
  21. #include "nsPresContext.h"
  22. #include "nsIContextMenuListener.h"
  23. #include "nsIContextMenuListener2.h"
  24. #include "nsITooltipListener.h"
  25. #include "nsIDOMNode.h"
  26. #include "nsIDOMNodeList.h"
  27. #include "nsIDOMDocument.h"
  28. #include "nsIDOMDocumentType.h"
  29. #include "nsIDOMElement.h"
  30. #include "Link.h"
  31. #include "mozilla/dom/Element.h"
  32. #include "mozilla/dom/SVGTitleElement.h"
  33. #include "nsIDOMEvent.h"
  34. #include "nsIDOMFileList.h"
  35. #include "nsIDOMMouseEvent.h"
  36. #include "nsIFormControl.h"
  37. #include "nsIDOMHTMLInputElement.h"
  38. #include "nsIDOMHTMLTextAreaElement.h"
  39. #include "nsIDOMHTMLHtmlElement.h"
  40. #include "nsIDOMHTMLAppletElement.h"
  41. #include "nsIDOMHTMLObjectElement.h"
  42. #include "nsIDOMHTMLEmbedElement.h"
  43. #include "nsIDOMHTMLDocument.h"
  44. #include "nsIImageLoadingContent.h"
  45. #include "nsIWebNavigation.h"
  46. #include "nsIDOMHTMLElement.h"
  47. #include "nsIPresShell.h"
  48. #include "nsIStringBundle.h"
  49. #include "nsPIDOMWindow.h"
  50. #include "nsPIWindowRoot.h"
  51. #include "nsIDOMWindowCollection.h"
  52. #include "nsIWindowWatcher.h"
  53. #include "nsPIWindowWatcher.h"
  54. #include "nsIPrompt.h"
  55. #include "nsITabParent.h"
  56. #include "nsITabChild.h"
  57. #include "nsRect.h"
  58. #include "nsIWebBrowserChromeFocus.h"
  59. #include "nsIContent.h"
  60. #include "imgIContainer.h"
  61. #include "nsContextMenuInfo.h"
  62. #include "nsPresContext.h"
  63. #include "nsViewManager.h"
  64. #include "nsView.h"
  65. #include "nsIDOMDragEvent.h"
  66. #include "nsIConstraintValidation.h"
  67. #include "mozilla/Attributes.h"
  68. #include "mozilla/EventListenerManager.h"
  69. #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
  70. #include "mozilla/dom/File.h" // for input type=file
  71. #include "mozilla/dom/FileList.h" // for input type=file
  72. using namespace mozilla;
  73. using namespace mozilla::dom;
  74. // A helper routine that navigates the tricky path from a |nsWebBrowser| to
  75. // a |EventTarget| via the window root and chrome event handler.
  76. static nsresult
  77. GetDOMEventTarget(nsWebBrowser* aInBrowser, EventTarget** aTarget)
  78. {
  79. if (!aInBrowser) {
  80. return NS_ERROR_INVALID_POINTER;
  81. }
  82. nsCOMPtr<mozIDOMWindowProxy> domWindow;
  83. aInBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
  84. if (!domWindow) {
  85. return NS_ERROR_FAILURE;
  86. }
  87. auto* outerWindow = nsPIDOMWindowOuter::From(domWindow);
  88. nsPIDOMWindowOuter* rootWindow = outerWindow->GetPrivateRoot();
  89. NS_ENSURE_TRUE(rootWindow, NS_ERROR_FAILURE);
  90. nsCOMPtr<EventTarget> target = rootWindow->GetChromeEventHandler();
  91. NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
  92. target.forget(aTarget);
  93. return NS_OK;
  94. }
  95. nsDocShellTreeOwner::nsDocShellTreeOwner()
  96. : mWebBrowser(nullptr)
  97. , mTreeOwner(nullptr)
  98. , mPrimaryContentShell(nullptr)
  99. , mWebBrowserChrome(nullptr)
  100. , mOwnerWin(nullptr)
  101. , mOwnerRequestor(nullptr)
  102. {
  103. }
  104. nsDocShellTreeOwner::~nsDocShellTreeOwner()
  105. {
  106. RemoveChromeListeners();
  107. }
  108. NS_IMPL_ADDREF(nsDocShellTreeOwner)
  109. NS_IMPL_RELEASE(nsDocShellTreeOwner)
  110. NS_INTERFACE_MAP_BEGIN(nsDocShellTreeOwner)
  111. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDocShellTreeOwner)
  112. NS_INTERFACE_MAP_ENTRY(nsIDocShellTreeOwner)
  113. NS_INTERFACE_MAP_ENTRY(nsIBaseWindow)
  114. NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
  115. NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
  116. NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
  117. NS_INTERFACE_MAP_ENTRY(nsICDocShellTreeOwner)
  118. NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
  119. NS_INTERFACE_MAP_END
  120. //*****************************************************************************
  121. // nsDocShellTreeOwner::nsIInterfaceRequestor
  122. //*****************************************************************************
  123. NS_IMETHODIMP
  124. nsDocShellTreeOwner::GetInterface(const nsIID& aIID, void** aSink)
  125. {
  126. NS_ENSURE_ARG_POINTER(aSink);
  127. if (NS_SUCCEEDED(QueryInterface(aIID, aSink))) {
  128. return NS_OK;
  129. }
  130. if (aIID.Equals(NS_GET_IID(nsIWebBrowserChromeFocus))) {
  131. if (mWebBrowserChromeWeak != nullptr) {
  132. return mWebBrowserChromeWeak->QueryReferent(aIID, aSink);
  133. }
  134. return mOwnerWin->QueryInterface(aIID, aSink);
  135. }
  136. if (aIID.Equals(NS_GET_IID(nsIPrompt))) {
  137. nsCOMPtr<nsIPrompt> prompt;
  138. EnsurePrompter();
  139. prompt = mPrompter;
  140. if (prompt) {
  141. prompt.forget(aSink);
  142. return NS_OK;
  143. }
  144. return NS_NOINTERFACE;
  145. }
  146. if (aIID.Equals(NS_GET_IID(nsIAuthPrompt))) {
  147. nsCOMPtr<nsIAuthPrompt> prompt;
  148. EnsureAuthPrompter();
  149. prompt = mAuthPrompter;
  150. if (prompt) {
  151. prompt.forget(aSink);
  152. return NS_OK;
  153. }
  154. return NS_NOINTERFACE;
  155. }
  156. nsCOMPtr<nsIInterfaceRequestor> req = GetOwnerRequestor();
  157. if (req) {
  158. return req->GetInterface(aIID, aSink);
  159. }
  160. return NS_NOINTERFACE;
  161. }
  162. //*****************************************************************************
  163. // nsDocShellTreeOwner::nsIDocShellTreeOwner
  164. //*****************************************************************************
  165. void
  166. nsDocShellTreeOwner::EnsurePrompter()
  167. {
  168. if (mPrompter) {
  169. return;
  170. }
  171. nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
  172. if (wwatch && mWebBrowser) {
  173. nsCOMPtr<mozIDOMWindowProxy> domWindow;
  174. mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
  175. if (domWindow) {
  176. wwatch->GetNewPrompter(domWindow, getter_AddRefs(mPrompter));
  177. }
  178. }
  179. }
  180. void
  181. nsDocShellTreeOwner::EnsureAuthPrompter()
  182. {
  183. if (mAuthPrompter) {
  184. return;
  185. }
  186. nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
  187. if (wwatch && mWebBrowser) {
  188. nsCOMPtr<mozIDOMWindowProxy> domWindow;
  189. mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
  190. if (domWindow) {
  191. wwatch->GetNewAuthPrompter(domWindow, getter_AddRefs(mAuthPrompter));
  192. }
  193. }
  194. }
  195. void
  196. nsDocShellTreeOwner::AddToWatcher()
  197. {
  198. if (mWebBrowser) {
  199. nsCOMPtr<mozIDOMWindowProxy> domWindow;
  200. mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
  201. if (domWindow) {
  202. nsCOMPtr<nsPIWindowWatcher> wwatch(
  203. do_GetService(NS_WINDOWWATCHER_CONTRACTID));
  204. if (wwatch) {
  205. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome = GetWebBrowserChrome();
  206. if (webBrowserChrome) {
  207. wwatch->AddWindow(domWindow, webBrowserChrome);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. void
  214. nsDocShellTreeOwner::RemoveFromWatcher()
  215. {
  216. if (mWebBrowser) {
  217. nsCOMPtr<mozIDOMWindowProxy> domWindow;
  218. mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
  219. if (domWindow) {
  220. nsCOMPtr<nsPIWindowWatcher> wwatch(
  221. do_GetService(NS_WINDOWWATCHER_CONTRACTID));
  222. if (wwatch) {
  223. wwatch->RemoveWindow(domWindow);
  224. }
  225. }
  226. }
  227. }
  228. void
  229. nsDocShellTreeOwner::EnsureContentTreeOwner()
  230. {
  231. if (mContentTreeOwner) {
  232. return;
  233. }
  234. mContentTreeOwner = new nsDocShellTreeOwner();
  235. nsCOMPtr<nsIWebBrowserChrome> browserChrome = GetWebBrowserChrome();
  236. if (browserChrome) {
  237. mContentTreeOwner->SetWebBrowserChrome(browserChrome);
  238. }
  239. if (mWebBrowser) {
  240. mContentTreeOwner->WebBrowser(mWebBrowser);
  241. }
  242. }
  243. NS_IMETHODIMP
  244. nsDocShellTreeOwner::ContentShellAdded(nsIDocShellTreeItem* aContentShell,
  245. bool aPrimary, bool aTargetable,
  246. const nsAString& aID)
  247. {
  248. if (mTreeOwner)
  249. return mTreeOwner->ContentShellAdded(aContentShell, aPrimary, aTargetable,
  250. aID);
  251. EnsureContentTreeOwner();
  252. aContentShell->SetTreeOwner(mContentTreeOwner);
  253. if (aPrimary) {
  254. mPrimaryContentShell = aContentShell;
  255. mPrimaryTabParent = nullptr;
  256. }
  257. return NS_OK;
  258. }
  259. NS_IMETHODIMP
  260. nsDocShellTreeOwner::ContentShellRemoved(nsIDocShellTreeItem* aContentShell)
  261. {
  262. if (mTreeOwner) {
  263. return mTreeOwner->ContentShellRemoved(aContentShell);
  264. }
  265. if (mPrimaryContentShell == aContentShell) {
  266. mPrimaryContentShell = nullptr;
  267. }
  268. return NS_OK;
  269. }
  270. NS_IMETHODIMP
  271. nsDocShellTreeOwner::GetPrimaryContentShell(nsIDocShellTreeItem** aShell)
  272. {
  273. NS_ENSURE_ARG_POINTER(aShell);
  274. if (mTreeOwner) {
  275. return mTreeOwner->GetPrimaryContentShell(aShell);
  276. }
  277. nsCOMPtr<nsIDocShellTreeItem> shell;
  278. if (!mPrimaryTabParent) {
  279. shell =
  280. mPrimaryContentShell ? mPrimaryContentShell : mWebBrowser->mDocShell;
  281. }
  282. shell.forget(aShell);
  283. return NS_OK;
  284. }
  285. NS_IMETHODIMP
  286. nsDocShellTreeOwner::TabParentAdded(nsITabParent* aTab, bool aPrimary)
  287. {
  288. if (mTreeOwner) {
  289. return mTreeOwner->TabParentAdded(aTab, aPrimary);
  290. }
  291. if (aPrimary) {
  292. mPrimaryTabParent = aTab;
  293. mPrimaryContentShell = nullptr;
  294. } else if (mPrimaryTabParent == aTab) {
  295. mPrimaryTabParent = nullptr;
  296. }
  297. return NS_OK;
  298. }
  299. NS_IMETHODIMP
  300. nsDocShellTreeOwner::TabParentRemoved(nsITabParent* aTab)
  301. {
  302. if (mTreeOwner) {
  303. return mTreeOwner->TabParentRemoved(aTab);
  304. }
  305. if (aTab == mPrimaryTabParent) {
  306. mPrimaryTabParent = nullptr;
  307. }
  308. return NS_OK;
  309. }
  310. NS_IMETHODIMP
  311. nsDocShellTreeOwner::GetPrimaryTabParent(nsITabParent** aTab)
  312. {
  313. if (mTreeOwner) {
  314. return mTreeOwner->GetPrimaryTabParent(aTab);
  315. }
  316. nsCOMPtr<nsITabParent> tab = mPrimaryTabParent;
  317. tab.forget(aTab);
  318. return NS_OK;
  319. }
  320. NS_IMETHODIMP
  321. nsDocShellTreeOwner::GetPrimaryContentSize(int32_t* aWidth,
  322. int32_t* aHeight)
  323. {
  324. return NS_ERROR_NOT_IMPLEMENTED;
  325. }
  326. NS_IMETHODIMP
  327. nsDocShellTreeOwner::SetPrimaryContentSize(int32_t aWidth,
  328. int32_t aHeight)
  329. {
  330. return NS_ERROR_NOT_IMPLEMENTED;
  331. }
  332. NS_IMETHODIMP
  333. nsDocShellTreeOwner::GetRootShellSize(int32_t* aWidth,
  334. int32_t* aHeight)
  335. {
  336. return NS_ERROR_NOT_IMPLEMENTED;
  337. }
  338. NS_IMETHODIMP
  339. nsDocShellTreeOwner::SetRootShellSize(int32_t aWidth,
  340. int32_t aHeight)
  341. {
  342. return NS_ERROR_NOT_IMPLEMENTED;
  343. }
  344. NS_IMETHODIMP
  345. nsDocShellTreeOwner::SizeShellTo(nsIDocShellTreeItem* aShellItem,
  346. int32_t aCX, int32_t aCY)
  347. {
  348. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome = GetWebBrowserChrome();
  349. NS_ENSURE_STATE(mTreeOwner || webBrowserChrome);
  350. if (mTreeOwner) {
  351. return mTreeOwner->SizeShellTo(aShellItem, aCX, aCY);
  352. }
  353. if (aShellItem == mWebBrowser->mDocShell) {
  354. nsCOMPtr<nsITabChild> tabChild = do_QueryInterface(webBrowserChrome);
  355. if (tabChild) {
  356. // The XUL window to resize is in the parent process, but there we
  357. // won't be able to get aShellItem to do the hack in nsXULWindow::SizeShellTo,
  358. // so let's send the width and height of aShellItem too.
  359. nsCOMPtr<nsIBaseWindow> shellAsWin(do_QueryInterface(aShellItem));
  360. NS_ENSURE_TRUE(shellAsWin, NS_ERROR_FAILURE);
  361. int32_t width = 0;
  362. int32_t height = 0;
  363. shellAsWin->GetSize(&width, &height);
  364. return tabChild->RemoteSizeShellTo(aCX, aCY, width, height);
  365. }
  366. return webBrowserChrome->SizeBrowserTo(aCX, aCY);
  367. }
  368. nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(aShellItem));
  369. NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
  370. nsCOMPtr<nsIDOMDocument> domDocument;
  371. webNav->GetDocument(getter_AddRefs(domDocument));
  372. NS_ENSURE_TRUE(domDocument, NS_ERROR_FAILURE);
  373. nsCOMPtr<nsIDOMElement> domElement;
  374. domDocument->GetDocumentElement(getter_AddRefs(domElement));
  375. NS_ENSURE_TRUE(domElement, NS_ERROR_FAILURE);
  376. // Set the preferred Size
  377. //XXX
  378. NS_ERROR("Implement this");
  379. /*
  380. Set the preferred size on the aShellItem.
  381. */
  382. RefPtr<nsPresContext> presContext;
  383. mWebBrowser->mDocShell->GetPresContext(getter_AddRefs(presContext));
  384. NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE);
  385. nsIPresShell* presShell = presContext->GetPresShell();
  386. NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
  387. NS_ENSURE_SUCCESS(
  388. presShell->ResizeReflow(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE),
  389. NS_ERROR_FAILURE);
  390. nsRect shellArea = presContext->GetVisibleArea();
  391. int32_t browserCX = presContext->AppUnitsToDevPixels(shellArea.width);
  392. int32_t browserCY = presContext->AppUnitsToDevPixels(shellArea.height);
  393. return webBrowserChrome->SizeBrowserTo(browserCX, browserCY);
  394. }
  395. NS_IMETHODIMP
  396. nsDocShellTreeOwner::SetPersistence(bool aPersistPosition,
  397. bool aPersistSize,
  398. bool aPersistSizeMode)
  399. {
  400. return NS_ERROR_NOT_IMPLEMENTED;
  401. }
  402. NS_IMETHODIMP
  403. nsDocShellTreeOwner::GetPersistence(bool* aPersistPosition,
  404. bool* aPersistSize,
  405. bool* aPersistSizeMode)
  406. {
  407. return NS_ERROR_NOT_IMPLEMENTED;
  408. }
  409. NS_IMETHODIMP
  410. nsDocShellTreeOwner::GetTargetableShellCount(uint32_t* aResult)
  411. {
  412. if (mTreeOwner) {
  413. mTreeOwner->GetTargetableShellCount(aResult);
  414. } else {
  415. *aResult = 0;
  416. }
  417. return NS_OK;
  418. }
  419. NS_IMETHODIMP
  420. nsDocShellTreeOwner::GetHasPrimaryContent(bool* aResult)
  421. {
  422. *aResult = mPrimaryTabParent || mPrimaryContentShell;
  423. return NS_OK;
  424. }
  425. //*****************************************************************************
  426. // nsDocShellTreeOwner::nsIBaseWindow
  427. //*****************************************************************************
  428. NS_IMETHODIMP
  429. nsDocShellTreeOwner::InitWindow(nativeWindow aParentNativeWindow,
  430. nsIWidget* aParentWidget, int32_t aX,
  431. int32_t aY, int32_t aCX, int32_t aCY)
  432. {
  433. return NS_ERROR_NULL_POINTER;
  434. }
  435. NS_IMETHODIMP
  436. nsDocShellTreeOwner::Create()
  437. {
  438. return NS_ERROR_NULL_POINTER;
  439. }
  440. NS_IMETHODIMP
  441. nsDocShellTreeOwner::Destroy()
  442. {
  443. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome = GetWebBrowserChrome();
  444. if (webBrowserChrome) {
  445. return webBrowserChrome->DestroyBrowserWindow();
  446. }
  447. return NS_ERROR_NULL_POINTER;
  448. }
  449. NS_IMETHODIMP
  450. nsDocShellTreeOwner::GetUnscaledDevicePixelsPerCSSPixel(double* aScale)
  451. {
  452. if (mWebBrowser) {
  453. return mWebBrowser->GetUnscaledDevicePixelsPerCSSPixel(aScale);
  454. }
  455. *aScale = 1.0;
  456. return NS_OK;
  457. }
  458. NS_IMETHODIMP
  459. nsDocShellTreeOwner::GetDevicePixelsPerDesktopPixel(double* aScale)
  460. {
  461. if (mWebBrowser) {
  462. return mWebBrowser->GetDevicePixelsPerDesktopPixel(aScale);
  463. }
  464. *aScale = 1.0;
  465. return NS_OK;
  466. }
  467. NS_IMETHODIMP
  468. nsDocShellTreeOwner::SetPositionDesktopPix(int32_t aX, int32_t aY)
  469. {
  470. if (mWebBrowser) {
  471. nsresult rv = mWebBrowser->SetPositionDesktopPix(aX, aY);
  472. NS_ENSURE_SUCCESS(rv, rv);
  473. }
  474. double scale = 1.0;
  475. GetDevicePixelsPerDesktopPixel(&scale);
  476. return SetPosition(NSToIntRound(aX * scale), NSToIntRound(aY * scale));
  477. }
  478. NS_IMETHODIMP
  479. nsDocShellTreeOwner::SetPosition(int32_t aX, int32_t aY)
  480. {
  481. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  482. if (ownerWin) {
  483. return ownerWin->SetDimensions(nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION,
  484. aX, aY, 0, 0);
  485. }
  486. return NS_ERROR_NULL_POINTER;
  487. }
  488. NS_IMETHODIMP
  489. nsDocShellTreeOwner::GetPosition(int32_t* aX, int32_t* aY)
  490. {
  491. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  492. if (ownerWin) {
  493. return ownerWin->GetDimensions(nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION,
  494. aX, aY, nullptr, nullptr);
  495. }
  496. return NS_ERROR_NULL_POINTER;
  497. }
  498. NS_IMETHODIMP
  499. nsDocShellTreeOwner::SetSize(int32_t aCX, int32_t aCY, bool aRepaint)
  500. {
  501. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  502. if (ownerWin) {
  503. return ownerWin->SetDimensions(nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER,
  504. 0, 0, aCX, aCY);
  505. }
  506. return NS_ERROR_NULL_POINTER;
  507. }
  508. NS_IMETHODIMP
  509. nsDocShellTreeOwner::GetSize(int32_t* aCX, int32_t* aCY)
  510. {
  511. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  512. if (ownerWin) {
  513. return ownerWin->GetDimensions(nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER,
  514. nullptr, nullptr, aCX, aCY);
  515. }
  516. return NS_ERROR_NULL_POINTER;
  517. }
  518. NS_IMETHODIMP
  519. nsDocShellTreeOwner::SetPositionAndSize(int32_t aX, int32_t aY, int32_t aCX,
  520. int32_t aCY, uint32_t aFlags)
  521. {
  522. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  523. if (ownerWin) {
  524. return ownerWin->SetDimensions(
  525. nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER |
  526. nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION,
  527. aX, aY, aCX, aCY);
  528. }
  529. return NS_ERROR_NULL_POINTER;
  530. }
  531. NS_IMETHODIMP
  532. nsDocShellTreeOwner::GetPositionAndSize(int32_t* aX, int32_t* aY, int32_t* aCX,
  533. int32_t* aCY)
  534. {
  535. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  536. if (ownerWin) {
  537. return ownerWin->GetDimensions(
  538. nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER |
  539. nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION,
  540. aX, aY, aCX, aCY);
  541. }
  542. return NS_ERROR_NULL_POINTER;
  543. }
  544. NS_IMETHODIMP
  545. nsDocShellTreeOwner::Repaint(bool aForce)
  546. {
  547. return NS_ERROR_NULL_POINTER;
  548. }
  549. NS_IMETHODIMP
  550. nsDocShellTreeOwner::GetParentWidget(nsIWidget** aParentWidget)
  551. {
  552. return NS_ERROR_NULL_POINTER;
  553. }
  554. NS_IMETHODIMP
  555. nsDocShellTreeOwner::SetParentWidget(nsIWidget* aParentWidget)
  556. {
  557. return NS_ERROR_NULL_POINTER;
  558. }
  559. NS_IMETHODIMP
  560. nsDocShellTreeOwner::GetParentNativeWindow(nativeWindow* aParentNativeWindow)
  561. {
  562. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  563. if (ownerWin) {
  564. return ownerWin->GetSiteWindow(aParentNativeWindow);
  565. }
  566. return NS_ERROR_NULL_POINTER;
  567. }
  568. NS_IMETHODIMP
  569. nsDocShellTreeOwner::SetParentNativeWindow(nativeWindow aParentNativeWindow)
  570. {
  571. return NS_ERROR_NULL_POINTER;
  572. }
  573. NS_IMETHODIMP
  574. nsDocShellTreeOwner::GetNativeHandle(nsAString& aNativeHandle)
  575. {
  576. // the nativeHandle should be accessed from nsIXULWindow
  577. return NS_ERROR_NOT_IMPLEMENTED;
  578. }
  579. NS_IMETHODIMP
  580. nsDocShellTreeOwner::GetVisibility(bool* aVisibility)
  581. {
  582. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  583. if (ownerWin) {
  584. return ownerWin->GetVisibility(aVisibility);
  585. }
  586. return NS_ERROR_NULL_POINTER;
  587. }
  588. NS_IMETHODIMP
  589. nsDocShellTreeOwner::SetVisibility(bool aVisibility)
  590. {
  591. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  592. if (ownerWin) {
  593. return ownerWin->SetVisibility(aVisibility);
  594. }
  595. return NS_ERROR_NULL_POINTER;
  596. }
  597. NS_IMETHODIMP
  598. nsDocShellTreeOwner::GetEnabled(bool* aEnabled)
  599. {
  600. NS_ENSURE_ARG_POINTER(aEnabled);
  601. *aEnabled = true;
  602. return NS_ERROR_NOT_IMPLEMENTED;
  603. }
  604. NS_IMETHODIMP
  605. nsDocShellTreeOwner::SetEnabled(bool aEnabled)
  606. {
  607. return NS_ERROR_NOT_IMPLEMENTED;
  608. }
  609. NS_IMETHODIMP
  610. nsDocShellTreeOwner::GetMainWidget(nsIWidget** aMainWidget)
  611. {
  612. return NS_ERROR_NULL_POINTER;
  613. }
  614. NS_IMETHODIMP
  615. nsDocShellTreeOwner::SetFocus()
  616. {
  617. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  618. if (ownerWin) {
  619. return ownerWin->SetFocus();
  620. }
  621. return NS_ERROR_NULL_POINTER;
  622. }
  623. NS_IMETHODIMP
  624. nsDocShellTreeOwner::GetTitle(char16_t** aTitle)
  625. {
  626. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  627. if (ownerWin) {
  628. return ownerWin->GetTitle(aTitle);
  629. }
  630. return NS_ERROR_NULL_POINTER;
  631. }
  632. NS_IMETHODIMP
  633. nsDocShellTreeOwner::SetTitle(const char16_t* aTitle)
  634. {
  635. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin = GetOwnerWin();
  636. if (ownerWin) {
  637. return ownerWin->SetTitle(aTitle);
  638. }
  639. return NS_ERROR_NULL_POINTER;
  640. }
  641. //*****************************************************************************
  642. // nsDocShellTreeOwner::nsIWebProgressListener
  643. //*****************************************************************************
  644. NS_IMETHODIMP
  645. nsDocShellTreeOwner::OnProgressChange(nsIWebProgress* aProgress,
  646. nsIRequest* aRequest,
  647. int32_t aCurSelfProgress,
  648. int32_t aMaxSelfProgress,
  649. int32_t aCurTotalProgress,
  650. int32_t aMaxTotalProgress)
  651. {
  652. // In the absence of DOM document creation event, this method is the
  653. // most convenient place to install the mouse listener on the
  654. // DOM document.
  655. return AddChromeListeners();
  656. }
  657. NS_IMETHODIMP
  658. nsDocShellTreeOwner::OnStateChange(nsIWebProgress* aProgress,
  659. nsIRequest* aRequest,
  660. uint32_t aProgressStateFlags,
  661. nsresult aStatus)
  662. {
  663. return NS_OK;
  664. }
  665. NS_IMETHODIMP
  666. nsDocShellTreeOwner::OnLocationChange(nsIWebProgress* aWebProgress,
  667. nsIRequest* aRequest,
  668. nsIURI* aURI,
  669. uint32_t aFlags)
  670. {
  671. return NS_OK;
  672. }
  673. NS_IMETHODIMP
  674. nsDocShellTreeOwner::OnStatusChange(nsIWebProgress* aWebProgress,
  675. nsIRequest* aRequest,
  676. nsresult aStatus,
  677. const char16_t* aMessage)
  678. {
  679. return NS_OK;
  680. }
  681. NS_IMETHODIMP
  682. nsDocShellTreeOwner::OnSecurityChange(nsIWebProgress* aWebProgress,
  683. nsIRequest* aRequest,
  684. uint32_t aState)
  685. {
  686. return NS_OK;
  687. }
  688. //*****************************************************************************
  689. // nsDocShellTreeOwner: Accessors
  690. //*****************************************************************************
  691. void
  692. nsDocShellTreeOwner::WebBrowser(nsWebBrowser* aWebBrowser)
  693. {
  694. if (!aWebBrowser) {
  695. RemoveChromeListeners();
  696. }
  697. if (aWebBrowser != mWebBrowser) {
  698. mPrompter = nullptr;
  699. mAuthPrompter = nullptr;
  700. }
  701. mWebBrowser = aWebBrowser;
  702. if (mContentTreeOwner) {
  703. mContentTreeOwner->WebBrowser(aWebBrowser);
  704. if (!aWebBrowser) {
  705. mContentTreeOwner = nullptr;
  706. }
  707. }
  708. }
  709. nsWebBrowser*
  710. nsDocShellTreeOwner::WebBrowser()
  711. {
  712. return mWebBrowser;
  713. }
  714. NS_IMETHODIMP
  715. nsDocShellTreeOwner::SetTreeOwner(nsIDocShellTreeOwner* aTreeOwner)
  716. {
  717. if (aTreeOwner) {
  718. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome(do_GetInterface(aTreeOwner));
  719. NS_ENSURE_TRUE(webBrowserChrome, NS_ERROR_INVALID_ARG);
  720. NS_ENSURE_SUCCESS(SetWebBrowserChrome(webBrowserChrome),
  721. NS_ERROR_INVALID_ARG);
  722. mTreeOwner = aTreeOwner;
  723. } else {
  724. mTreeOwner = nullptr;
  725. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome = GetWebBrowserChrome();
  726. if (!webBrowserChrome) {
  727. NS_ENSURE_SUCCESS(SetWebBrowserChrome(nullptr), NS_ERROR_FAILURE);
  728. }
  729. }
  730. return NS_OK;
  731. }
  732. NS_IMETHODIMP
  733. nsDocShellTreeOwner::SetWebBrowserChrome(nsIWebBrowserChrome* aWebBrowserChrome)
  734. {
  735. if (!aWebBrowserChrome) {
  736. mWebBrowserChrome = nullptr;
  737. mOwnerWin = nullptr;
  738. mOwnerRequestor = nullptr;
  739. mWebBrowserChromeWeak = nullptr;
  740. } else {
  741. nsCOMPtr<nsISupportsWeakReference> supportsweak =
  742. do_QueryInterface(aWebBrowserChrome);
  743. if (supportsweak) {
  744. supportsweak->GetWeakReference(getter_AddRefs(mWebBrowserChromeWeak));
  745. } else {
  746. nsCOMPtr<nsIEmbeddingSiteWindow> ownerWin(
  747. do_QueryInterface(aWebBrowserChrome));
  748. nsCOMPtr<nsIInterfaceRequestor> requestor(
  749. do_QueryInterface(aWebBrowserChrome));
  750. // it's ok for ownerWin or requestor to be null.
  751. mWebBrowserChrome = aWebBrowserChrome;
  752. mOwnerWin = ownerWin;
  753. mOwnerRequestor = requestor;
  754. }
  755. }
  756. if (mContentTreeOwner) {
  757. mContentTreeOwner->SetWebBrowserChrome(aWebBrowserChrome);
  758. }
  759. return NS_OK;
  760. }
  761. // Hook up things to the chrome like context menus and tooltips, if the chrome
  762. // has implemented the right interfaces.
  763. NS_IMETHODIMP
  764. nsDocShellTreeOwner::AddChromeListeners()
  765. {
  766. nsresult rv = NS_OK;
  767. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome = GetWebBrowserChrome();
  768. if (!webBrowserChrome) {
  769. return NS_ERROR_FAILURE;
  770. }
  771. // install tooltips
  772. if (!mChromeTooltipListener) {
  773. nsCOMPtr<nsITooltipListener> tooltipListener(
  774. do_QueryInterface(webBrowserChrome));
  775. if (tooltipListener) {
  776. mChromeTooltipListener = new ChromeTooltipListener(mWebBrowser,
  777. webBrowserChrome);
  778. rv = mChromeTooltipListener->AddChromeListeners();
  779. }
  780. }
  781. // install context menus
  782. if (!mChromeContextMenuListener) {
  783. nsCOMPtr<nsIContextMenuListener2> contextListener2(
  784. do_QueryInterface(webBrowserChrome));
  785. nsCOMPtr<nsIContextMenuListener> contextListener(
  786. do_QueryInterface(webBrowserChrome));
  787. if (contextListener2 || contextListener) {
  788. mChromeContextMenuListener =
  789. new ChromeContextMenuListener(mWebBrowser, webBrowserChrome);
  790. rv = mChromeContextMenuListener->AddChromeListeners();
  791. }
  792. }
  793. // register dragover and drop event listeners with the listener manager
  794. nsCOMPtr<EventTarget> target;
  795. GetDOMEventTarget(mWebBrowser, getter_AddRefs(target));
  796. EventListenerManager* elmP = target->GetOrCreateListenerManager();
  797. if (elmP) {
  798. elmP->AddEventListenerByType(this, NS_LITERAL_STRING("dragover"),
  799. TrustedEventsAtSystemGroupBubble());
  800. elmP->AddEventListenerByType(this, NS_LITERAL_STRING("drop"),
  801. TrustedEventsAtSystemGroupBubble());
  802. }
  803. return rv;
  804. }
  805. NS_IMETHODIMP
  806. nsDocShellTreeOwner::RemoveChromeListeners()
  807. {
  808. if (mChromeTooltipListener) {
  809. mChromeTooltipListener->RemoveChromeListeners();
  810. mChromeTooltipListener = nullptr;
  811. }
  812. if (mChromeContextMenuListener) {
  813. mChromeContextMenuListener->RemoveChromeListeners();
  814. mChromeContextMenuListener = nullptr;
  815. }
  816. nsCOMPtr<EventTarget> piTarget;
  817. GetDOMEventTarget(mWebBrowser, getter_AddRefs(piTarget));
  818. if (!piTarget) {
  819. return NS_OK;
  820. }
  821. EventListenerManager* elmP = piTarget->GetOrCreateListenerManager();
  822. if (elmP) {
  823. elmP->RemoveEventListenerByType(this, NS_LITERAL_STRING("dragover"),
  824. TrustedEventsAtSystemGroupBubble());
  825. elmP->RemoveEventListenerByType(this, NS_LITERAL_STRING("drop"),
  826. TrustedEventsAtSystemGroupBubble());
  827. }
  828. return NS_OK;
  829. }
  830. NS_IMETHODIMP
  831. nsDocShellTreeOwner::HandleEvent(nsIDOMEvent* aEvent)
  832. {
  833. nsCOMPtr<nsIDOMDragEvent> dragEvent = do_QueryInterface(aEvent);
  834. NS_ENSURE_TRUE(dragEvent, NS_ERROR_INVALID_ARG);
  835. bool defaultPrevented;
  836. aEvent->GetDefaultPrevented(&defaultPrevented);
  837. if (defaultPrevented) {
  838. return NS_OK;
  839. }
  840. nsCOMPtr<nsIDroppedLinkHandler> handler =
  841. do_GetService("@mozilla.org/content/dropped-link-handler;1");
  842. if (handler) {
  843. nsAutoString eventType;
  844. aEvent->GetType(eventType);
  845. if (eventType.EqualsLiteral("dragover")) {
  846. bool canDropLink = false;
  847. handler->CanDropLink(dragEvent, false, &canDropLink);
  848. if (canDropLink) {
  849. aEvent->PreventDefault();
  850. }
  851. } else if (eventType.EqualsLiteral("drop")) {
  852. nsIWebNavigation* webnav = static_cast<nsIWebNavigation*>(mWebBrowser);
  853. uint32_t linksCount;
  854. nsIDroppedLinkItem** links;
  855. if (webnav &&
  856. NS_SUCCEEDED(handler->DropLinks(dragEvent, true, &linksCount, &links))) {
  857. if (linksCount >= 1) {
  858. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome = GetWebBrowserChrome();
  859. if (webBrowserChrome) {
  860. nsCOMPtr<nsITabChild> tabChild = do_QueryInterface(webBrowserChrome);
  861. if (tabChild) {
  862. nsresult rv = tabChild->RemoteDropLinks(linksCount, links);
  863. for (uint32_t i = 0; i < linksCount; i++) {
  864. NS_RELEASE(links[i]);
  865. }
  866. free(links);
  867. return rv;
  868. }
  869. }
  870. nsAutoString url;
  871. if (NS_SUCCEEDED(links[0]->GetUrl(url))) {
  872. if (!url.IsEmpty()) {
  873. webnav->LoadURI(url.get(), 0, nullptr, nullptr, nullptr);
  874. }
  875. }
  876. for (uint32_t i = 0; i < linksCount; i++) {
  877. NS_RELEASE(links[i]);
  878. }
  879. free(links);
  880. }
  881. } else {
  882. aEvent->StopPropagation();
  883. aEvent->PreventDefault();
  884. }
  885. }
  886. }
  887. return NS_OK;
  888. }
  889. already_AddRefed<nsIWebBrowserChrome>
  890. nsDocShellTreeOwner::GetWebBrowserChrome()
  891. {
  892. nsCOMPtr<nsIWebBrowserChrome> chrome;
  893. if (mWebBrowserChromeWeak) {
  894. chrome = do_QueryReferent(mWebBrowserChromeWeak);
  895. } else if (mWebBrowserChrome) {
  896. chrome = mWebBrowserChrome;
  897. }
  898. return chrome.forget();
  899. }
  900. already_AddRefed<nsIEmbeddingSiteWindow>
  901. nsDocShellTreeOwner::GetOwnerWin()
  902. {
  903. nsCOMPtr<nsIEmbeddingSiteWindow> win;
  904. if (mWebBrowserChromeWeak) {
  905. win = do_QueryReferent(mWebBrowserChromeWeak);
  906. } else if (mOwnerWin) {
  907. win = mOwnerWin;
  908. }
  909. return win.forget();
  910. }
  911. already_AddRefed<nsIInterfaceRequestor>
  912. nsDocShellTreeOwner::GetOwnerRequestor()
  913. {
  914. nsCOMPtr<nsIInterfaceRequestor> req;
  915. if (mWebBrowserChromeWeak) {
  916. req = do_QueryReferent(mWebBrowserChromeWeak);
  917. } else if (mOwnerRequestor) {
  918. req = mOwnerRequestor;
  919. }
  920. return req.forget();
  921. }
  922. NS_IMPL_ISUPPORTS(ChromeTooltipListener, nsIDOMEventListener)
  923. ChromeTooltipListener::ChromeTooltipListener(nsWebBrowser* aInBrowser,
  924. nsIWebBrowserChrome* aInChrome)
  925. : mWebBrowser(aInBrowser)
  926. , mWebBrowserChrome(aInChrome)
  927. , mTooltipListenerInstalled(false)
  928. , mMouseClientX(0)
  929. , mMouseClientY(0)
  930. , mMouseScreenX(0)
  931. , mMouseScreenY(0)
  932. , mShowingTooltip(false)
  933. , mTooltipShownOnce(false)
  934. {
  935. mTooltipTextProvider = do_GetService(NS_TOOLTIPTEXTPROVIDER_CONTRACTID);
  936. if (!mTooltipTextProvider) {
  937. mTooltipTextProvider = do_GetService(NS_DEFAULTTOOLTIPTEXTPROVIDER_CONTRACTID);
  938. }
  939. }
  940. ChromeTooltipListener::~ChromeTooltipListener()
  941. {
  942. }
  943. // Hook up things to the chrome like context menus and tooltips, if the chrome
  944. // has implemented the right interfaces.
  945. NS_IMETHODIMP
  946. ChromeTooltipListener::AddChromeListeners()
  947. {
  948. if (!mEventTarget) {
  949. GetDOMEventTarget(mWebBrowser, getter_AddRefs(mEventTarget));
  950. }
  951. // Register the appropriate events for tooltips, but only if
  952. // the embedding chrome cares.
  953. nsresult rv = NS_OK;
  954. nsCOMPtr<nsITooltipListener> tooltipListener(
  955. do_QueryInterface(mWebBrowserChrome));
  956. if (tooltipListener && !mTooltipListenerInstalled) {
  957. rv = AddTooltipListener();
  958. if (NS_FAILED(rv)) {
  959. return rv;
  960. }
  961. }
  962. return rv;
  963. }
  964. // Subscribe to the events that will allow us to track tooltips. We need "mouse"
  965. // for mouseExit, "mouse motion" for mouseMove, and "key" for keyDown. As we
  966. // add the listeners, keep track of how many succeed so we can clean up
  967. // correctly in Release().
  968. NS_IMETHODIMP
  969. ChromeTooltipListener::AddTooltipListener()
  970. {
  971. if (mEventTarget) {
  972. nsresult rv = NS_OK;
  973. rv = mEventTarget->AddSystemEventListener(NS_LITERAL_STRING("keydown"),
  974. this, false, false);
  975. NS_ENSURE_SUCCESS(rv, rv);
  976. rv = mEventTarget->AddSystemEventListener(NS_LITERAL_STRING("mousedown"),
  977. this, false, false);
  978. NS_ENSURE_SUCCESS(rv, rv);
  979. rv = mEventTarget->AddSystemEventListener(NS_LITERAL_STRING("mouseout"),
  980. this, false, false);
  981. NS_ENSURE_SUCCESS(rv, rv);
  982. rv = mEventTarget->AddSystemEventListener(NS_LITERAL_STRING("mousemove"),
  983. this, false, false);
  984. NS_ENSURE_SUCCESS(rv, rv);
  985. mTooltipListenerInstalled = true;
  986. }
  987. return NS_OK;
  988. }
  989. // Unsubscribe from the various things we've hooked up to the window root.
  990. NS_IMETHODIMP
  991. ChromeTooltipListener::RemoveChromeListeners()
  992. {
  993. HideTooltip();
  994. if (mTooltipListenerInstalled) {
  995. RemoveTooltipListener();
  996. }
  997. mEventTarget = nullptr;
  998. // it really doesn't matter if these fail...
  999. return NS_OK;
  1000. }
  1001. // Unsubscribe from all the various tooltip events that we were listening to.
  1002. NS_IMETHODIMP
  1003. ChromeTooltipListener::RemoveTooltipListener()
  1004. {
  1005. if (mEventTarget) {
  1006. nsresult rv = NS_OK;
  1007. rv = mEventTarget->RemoveSystemEventListener(NS_LITERAL_STRING("keydown"),
  1008. this, false);
  1009. NS_ENSURE_SUCCESS(rv, rv);
  1010. rv = mEventTarget->RemoveSystemEventListener(NS_LITERAL_STRING("mousedown"),
  1011. this, false);
  1012. NS_ENSURE_SUCCESS(rv, rv);
  1013. rv = mEventTarget->RemoveSystemEventListener(NS_LITERAL_STRING("mouseout"),
  1014. this, false);
  1015. NS_ENSURE_SUCCESS(rv, rv);
  1016. rv = mEventTarget->RemoveSystemEventListener(NS_LITERAL_STRING("mousemove"),
  1017. this, false);
  1018. NS_ENSURE_SUCCESS(rv, rv);
  1019. mTooltipListenerInstalled = false;
  1020. }
  1021. return NS_OK;
  1022. }
  1023. NS_IMETHODIMP
  1024. ChromeTooltipListener::HandleEvent(nsIDOMEvent* aEvent)
  1025. {
  1026. nsAutoString eventType;
  1027. aEvent->GetType(eventType);
  1028. if (eventType.EqualsLiteral("keydown") ||
  1029. eventType.EqualsLiteral("mousedown")) {
  1030. return HideTooltip();
  1031. } else if (eventType.EqualsLiteral("mouseout")) {
  1032. // Reset flag so that tooltip will display on the next MouseMove
  1033. mTooltipShownOnce = false;
  1034. return HideTooltip();
  1035. } else if (eventType.EqualsLiteral("mousemove")) {
  1036. return MouseMove(aEvent);
  1037. }
  1038. NS_ERROR("Unexpected event type");
  1039. return NS_OK;
  1040. }
  1041. // If we're a tooltip, fire off a timer to see if a tooltip should be shown. If
  1042. // the timer fires, we cache the node in |mPossibleTooltipNode|.
  1043. nsresult
  1044. ChromeTooltipListener::MouseMove(nsIDOMEvent* aMouseEvent)
  1045. {
  1046. nsCOMPtr<nsIDOMMouseEvent> mouseEvent(do_QueryInterface(aMouseEvent));
  1047. if (!mouseEvent) {
  1048. return NS_OK;
  1049. }
  1050. // stash the coordinates of the event so that we can still get back to it from
  1051. // within the timer callback. On win32, we'll get a MouseMove event even when
  1052. // a popup goes away -- even when the mouse doesn't change position! To get
  1053. // around this, we make sure the mouse has really moved before proceeding.
  1054. int32_t newMouseX, newMouseY;
  1055. mouseEvent->GetClientX(&newMouseX);
  1056. mouseEvent->GetClientY(&newMouseY);
  1057. if (mMouseClientX == newMouseX && mMouseClientY == newMouseY) {
  1058. return NS_OK;
  1059. }
  1060. // Filter out minor mouse movements.
  1061. if (mShowingTooltip &&
  1062. (abs(mMouseClientX - newMouseX) <= kTooltipMouseMoveTolerance) &&
  1063. (abs(mMouseClientY - newMouseY) <= kTooltipMouseMoveTolerance)) {
  1064. return NS_OK;
  1065. }
  1066. mMouseClientX = newMouseX;
  1067. mMouseClientY = newMouseY;
  1068. mouseEvent->GetScreenX(&mMouseScreenX);
  1069. mouseEvent->GetScreenY(&mMouseScreenY);
  1070. if (mTooltipTimer) {
  1071. mTooltipTimer->Cancel();
  1072. }
  1073. if (!mShowingTooltip && !mTooltipShownOnce) {
  1074. mTooltipTimer = do_CreateInstance("@mozilla.org/timer;1");
  1075. if (mTooltipTimer) {
  1076. nsCOMPtr<EventTarget> eventTarget =
  1077. aMouseEvent->InternalDOMEvent()->GetTarget();
  1078. if (eventTarget) {
  1079. mPossibleTooltipNode = do_QueryInterface(eventTarget);
  1080. }
  1081. if (mPossibleTooltipNode) {
  1082. nsresult rv = mTooltipTimer->InitWithFuncCallback(
  1083. sTooltipCallback, this,
  1084. LookAndFeel::GetInt(LookAndFeel::eIntID_TooltipDelay, 500),
  1085. nsITimer::TYPE_ONE_SHOT);
  1086. if (NS_FAILED(rv)) {
  1087. mPossibleTooltipNode = nullptr;
  1088. }
  1089. }
  1090. } else {
  1091. NS_WARNING("Could not create a timer for tooltip tracking");
  1092. }
  1093. } else {
  1094. mTooltipShownOnce = true;
  1095. return HideTooltip();
  1096. }
  1097. return NS_OK;
  1098. }
  1099. // Tell the registered chrome that they should show the tooltip.
  1100. NS_IMETHODIMP
  1101. ChromeTooltipListener::ShowTooltip(int32_t aInXCoords, int32_t aInYCoords,
  1102. const nsAString& aInTipText,
  1103. const nsAString& aTipDir)
  1104. {
  1105. nsresult rv = NS_OK;
  1106. // do the work to call the client
  1107. nsCOMPtr<nsITooltipListener> tooltipListener(
  1108. do_QueryInterface(mWebBrowserChrome));
  1109. if (tooltipListener) {
  1110. rv = tooltipListener->OnShowTooltip(aInXCoords, aInYCoords,
  1111. PromiseFlatString(aInTipText).get(),
  1112. PromiseFlatString(aTipDir).get());
  1113. if (NS_SUCCEEDED(rv)) {
  1114. mShowingTooltip = true;
  1115. }
  1116. }
  1117. return rv;
  1118. }
  1119. // Tell the registered chrome that they should rollup the tooltip
  1120. // NOTE: This routine is safe to call even if the popup is already closed.
  1121. NS_IMETHODIMP
  1122. ChromeTooltipListener::HideTooltip()
  1123. {
  1124. nsresult rv = NS_OK;
  1125. // shut down the relevant timers
  1126. if (mTooltipTimer) {
  1127. mTooltipTimer->Cancel();
  1128. mTooltipTimer = nullptr;
  1129. // release tooltip target
  1130. mPossibleTooltipNode = nullptr;
  1131. }
  1132. // if we're showing the tip, tell the chrome to hide it
  1133. if (mShowingTooltip) {
  1134. nsCOMPtr<nsITooltipListener> tooltipListener(
  1135. do_QueryInterface(mWebBrowserChrome));
  1136. if (tooltipListener) {
  1137. rv = tooltipListener->OnHideTooltip();
  1138. if (NS_SUCCEEDED(rv)) {
  1139. mShowingTooltip = false;
  1140. }
  1141. }
  1142. }
  1143. return rv;
  1144. }
  1145. // A timer callback, fired when the mouse has hovered inside of a frame for the
  1146. // appropriate amount of time. Getting to this point means that we should show
  1147. // the tooltip, but only after we determine there is an appropriate TITLE
  1148. // element.
  1149. //
  1150. // This relies on certain things being cached into the |aChromeTooltipListener|
  1151. // object passed to us by the timer:
  1152. // -- the x/y coordinates of the mouse (mMouseClientY, mMouseClientX)
  1153. // -- the dom node the user hovered over (mPossibleTooltipNode)
  1154. void
  1155. ChromeTooltipListener::sTooltipCallback(nsITimer* aTimer,
  1156. void* aChromeTooltipListener)
  1157. {
  1158. auto self = static_cast<ChromeTooltipListener*>(aChromeTooltipListener);
  1159. if (self && self->mPossibleTooltipNode) {
  1160. // The actual coordinates we want to put the tooltip at are relative to the
  1161. // toplevel docshell of our mWebBrowser. We know what the screen
  1162. // coordinates of the mouse event were, which means we just need the screen
  1163. // coordinates of the docshell. Unfortunately, there is no good way to
  1164. // find those short of groveling for the presentation in that docshell and
  1165. // finding the screen coords of its toplevel widget...
  1166. nsCOMPtr<nsIDocShell> docShell =
  1167. do_GetInterface(static_cast<nsIWebBrowser*>(self->mWebBrowser));
  1168. nsCOMPtr<nsIPresShell> shell;
  1169. if (docShell) {
  1170. shell = docShell->GetPresShell();
  1171. }
  1172. nsIWidget* widget = nullptr;
  1173. if (shell) {
  1174. nsViewManager* vm = shell->GetViewManager();
  1175. if (vm) {
  1176. nsView* view = vm->GetRootView();
  1177. if (view) {
  1178. nsPoint offset;
  1179. widget = view->GetNearestWidget(&offset);
  1180. }
  1181. }
  1182. }
  1183. if (!widget) {
  1184. // release tooltip target if there is one, NO MATTER WHAT
  1185. self->mPossibleTooltipNode = nullptr;
  1186. return;
  1187. }
  1188. // if there is text associated with the node, show the tip and fire
  1189. // off a timer to auto-hide it.
  1190. nsXPIDLString tooltipText;
  1191. nsXPIDLString directionText;
  1192. if (self->mTooltipTextProvider) {
  1193. bool textFound = false;
  1194. self->mTooltipTextProvider->GetNodeText(
  1195. self->mPossibleTooltipNode, getter_Copies(tooltipText),
  1196. getter_Copies(directionText), &textFound);
  1197. if (textFound) {
  1198. nsString tipText(tooltipText);
  1199. nsString dirText(directionText);
  1200. LayoutDeviceIntPoint screenDot = widget->WidgetToScreenOffset();
  1201. double scaleFactor = 1.0;
  1202. if (shell->GetPresContext()) {
  1203. nsDeviceContext* dc = shell->GetPresContext()->DeviceContext();
  1204. scaleFactor = double(nsPresContext::AppUnitsPerCSSPixel()) /
  1205. dc->AppUnitsPerDevPixelAtUnitFullZoom();
  1206. }
  1207. // ShowTooltip expects widget-relative position.
  1208. self->ShowTooltip(self->mMouseScreenX - screenDot.x / scaleFactor,
  1209. self->mMouseScreenY - screenDot.y / scaleFactor,
  1210. tipText, dirText);
  1211. }
  1212. }
  1213. // release tooltip target if there is one, NO MATTER WHAT
  1214. self->mPossibleTooltipNode = nullptr;
  1215. }
  1216. }
  1217. NS_IMPL_ISUPPORTS(ChromeContextMenuListener, nsIDOMEventListener)
  1218. ChromeContextMenuListener::ChromeContextMenuListener(
  1219. nsWebBrowser* aInBrowser,
  1220. nsIWebBrowserChrome* aInChrome)
  1221. : mContextMenuListenerInstalled(false)
  1222. , mWebBrowser(aInBrowser)
  1223. , mWebBrowserChrome(aInChrome)
  1224. {
  1225. }
  1226. ChromeContextMenuListener::~ChromeContextMenuListener()
  1227. {
  1228. }
  1229. // Subscribe to the events that will allow us to track context menus. Bascially,
  1230. // this is just the context-menu DOM event.
  1231. NS_IMETHODIMP
  1232. ChromeContextMenuListener::AddContextMenuListener()
  1233. {
  1234. if (mEventTarget) {
  1235. nsresult rv = mEventTarget->AddEventListener(
  1236. NS_LITERAL_STRING("contextmenu"), this, false, false);
  1237. NS_ENSURE_SUCCESS(rv, rv);
  1238. mContextMenuListenerInstalled = true;
  1239. }
  1240. return NS_OK;
  1241. }
  1242. // Unsubscribe from all the various context menu events that we were listening
  1243. // to.
  1244. NS_IMETHODIMP
  1245. ChromeContextMenuListener::RemoveContextMenuListener()
  1246. {
  1247. if (mEventTarget) {
  1248. nsresult rv = mEventTarget->RemoveEventListener(
  1249. NS_LITERAL_STRING("contextmenu"), this, false);
  1250. NS_ENSURE_SUCCESS(rv, rv);
  1251. mContextMenuListenerInstalled = false;
  1252. }
  1253. return NS_OK;
  1254. }
  1255. // Hook up things to the chrome like context menus and tooltips, if the chrome
  1256. // has implemented the right interfaces.
  1257. NS_IMETHODIMP
  1258. ChromeContextMenuListener::AddChromeListeners()
  1259. {
  1260. if (!mEventTarget) {
  1261. GetDOMEventTarget(mWebBrowser, getter_AddRefs(mEventTarget));
  1262. }
  1263. // Register the appropriate events for context menus, but only if
  1264. // the embedding chrome cares.
  1265. nsresult rv = NS_OK;
  1266. nsCOMPtr<nsIContextMenuListener2> contextListener2(
  1267. do_QueryInterface(mWebBrowserChrome));
  1268. nsCOMPtr<nsIContextMenuListener> contextListener(
  1269. do_QueryInterface(mWebBrowserChrome));
  1270. if ((contextListener || contextListener2) && !mContextMenuListenerInstalled) {
  1271. rv = AddContextMenuListener();
  1272. }
  1273. return rv;
  1274. }
  1275. // Unsubscribe from the various things we've hooked up to the window root.
  1276. NS_IMETHODIMP
  1277. ChromeContextMenuListener::RemoveChromeListeners()
  1278. {
  1279. if (mContextMenuListenerInstalled) {
  1280. RemoveContextMenuListener();
  1281. }
  1282. mEventTarget = nullptr;
  1283. // it really doesn't matter if these fail...
  1284. return NS_OK;
  1285. }
  1286. // We're on call to show the context menu. Dig around in the DOM to find the
  1287. // type of object we're dealing with and notify the front end chrome.
  1288. NS_IMETHODIMP
  1289. ChromeContextMenuListener::HandleEvent(nsIDOMEvent* aMouseEvent)
  1290. {
  1291. nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(aMouseEvent);
  1292. NS_ENSURE_TRUE(mouseEvent, NS_ERROR_UNEXPECTED);
  1293. bool isDefaultPrevented = false;
  1294. aMouseEvent->GetDefaultPrevented(&isDefaultPrevented);
  1295. if (isDefaultPrevented) {
  1296. return NS_OK;
  1297. }
  1298. nsCOMPtr<EventTarget> targetNode =
  1299. aMouseEvent->InternalDOMEvent()->GetTarget();
  1300. if (!targetNode) {
  1301. return NS_ERROR_NULL_POINTER;
  1302. }
  1303. nsCOMPtr<nsIDOMNode> targetDOMnode;
  1304. nsCOMPtr<nsIDOMNode> node = do_QueryInterface(targetNode);
  1305. if (!node) {
  1306. return NS_OK;
  1307. }
  1308. // Stop the context menu event going to other windows (bug 78396)
  1309. aMouseEvent->PreventDefault();
  1310. // If the listener is a nsIContextMenuListener2, create the info object
  1311. nsCOMPtr<nsIContextMenuListener2> menuListener2(
  1312. do_QueryInterface(mWebBrowserChrome));
  1313. nsContextMenuInfo* menuInfoImpl = nullptr;
  1314. nsCOMPtr<nsIContextMenuInfo> menuInfo;
  1315. if (menuListener2) {
  1316. menuInfoImpl = new nsContextMenuInfo;
  1317. menuInfo = menuInfoImpl;
  1318. }
  1319. uint32_t flags = nsIContextMenuListener::CONTEXT_NONE;
  1320. uint32_t flags2 = nsIContextMenuListener2::CONTEXT_NONE;
  1321. // XXX test for selected text
  1322. uint16_t nodeType;
  1323. nsresult res = node->GetNodeType(&nodeType);
  1324. NS_ENSURE_SUCCESS(res, res);
  1325. // First, checks for nodes that never have children.
  1326. if (nodeType == nsIDOMNode::ELEMENT_NODE) {
  1327. nsCOMPtr<nsIImageLoadingContent> content(do_QueryInterface(node));
  1328. if (content) {
  1329. nsCOMPtr<nsIURI> imgUri;
  1330. content->GetCurrentURI(getter_AddRefs(imgUri));
  1331. if (imgUri) {
  1332. flags |= nsIContextMenuListener::CONTEXT_IMAGE;
  1333. flags2 |= nsIContextMenuListener2::CONTEXT_IMAGE;
  1334. targetDOMnode = node;
  1335. }
  1336. }
  1337. nsCOMPtr<nsIFormControl> formControl(do_QueryInterface(node));
  1338. if (formControl) {
  1339. if (formControl->GetType() == NS_FORM_TEXTAREA) {
  1340. flags |= nsIContextMenuListener::CONTEXT_TEXT;
  1341. flags2 |= nsIContextMenuListener2::CONTEXT_TEXT;
  1342. targetDOMnode = node;
  1343. } else {
  1344. nsCOMPtr<nsIDOMHTMLInputElement> inputElement(
  1345. do_QueryInterface(formControl));
  1346. if (inputElement) {
  1347. flags |= nsIContextMenuListener::CONTEXT_INPUT;
  1348. flags2 |= nsIContextMenuListener2::CONTEXT_INPUT;
  1349. if (menuListener2) {
  1350. if (formControl->IsSingleLineTextControl(false)) {
  1351. flags2 |= nsIContextMenuListener2::CONTEXT_TEXT;
  1352. }
  1353. }
  1354. targetDOMnode = node;
  1355. }
  1356. }
  1357. }
  1358. // always consume events for plugins and Java who may throw their
  1359. // own context menus but not for image objects. Document objects
  1360. // will never be targets or ancestors of targets, so that's OK.
  1361. nsCOMPtr<nsIDOMHTMLObjectElement> objectElement;
  1362. if (!(flags & nsIContextMenuListener::CONTEXT_IMAGE)) {
  1363. objectElement = do_QueryInterface(node);
  1364. }
  1365. nsCOMPtr<nsIDOMHTMLEmbedElement> embedElement(do_QueryInterface(node));
  1366. nsCOMPtr<nsIDOMHTMLAppletElement> appletElement(do_QueryInterface(node));
  1367. if (objectElement || embedElement || appletElement) {
  1368. return NS_OK;
  1369. }
  1370. }
  1371. // Bubble out, looking for items of interest
  1372. do {
  1373. uint16_t nodeType;
  1374. res = node->GetNodeType(&nodeType);
  1375. NS_ENSURE_SUCCESS(res, res);
  1376. if (nodeType == nsIDOMNode::ELEMENT_NODE) {
  1377. // Test if the element has an associated link
  1378. nsCOMPtr<nsIDOMElement> element(do_QueryInterface(node));
  1379. bool hasAttr = false;
  1380. res = element->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
  1381. if (NS_SUCCEEDED(res) && hasAttr) {
  1382. flags |= nsIContextMenuListener::CONTEXT_LINK;
  1383. flags2 |= nsIContextMenuListener2::CONTEXT_LINK;
  1384. if (!targetDOMnode) {
  1385. targetDOMnode = node;
  1386. }
  1387. if (menuInfoImpl) {
  1388. menuInfoImpl->SetAssociatedLink(node);
  1389. }
  1390. break; // exit do-while
  1391. }
  1392. }
  1393. // walk-up-the-tree
  1394. nsCOMPtr<nsIDOMNode> parentNode;
  1395. node->GetParentNode(getter_AddRefs(parentNode));
  1396. node = parentNode;
  1397. } while (node);
  1398. if (!flags && !flags2) {
  1399. // We found nothing of interest so far, check if we
  1400. // have at least an html document.
  1401. nsCOMPtr<nsIDOMDocument> document;
  1402. node = do_QueryInterface(targetNode);
  1403. node->GetOwnerDocument(getter_AddRefs(document));
  1404. nsCOMPtr<nsIDOMHTMLDocument> htmlDocument(do_QueryInterface(document));
  1405. if (htmlDocument) {
  1406. flags |= nsIContextMenuListener::CONTEXT_DOCUMENT;
  1407. flags2 |= nsIContextMenuListener2::CONTEXT_DOCUMENT;
  1408. targetDOMnode = node;
  1409. if (!(flags & nsIContextMenuListener::CONTEXT_IMAGE)) {
  1410. // check if this is a background image that the user was trying to click
  1411. // on and if the listener is ready for that (only
  1412. // nsIContextMenuListener2 and up)
  1413. if (menuInfoImpl && menuInfoImpl->HasBackgroundImage(targetDOMnode)) {
  1414. flags2 |= nsIContextMenuListener2::CONTEXT_BACKGROUND_IMAGE;
  1415. // For the embedder to get the correct background image
  1416. // targetDOMnode must point to the original node.
  1417. targetDOMnode = do_QueryInterface(targetNode);
  1418. }
  1419. }
  1420. }
  1421. }
  1422. // we need to cache the event target into the focus controller's popupNode
  1423. // so we can get at it later from command code, etc.:
  1424. // get the dom window
  1425. nsCOMPtr<mozIDOMWindowProxy> win;
  1426. res = mWebBrowser->GetContentDOMWindow(getter_AddRefs(win));
  1427. NS_ENSURE_SUCCESS(res, res);
  1428. NS_ENSURE_TRUE(win, NS_ERROR_FAILURE);
  1429. auto* window = nsPIDOMWindowOuter::From(win);
  1430. nsCOMPtr<nsPIWindowRoot> root = window->GetTopWindowRoot();
  1431. NS_ENSURE_TRUE(root, NS_ERROR_FAILURE);
  1432. if (root) {
  1433. // set the window root's popup node to the event target
  1434. root->SetPopupNode(targetDOMnode);
  1435. }
  1436. // Tell the listener all about the event
  1437. if (menuListener2) {
  1438. menuInfoImpl->SetMouseEvent(aMouseEvent);
  1439. menuInfoImpl->SetDOMNode(targetDOMnode);
  1440. menuListener2->OnShowContextMenu(flags2, menuInfo);
  1441. } else {
  1442. nsCOMPtr<nsIContextMenuListener> menuListener(
  1443. do_QueryInterface(mWebBrowserChrome));
  1444. if (menuListener) {
  1445. menuListener->OnShowContextMenu(flags, aMouseEvent, targetDOMnode);
  1446. }
  1447. }
  1448. return NS_OK;
  1449. }