Back to index

lightning-sunbird  0.9+nobinonly
nsXULCommandDispatcher.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim: set ts=2 sw=2 et tw=80: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Original Author: David W. Hyatt (hyatt@netscape.com)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041 
00042   This file provides the implementation for the XUL Command Dispatcher.
00043 
00044  */
00045 
00046 #include "nsIContent.h"
00047 #include "nsIFocusController.h"
00048 #include "nsIControllers.h"
00049 #include "nsIDOMDocument.h"
00050 #include "nsIDOMXULDocument.h"
00051 #include "nsIDOMHTMLDocument.h"
00052 #include "nsIDOMElement.h"
00053 #include "nsIDOMNSHTMLInputElement.h"
00054 #include "nsIDOMNSHTMLTextAreaElement.h"
00055 #include "nsIDOMWindowInternal.h"
00056 #include "nsIDOMXULElement.h"
00057 #include "nsIDocument.h"
00058 #include "nsPresContext.h"
00059 #include "nsIPresShell.h"
00060 #include "nsIScriptGlobalObject.h"
00061 #include "nsPIDOMWindow.h"
00062 #include "nsRDFCID.h"
00063 #include "nsXULCommandDispatcher.h"
00064 #include "prlog.h"
00065 #include "nsIDOMEventTarget.h"
00066 #include "nsGUIEvent.h"
00067 #include "nsContentUtils.h"
00068 #include "nsReadableUtils.h"
00069 #include "nsCRT.h"
00070 #include "nsDOMError.h"
00071 #include "nsIDOMNSDocument.h"
00072 
00073 #ifdef PR_LOGGING
00074 static PRLogModuleInfo* gLog;
00075 #endif
00076 
00078 
00079 nsXULCommandDispatcher::nsXULCommandDispatcher(nsIDocument* aDocument)
00080     : mFocusController(nsnull), mDocument(aDocument), mUpdaters(nsnull)
00081 {
00082 
00083 #ifdef PR_LOGGING
00084   if (! gLog)
00085     gLog = PR_NewLogModule("nsXULCommandDispatcher");
00086 #endif
00087 }
00088 
00089 nsXULCommandDispatcher::~nsXULCommandDispatcher()
00090 {
00091   while (mUpdaters) {
00092     Updater* doomed = mUpdaters;
00093     mUpdaters = mUpdaters->mNext;
00094     delete doomed;
00095   }
00096 }
00097 
00098 // QueryInterface implementation for nsXULCommandDispatcher
00099 NS_INTERFACE_MAP_BEGIN(nsXULCommandDispatcher)
00100     NS_INTERFACE_MAP_ENTRY(nsIDOMXULCommandDispatcher)
00101     NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
00102     NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMXULCommandDispatcher)
00103     NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(XULCommandDispatcher)
00104 NS_INTERFACE_MAP_END
00105 
00106 
00107 NS_IMPL_ADDREF(nsXULCommandDispatcher)
00108 NS_IMPL_RELEASE(nsXULCommandDispatcher)
00109 
00110 
00111 NS_IMETHODIMP
00112 nsXULCommandDispatcher::Create(nsIDocument* aDoc, nsXULCommandDispatcher** aResult)
00113 {
00114   nsXULCommandDispatcher* dispatcher = new nsXULCommandDispatcher(aDoc);
00115   if (!dispatcher)
00116     return NS_ERROR_OUT_OF_MEMORY;
00117 
00118   *aResult = dispatcher;
00119   NS_ADDREF(*aResult);
00120   return NS_OK;
00121 }
00122 
00123 void
00124 nsXULCommandDispatcher::EnsureFocusController()
00125 {
00126   if (!mFocusController && mDocument) {
00127     nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(mDocument->GetScriptGlobalObject()));
00128   
00129     // An inelegant way to retrieve this to be sure, but we are
00130     // guaranteed that the focus controller outlives us, so it
00131     // is safe to hold on to it (since we can't die until it has
00132     // died).
00133     mFocusController = win->GetRootFocusController(); // Store as a weak ptr.
00134   }
00135 }
00136 
00138 // nsIDOMXULTracker Interface
00139 
00140 NS_IMETHODIMP
00141 nsXULCommandDispatcher::GetFocusedElement(nsIDOMElement** aElement)
00142 {
00143   EnsureFocusController();
00144   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00145 
00146   nsresult rv = mFocusController->GetFocusedElement(aElement);
00147   NS_ENSURE_SUCCESS(rv, rv);
00148 
00149   // Make sure the caller can access the focused element.
00150   if (*aElement && !nsContentUtils::CanCallerAccess(*aElement)) {
00151     // XXX This might want to return null, but we use that return value
00152     // to mean "there is no focused element," so to be clear, throw an
00153     // exception.
00154     NS_RELEASE(*aElement);
00155     return NS_ERROR_DOM_SECURITY_ERR;
00156   }
00157 
00158   return NS_OK;
00159 }
00160 
00161 NS_IMETHODIMP
00162 nsXULCommandDispatcher::GetFocusedWindow(nsIDOMWindow** aWindow)
00163 {
00164   EnsureFocusController();
00165   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00166 
00167   nsCOMPtr<nsIDOMWindowInternal> window;
00168   nsresult rv = mFocusController->GetFocusedWindow(getter_AddRefs(window));
00169   NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && window, rv);
00170 
00171   rv = CallQueryInterface(window, aWindow);
00172   NS_ENSURE_SUCCESS(rv, rv);
00173 
00174   // Make sure the caller can access this window. The caller can access this
00175   // window iff it can access the document.
00176   nsCOMPtr<nsIDOMDocument> domdoc;
00177   rv = (*aWindow)->GetDocument(getter_AddRefs(domdoc));
00178   NS_ENSURE_SUCCESS(rv, rv);
00179 
00180   // Note: If there is no document, then this window has been cleared and
00181   // there's nothing left to protect, so let the window pass through.
00182   if (domdoc && !nsContentUtils::CanCallerAccess(domdoc)) {
00183     NS_RELEASE(*aWindow);
00184     return NS_ERROR_DOM_SECURITY_ERR;
00185   }
00186 
00187   return NS_OK;
00188 }
00189 
00190 NS_IMETHODIMP
00191 nsXULCommandDispatcher::SetFocusedElement(nsIDOMElement* aElement)
00192 {
00193   EnsureFocusController();
00194   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00195 
00196   return mFocusController->SetFocusedElement(aElement);
00197 }
00198 
00199 NS_IMETHODIMP
00200 nsXULCommandDispatcher::SetFocusedWindow(nsIDOMWindow* aWindow)
00201 {
00202   EnsureFocusController();
00203   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00204 
00205   nsCOMPtr<nsIDOMWindowInternal> window(do_QueryInterface(aWindow));
00206 
00207   return mFocusController->SetFocusedWindow(window);
00208 }
00209 
00210 NS_IMETHODIMP
00211 nsXULCommandDispatcher::AdvanceFocus()
00212 {
00213   EnsureFocusController();
00214   if (mFocusController)
00215     return mFocusController->MoveFocus(PR_TRUE, nsnull);
00216   return NS_OK;
00217 }
00218 
00219 NS_IMETHODIMP
00220 nsXULCommandDispatcher::RewindFocus()
00221 {
00222   EnsureFocusController();
00223   if (mFocusController)
00224     return mFocusController->MoveFocus(PR_FALSE, nsnull);
00225   return NS_OK;
00226 }
00227 
00228 NS_IMETHODIMP
00229 nsXULCommandDispatcher::AdvanceFocusIntoSubtree(nsIDOMElement* aElt)
00230 {
00231   EnsureFocusController();
00232   if (mFocusController)
00233     return mFocusController->MoveFocus(PR_TRUE, aElt);
00234   return NS_OK;
00235 }
00236 
00237 NS_IMETHODIMP
00238 nsXULCommandDispatcher::AddCommandUpdater(nsIDOMElement* aElement,
00239                                           const nsAString& aEvents,
00240                                           const nsAString& aTargets)
00241 {
00242   NS_PRECONDITION(aElement != nsnull, "null ptr");
00243   if (! aElement)
00244     return NS_ERROR_NULL_POINTER;
00245 
00246   NS_ENSURE_STATE(mDocument);
00247   nsCOMPtr<nsIDOMNode> doc(do_QueryInterface(mDocument));
00248 
00249   nsresult rv = nsContentUtils::CheckSameOrigin(doc, aElement);
00250 
00251   if (NS_FAILED(rv)) {
00252     return rv;
00253   }
00254 
00255   nsCOMPtr<nsIDOMDocument> ownerDomDoc;
00256   aElement->GetOwnerDocument(getter_AddRefs(ownerDomDoc));
00257   nsCOMPtr<nsIDOMNSDocument> ownerDoc = do_QueryInterface(ownerDomDoc);
00258   NS_ENSURE_STATE(ownerDoc);
00259 
00260   // Box object is used as a weak reference to aElement.
00261   nsCOMPtr<nsIBoxObject> boxObject;
00262   ownerDoc->GetBoxObjectFor(aElement, getter_AddRefs(boxObject));
00263   NS_ENSURE_STATE(boxObject);
00264 
00265   Updater* updater = mUpdaters;
00266   Updater** link = &mUpdaters;
00267 
00268   while (updater) {
00269     nsCOMPtr<nsIDOMElement> element;
00270     updater->mWeakElement->GetElement(getter_AddRefs(element));
00271     if (element == aElement) {
00272 
00273 #ifdef NS_DEBUG
00274       if (PR_LOG_TEST(gLog, PR_LOG_NOTICE)) {
00275         nsCAutoString eventsC, targetsC, aeventsC, atargetsC; 
00276         eventsC.AssignWithConversion(updater->mEvents);
00277         targetsC.AssignWithConversion(updater->mTargets);
00278         CopyUTF16toUTF8(aEvents, aeventsC);
00279         CopyUTF16toUTF8(aTargets, atargetsC);
00280         PR_LOG(gLog, PR_LOG_NOTICE,
00281                ("xulcmd[%p] replace %p(events=%s targets=%s) with (events=%s targets=%s)",
00282                 this, aElement,
00283                 eventsC.get(),
00284                 targetsC.get(),
00285                 aeventsC.get(),
00286                 atargetsC.get()));
00287       }
00288 #endif
00289 
00290       // If the updater was already in the list, then replace
00291       // (?) the 'events' and 'targets' filters with the new
00292       // specification.
00293       updater->mEvents  = aEvents;
00294       updater->mTargets = aTargets;
00295       return NS_OK;
00296     }
00297 
00298     link = &(updater->mNext);
00299     updater = updater->mNext;
00300   }
00301 #ifdef NS_DEBUG
00302   if (PR_LOG_TEST(gLog, PR_LOG_NOTICE)) {
00303     nsCAutoString aeventsC, atargetsC; 
00304     CopyUTF16toUTF8(aEvents, aeventsC);
00305     CopyUTF16toUTF8(aTargets, atargetsC);
00306 
00307     PR_LOG(gLog, PR_LOG_NOTICE,
00308            ("xulcmd[%p] add     %p(events=%s targets=%s)",
00309             this, aElement,
00310             aeventsC.get(),
00311             atargetsC.get()));
00312   }
00313 #endif
00314 
00315   // If we get here, this is a new updater. Append it to the list.
00316   updater = new Updater(boxObject, aEvents, aTargets);
00317   if (! updater)
00318       return NS_ERROR_OUT_OF_MEMORY;
00319 
00320   *link = updater;
00321   return NS_OK;
00322 }
00323 
00324 NS_IMETHODIMP
00325 nsXULCommandDispatcher::RemoveCommandUpdater(nsIDOMElement* aElement)
00326 {
00327   NS_PRECONDITION(aElement != nsnull, "null ptr");
00328   if (! aElement)
00329     return NS_ERROR_NULL_POINTER;
00330 
00331   Updater* updater = mUpdaters;
00332   Updater** link = &mUpdaters;
00333 
00334   while (updater) {
00335     nsCOMPtr<nsIDOMElement> element;
00336     updater->mWeakElement->GetElement(getter_AddRefs(element));
00337     if (element == aElement) {
00338 #ifdef NS_DEBUG
00339       if (PR_LOG_TEST(gLog, PR_LOG_NOTICE)) {
00340         nsCAutoString eventsC, targetsC; 
00341         eventsC.AssignWithConversion(updater->mEvents);
00342         targetsC.AssignWithConversion(updater->mTargets);
00343         PR_LOG(gLog, PR_LOG_NOTICE,
00344                ("xulcmd[%p] remove  %p(events=%s targets=%s)",
00345                 this, aElement,
00346                 eventsC.get(),
00347                 targetsC.get()));
00348       }
00349 #endif
00350 
00351       *link = updater->mNext;
00352       delete updater;
00353       return NS_OK;
00354     }
00355 
00356     link = &(updater->mNext);
00357     updater = updater->mNext;
00358   }
00359 
00360   // Hmm. Not found. Oh well.
00361   return NS_OK;
00362 }
00363 
00364 NS_IMETHODIMP
00365 nsXULCommandDispatcher::UpdateCommands(const nsAString& aEventName)
00366 {
00367   EnsureFocusController();
00368   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00369 
00370   nsAutoString id;
00371   nsCOMPtr<nsIDOMElement> element;
00372   mFocusController->GetFocusedElement(getter_AddRefs(element));
00373   if (element) {
00374     nsresult rv = element->GetAttribute(NS_LITERAL_STRING("id"), id);
00375     NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get element's id");
00376     if (NS_FAILED(rv)) return rv;
00377   }
00378 
00379 #if 0
00380   {
00381     char*   actionString = ToNewCString(aEventName);
00382     printf("Doing UpdateCommands(\"%s\")\n", actionString);
00383     free(actionString);    
00384   }
00385 #endif
00386   
00387   for (Updater* updater = mUpdaters; updater != nsnull; updater = updater->mNext) {
00388     nsCOMPtr<nsIDOMElement> element;
00389     updater->mWeakElement->GetElement(getter_AddRefs(element));
00390     if (!element)
00391       continue;
00392     // Skip any nodes that don't match our 'events' or 'targets'
00393     // filters.
00394     if (! Matches(updater->mEvents, aEventName))
00395       continue;
00396 
00397     if (! Matches(updater->mTargets, id))
00398       continue;
00399 
00400     nsCOMPtr<nsIContent> content = do_QueryInterface(element);
00401     NS_ASSERTION(content != nsnull, "not an nsIContent");
00402     if (! content)
00403       return NS_ERROR_UNEXPECTED;
00404 
00405     nsCOMPtr<nsIDocument> document = content->GetDocument();
00406 
00407     NS_ASSERTION(document != nsnull, "element has no document");
00408     if (! document)
00409       continue;
00410 
00411 #ifdef NS_DEBUG
00412     if (PR_LOG_TEST(gLog, PR_LOG_NOTICE)) {
00413       nsCAutoString aeventnameC; 
00414       CopyUTF16toUTF8(aEventName, aeventnameC);
00415       PR_LOG(gLog, PR_LOG_NOTICE,
00416              ("xulcmd[%p] update %p event=%s",
00417               this, element.get(),
00418               aeventnameC.get()));
00419     }
00420 #endif
00421 
00422     PRUint32 count = document->GetNumberOfShells();
00423     for (PRUint32 i = 0; i < count; i++) {
00424       nsIPresShell *shell = document->GetShellAt(i);
00425 
00426       // Retrieve the context in which our DOM event will fire.
00427       nsCOMPtr<nsPresContext> context = shell->GetPresContext();
00428 
00429       // Handle the DOM event
00430       nsEventStatus status = nsEventStatus_eIgnore;
00431 
00432       nsEvent event(PR_TRUE, NS_XUL_COMMAND_UPDATE);
00433 
00434       content->HandleDOMEvent(context, &event, nsnull, NS_EVENT_FLAG_INIT, &status);
00435     }
00436   }
00437   return NS_OK;
00438 }
00439 
00440 PRBool
00441 nsXULCommandDispatcher::Matches(const nsString& aList, 
00442                                 const nsAString& aElement)
00443 {
00444   if (aList.EqualsLiteral("*"))
00445     return PR_TRUE; // match _everything_!
00446 
00447   PRInt32 indx = aList.Find(PromiseFlatString(aElement));
00448   if (indx == -1)
00449     return PR_FALSE; // not in the list at all
00450 
00451   // okay, now make sure it's not a substring snafu; e.g., 'ur'
00452   // found inside of 'blur'.
00453   if (indx > 0) {
00454     PRUnichar ch = aList[indx - 1];
00455     if (! nsCRT::IsAsciiSpace(ch) && ch != PRUnichar(','))
00456       return PR_FALSE;
00457   }
00458 
00459   if (indx + aElement.Length() < aList.Length()) {
00460     PRUnichar ch = aList[indx + aElement.Length()];
00461     if (! nsCRT::IsAsciiSpace(ch) && ch != PRUnichar(','))
00462       return PR_FALSE;
00463   }
00464 
00465   return PR_TRUE;
00466 }
00467 
00468 NS_IMETHODIMP
00469 nsXULCommandDispatcher::GetControllers(nsIControllers** aResult)
00470 {
00471   EnsureFocusController();
00472   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00473 
00474   return mFocusController->GetControllers(aResult);
00475 }
00476 
00477 NS_IMETHODIMP
00478 nsXULCommandDispatcher::GetControllerForCommand(const char *aCommand, nsIController** _retval)
00479 {
00480   EnsureFocusController();
00481   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00482 
00483   return mFocusController->GetControllerForCommand(aCommand, _retval);
00484 }
00485 
00486 NS_IMETHODIMP
00487 nsXULCommandDispatcher::GetSuppressFocusScroll(PRBool* aSuppressFocusScroll)
00488 {
00489   EnsureFocusController();
00490   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00491 
00492   return mFocusController->GetSuppressFocusScroll(aSuppressFocusScroll);
00493 }
00494 
00495 NS_IMETHODIMP
00496 nsXULCommandDispatcher::SetSuppressFocusScroll(PRBool aSuppressFocusScroll)
00497 {
00498   EnsureFocusController();
00499   NS_ENSURE_TRUE(mFocusController, NS_ERROR_FAILURE);
00500 
00501   return mFocusController->SetSuppressFocusScroll(aSuppressFocusScroll);
00502 }
00503