LCOV - app.info - content/base/src/nsEventSource.h

LCOV - code coverage report
| Current view: | directory - content/base/src - nsEventSource.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 2 | 1 | 50.0 %
| Date: | 2012-04-21 | Functions: | 5 | 1 | 20.0 %

           1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
           2                 : /* ***** BEGIN LICENSE BLOCK *****
           3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           4                 :  *
           5                 :  * The contents of this file are subject to the Mozilla Public License Version
           6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           7                 :  * the License. You may obtain a copy of the License at
           8                 :  * http://www.mozilla.org/MPL/
           9                 :  *
          10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          12                 :  * for the specific language governing rights and limitations under the
          13                 :  * License.
          14                 :  *
          15                 :  * The Original Code is mozilla.org code.
          16                 :  *
          17                 :  * The Initial Developer of the Original Code is
          18                 :  * Wellington Fernando de Macedo.
          19                 :  * Portions created by the Initial Developer are Copyright (C) 2008
          20                 :  * the Initial Developer. All Rights Reserved.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *    Wellington Fernando de Macedo <wfernandom2004@gmail.com> (original author)
          24                 :  *
          25                 :  * Alternatively, the contents of this file may be used under the terms of
          26                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          27                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          29                 :  * of those above. If you wish to allow use of your version of this file only
          30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          31                 :  * use your version of this file under the terms of the MPL, indicate your
          32                 :  * decision by deleting the provisions above and replace them with the notice
          33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          34                 :  * the provisions above, a recipient may use your version of this file under
          35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          36                 :  *
          37                 :  * ***** END LICENSE BLOCK ***** */
          38                 :
          39                 : /*
          40                 :  * This implementation has support only for http requests. It is because the
          41                 :  * spec has defined event streams only for http. HTTP is required because
          42                 :  * this implementation uses some http headers: "Last-Event-ID", "Cache-Control"
          43                 :  * and "Accept".
          44                 :  */
          45                 :
          46                 : #ifndef nsEventSource_h__
          47                 : #define nsEventSource_h__
          48                 :
          49                 : #include "nsIEventSource.h"
          50                 : #include "nsIJSNativeInitializer.h"
          51                 : #include "nsDOMEventTargetHelper.h"
          52                 : #include "nsIObserver.h"
          53                 : #include "nsIStreamListener.h"
          54                 : #include "nsIChannelEventSink.h"
          55                 : #include "nsIInterfaceRequestor.h"
          56                 : #include "nsITimer.h"
          57                 : #include "nsIHttpChannel.h"
          58                 : #include "nsWeakReference.h"
          59                 : #include "nsDeque.h"
          60                 : #include "nsIUnicodeDecoder.h"
          61                 :
          62                 : #define NS_EVENTSOURCE_CID                          \
          63                 :  { /* 755e2d2d-a836-4539-83f4-16b51156341f */       \
          64                 :   0x755e2d2d, 0xa836, 0x4539,                       \
          65                 :  {0x83, 0xf4, 0x16, 0xb5, 0x11, 0x56, 0x34, 0x1f} }
          66                 :
          67                 : #define NS_EVENTSOURCE_CONTRACTID "@mozilla.org/eventsource;1"
          68                 :
          69                 : class AsyncVerifyRedirectCallbackFwr;
          70                 : class nsAutoClearFields;
          71                 :
          72                 : class nsEventSource: public nsDOMEventTargetHelper,
          73                 :                      public nsIEventSource,
          74                 :                      public nsIJSNativeInitializer,
          75                 :                      public nsIObserver,
          76                 :                      public nsIStreamListener,
          77                 :                      public nsIChannelEventSink,
          78                 :                      public nsIInterfaceRequestor,
          79                 :                      public nsSupportsWeakReference
          80                 : {
          81                 : friend class AsyncVerifyRedirectCallbackFwr;
          82                 :
          83                 : public:
          84                 :   nsEventSource();
          85                 :   virtual ~nsEventSource();
          86                 :   NS_DECL_ISUPPORTS_INHERITED
          87            1396 :   NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(nsEventSource,
          88                 :                                                                    nsDOMEventTargetHelper)
          89                 :
          90                 :   NS_DECL_NSIEVENTSOURCE
          91                 :
          92                 :   // nsIJSNativeInitializer
          93                 :   NS_IMETHOD Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
          94                 :                         PRUint32 argc, jsval* argv);
          95                 :
          96                 :   NS_DECL_NSIOBSERVER
          97                 :   NS_DECL_NSISTREAMLISTENER
          98                 :   NS_DECL_NSIREQUESTOBSERVER
          99                 :   NS_DECL_NSICHANNELEVENTSINK
         100                 :   NS_DECL_NSIINTERFACEREQUESTOR
         101                 :
         102                 :   // Determine if preferences allow EventSource
         103                 :   static bool PrefEnabled();
         104                 :
         105                 :   virtual void DisconnectFromOwner();
         106                 : protected:
         107                 :   nsresult GetBaseURI(nsIURI **aBaseURI);
         108                 :
         109                 :   nsresult SetupHttpChannel();
         110                 :   nsresult InitChannelAndRequestEventSource();
         111                 :   nsresult ResetConnection();
         112                 :   nsresult DispatchFailConnection();
         113                 :   nsresult SetReconnectionTimeout();
         114                 :
         115                 :   void AnnounceConnection();
         116                 :   void DispatchAllMessageEvents();
         117                 :   void ReestablishConnection();
         118                 :   void FailConnection();
         119                 :
         120                 :   nsresult Thaw();
         121                 :   nsresult Freeze();
         122                 :
         123                 :   static void TimerCallback(nsITimer *aTimer, void *aClosure);
         124                 :
         125                 :   nsresult PrintErrorOnConsole(const char       *aBundleURI,
         126                 :                                const PRUnichar  *aError,
         127                 :                                const PRUnichar **aFormatStrings,
         128                 :                                PRUint32          aFormatStringsLen);
         129                 :   nsresult ConsoleError();
         130                 :
         131                 :   static NS_METHOD StreamReaderFunc(nsIInputStream *aInputStream,
         132                 :                                     void           *aClosure,
         133                 :                                     const char     *aFromRawSegment,
         134                 :                                     PRUint32        aToOffset,
         135                 :                                     PRUint32        aCount,
         136                 :                                     PRUint32       *aWriteCount);
         137                 :   nsresult SetFieldAndClear();
         138                 :   nsresult ClearFields();
         139                 :   nsresult ResetEvent();
         140                 :   nsresult DispatchCurrentMessageEvent();
         141                 :   nsresult ParseCharacter(PRUnichar aChr);
         142                 :   bool CheckCanRequestSrc(nsIURI* aSrc = nsnull);  // if null, it tests mSrc
         143                 :   nsresult CheckHealthOfRequestCallback(nsIRequest *aRequestCallback);
         144                 :   nsresult OnRedirectVerifyCallback(nsresult result);
         145                 :
         146                 :   nsCOMPtr<nsIURI> mSrc;
         147                 :
         148                 :   nsString mLastEventID;
         149                 :   PRUint32 mReconnectionTime;  // in ms
         150                 :
         151               0 :   struct Message {
         152                 :     nsString mEventName;
         153                 :     nsString mLastEventID;
         154                 :     nsString mData;
         155                 :   };
         156                 :   nsDeque mMessagesToDispatch;
         157                 :   Message mCurrentMessage;
         158                 :
         159                 :   /**
         160                 :    * A simple state machine used to manage the event-source's line buffer
         161                 :    *
         162                 :    * PARSE_STATE_OFF              -> PARSE_STATE_BEGIN_OF_STREAM
         163                 :    *
         164                 :    * PARSE_STATE_BEGIN_OF_STREAM  -> PARSE_STATE_BOM_WAS_READ |
         165                 :    *                                 PARSE_STATE_CR_CHAR |
         166                 :    *                                 PARSE_STATE_BEGIN_OF_LINE |
         167                 :    *                                 PARSE_STATE_COMMENT |
         168                 :    *                                 PARSE_STATE_FIELD_NAME
         169                 :    *
         170                 :    * PARSE_STATE_BOM_WAS_READ     -> PARSE_STATE_CR_CHAR |
         171                 :    *                                 PARSE_STATE_BEGIN_OF_LINE |
         172                 :    *                                 PARSE_STATE_COMMENT |
         173                 :    *                                 PARSE_STATE_FIELD_NAME
         174                 :    *
         175                 :    * PARSE_STATE_CR_CHAR -> PARSE_STATE_CR_CHAR |
         176                 :    *                        PARSE_STATE_COMMENT |
         177                 :    *                        PARSE_STATE_FIELD_NAME |
         178                 :    *                        PARSE_STATE_BEGIN_OF_LINE
         179                 :    *
         180                 :    * PARSE_STATE_COMMENT -> PARSE_STATE_CR_CHAR |
         181                 :    *                        PARSE_STATE_BEGIN_OF_LINE
         182                 :    *
         183                 :    * PARSE_STATE_FIELD_NAME   -> PARSE_STATE_CR_CHAR |
         184                 :    *                             PARSE_STATE_BEGIN_OF_LINE |
         185                 :    *                             PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE
         186                 :    *
         187                 :    * PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE  -> PARSE_STATE_FIELD_VALUE |
         188                 :    *                                           PARSE_STATE_CR_CHAR |
         189                 :    *                                           PARSE_STATE_BEGIN_OF_LINE
         190                 :    *
         191                 :    * PARSE_STATE_FIELD_VALUE      -> PARSE_STATE_CR_CHAR |
         192                 :    *                                 PARSE_STATE_BEGIN_OF_LINE
         193                 :    *
         194                 :    * PARSE_STATE_BEGIN_OF_LINE   -> PARSE_STATE_CR_CHAR |
         195                 :    *                                PARSE_STATE_COMMENT |
         196                 :    *                                PARSE_STATE_FIELD_NAME |
         197                 :    *                                PARSE_STATE_BEGIN_OF_LINE
         198                 :    *
         199                 :    * Whenever the parser find an empty line or the end-of-file
         200                 :    * it dispatches the stacked event.
         201                 :    *
         202                 :    */
         203                 :   enum ParserStatus {
         204                 :     PARSE_STATE_OFF,
         205                 :     PARSE_STATE_BEGIN_OF_STREAM,
         206                 :     PARSE_STATE_BOM_WAS_READ,
         207                 :     PARSE_STATE_CR_CHAR,
         208                 :     PARSE_STATE_COMMENT,
         209                 :     PARSE_STATE_FIELD_NAME,
         210                 :     PARSE_STATE_FIRST_CHAR_OF_FIELD_VALUE,
         211                 :     PARSE_STATE_FIELD_VALUE,
         212                 :     PARSE_STATE_BEGIN_OF_LINE
         213                 :   };
         214                 :   ParserStatus mStatus;
         215                 :
         216                 :   bool mFrozen;
         217                 :   bool mErrorLoadOnRedirect;
         218                 :   bool mGoingToDispatchAllMessages;
         219                 :   bool mWithCredentials;
         220                 :   bool mWaitingForOnStopRequest;
         221                 :
         222                 :   // used while reading the input streams
         223                 :   nsCOMPtr<nsIUnicodeDecoder> mUnicodeDecoder;
         224                 :   nsresult mLastConvertionResult;
         225                 :   nsString mLastFieldName;
         226                 :   nsString mLastFieldValue;
         227                 :
         228                 :   nsRefPtr<nsDOMEventListenerWrapper> mOnOpenListener;
         229                 :   nsRefPtr<nsDOMEventListenerWrapper> mOnErrorListener;
         230                 :   nsRefPtr<nsDOMEventListenerWrapper> mOnMessageListener;
         231                 :
         232                 :   nsCOMPtr<nsILoadGroup> mLoadGroup;
         233                 :
         234                 :   /**
         235                 :    * The notification callbacks the channel had initially.
         236                 :    * We want to forward things here as needed.
         237                 :    */
         238                 :   nsCOMPtr<nsIInterfaceRequestor> mNotificationCallbacks;
         239                 :   nsCOMPtr<nsIChannelEventSink>   mChannelEventSink;
         240                 :
         241                 :   nsCOMPtr<nsIHttpChannel> mHttpChannel;
         242                 :
         243                 :   nsCOMPtr<nsITimer> mTimer;
         244                 :
         245                 :   PRInt32 mReadyState;
         246                 :   nsString mOriginalURL;
         247                 :
         248                 :   nsCOMPtr<nsIPrincipal> mPrincipal;
         249                 :   nsString mOrigin;
         250                 :
         251                 :   PRUint32 mRedirectFlags;
         252                 :   nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
         253                 :   nsCOMPtr<nsIChannel> mNewRedirectChannel;
         254                 :
         255                 :   // Event Source owner information:
         256                 :   // - the script file name
         257                 :   // - source code line number where the Event Source object was constructed.
         258                 :   // - the ID of the inner window where the script lives. Note that this may not
         259                 :   //   be the same as the Event Source owner window.
         260                 :   // These attributes are used for error reporting.
         261                 :   nsString mScriptFile;
         262                 :   PRUint32 mScriptLine;
         263                 :   PRUint64 mInnerWindowID;
         264                 :
         265                 : private:
         266                 :   nsEventSource(const nsEventSource& x);   // prevent bad usage
         267                 :   nsEventSource& operator=(const nsEventSource& x);
         268                 : };
         269                 :
         270                 : #endif // nsEventSource_h__

Generated by: LCOV version 1.7