Back to index

lightning-sunbird  0.9+nobinonly
nsLSParser.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Johnny Stenback <jst@mozilla.jstenback.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsLSParser.h"
00040 #include "nsLSEvent.h"
00041 #include "nsAutoPtr.h"
00042 #include "nsXMLHttpRequest.h"
00043 #include "nsIDOMLSParserFilter.h"
00044 #include "nsIDOMDocument.h"
00045 #include "nsIDOMDOMImplementation.h"
00046 #include "nsIDOMEventListener.h"
00047 #include "nsIDOMEventTarget.h"
00048 
00049 #include "nsIDOMClassInfo.h"
00050 
00051 
00052 // QueryInterface implementation for nsLSParser
00053 NS_INTERFACE_MAP_BEGIN(nsLSParser)
00054   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMLSParser)
00055   NS_INTERFACE_MAP_ENTRY(nsIDOMLSParser)
00056   NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
00057   NS_INTERFACE_MAP_ENTRY_EXTERNAL_DOM_CLASSINFO(LSParser)
00058 NS_INTERFACE_MAP_END
00059 
00060 
00061 NS_IMPL_ADDREF(nsLSParser)
00062 NS_IMPL_RELEASE(nsLSParser)
00063 
00064 nsLSParser::nsLSParser()
00065   : mIsAsync(PR_TRUE)
00066 {
00067 }
00068 
00069 nsLSParser::~nsLSParser()
00070 {
00071 }
00072 
00073 NS_IMETHODIMP
00074 nsLSParser::GetDomConfig(nsIDOMDOMConfiguration * *aDomConfig)
00075 {
00076   *aDomConfig = nsnull;
00077 
00078   return NS_ERROR_NOT_IMPLEMENTED;
00079 }
00080 
00081 NS_IMETHODIMP
00082 nsLSParser::GetFilter(nsIDOMLSParserFilter * *aFilter)
00083 {
00084   *aFilter = mFilter;
00085 
00086   return NS_OK;
00087 }
00088 
00089 NS_IMETHODIMP
00090 nsLSParser::SetFilter(nsIDOMLSParserFilter * aFilter)
00091 {
00092   mFilter = aFilter;
00093 
00094   return NS_OK;
00095 }
00096 
00097 NS_IMETHODIMP
00098 nsLSParser::GetAsync(PRBool *aAsync)
00099 {
00100   *aAsync = mIsAsync;
00101 
00102   return NS_OK;
00103 }
00104 
00105 NS_IMETHODIMP
00106 nsLSParser::GetBusy(PRBool *aBusy)
00107 {
00108   PRInt32 readyState;
00109   nsresult rv = mXMLHttpRequest->GetReadyState(&readyState);
00110   NS_ENSURE_SUCCESS(rv, rv);
00111 
00112   *aBusy = readyState == 0 || readyState == 4;
00113 
00114   return NS_OK;
00115 }
00116 
00117 class nsLSParserEventListener : public nsIDOMEventListener
00118 {
00119 public:
00120   nsLSParserEventListener(nsLSParser *aParser)
00121     : mParser(aParser)
00122   {
00123   }
00124 
00125   virtual ~nsLSParserEventListener()
00126   {
00127   }
00128 
00129   NS_DECL_ISUPPORTS
00130 
00131   NS_DECL_NSIDOMEVENTLISTENER
00132 
00133 protected:
00134   nsRefPtr<nsLSParser> mParser;
00135 };
00136 
00137 
00138 NS_IMPL_ISUPPORTS1(nsLSParserEventListener, nsIDOMEventListener)
00139 
00140 NS_IMETHODIMP
00141 nsLSParserEventListener::HandleEvent(nsIDOMEvent *aEvent)
00142 {
00143   mParser->FireOnLoad();
00144 
00145   return NS_OK;
00146 }
00147 
00148 
00149 NS_IMETHODIMP
00150 nsLSParser::Parse(nsIDOMLSInput *input, nsIDOMDocument **_retval)
00151 {
00152   return NS_ERROR_NOT_IMPLEMENTED;
00153 }
00154 
00155 NS_IMETHODIMP
00156 nsLSParser::ParseURI(const nsAString & uri, nsIDOMDocument **_retval)
00157 {
00158   *_retval = nsnull;
00159 
00160   nsCOMPtr<nsIDOMEventListener> listener = new nsLSParserEventListener(this);
00161   NS_ENSURE_TRUE(listener, NS_ERROR_OUT_OF_MEMORY);
00162 
00163   nsresult rv;
00164   rv = mXMLHttpRequest->OpenRequest(NS_LITERAL_CSTRING("GET"),
00165                                     NS_ConvertUTF16toUTF8(uri),
00166                                     mIsAsync, EmptyString(), EmptyString());
00167   NS_ENSURE_SUCCESS(rv, rv);
00168 
00169   nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(mXMLHttpRequest));
00170   rv = target->AddEventListener(NS_LITERAL_STRING("load"), listener, PR_FALSE);
00171   NS_ENSURE_SUCCESS(rv, rv);
00172 
00173   rv = mXMLHttpRequest->Send(nsnull);
00174 
00175   if (!mIsAsync && NS_SUCCEEDED(rv)) {
00176     rv = mXMLHttpRequest->GetResponseXML(_retval);
00177   }
00178 
00179   return rv;
00180 }
00181 
00182 NS_IMETHODIMP
00183 nsLSParser::ParseWithContext(nsIDOMLSInput *input, nsIDOMNode *contextArg,
00184                              PRUint16 action, nsIDOMNode **_retval)
00185 {
00186   return NS_ERROR_NOT_IMPLEMENTED;
00187 }
00188 
00189 NS_IMETHODIMP
00190 nsLSParser::Abort()
00191 {
00192   return mXMLHttpRequest->Abort();
00193 }
00194 
00195 NS_IMETHODIMP
00196 nsLSParser::AddEventListener(const nsAString& aType,
00197                              nsIDOMEventListener *aListener,
00198                              PRBool aUseCapture)
00199 {
00200   nsCOMArray<nsIDOMEventListener> *listeners;
00201 
00202   if (aType.EqualsLiteral("ls-load")) {
00203     listeners = &mLoadListeners;
00204   } else if (aType.EqualsLiteral("ls-progress")) {
00205     listeners = &mProgressListeners;
00206   } else {
00207     // Not a supported event
00208 
00209     return NS_OK;
00210   }
00211 
00212   if (listeners->IndexOf(aListener) < 0) {
00213     listeners->AppendObject(aListener);
00214   }
00215 
00216   return NS_OK;
00217 }
00218 
00219 NS_IMETHODIMP
00220 nsLSParser::RemoveEventListener(const nsAString& aType,
00221                                 nsIDOMEventListener *aListener,
00222                                 PRBool aUseCapture)
00223 {
00224   nsCOMArray<nsIDOMEventListener> *listeners;
00225 
00226   if (aType.EqualsLiteral("ls-load")) {
00227     listeners = &mLoadListeners;
00228   } else if (aType.EqualsLiteral("ls-progress")) {
00229     listeners = &mProgressListeners;
00230   } else {
00231     // Not a supported event
00232 
00233     return NS_OK;
00234   }
00235 
00236   listeners->RemoveObject(aListener);
00237 
00238   return NS_OK;
00239 }
00240 
00241 NS_IMETHODIMP
00242 nsLSParser::DispatchEvent(nsIDOMEvent *aEvt, PRBool *_retval)
00243 {
00244   return NS_ERROR_NOT_IMPLEMENTED;
00245 }
00246 
00247 void
00248 nsLSParser::FireOnLoad()
00249 {
00250   for (PRInt32 i = 0; i < mLoadListeners.Count(); ++i) {
00251     nsRefPtr<nsLSParserLoadEvent> event = new nsLSParserLoadEvent(this);
00252 
00253     if (event) {
00254       mLoadListeners.ObjectAt(i)->HandleEvent(NS_STATIC_CAST(nsLSEvent *,
00255                                                              event));
00256     }
00257   }
00258 }
00259 
00260 nsresult
00261 nsLSParser::Init()
00262 {
00263   mXMLHttpRequest = new nsXMLHttpRequest();
00264 
00265   return mXMLHttpRequest ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00266 }