Back to index

lightning-sunbird  0.9+nobinonly
nsCommandHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
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 the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Adam Lock <adamlock@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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 #include "nsCommandHandler.h"
00041 #include "nsWebBrowser.h"
00042 #include "nsDocShellTreeOwner.h"
00043 
00044 #include "nsIAllocator.h"
00045 #include "nsIScriptGlobalObject.h"
00046 
00047 nsCommandHandler::nsCommandHandler() :
00048     mWindow(nsnull)
00049 {
00050 }
00051 
00052 nsCommandHandler::~nsCommandHandler()
00053 {
00054 }
00055 
00056 nsresult nsCommandHandler::GetCommandHandler(nsICommandHandler **aCommandHandler)
00057 {
00058     NS_ENSURE_ARG_POINTER(aCommandHandler);
00059 
00060     *aCommandHandler = nsnull;
00061     if (mWindow == nsnull)
00062     {
00063         return NS_ERROR_FAILURE;
00064     }
00065 
00066     nsCOMPtr<nsIScriptGlobalObject> globalObj( do_QueryInterface(mWindow) );
00067     if (!globalObj)
00068     {
00069         return NS_ERROR_FAILURE;
00070     }
00071 
00072     // Get the document tree owner
00073 
00074     nsCOMPtr<nsIDocShellTreeItem> docShellAsTreeItem =
00075       do_QueryInterface(globalObj->GetDocShell());
00076     nsIDocShellTreeOwner *treeOwner = nsnull;
00077     docShellAsTreeItem->GetTreeOwner(&treeOwner);
00078 
00079     // Make sure the tree owner is an an nsDocShellTreeOwner object
00080     // by QI'ing for a hidden interface. If it doesn't have the interface
00081     // then it's not safe to do the casting.
00082 
00083     nsCOMPtr<nsICDocShellTreeOwner> realTreeOwner(do_QueryInterface(treeOwner));
00084     if (realTreeOwner)
00085     {
00086         nsDocShellTreeOwner *tree = NS_STATIC_CAST(nsDocShellTreeOwner *, treeOwner);
00087         if (tree->mTreeOwner)
00088         {
00089             nsresult rv;
00090             rv = tree->mTreeOwner->QueryInterface(NS_GET_IID(nsICommandHandler), (void **)aCommandHandler);
00091             NS_RELEASE(treeOwner);
00092             return rv;
00093         }
00094      
00095         NS_RELEASE(treeOwner);
00096      }
00097 
00098     *aCommandHandler = nsnull;
00099 
00100     return NS_OK;
00101 }
00102 
00103 
00104 NS_IMPL_ADDREF(nsCommandHandler)
00105 NS_IMPL_RELEASE(nsCommandHandler)
00106 
00107 NS_INTERFACE_MAP_BEGIN(nsCommandHandler)
00108     NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsICommandHandler)
00109     NS_INTERFACE_MAP_ENTRY(nsICommandHandlerInit)
00110     NS_INTERFACE_MAP_ENTRY(nsICommandHandler)
00111 NS_INTERFACE_MAP_END
00112 
00114 // nsICommandHandlerInit implementation
00115 
00116 /* attribute nsIDocShell docShell; */
00117 NS_IMETHODIMP nsCommandHandler::GetWindow(nsIDOMWindow * *aWindow)
00118 {
00119     *aWindow = nsnull;
00120     return NS_OK;
00121 }
00122 
00123 NS_IMETHODIMP nsCommandHandler::SetWindow(nsIDOMWindow * aWindow)
00124 {
00125     if (aWindow == nsnull)
00126     {
00127        return NS_ERROR_FAILURE;
00128     }
00129     mWindow = aWindow;
00130     return NS_OK;
00131 }
00132 
00134 // nsICommandHandler implementation
00135 
00136 /* string exec (in string aCommand, in string aStatus); */
00137 NS_IMETHODIMP nsCommandHandler::Exec(const char *aCommand, const char *aStatus, char **aResult)
00138 {
00139     NS_ENSURE_ARG_POINTER(aCommand);
00140     NS_ENSURE_ARG_POINTER(aResult);
00141 
00142     nsCOMPtr<nsICommandHandler> commandHandler;
00143     GetCommandHandler(getter_AddRefs(commandHandler));
00144 
00145     // Call the client's command handler to deal with this command
00146     if (commandHandler)
00147     {
00148         *aResult = nsnull;
00149         return commandHandler->Exec(aCommand, aStatus, aResult);
00150     }
00151 
00152     // Return an empty string
00153     const char szEmpty[] = "";
00154     *aResult = (char *) nsAllocator::Clone(szEmpty, sizeof(szEmpty));
00155 
00156     return NS_OK;
00157 }
00158 
00159 /* string query (in string aCommand, in string aStatus); */
00160 NS_IMETHODIMP nsCommandHandler::Query(const char *aCommand, const char *aStatus, char **aResult)
00161 {
00162     NS_ENSURE_ARG_POINTER(aCommand);
00163     NS_ENSURE_ARG_POINTER(aResult);
00164 
00165     nsCOMPtr<nsICommandHandler> commandHandler;
00166     GetCommandHandler(getter_AddRefs(commandHandler));
00167 
00168     // Call the client's command handler to deal with this command
00169     if (commandHandler)
00170     {
00171         *aResult = nsnull;
00172         return commandHandler->Query(aCommand, aStatus, aResult);
00173     }
00174 
00175     // Return an empty string
00176     const char szEmpty[] = "";
00177     *aResult = (char *) nsAllocator::Clone(szEmpty, sizeof(szEmpty));
00178 
00179     return NS_OK;
00180 }