parser/html/nsHtml5StreamListener.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/html/nsHtml5StreamListener.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,54 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef nsHtml5StreamListener_h
     1.9 +#define nsHtml5StreamListener_h
    1.10 +
    1.11 +#include "nsIStreamListener.h"
    1.12 +#include "nsIThreadRetargetableStreamListener.h"
    1.13 +#include "nsHtml5RefPtr.h"
    1.14 +#include "nsHtml5StreamParser.h"
    1.15 +
    1.16 +/**
    1.17 + * The purpose of this class is to reconcile the problem that
    1.18 + * nsHtml5StreamParser is a cycle collection participant, which means that it
    1.19 + * can only be refcounted on the main thread, but
    1.20 + * nsIThreadRetargetableStreamListener can be refcounted from another thread,
    1.21 + * so nsHtml5StreamParser being an nsIThreadRetargetableStreamListener was
    1.22 + * a memory corruption problem.
    1.23 + *
    1.24 + * mDelegate is an nsHtml5RefPtr, which releases the object that it points
    1.25 + * to from a runnable on the main thread. DropDelegate() is only called on
    1.26 + * the main thread. This call will finish before the main-thread derefs the
    1.27 + * nsHtml5StreamListener itself, so there is no risk of another thread making
    1.28 + * the refcount of nsHtml5StreamListener go to zero and running the destructor
    1.29 + * concurrently. Other than that, the thread-safe nsISupports implementation
    1.30 + * takes care of the destructor not running concurrently from different
    1.31 + * threads, so there is no need to have a mutex around nsHtml5RefPtr to
    1.32 + * prevent it from double-releasing nsHtml5StreamParser.
    1.33 + */
    1.34 +class nsHtml5StreamListener : public nsIStreamListener,
    1.35 +                              public nsIThreadRetargetableStreamListener
    1.36 +{
    1.37 +public:
    1.38 +  nsHtml5StreamListener(nsHtml5StreamParser* aDelegate);
    1.39 +  virtual ~nsHtml5StreamListener();
    1.40 +
    1.41 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.42 +  NS_DECL_NSIREQUESTOBSERVER
    1.43 +  NS_DECL_NSISTREAMLISTENER
    1.44 +  NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
    1.45 +
    1.46 +  inline nsHtml5StreamParser* GetDelegate()
    1.47 +  {
    1.48 +    return mDelegate;
    1.49 +  }
    1.50 +
    1.51 +  void DropDelegate();
    1.52 +
    1.53 +private:
    1.54 +  nsHtml5RefPtr<nsHtml5StreamParser> mDelegate;
    1.55 +};
    1.56 +
    1.57 +#endif // nsHtml5StreamListener_h

mercurial