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