michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 sw=2 et tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: michael@0: This file provides the implementation for the XUL Command Dispatcher. michael@0: michael@0: */ michael@0: michael@0: #include "nsIContent.h" michael@0: #include "nsFocusManager.h" michael@0: #include "nsIControllers.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsIDOMElement.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsIDOMXULElement.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsIScriptGlobalObject.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsPIWindowRoot.h" michael@0: #include "nsRDFCID.h" michael@0: #include "nsXULCommandDispatcher.h" michael@0: #include "prlog.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsCRT.h" michael@0: #include "nsError.h" michael@0: #include "nsDOMClassInfoID.h" michael@0: #include "mozilla/BasicEvents.h" michael@0: #include "mozilla/EventDispatcher.h" michael@0: #include "mozilla/dom/Element.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: #ifdef PR_LOGGING michael@0: static PRLogModuleInfo* gCommandLog; michael@0: #endif michael@0: michael@0: //////////////////////////////////////////////////////////////////////// michael@0: michael@0: nsXULCommandDispatcher::nsXULCommandDispatcher(nsIDocument* aDocument) michael@0: : mDocument(aDocument), mUpdaters(nullptr) michael@0: { michael@0: michael@0: #ifdef PR_LOGGING michael@0: if (! gCommandLog) michael@0: gCommandLog = PR_NewLogModule("nsXULCommandDispatcher"); michael@0: #endif michael@0: } michael@0: michael@0: nsXULCommandDispatcher::~nsXULCommandDispatcher() michael@0: { michael@0: Disconnect(); michael@0: } michael@0: michael@0: // QueryInterface implementation for nsXULCommandDispatcher michael@0: michael@0: DOMCI_DATA(XULCommandDispatcher, nsXULCommandDispatcher) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULCommandDispatcher) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMXULCommandDispatcher) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMXULCommandDispatcher) michael@0: NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(XULCommandDispatcher) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULCommandDispatcher) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULCommandDispatcher) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULCommandDispatcher) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULCommandDispatcher) michael@0: tmp->Disconnect(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULCommandDispatcher) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument) michael@0: Updater* updater = tmp->mUpdaters; michael@0: while (updater) { michael@0: cb.NoteXPCOMChild(updater->mElement); michael@0: updater = updater->mNext; michael@0: } michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: void michael@0: nsXULCommandDispatcher::Disconnect() michael@0: { michael@0: while (mUpdaters) { michael@0: Updater* doomed = mUpdaters; michael@0: mUpdaters = mUpdaters->mNext; michael@0: delete doomed; michael@0: } michael@0: mDocument = nullptr; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsXULCommandDispatcher::GetWindowRoot() michael@0: { michael@0: if (mDocument) { michael@0: nsCOMPtr window(mDocument->GetWindow()); michael@0: if (window) { michael@0: return window->GetTopWindowRoot(); michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIContent* michael@0: nsXULCommandDispatcher::GetRootFocusedContentAndWindow(nsPIDOMWindow** aWindow) michael@0: { michael@0: *aWindow = nullptr; michael@0: michael@0: if (mDocument) { michael@0: nsCOMPtr win = mDocument->GetWindow(); michael@0: if (win) { michael@0: nsCOMPtr rootWindow = win->GetPrivateRoot(); michael@0: if (rootWindow) { michael@0: return nsFocusManager::GetFocusedDescendant(rootWindow, true, aWindow); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::GetFocusedElement(nsIDOMElement** aElement) michael@0: { michael@0: *aElement = nullptr; michael@0: michael@0: nsCOMPtr focusedWindow; michael@0: nsIContent* focusedContent = michael@0: GetRootFocusedContentAndWindow(getter_AddRefs(focusedWindow)); michael@0: if (focusedContent) { michael@0: CallQueryInterface(focusedContent, aElement); michael@0: michael@0: // Make sure the caller can access the focused element. michael@0: if (!nsContentUtils::CanCallerAccess(*aElement)) { michael@0: // XXX This might want to return null, but we use that return value michael@0: // to mean "there is no focused element," so to be clear, throw an michael@0: // exception. michael@0: NS_RELEASE(*aElement); michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::GetFocusedWindow(nsIDOMWindow** aWindow) michael@0: { michael@0: *aWindow = nullptr; michael@0: michael@0: nsCOMPtr window; michael@0: GetRootFocusedContentAndWindow(getter_AddRefs(window)); michael@0: if (!window) michael@0: return NS_OK; michael@0: michael@0: // Make sure the caller can access this window. The caller can access this michael@0: // window iff it can access the document. michael@0: nsCOMPtr domdoc; michael@0: nsresult rv = window->GetDocument(getter_AddRefs(domdoc)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Note: If there is no document, then this window has been cleared and michael@0: // there's nothing left to protect, so let the window pass through. michael@0: if (domdoc && !nsContentUtils::CanCallerAccess(domdoc)) michael@0: return NS_ERROR_DOM_SECURITY_ERR; michael@0: michael@0: CallQueryInterface(window, aWindow); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::SetFocusedElement(nsIDOMElement* aElement) michael@0: { michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: NS_ENSURE_TRUE(fm, NS_ERROR_FAILURE); michael@0: michael@0: if (aElement) michael@0: return fm->SetFocus(aElement, 0); michael@0: michael@0: // if aElement is null, clear the focus in the currently focused child window michael@0: nsCOMPtr focusedWindow; michael@0: GetRootFocusedContentAndWindow(getter_AddRefs(focusedWindow)); michael@0: return fm->ClearFocus(focusedWindow); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::SetFocusedWindow(nsIDOMWindow* aWindow) michael@0: { michael@0: NS_ENSURE_TRUE(aWindow, NS_OK); // do nothing if set to null michael@0: michael@0: nsCOMPtr window(do_QueryInterface(aWindow)); michael@0: NS_ENSURE_TRUE(window, NS_ERROR_FAILURE); michael@0: michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: NS_ENSURE_TRUE(fm, NS_ERROR_FAILURE); michael@0: michael@0: // get the containing frame for the window, and set it as focused. This will michael@0: // end up focusing whatever is currently focused inside the frame. Since michael@0: // setting the command dispatcher's focused window doesn't raise the window, michael@0: // setting it to a top-level window doesn't need to do anything. michael@0: nsCOMPtr frameElement = michael@0: do_QueryInterface(window->GetFrameElementInternal()); michael@0: if (frameElement) michael@0: return fm->SetFocus(frameElement, 0); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::AdvanceFocus() michael@0: { michael@0: return AdvanceFocusIntoSubtree(nullptr); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::RewindFocus() michael@0: { michael@0: nsCOMPtr win; michael@0: GetRootFocusedContentAndWindow(getter_AddRefs(win)); michael@0: michael@0: nsCOMPtr result; michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (fm) michael@0: return fm->MoveFocus(win, nullptr, nsIFocusManager::MOVEFOCUS_BACKWARD, michael@0: 0, getter_AddRefs(result)); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::AdvanceFocusIntoSubtree(nsIDOMElement* aElt) michael@0: { michael@0: nsCOMPtr win; michael@0: GetRootFocusedContentAndWindow(getter_AddRefs(win)); michael@0: michael@0: nsCOMPtr result; michael@0: nsIFocusManager* fm = nsFocusManager::GetFocusManager(); michael@0: if (fm) michael@0: return fm->MoveFocus(win, aElt, nsIFocusManager::MOVEFOCUS_FORWARD, michael@0: 0, getter_AddRefs(result)); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::AddCommandUpdater(nsIDOMElement* aElement, michael@0: const nsAString& aEvents, michael@0: const nsAString& aTargets) michael@0: { michael@0: NS_PRECONDITION(aElement != nullptr, "null ptr"); michael@0: if (! aElement) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: NS_ENSURE_TRUE(mDocument, NS_ERROR_UNEXPECTED); michael@0: michael@0: nsresult rv = nsContentUtils::CheckSameOrigin(mDocument, aElement); michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: Updater* updater = mUpdaters; michael@0: Updater** link = &mUpdaters; michael@0: michael@0: while (updater) { michael@0: if (updater->mElement == aElement) { michael@0: michael@0: #ifdef DEBUG michael@0: if (PR_LOG_TEST(gCommandLog, PR_LOG_NOTICE)) { michael@0: nsAutoCString eventsC, targetsC, aeventsC, atargetsC; michael@0: eventsC.AssignWithConversion(updater->mEvents); michael@0: targetsC.AssignWithConversion(updater->mTargets); michael@0: CopyUTF16toUTF8(aEvents, aeventsC); michael@0: CopyUTF16toUTF8(aTargets, atargetsC); michael@0: PR_LOG(gCommandLog, PR_LOG_NOTICE, michael@0: ("xulcmd[%p] replace %p(events=%s targets=%s) with (events=%s targets=%s)", michael@0: this, aElement, michael@0: eventsC.get(), michael@0: targetsC.get(), michael@0: aeventsC.get(), michael@0: atargetsC.get())); michael@0: } michael@0: #endif michael@0: michael@0: // If the updater was already in the list, then replace michael@0: // (?) the 'events' and 'targets' filters with the new michael@0: // specification. michael@0: updater->mEvents = aEvents; michael@0: updater->mTargets = aTargets; michael@0: return NS_OK; michael@0: } michael@0: michael@0: link = &(updater->mNext); michael@0: updater = updater->mNext; michael@0: } michael@0: #ifdef DEBUG michael@0: if (PR_LOG_TEST(gCommandLog, PR_LOG_NOTICE)) { michael@0: nsAutoCString aeventsC, atargetsC; michael@0: CopyUTF16toUTF8(aEvents, aeventsC); michael@0: CopyUTF16toUTF8(aTargets, atargetsC); michael@0: michael@0: PR_LOG(gCommandLog, PR_LOG_NOTICE, michael@0: ("xulcmd[%p] add %p(events=%s targets=%s)", michael@0: this, aElement, michael@0: aeventsC.get(), michael@0: atargetsC.get())); michael@0: } michael@0: #endif michael@0: michael@0: // If we get here, this is a new updater. Append it to the list. michael@0: updater = new Updater(aElement, aEvents, aTargets); michael@0: if (! updater) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: *link = updater; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::RemoveCommandUpdater(nsIDOMElement* aElement) michael@0: { michael@0: NS_PRECONDITION(aElement != nullptr, "null ptr"); michael@0: if (! aElement) michael@0: return NS_ERROR_NULL_POINTER; michael@0: michael@0: Updater* updater = mUpdaters; michael@0: Updater** link = &mUpdaters; michael@0: michael@0: while (updater) { michael@0: if (updater->mElement == aElement) { michael@0: #ifdef DEBUG michael@0: if (PR_LOG_TEST(gCommandLog, PR_LOG_NOTICE)) { michael@0: nsAutoCString eventsC, targetsC; michael@0: eventsC.AssignWithConversion(updater->mEvents); michael@0: targetsC.AssignWithConversion(updater->mTargets); michael@0: PR_LOG(gCommandLog, PR_LOG_NOTICE, michael@0: ("xulcmd[%p] remove %p(events=%s targets=%s)", michael@0: this, aElement, michael@0: eventsC.get(), michael@0: targetsC.get())); michael@0: } michael@0: #endif michael@0: michael@0: *link = updater->mNext; michael@0: delete updater; michael@0: return NS_OK; michael@0: } michael@0: michael@0: link = &(updater->mNext); michael@0: updater = updater->mNext; michael@0: } michael@0: michael@0: // Hmm. Not found. Oh well. michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::UpdateCommands(const nsAString& aEventName) michael@0: { michael@0: nsAutoString id; michael@0: nsCOMPtr element; michael@0: GetFocusedElement(getter_AddRefs(element)); michael@0: if (element) { michael@0: nsresult rv = element->GetAttribute(NS_LITERAL_STRING("id"), id); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get element's id"); michael@0: if (NS_FAILED(rv)) return rv; michael@0: } michael@0: michael@0: nsCOMArray updaters; michael@0: michael@0: for (Updater* updater = mUpdaters; updater != nullptr; updater = updater->mNext) { michael@0: // Skip any nodes that don't match our 'events' or 'targets' michael@0: // filters. michael@0: if (! Matches(updater->mEvents, aEventName)) michael@0: continue; michael@0: michael@0: if (! Matches(updater->mTargets, id)) michael@0: continue; michael@0: michael@0: nsCOMPtr content = do_QueryInterface(updater->mElement); michael@0: NS_ASSERTION(content != nullptr, "not an nsIContent"); michael@0: if (! content) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: updaters.AppendObject(content); michael@0: } michael@0: michael@0: for (int32_t u = 0; u < updaters.Count(); u++) { michael@0: nsIContent* content = updaters[u]; michael@0: michael@0: nsCOMPtr document = content->GetDocument(); michael@0: michael@0: NS_ASSERTION(document != nullptr, "element has no document"); michael@0: if (! document) michael@0: continue; michael@0: michael@0: #ifdef DEBUG michael@0: if (PR_LOG_TEST(gCommandLog, PR_LOG_NOTICE)) { michael@0: nsAutoCString aeventnameC; michael@0: CopyUTF16toUTF8(aEventName, aeventnameC); michael@0: PR_LOG(gCommandLog, PR_LOG_NOTICE, michael@0: ("xulcmd[%p] update %p event=%s", michael@0: this, content, michael@0: aeventnameC.get())); michael@0: } michael@0: #endif michael@0: michael@0: nsCOMPtr shell = document->GetShell(); michael@0: if (shell) { michael@0: // Retrieve the context in which our DOM event will fire. michael@0: nsRefPtr context = shell->GetPresContext(); michael@0: michael@0: // Handle the DOM event michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: michael@0: WidgetEvent event(true, NS_XUL_COMMAND_UPDATE); michael@0: michael@0: EventDispatcher::Dispatch(content, context, &event, nullptr, &status); michael@0: } michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: nsXULCommandDispatcher::Matches(const nsString& aList, michael@0: const nsAString& aElement) michael@0: { michael@0: if (aList.EqualsLiteral("*")) michael@0: return true; // match _everything_! michael@0: michael@0: int32_t indx = aList.Find(PromiseFlatString(aElement)); michael@0: if (indx == -1) michael@0: return false; // not in the list at all michael@0: michael@0: // okay, now make sure it's not a substring snafu; e.g., 'ur' michael@0: // found inside of 'blur'. michael@0: if (indx > 0) { michael@0: char16_t ch = aList[indx - 1]; michael@0: if (! nsCRT::IsAsciiSpace(ch) && ch != char16_t(',')) michael@0: return false; michael@0: } michael@0: michael@0: if (indx + aElement.Length() < aList.Length()) { michael@0: char16_t ch = aList[indx + aElement.Length()]; michael@0: if (! nsCRT::IsAsciiSpace(ch) && ch != char16_t(',')) michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::GetControllers(nsIControllers** aResult) michael@0: { michael@0: nsCOMPtr root = GetWindowRoot(); michael@0: NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); michael@0: michael@0: return root->GetControllers(aResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::GetControllerForCommand(const char *aCommand, nsIController** _retval) michael@0: { michael@0: nsCOMPtr root = GetWindowRoot(); michael@0: NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); michael@0: michael@0: return root->GetControllerForCommand(aCommand, _retval); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::GetSuppressFocusScroll(bool* aSuppressFocusScroll) michael@0: { michael@0: *aSuppressFocusScroll = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXULCommandDispatcher::SetSuppressFocusScroll(bool aSuppressFocusScroll) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: