nsWindowDataSource.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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 "nsWindowDataSource.h"
  6. #include "nsIXULWindow.h"
  7. #include "rdf.h"
  8. #include "nsIRDFContainerUtils.h"
  9. #include "nsIServiceManager.h"
  10. #include "nsReadableUtils.h"
  11. #include "nsIObserverService.h"
  12. #include "nsIWindowMediator.h"
  13. #include "nsXPCOMCID.h"
  14. #include "mozilla/ModuleUtils.h"
  15. #include "nsString.h"
  16. // just to do the reverse-lookup! sheesh.
  17. #include "nsIInterfaceRequestorUtils.h"
  18. #include "nsIDocShell.h"
  19. uint32_t nsWindowDataSource::windowCount = 0;
  20. nsIRDFResource* nsWindowDataSource::kNC_Name = nullptr;
  21. nsIRDFResource* nsWindowDataSource::kNC_WindowRoot = nullptr;
  22. nsIRDFResource* nsWindowDataSource::kNC_KeyIndex = nullptr;
  23. nsIRDFService* nsWindowDataSource::gRDFService = nullptr;
  24. uint32_t nsWindowDataSource::gRefCnt = 0;
  25. #define URINC_WINDOWROOT "NC:WindowMediatorRoot"
  26. #define URINC_NAME NC_NAMESPACE_URI "Name"
  27. #define URINC_KEYINDEX NC_NAMESPACE_URI "KeyIndex"
  28. nsresult
  29. nsWindowDataSource::Init()
  30. {
  31. nsresult rv;
  32. if (gRefCnt++ == 0) {
  33. rv = CallGetService("@mozilla.org/rdf/rdf-service;1", &gRDFService);
  34. if (NS_FAILED(rv)) return rv;
  35. gRDFService->GetResource(NS_LITERAL_CSTRING(URINC_WINDOWROOT), &kNC_WindowRoot);
  36. gRDFService->GetResource(NS_LITERAL_CSTRING(URINC_NAME), &kNC_Name);
  37. gRDFService->GetResource(NS_LITERAL_CSTRING(URINC_KEYINDEX), &kNC_KeyIndex);
  38. }
  39. mInner = do_CreateInstance("@mozilla.org/rdf/datasource;1?name=in-memory-datasource", &rv);
  40. if (NS_FAILED(rv)) return rv;
  41. nsCOMPtr<nsIRDFContainerUtils> rdfc =
  42. do_GetService("@mozilla.org/rdf/container-utils;1", &rv);
  43. if (NS_FAILED(rv)) return rv;
  44. rv = rdfc->MakeSeq(this, kNC_WindowRoot, getter_AddRefs(mContainer));
  45. if (NS_FAILED(rv)) return rv;
  46. nsCOMPtr<nsIWindowMediator> windowMediator =
  47. do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
  48. if (NS_FAILED(rv)) return rv;
  49. rv = windowMediator->AddListener(this);
  50. if (NS_FAILED(rv)) return rv;
  51. nsCOMPtr<nsIObserverService> observerService =
  52. do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
  53. if (NS_SUCCEEDED(rv)) {
  54. rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
  55. false);
  56. }
  57. return NS_OK;
  58. }
  59. nsWindowDataSource::~nsWindowDataSource()
  60. {
  61. if (--gRefCnt == 0) {
  62. NS_IF_RELEASE(kNC_Name);
  63. NS_IF_RELEASE(kNC_KeyIndex);
  64. NS_IF_RELEASE(kNC_WindowRoot);
  65. NS_IF_RELEASE(gRDFService);
  66. }
  67. }
  68. NS_IMETHODIMP
  69. nsWindowDataSource::Observe(nsISupports *aSubject, const char* aTopic, const char16_t *aData)
  70. {
  71. if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
  72. // release these objects so that they release their reference
  73. // to us
  74. mContainer = nullptr;
  75. mInner = nullptr;
  76. }
  77. return NS_OK;
  78. }
  79. NS_IMPL_CYCLE_COLLECTION_CLASS(nsWindowDataSource)
  80. NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsWindowDataSource)
  81. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsWindowDataSource)
  82. // XXX mContainer?
  83. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInner)
  84. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  85. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsWindowDataSource)
  86. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsWindowDataSource)
  87. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsWindowDataSource)
  88. NS_INTERFACE_MAP_ENTRY(nsIObserver)
  89. NS_INTERFACE_MAP_ENTRY(nsIWindowMediatorListener)
  90. NS_INTERFACE_MAP_ENTRY(nsIWindowDataSource)
  91. NS_INTERFACE_MAP_ENTRY(nsIRDFDataSource)
  92. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
  93. NS_INTERFACE_MAP_END
  94. // nsIWindowMediatorListener implementation
  95. // handle notifications from the window mediator and reflect them into
  96. // RDF
  97. NS_IMETHODIMP
  98. nsWindowDataSource::OnWindowTitleChange(nsIXULWindow *window,
  99. const char16_t *newTitle)
  100. {
  101. nsresult rv;
  102. nsCOMPtr<nsIRDFResource> windowResource;
  103. mWindowResources.Get(window, getter_AddRefs(windowResource));
  104. // oops, make sure this window is in the hashtable!
  105. if (!windowResource) {
  106. OnOpenWindow(window);
  107. mWindowResources.Get(window, getter_AddRefs(windowResource));
  108. }
  109. NS_ENSURE_TRUE(windowResource, NS_ERROR_UNEXPECTED);
  110. nsCOMPtr<nsIRDFLiteral> newTitleLiteral;
  111. rv = gRDFService->GetLiteral(newTitle, getter_AddRefs(newTitleLiteral));
  112. NS_ENSURE_SUCCESS(rv, rv);
  113. // get the old title
  114. nsCOMPtr<nsIRDFNode> oldTitleNode;
  115. rv = GetTarget(windowResource, kNC_Name, true,
  116. getter_AddRefs(oldTitleNode));
  117. // assert the change
  118. if (NS_SUCCEEDED(rv) && oldTitleNode)
  119. // has an existing window title, update it
  120. rv = Change(windowResource, kNC_Name, oldTitleNode, newTitleLiteral);
  121. else
  122. // removed from the tasklist
  123. rv = Assert(windowResource, kNC_Name, newTitleLiteral, true);
  124. if (rv != NS_RDF_ASSERTION_ACCEPTED)
  125. {
  126. NS_ERROR("unable to set window name");
  127. }
  128. return NS_OK;
  129. }
  130. NS_IMETHODIMP
  131. nsWindowDataSource::OnOpenWindow(nsIXULWindow *window)
  132. {
  133. nsAutoCString windowId(NS_LITERAL_CSTRING("window-"));
  134. windowId.AppendInt(windowCount++, 10);
  135. nsCOMPtr<nsIRDFResource> windowResource;
  136. gRDFService->GetResource(windowId, getter_AddRefs(windowResource));
  137. mWindowResources.Put(window, windowResource);
  138. // assert the new window
  139. if (mContainer)
  140. mContainer->AppendElement(windowResource);
  141. return NS_OK;
  142. }
  143. NS_IMETHODIMP
  144. nsWindowDataSource::OnCloseWindow(nsIXULWindow *window)
  145. {
  146. nsresult rv;
  147. nsCOMPtr<nsIRDFResource> resource;
  148. mWindowResources.Get(window, getter_AddRefs(resource));
  149. if (!resource) {
  150. return NS_ERROR_UNEXPECTED;
  151. }
  152. mWindowResources.Remove(window);
  153. // make sure we're not shutting down
  154. if (!mContainer) return NS_OK;
  155. nsCOMPtr<nsIRDFNode> oldKeyNode;
  156. nsCOMPtr<nsIRDFInt> oldKeyInt;
  157. // get the old keyIndex, if any
  158. rv = GetTarget(resource, kNC_KeyIndex, true,
  159. getter_AddRefs(oldKeyNode));
  160. if (NS_SUCCEEDED(rv) && (rv != NS_RDF_NO_VALUE))
  161. oldKeyInt = do_QueryInterface(oldKeyNode);
  162. // update RDF and keyindex - from this point forward we'll ignore
  163. // errors, because they just indicate some kind of RDF inconsistency
  164. int32_t winIndex = -1;
  165. rv = mContainer->IndexOf(resource, &winIndex);
  166. if (NS_FAILED(rv))
  167. return NS_OK;
  168. // unassert the old window, ignore any error
  169. mContainer->RemoveElement(resource, true);
  170. nsCOMPtr<nsISimpleEnumerator> children;
  171. rv = mContainer->GetElements(getter_AddRefs(children));
  172. if (NS_FAILED(rv))
  173. return NS_OK;
  174. bool more = false;
  175. while (NS_SUCCEEDED(rv = children->HasMoreElements(&more)) && more) {
  176. nsCOMPtr<nsISupports> sup;
  177. rv = children->GetNext(getter_AddRefs(sup));
  178. if (NS_FAILED(rv))
  179. break;
  180. nsCOMPtr<nsIRDFResource> windowResource = do_QueryInterface(sup, &rv);
  181. if (NS_FAILED(rv))
  182. continue;
  183. int32_t currentIndex = -1;
  184. mContainer->IndexOf(windowResource, &currentIndex);
  185. // can skip updating windows with lower indexes
  186. // than the window that was removed
  187. if (currentIndex < winIndex)
  188. continue;
  189. nsCOMPtr<nsIRDFNode> newKeyNode;
  190. nsCOMPtr<nsIRDFInt> newKeyInt;
  191. rv = GetTarget(windowResource, kNC_KeyIndex, true,
  192. getter_AddRefs(newKeyNode));
  193. if (NS_SUCCEEDED(rv) && (rv != NS_RDF_NO_VALUE))
  194. newKeyInt = do_QueryInterface(newKeyNode);
  195. // changing from one key index to another
  196. if (oldKeyInt && newKeyInt)
  197. Change(windowResource, kNC_KeyIndex, oldKeyInt, newKeyInt);
  198. // creating a new keyindex - probably window going
  199. // from (none) to "9"
  200. else if (newKeyInt)
  201. Assert(windowResource, kNC_KeyIndex, newKeyInt, true);
  202. // somehow inserting a window above this one,
  203. // "9" to (none)
  204. else if (oldKeyInt)
  205. Unassert(windowResource, kNC_KeyIndex, oldKeyInt);
  206. }
  207. return NS_OK;
  208. }
  209. // nsIWindowDataSource implementation
  210. NS_IMETHODIMP
  211. nsWindowDataSource::GetWindowForResource(const char *aResourceString,
  212. nsIDOMWindow** aResult)
  213. {
  214. if (NS_WARN_IF(!aResourceString)) {
  215. return NS_ERROR_INVALID_ARG;
  216. }
  217. nsCOMPtr<nsIRDFResource> windowResource;
  218. gRDFService->GetResource(nsDependentCString(aResourceString),
  219. getter_AddRefs(windowResource));
  220. // now reverse-lookup in the hashtable
  221. for (auto iter = mWindowResources.Iter(); !iter.Done(); iter.Next()) {
  222. nsIXULWindow* window = iter.Key();
  223. nsIRDFResource* resource = iter.UserData();
  224. if (resource == windowResource) {
  225. // This sucks, we have to jump through docshell to go from
  226. // nsIXULWindow -> nsIDOMWindow.
  227. nsCOMPtr<nsIDocShell> docShell;
  228. window->GetDocShell(getter_AddRefs(docShell));
  229. if (docShell) {
  230. nsCOMPtr<nsIDOMWindow> result = do_GetInterface(docShell);
  231. *aResult = result;
  232. NS_IF_ADDREF(*aResult);
  233. }
  234. break;
  235. }
  236. }
  237. return NS_OK;
  238. }
  239. // nsIRDFDataSource implementation
  240. // mostly, we just forward to mInner, except:
  241. // GetURI() - need to return "rdf:window-mediator"
  242. // GetTarget() - need to handle kNC_KeyIndex
  243. NS_IMETHODIMP nsWindowDataSource::GetURI(char * *aURI)
  244. {
  245. NS_ENSURE_ARG_POINTER(aURI);
  246. *aURI = ToNewCString(NS_LITERAL_CSTRING("rdf:window-mediator"));
  247. if (!*aURI)
  248. return NS_ERROR_OUT_OF_MEMORY;
  249. return NS_OK;
  250. }
  251. NS_IMETHODIMP nsWindowDataSource::GetTarget(nsIRDFResource *aSource, nsIRDFResource *aProperty, bool aTruthValue, nsIRDFNode **_retval)
  252. {
  253. NS_ENSURE_ARG_POINTER(_retval);
  254. // add extra nullptr checking for top-crash bug # 146466
  255. if (!gRDFService) return NS_RDF_NO_VALUE;
  256. if (!mInner) return NS_RDF_NO_VALUE;
  257. if (!mContainer) return NS_RDF_NO_VALUE;
  258. // special case kNC_KeyIndex before we forward to mInner
  259. if (aProperty == kNC_KeyIndex) {
  260. int32_t theIndex = 0;
  261. nsresult rv = mContainer->IndexOf(aSource, &theIndex);
  262. if (NS_FAILED(rv)) return rv;
  263. // only allow the range of 1 to 9 for single key access
  264. if (theIndex < 1 || theIndex > 9) return(NS_RDF_NO_VALUE);
  265. nsCOMPtr<nsIRDFInt> indexInt;
  266. rv = gRDFService->GetIntLiteral(theIndex, getter_AddRefs(indexInt));
  267. if (NS_FAILED(rv)) return(rv);
  268. if (!indexInt) return(NS_ERROR_FAILURE);
  269. indexInt.forget(_retval);
  270. return NS_OK;
  271. }
  272. return mInner->GetTarget(aSource, aProperty, aTruthValue, _retval);
  273. }
  274. NS_IMETHODIMP nsWindowDataSource::GetSource(nsIRDFResource *aProperty, nsIRDFNode *aTarget, bool aTruthValue, nsIRDFResource **_retval)
  275. {
  276. if (mInner)
  277. return mInner->GetSource(aProperty, aTarget, aTruthValue, _retval);
  278. return NS_OK;
  279. }
  280. NS_IMETHODIMP nsWindowDataSource::GetSources(nsIRDFResource *aProperty, nsIRDFNode *aTarget, bool aTruthValue, nsISimpleEnumerator **_retval)
  281. {
  282. if (mInner)
  283. return mInner->GetSources(aProperty, aTarget, aTruthValue, _retval);
  284. return NS_OK;
  285. }
  286. NS_IMETHODIMP nsWindowDataSource::GetTargets(nsIRDFResource *aSource, nsIRDFResource *aProperty, bool aTruthValue, nsISimpleEnumerator **_retval)
  287. {
  288. if (mInner)
  289. return mInner->GetTargets(aSource, aProperty, aTruthValue, _retval);
  290. return NS_OK;
  291. }
  292. NS_IMETHODIMP nsWindowDataSource::Assert(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, bool aTruthValue)
  293. {
  294. if (mInner)
  295. return mInner->Assert(aSource, aProperty, aTarget, aTruthValue);
  296. return NS_OK;
  297. }
  298. NS_IMETHODIMP nsWindowDataSource::Unassert(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget)
  299. {
  300. if (mInner)
  301. return mInner->Unassert(aSource, aProperty, aTarget);
  302. return NS_OK;
  303. }
  304. NS_IMETHODIMP nsWindowDataSource::Change(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aOldTarget, nsIRDFNode *aNewTarget)
  305. {
  306. if (mInner)
  307. return mInner->Change(aSource, aProperty, aOldTarget, aNewTarget);
  308. return NS_OK;
  309. }
  310. NS_IMETHODIMP nsWindowDataSource::Move(nsIRDFResource *aOldSource, nsIRDFResource *aNewSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget)
  311. {
  312. if (mInner)
  313. return mInner->Move(aOldSource, aNewSource, aProperty, aTarget);
  314. return NS_OK;
  315. }
  316. NS_IMETHODIMP nsWindowDataSource::HasAssertion(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, bool aTruthValue, bool *_retval)
  317. {
  318. if (mInner)
  319. return mInner->HasAssertion(aSource, aProperty, aTarget, aTruthValue, _retval);
  320. return NS_OK;
  321. }
  322. NS_IMETHODIMP nsWindowDataSource::AddObserver(nsIRDFObserver *aObserver)
  323. {
  324. if (mInner)
  325. return mInner->AddObserver(aObserver);
  326. return NS_OK;
  327. }
  328. NS_IMETHODIMP nsWindowDataSource::RemoveObserver(nsIRDFObserver *aObserver)
  329. {
  330. if (mInner)
  331. return mInner->RemoveObserver(aObserver);
  332. return NS_OK;
  333. }
  334. NS_IMETHODIMP nsWindowDataSource::ArcLabelsIn(nsIRDFNode *aNode, nsISimpleEnumerator **_retval)
  335. {
  336. if (mInner)
  337. return mInner->ArcLabelsIn(aNode, _retval);
  338. return NS_OK;
  339. }
  340. NS_IMETHODIMP nsWindowDataSource::ArcLabelsOut(nsIRDFResource *aSource, nsISimpleEnumerator **_retval)
  341. {
  342. if (mInner)
  343. return mInner->ArcLabelsOut(aSource, _retval);
  344. return NS_OK;
  345. }
  346. NS_IMETHODIMP nsWindowDataSource::GetAllResources(nsISimpleEnumerator **_retval)
  347. {
  348. if (mInner)
  349. return mInner->GetAllResources(_retval);
  350. return NS_OK;
  351. }
  352. NS_IMETHODIMP nsWindowDataSource::IsCommandEnabled(nsISupports *aSources, nsIRDFResource *aCommand, nsISupports *aArguments, bool *_retval)
  353. {
  354. return NS_ERROR_NOT_IMPLEMENTED;
  355. }
  356. NS_IMETHODIMP nsWindowDataSource::DoCommand(nsISupports *aSources, nsIRDFResource *aCommand, nsISupports *aArguments)
  357. {
  358. return NS_ERROR_NOT_IMPLEMENTED;
  359. }
  360. NS_IMETHODIMP nsWindowDataSource::GetAllCmds(nsIRDFResource *aSource, nsISimpleEnumerator **_retval)
  361. {
  362. if (mInner)
  363. return mInner->GetAllCmds(aSource, _retval);
  364. return NS_OK;
  365. }
  366. NS_IMETHODIMP nsWindowDataSource::HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, bool *_retval)
  367. {
  368. if (mInner)
  369. return mInner->HasArcIn(aNode, aArc, _retval);
  370. return NS_OK;
  371. }
  372. NS_IMETHODIMP nsWindowDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, bool *_retval)
  373. {
  374. if (mInner)
  375. return mInner->HasArcOut(aSource, aArc, _retval);
  376. return NS_OK;
  377. }
  378. NS_IMETHODIMP nsWindowDataSource::BeginUpdateBatch()
  379. {
  380. if (mInner)
  381. return mInner->BeginUpdateBatch();
  382. return NS_OK;
  383. }
  384. NS_IMETHODIMP nsWindowDataSource::EndUpdateBatch()
  385. {
  386. if (mInner)
  387. return mInner->EndUpdateBatch();
  388. return NS_OK;
  389. }
  390. // The module goop
  391. NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsWindowDataSource, Init)
  392. NS_DEFINE_NAMED_CID(NS_WINDOWDATASOURCE_CID);
  393. static const mozilla::Module::CIDEntry kWindowDSCIDs[] = {
  394. { &kNS_WINDOWDATASOURCE_CID, false, nullptr, nsWindowDataSourceConstructor },
  395. { nullptr }
  396. };
  397. static const mozilla::Module::ContractIDEntry kWindowDSContracts[] = {
  398. { NS_RDF_DATASOURCE_CONTRACTID_PREFIX "window-mediator", &kNS_WINDOWDATASOURCE_CID },
  399. { nullptr }
  400. };
  401. static const mozilla::Module::CategoryEntry kWindowDSCategories[] = {
  402. { "app-startup", "Window Data Source", "service," NS_RDF_DATASOURCE_CONTRACTID_PREFIX "window-mediator" },
  403. { nullptr }
  404. };
  405. static const mozilla::Module kWindowDSModule = {
  406. mozilla::Module::kVersion,
  407. kWindowDSCIDs,
  408. kWindowDSContracts,
  409. kWindowDSCategories
  410. };
  411. NSMODULE_DEFN(nsWindowDataSourceModule) = &kWindowDSModule;