netwerk/base/public/nsAsyncRedirectVerifyHelper.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef nsAsyncRedirectVerifyHelper_h
     7 #define nsAsyncRedirectVerifyHelper_h
     9 #include "nsIRunnable.h"
    10 #include "nsIThread.h"
    11 #include "nsIChannelEventSink.h"
    12 #include "nsIInterfaceRequestor.h"
    13 #include "nsIAsyncVerifyRedirectCallback.h"
    14 #include "nsCOMPtr.h"
    15 #include "nsAutoPtr.h"
    16 #include "nsCycleCollectionParticipant.h"
    17 #include "mozilla/Attributes.h"
    19 class nsIChannel;
    21 /**
    22  * This class simplifies call of OnChannelRedirect of IOService and
    23  * the sink bound with the channel being redirected while the result of
    24  * redirect decision is returned through the callback.
    25  */
    26 class nsAsyncRedirectVerifyHelper MOZ_FINAL : public nsIRunnable,
    27                                               public nsIAsyncVerifyRedirectCallback
    28 {
    29     NS_DECL_THREADSAFE_ISUPPORTS
    30     NS_DECL_NSIRUNNABLE
    31     NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
    33 public:
    34     nsAsyncRedirectVerifyHelper();
    36     /*
    37      * Calls AsyncOnChannelRedirect() on the given sink with the given
    38      * channels and flags. Keeps track of number of async callbacks to expect.
    39      */
    40     nsresult DelegateOnChannelRedirect(nsIChannelEventSink *sink,
    41                                        nsIChannel *oldChannel, 
    42                                        nsIChannel *newChannel,
    43                                        uint32_t flags);
    45     /**
    46      * Initialize and run the chain of AsyncOnChannelRedirect calls. OldChannel
    47      * is QI'ed for nsIAsyncVerifyRedirectCallback. The result of the redirect
    48      * decision is passed through this interface back to the oldChannel.
    49      *
    50      * @param oldChan
    51      *    channel being redirected, MUST implement
    52      *    nsIAsyncVerifyRedirectCallback
    53      * @param newChan
    54      *    target of the redirect channel
    55      * @param flags
    56      *    redirect flags
    57      * @param synchronize
    58      *    set to TRUE if you want the Init method wait synchronously for
    59      *    all redirect callbacks
    60      */
    61     nsresult Init(nsIChannel* oldChan,
    62                   nsIChannel* newChan,
    63                   uint32_t flags,
    64                   bool synchronize = false);
    66 protected:
    67     nsCOMPtr<nsIChannel> mOldChan;
    68     nsCOMPtr<nsIChannel> mNewChan;
    69     uint32_t mFlags;
    70     bool mWaitingForRedirectCallback;
    71     nsCOMPtr<nsIThread>      mCallbackThread;
    72     bool                     mCallbackInitiated;
    73     int32_t                  mExpectedCallbacks;
    74     nsresult                 mResult; // value passed to callback
    76     void InitCallback();
    78     /**
    79      * Calls back to |oldChan| as described in Init()
    80      */
    81     void ExplicitCallback(nsresult result);
    83 private:
    84     ~nsAsyncRedirectVerifyHelper();
    86     bool IsOldChannelCanceled();
    87 };
    89 /*
    90  * Helper to make the call-stack handle some control-flow for us
    91  */
    92 class nsAsyncRedirectAutoCallback
    93 {
    94 public:
    95     nsAsyncRedirectAutoCallback(nsIAsyncVerifyRedirectCallback* aCallback)
    96         : mCallback(aCallback)
    97     {
    98         mResult = NS_OK;
    99     }
   100     ~nsAsyncRedirectAutoCallback()
   101     {
   102         if (mCallback)
   103             mCallback->OnRedirectVerifyCallback(mResult);
   104     }
   105     /*
   106      * Call this is you want it to call back with a different result-code
   107      */
   108     void SetResult(nsresult aRes)
   109     {
   110         mResult = aRes;
   111     }
   112     /*
   113      * Call this is you want to avoid the callback
   114      */
   115     void DontCallback()
   116     {
   117         mCallback = nullptr;
   118     }
   119 private:
   120     nsIAsyncVerifyRedirectCallback* mCallback;
   121     nsresult mResult;
   122 };
   124 #endif

mercurial