netwerk/base/public/nsAsyncRedirectVerifyHelper.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/base/public/nsAsyncRedirectVerifyHelper.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsAsyncRedirectVerifyHelper_h
    1.10 +#define nsAsyncRedirectVerifyHelper_h
    1.11 +
    1.12 +#include "nsIRunnable.h"
    1.13 +#include "nsIThread.h"
    1.14 +#include "nsIChannelEventSink.h"
    1.15 +#include "nsIInterfaceRequestor.h"
    1.16 +#include "nsIAsyncVerifyRedirectCallback.h"
    1.17 +#include "nsCOMPtr.h"
    1.18 +#include "nsAutoPtr.h"
    1.19 +#include "nsCycleCollectionParticipant.h"
    1.20 +#include "mozilla/Attributes.h"
    1.21 +
    1.22 +class nsIChannel;
    1.23 +
    1.24 +/**
    1.25 + * This class simplifies call of OnChannelRedirect of IOService and
    1.26 + * the sink bound with the channel being redirected while the result of
    1.27 + * redirect decision is returned through the callback.
    1.28 + */
    1.29 +class nsAsyncRedirectVerifyHelper MOZ_FINAL : public nsIRunnable,
    1.30 +                                              public nsIAsyncVerifyRedirectCallback
    1.31 +{
    1.32 +    NS_DECL_THREADSAFE_ISUPPORTS
    1.33 +    NS_DECL_NSIRUNNABLE
    1.34 +    NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
    1.35 +
    1.36 +public:
    1.37 +    nsAsyncRedirectVerifyHelper();
    1.38 +
    1.39 +    /*
    1.40 +     * Calls AsyncOnChannelRedirect() on the given sink with the given
    1.41 +     * channels and flags. Keeps track of number of async callbacks to expect.
    1.42 +     */
    1.43 +    nsresult DelegateOnChannelRedirect(nsIChannelEventSink *sink,
    1.44 +                                       nsIChannel *oldChannel, 
    1.45 +                                       nsIChannel *newChannel,
    1.46 +                                       uint32_t flags);
    1.47 + 
    1.48 +    /**
    1.49 +     * Initialize and run the chain of AsyncOnChannelRedirect calls. OldChannel
    1.50 +     * is QI'ed for nsIAsyncVerifyRedirectCallback. The result of the redirect
    1.51 +     * decision is passed through this interface back to the oldChannel.
    1.52 +     *
    1.53 +     * @param oldChan
    1.54 +     *    channel being redirected, MUST implement
    1.55 +     *    nsIAsyncVerifyRedirectCallback
    1.56 +     * @param newChan
    1.57 +     *    target of the redirect channel
    1.58 +     * @param flags
    1.59 +     *    redirect flags
    1.60 +     * @param synchronize
    1.61 +     *    set to TRUE if you want the Init method wait synchronously for
    1.62 +     *    all redirect callbacks
    1.63 +     */
    1.64 +    nsresult Init(nsIChannel* oldChan,
    1.65 +                  nsIChannel* newChan,
    1.66 +                  uint32_t flags,
    1.67 +                  bool synchronize = false);
    1.68 +
    1.69 +protected:
    1.70 +    nsCOMPtr<nsIChannel> mOldChan;
    1.71 +    nsCOMPtr<nsIChannel> mNewChan;
    1.72 +    uint32_t mFlags;
    1.73 +    bool mWaitingForRedirectCallback;
    1.74 +    nsCOMPtr<nsIThread>      mCallbackThread;
    1.75 +    bool                     mCallbackInitiated;
    1.76 +    int32_t                  mExpectedCallbacks;
    1.77 +    nsresult                 mResult; // value passed to callback
    1.78 +
    1.79 +    void InitCallback();
    1.80 +    
    1.81 +    /**
    1.82 +     * Calls back to |oldChan| as described in Init()
    1.83 +     */
    1.84 +    void ExplicitCallback(nsresult result);
    1.85 +
    1.86 +private:
    1.87 +    ~nsAsyncRedirectVerifyHelper();
    1.88 +    
    1.89 +    bool IsOldChannelCanceled();
    1.90 +};
    1.91 +
    1.92 +/*
    1.93 + * Helper to make the call-stack handle some control-flow for us
    1.94 + */
    1.95 +class nsAsyncRedirectAutoCallback
    1.96 +{
    1.97 +public:
    1.98 +    nsAsyncRedirectAutoCallback(nsIAsyncVerifyRedirectCallback* aCallback)
    1.99 +        : mCallback(aCallback)
   1.100 +    {
   1.101 +        mResult = NS_OK;
   1.102 +    }
   1.103 +    ~nsAsyncRedirectAutoCallback()
   1.104 +    {
   1.105 +        if (mCallback)
   1.106 +            mCallback->OnRedirectVerifyCallback(mResult);
   1.107 +    }
   1.108 +    /*
   1.109 +     * Call this is you want it to call back with a different result-code
   1.110 +     */
   1.111 +    void SetResult(nsresult aRes)
   1.112 +    {
   1.113 +        mResult = aRes;
   1.114 +    }
   1.115 +    /*
   1.116 +     * Call this is you want to avoid the callback
   1.117 +     */
   1.118 +    void DontCallback()
   1.119 +    {
   1.120 +        mCallback = nullptr;
   1.121 +    }
   1.122 +private:
   1.123 +    nsIAsyncVerifyRedirectCallback* mCallback;
   1.124 +    nsresult mResult;
   1.125 +};
   1.126 +
   1.127 +#endif

mercurial