gecko-dev/dom/xul/nsXULCommandDispatcher.cpp

460 строки
13 KiB
C++
Исходник Обычный вид История

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