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