Back to index

lightning-sunbird  0.9+nobinonly
nsXMLHttpRequest.h
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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsXMLHttpRequest_h__
00039 #define nsXMLHttpRequest_h__
00040 
00041 #include "nsIXMLHttpRequest.h"
00042 #include "nsISupportsUtils.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsString.h"
00045 #include "nsIDOMLoadListener.h"
00046 #include "nsIDOMEventTarget.h"
00047 #include "nsIDOMDocument.h"
00048 #include "nsIURI.h"
00049 #include "nsIHttpChannel.h"
00050 #include "nsIDocument.h"
00051 #include "nsIStreamListener.h"
00052 #include "nsIEventQueueService.h"
00053 #include "nsWeakReference.h"
00054 #include "nsWeakPtr.h"
00055 #include "jsapi.h"
00056 #include "nsIScriptContext.h"
00057 #include "nsIChannelEventSink.h"
00058 #include "nsIInterfaceRequestor.h"
00059 #include "nsIHttpHeaderVisitor.h"
00060 #include "nsIProgressEventSink.h"
00061 #include "nsJSUtils.h"
00062 #include "nsTArray.h"
00063 #include "nsIDOMGCParticipant.h"
00064 #include "nsIJSNativeInitializer.h"
00065 #include "nsPIDOMWindow.h"
00066 #include "nsIDOMLSProgressEvent.h"
00067 
00068 class nsILoadGroup;
00069 
00070 class nsXMLHttpRequest : public nsIXMLHttpRequest,
00071                          public nsIJSXMLHttpRequest,
00072                          public nsIDOMLoadListener,
00073                          public nsIDOMEventTarget,
00074                          public nsIStreamListener,
00075                          public nsIChannelEventSink,
00076                          public nsIProgressEventSink,
00077                          public nsIInterfaceRequestor,
00078                          public nsIDOMGCParticipant,
00079                          public nsSupportsWeakReference,
00080                          public nsIJSNativeInitializer_MOZILLA_1_8_BRANCH
00081 {
00082 public:
00083   nsXMLHttpRequest();
00084   virtual ~nsXMLHttpRequest();
00085 
00086   NS_DECL_ISUPPORTS
00087 
00088   // nsIXMLHttpRequest
00089   NS_DECL_NSIXMLHTTPREQUEST
00090 
00091   // nsIJSXMLHttpRequest
00092   NS_DECL_NSIJSXMLHTTPREQUEST
00093 
00094   // nsIDOMEventTarget
00095   NS_DECL_NSIDOMEVENTTARGET
00096 
00097   // nsIDOMEventListener
00098   NS_DECL_NSIDOMEVENTLISTENER
00099 
00100   // nsIDOMLoadListener
00101   NS_IMETHOD Load(nsIDOMEvent* aEvent);
00102   NS_IMETHOD BeforeUnload(nsIDOMEvent* aEvent);
00103   NS_IMETHOD Unload(nsIDOMEvent* aEvent);
00104   NS_IMETHOD Abort(nsIDOMEvent* aEvent);
00105   NS_IMETHOD Error(nsIDOMEvent* aEvent);
00106 
00107   // nsIStreamListener
00108   NS_DECL_NSISTREAMLISTENER
00109 
00110   // nsIRequestObserver
00111   NS_DECL_NSIREQUESTOBSERVER
00112 
00113   // nsIChannelEventSink
00114   NS_DECL_NSICHANNELEVENTSINK
00115 
00116   // nsIProgressEventSink
00117   NS_DECL_NSIPROGRESSEVENTSINK
00118 
00119   // nsIInterfaceRequestor
00120   NS_DECL_NSIINTERFACEREQUESTOR
00121 
00122   // nsIJSNativeInitializer
00123   NS_IMETHOD Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
00124                        PRUint32 argc, jsval* argv);
00125 
00126   // This is called by the factory constructor.
00127   nsresult Init();
00128 
00129   // nsIDOMGCParticipant
00130   virtual nsIDOMGCParticipant* GetSCCIndex();
00131   virtual void AppendReachableList(nsCOMArray<nsIDOMGCParticipant>& aArray);
00132 
00133 protected:
00134   typedef nsMarkedJSFunctionHolder<nsIDOMEventListener> ListenerHolder;
00135 
00136   nsresult GetStreamForWString(const PRUnichar* aStr,
00137                                PRInt32 aLength,
00138                                nsIInputStream** aStream);
00139   nsresult DetectCharset(nsACString& aCharset);
00140   nsresult ConvertBodyToText(nsAString& aOutBuffer);
00141   static NS_METHOD StreamReaderFunc(nsIInputStream* in,
00142                 void* closure,
00143                 const char* fromRawSegment,
00144                 PRUint32 toOffset,
00145                 PRUint32 count,
00146                 PRUint32 *writeCount);
00147   // Change the state of the object with this. The broadcast argument
00148   // determines if the onreadystatechange listener should be called.
00149   // If aClearEventListeners is true, ChangeState will take refs to
00150   // any event listeners it needs and call ClearEventListeners before
00151   // making any HandleEvent() calls that could change the listener
00152   // values.
00153   nsresult ChangeState(PRUint32 aState, PRBool aBroadcast = PR_TRUE,
00154                        PRBool aClearEventListeners = PR_FALSE);
00155   nsresult RequestCompleted();
00156   nsresult GetLoadGroup(nsILoadGroup **aLoadGroup);
00157   nsIURI *GetBaseURI();
00158   nsresult CreateEvent(nsEvent* event, nsIDOMEvent** domevent);
00159   void NotifyEventListeners(nsIDOMEventListener* aHandler,
00160                             const nsCOMArray<nsIDOMEventListener>* aListeners,
00161                             nsIDOMEvent* aEvent);
00162   void ClearEventListeners();
00163   already_AddRefed<nsIHttpChannel> GetCurrentHttpChannel();
00164 
00165   nsresult CheckInnerWindowCorrectness()
00166   {
00167     if (mOwner) {
00168       nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mOwner);
00169       NS_ENSURE_STATE(win);
00170       NS_ASSERTION(win->IsInnerWindow(), "Should have inner window here!\n");
00171       nsPIDOMWindow* outer = win->GetOuterWindow();
00172       if (!outer || outer->GetCurrentInnerWindow() != win) {
00173         return NS_ERROR_FAILURE;
00174       }
00175     }
00176     return NS_OK;
00177   }
00178 
00179   nsCOMPtr<nsISupports> mContext;
00180   nsCOMPtr<nsIPrincipal> mPrincipal;
00181   nsCOMPtr<nsIChannel> mChannel;
00182   nsCOMPtr<nsIRequest> mReadRequest;
00183   nsCOMPtr<nsIDOMDocument> mDocument;
00184 
00185   nsTArray<ListenerHolder*> mLoadEventListeners;
00186   nsTArray<ListenerHolder*> mErrorEventListeners;
00187   nsCOMPtr<nsIScriptContext> mScriptContext;
00188   nsWeakPtr mOwner; // Inner window.
00189 
00190   nsMarkedJSFunctionHolder<nsIDOMEventListener> mOnLoadListener;
00191   nsMarkedJSFunctionHolder<nsIDOMEventListener> mOnErrorListener;
00192   nsMarkedJSFunctionHolder<nsIDOMEventListener> mOnProgressListener;
00193 
00194   nsMarkedJSFunctionHolder<nsIOnReadyStateChangeHandler> mOnReadystatechangeListener;
00195 
00196   nsCOMPtr<nsIStreamListener> mXMLParserStreamListener;
00197   nsCOMPtr<nsIEventQueueService> mEventQService;
00198 
00199   // used to implement getAllResponseHeaders()
00200   class nsHeaderVisitor : public nsIHttpHeaderVisitor {
00201   public:
00202     NS_DECL_ISUPPORTS
00203     NS_DECL_NSIHTTPHEADERVISITOR
00204     nsHeaderVisitor() { }
00205     virtual ~nsHeaderVisitor() {}
00206     const nsACString &Headers() { return mHeaders; }
00207   private:
00208     nsCString mHeaders;
00209   };
00210 
00211   nsCString mResponseBody;
00212 
00213   nsCString mOverrideMimeType;
00214 
00219   nsCOMPtr<nsIInterfaceRequestor> mNotificationCallbacks;
00225   nsCOMPtr<nsIChannelEventSink> mChannelEventSink;
00226   nsCOMPtr<nsIProgressEventSink> mProgressEventSink;
00227 
00228   PRUint32 mState;
00229 };
00230 
00231 
00232 // helper class to expose a progress DOM Event
00233 
00234 class nsXMLHttpProgressEvent : public nsIDOMLSProgressEvent
00235 {
00236 public:
00237   nsXMLHttpProgressEvent(nsIDOMEvent * aInner, PRUint64 aCurrentProgress, PRUint64 aMaxProgress);
00238   virtual ~nsXMLHttpProgressEvent();
00239 
00240   NS_DECL_ISUPPORTS
00241   NS_DECL_NSIDOMLSPROGRESSEVENT
00242   NS_FORWARD_NSIDOMEVENT(mInner->)
00243 
00244 protected:
00245   nsCOMPtr<nsIDOMEvent> mInner;
00246   PRUint64 mCurProgress;
00247   PRUint64 mMaxProgress;
00248 };
00249 
00250 #endif