1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
5 
6 #ifndef nsNPAPIPluginStreamListener_h_
7 #define nsNPAPIPluginStreamListener_h_
8 
9 #include "nscore.h"
10 #include "nsIHTTPHeaderListener.h"
11 #include "nsINamed.h"
12 #include "nsITimer.h"
13 #include "nsCOMArray.h"
14 #include "nsIOutputStream.h"
15 #include "nsString.h"
16 #include "nsIAsyncVerifyRedirectCallback.h"
17 #include "mozilla/PluginLibrary.h"
18 
19 #define MAX_PLUGIN_NECKO_BUFFER 16384
20 
21 class nsPluginStreamListenerPeer;
22 class nsNPAPIPluginStreamListener;
23 class nsNPAPIPluginInstance;
24 class nsIChannel;
25 
26 class nsNPAPIStreamWrapper {
27  public:
28   nsNPAPIStreamWrapper(nsIOutputStream* outputStream,
29                        nsNPAPIPluginStreamListener* streamListener);
30   ~nsNPAPIStreamWrapper();
31 
GetOutputStream()32   nsIOutputStream* GetOutputStream() { return mOutputStream.get(); }
GetStreamListener()33   nsNPAPIPluginStreamListener* GetStreamListener() { return mStreamListener; }
34 
35   NPStream mNPStream;
36 
37  protected:
38   nsCOMPtr<nsIOutputStream>
39       mOutputStream;  // only valid if not browser initiated
40   nsNPAPIPluginStreamListener*
41       mStreamListener;  // only valid if browser initiated
42 };
43 
44 class nsNPAPIPluginStreamListener : public nsITimerCallback,
45                                     public nsIHTTPHeaderListener,
46                                     public nsINamed {
47  private:
48   typedef mozilla::PluginLibrary PluginLibrary;
49 
50  public:
51   NS_DECL_ISUPPORTS
52   NS_DECL_NSITIMERCALLBACK
53   NS_DECL_NSIHTTPHEADERLISTENER
54   NS_DECL_NSINAMED
55 
56   nsNPAPIPluginStreamListener(nsNPAPIPluginInstance* inst, void* notifyData,
57                               const char* aURL);
58 
59   nsresult OnStartBinding(nsPluginStreamListenerPeer* streamPeer);
60   nsresult OnDataAvailable(nsPluginStreamListenerPeer* streamPeer,
61                            nsIInputStream* input, uint32_t length);
62   nsresult OnFileAvailable(nsPluginStreamListenerPeer* streamPeer,
63                            const char* fileName);
64   nsresult OnStopBinding(nsPluginStreamListenerPeer* streamPeer,
65                          nsresult status);
66 
67   bool IsStarted();
68   nsresult CleanUpStream(NPReason reason);
69   void CallURLNotify(NPReason reason);
SetCallNotify(bool aCallNotify)70   void SetCallNotify(bool aCallNotify) { mCallNotify = aCallNotify; }
71   void SuspendRequest();
72   void ResumeRequest();
73   nsresult StartDataPump();
74   void StopDataPump();
75   bool PluginInitJSLoadInProgress();
76 
77   void* GetNotifyData();
GetStreamListenerPeer()78   nsPluginStreamListenerPeer* GetStreamListenerPeer() {
79     return mStreamListenerPeer;
80   }
SetStreamListenerPeer(nsPluginStreamListenerPeer * aPeer)81   void SetStreamListenerPeer(nsPluginStreamListenerPeer* aPeer) {
82     mStreamListenerPeer = aPeer;
83   }
84 
85   // Returns true if the redirect will be handled by NPAPI, false otherwise.
86   bool HandleRedirectNotification(nsIChannel* oldChannel,
87                                   nsIChannel* newChannel,
88                                   nsIAsyncVerifyRedirectCallback* callback);
89   void URLRedirectResponse(NPBool allow);
90 
91  protected:
92   enum StreamState {
93     eStreamStopped = 0,  // The stream is stopped
94     eNewStreamCalled,    // NPP_NewStream was called but has not completed yet
95     eStreamTypeSet       // The stream is fully initialized
96   };
97 
98   enum StreamStopMode { eNormalStop = 0, eDoDeferredStop, eStopPending };
99 
100   virtual ~nsNPAPIPluginStreamListener();
101   bool MaybeRunStopBinding();
102 
103   char* mStreamBuffer;
104   char* mNotifyURL;
105   RefPtr<nsNPAPIPluginInstance> mInst;
106   nsNPAPIStreamWrapper* mNPStreamWrapper;
107   uint32_t mStreamBufferSize;
108   int32_t mStreamBufferByteCount;
109   StreamState mStreamState;
110   bool mStreamCleanedUp;
111   bool mCallNotify;
112   bool mIsSuspended;
113   bool mIsPluginInitJSStream;
114   bool mRedirectDenied;
115   nsCString mResponseHeaders;
116   char* mResponseHeaderBuf;
117   nsCOMPtr<nsITimer> mDataPumpTimer;
118   nsCOMPtr<nsIAsyncVerifyRedirectCallback> mHTTPRedirectCallback;
119   StreamStopMode mStreamStopMode;
120   nsresult mPendingStopBindingStatus;
121 
122  public:
123   RefPtr<nsPluginStreamListenerPeer> mStreamListenerPeer;
124 };
125 
126 #endif  // nsNPAPIPluginStreamListener_h_
127