1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Josh Aas <josh@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsPluginHost_h_
40 : #define nsPluginHost_h_
41 :
42 : #include "nsIPluginHost.h"
43 : #include "nsIObserver.h"
44 : #include "nsCOMPtr.h"
45 : #include "prlink.h"
46 : #include "prclist.h"
47 : #include "npapi.h"
48 : #include "nsNPAPIPluginInstance.h"
49 : #include "nsIPluginTag.h"
50 : #include "nsPluginsDir.h"
51 : #include "nsPluginDirServiceProvider.h"
52 : #include "nsAutoPtr.h"
53 : #include "nsWeakPtr.h"
54 : #include "nsIPrompt.h"
55 : #include "nsISupportsArray.h"
56 : #include "nsIPrefBranch.h"
57 : #include "nsWeakReference.h"
58 : #include "nsThreadUtils.h"
59 : #include "nsTArray.h"
60 : #include "nsTObserverArray.h"
61 : #include "nsITimer.h"
62 : #include "nsPluginTags.h"
63 : #include "nsIEffectiveTLDService.h"
64 : #include "nsIIDNService.h"
65 : #include "nsCRT.h"
66 :
67 : class nsNPAPIPlugin;
68 : class nsIComponentManager;
69 : class nsIFile;
70 : class nsIChannel;
71 : class nsPluginNativeWindow;
72 : class nsObjectLoadingContent;
73 : class nsPluginInstanceOwner;
74 :
75 : #if defined(XP_MACOSX) && !defined(NP_NO_CARBON)
76 : #define MAC_CARBON_PLUGINS
77 : #endif
78 :
79 : class nsInvalidPluginTag : public nsISupports
80 : {
81 : public:
82 : nsInvalidPluginTag(const char* aFullPath, PRInt64 aLastModifiedTime = 0);
83 : virtual ~nsInvalidPluginTag();
84 :
85 : NS_DECL_ISUPPORTS
86 :
87 : nsCString mFullPath;
88 : PRInt64 mLastModifiedTime;
89 : bool mSeen;
90 :
91 : nsRefPtr<nsInvalidPluginTag> mPrev;
92 : nsRefPtr<nsInvalidPluginTag> mNext;
93 : };
94 :
95 : class nsPluginHost : public nsIPluginHost,
96 : public nsIObserver,
97 : public nsITimerCallback,
98 : public nsSupportsWeakReference
99 : {
100 : public:
101 : nsPluginHost();
102 : virtual ~nsPluginHost();
103 :
104 : static nsPluginHost* GetInst();
105 :
106 692 : NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
107 :
108 : NS_DECL_ISUPPORTS
109 : NS_DECL_NSIPLUGINHOST
110 : NS_DECL_NSIOBSERVER
111 : NS_DECL_NSITIMERCALLBACK
112 :
113 : nsresult Init();
114 : nsresult Destroy();
115 : nsresult LoadPlugins();
116 : nsresult CreateListenerForChannel(nsIChannel* aChannel,
117 : nsObjectLoadingContent* aContent,
118 : nsIStreamListener** aListener);
119 : nsresult SetUpPluginInstance(const char *aMimeType,
120 : nsIURI *aURL,
121 : nsIPluginInstanceOwner *aOwner);
122 : nsresult IsPluginEnabledForType(const char* aMimeType);
123 : nsresult IsPluginEnabledForExtension(const char* aExtension, const char* &aMimeType);
124 :
125 : nsresult GetPluginCount(PRUint32* aPluginCount);
126 : nsresult GetPlugins(PRUint32 aPluginCount, nsIDOMPlugin** aPluginArray);
127 :
128 : nsresult GetURL(nsISupports* pluginInst,
129 : const char* url,
130 : const char* target,
131 : nsIPluginStreamListener* streamListener,
132 : const char* altHost,
133 : const char* referrer,
134 : bool forceJSEnabled);
135 : nsresult PostURL(nsISupports* pluginInst,
136 : const char* url,
137 : PRUint32 postDataLen,
138 : const char* postData,
139 : bool isFile,
140 : const char* target,
141 : nsIPluginStreamListener* streamListener,
142 : const char* altHost,
143 : const char* referrer,
144 : bool forceJSEnabled,
145 : PRUint32 postHeadersLength,
146 : const char* postHeaders);
147 :
148 : nsresult FindProxyForURL(const char* url, char* *result);
149 : nsresult UserAgent(const char **retstring);
150 : nsresult ParsePostBufferToFixHeaders(const char *inPostData, PRUint32 inPostDataLen,
151 : char **outPostData, PRUint32 *outPostDataLen);
152 : nsresult CreateTempFileToPost(const char *aPostDataURL, nsIFile **aTmpFile);
153 : nsresult NewPluginNativeWindow(nsPluginNativeWindow ** aPluginNativeWindow);
154 : nsresult InstantiateDummyJavaPlugin(nsIPluginInstanceOwner *aOwner);
155 :
156 : void AddIdleTimeTarget(nsIPluginInstanceOwner* objectFrame, bool isVisible);
157 : void RemoveIdleTimeTarget(nsIPluginInstanceOwner* objectFrame);
158 :
159 : nsresult GetPluginName(nsNPAPIPluginInstance *aPluginInstance, const char** aPluginName);
160 : nsresult StopPluginInstance(nsNPAPIPluginInstance* aInstance);
161 : nsresult HandleBadPlugin(PRLibrary* aLibrary, nsNPAPIPluginInstance *aInstance);
162 : nsresult GetPluginTagForInstance(nsNPAPIPluginInstance *aPluginInstance, nsIPluginTag **aPluginTag);
163 :
164 : nsresult
165 : NewPluginURLStream(const nsString& aURL,
166 : nsNPAPIPluginInstance *aInstance,
167 : nsIPluginStreamListener *aListener,
168 : nsIInputStream *aPostStream = nsnull,
169 : const char *aHeadersData = nsnull,
170 : PRUint32 aHeadersDataLen = 0);
171 :
172 : nsresult
173 : GetURLWithHeaders(nsNPAPIPluginInstance *pluginInst,
174 : const char* url,
175 : const char* target = NULL,
176 : nsIPluginStreamListener* streamListener = NULL,
177 : const char* altHost = NULL,
178 : const char* referrer = NULL,
179 : bool forceJSEnabled = false,
180 : PRUint32 getHeadersLength = 0,
181 : const char* getHeaders = NULL);
182 :
183 : nsresult
184 : DoURLLoadSecurityCheck(nsNPAPIPluginInstance *aInstance,
185 : const char* aURL);
186 :
187 : nsresult
188 : AddHeadersToChannel(const char *aHeadersData, PRUint32 aHeadersDataLen,
189 : nsIChannel *aGenericChannel);
190 :
191 : static nsresult GetPluginTempDir(nsIFile **aDir);
192 :
193 : // Writes updated plugins settings to disk and unloads the plugin
194 : // if it is now disabled
195 : nsresult UpdatePluginInfo(nsPluginTag* aPluginTag);
196 :
197 : // checks whether aTag is a "java" plugin tag (a tag for a plugin
198 : // that does Java)
199 : static bool IsJavaMIMEType(const char *aType);
200 :
201 : static nsresult GetPrompt(nsIPluginInstanceOwner *aOwner, nsIPrompt **aPrompt);
202 :
203 : static nsresult PostPluginUnloadEvent(PRLibrary* aLibrary);
204 :
205 : void PluginCrashed(nsNPAPIPlugin* plugin,
206 : const nsAString& pluginDumpID,
207 : const nsAString& browserDumpID);
208 :
209 : nsNPAPIPluginInstance *FindInstance(const char *mimetype);
210 : nsNPAPIPluginInstance *FindOldestStoppedInstance();
211 : PRUint32 StoppedInstanceCount();
212 :
213 : nsTArray< nsRefPtr<nsNPAPIPluginInstance> > *InstanceArray();
214 :
215 : void DestroyRunningInstances(nsISupportsArray* aReloadDocs, nsPluginTag* aPluginTag);
216 :
217 : // Return the tag for |aLibrary| if found, nsnull if not.
218 : nsPluginTag* FindTagForLibrary(PRLibrary* aLibrary);
219 :
220 : // The last argument should be false if we already have an in-flight stream
221 : // and don't need to set up a new stream.
222 : nsresult InstantiateEmbeddedPlugin(const char *aMimeType, nsIURI* aURL,
223 : nsObjectLoadingContent *aContent,
224 : nsPluginInstanceOwner** aOwner);
225 :
226 : nsresult InstantiateFullPagePlugin(const char *aMimeType,
227 : nsIURI* aURI,
228 : nsObjectLoadingContent *aContent,
229 : nsPluginInstanceOwner **aOwner,
230 : nsIStreamListener **aStreamListener);
231 :
232 : // Does not accept NULL and should never fail.
233 : nsPluginTag* TagForPlugin(nsNPAPIPlugin* aPlugin);
234 :
235 : nsresult GetPlugin(const char *aMimeType, nsNPAPIPlugin** aPlugin);
236 :
237 : private:
238 : nsresult
239 : TrySetUpPluginInstance(const char *aMimeType, nsIURI *aURL, nsIPluginInstanceOwner *aOwner);
240 :
241 : nsresult
242 : NewEmbeddedPluginStreamListener(nsIURI* aURL, nsObjectLoadingContent *aContent,
243 : nsNPAPIPluginInstance* aInstance,
244 : nsIStreamListener** aListener);
245 :
246 : nsresult
247 : NewEmbeddedPluginStream(nsIURI* aURL, nsObjectLoadingContent *aContent, nsNPAPIPluginInstance* aInstance);
248 :
249 : nsresult
250 : NewFullPagePluginStream(nsIURI* aURI,
251 : nsNPAPIPluginInstance *aInstance,
252 : nsIStreamListener **aStreamListener);
253 :
254 : nsPluginTag*
255 : FindPreferredPlugin(const InfallibleTArray<nsPluginTag*>& matches);
256 :
257 : // Return an nsPluginTag for this type, if any. If aCheckEnabled is
258 : // true, only enabled plugins will be returned.
259 : nsPluginTag*
260 : FindPluginForType(const char* aMimeType, bool aCheckEnabled);
261 :
262 : nsPluginTag*
263 : FindPluginEnabledForExtension(const char* aExtension, const char* &aMimeType);
264 :
265 : nsresult
266 : FindStoppedPluginForURL(nsIURI* aURL, nsIPluginInstanceOwner *aOwner);
267 :
268 : nsresult
269 : FindPlugins(bool aCreatePluginList, bool * aPluginsChanged);
270 :
271 : nsresult
272 : ScanPluginsDirectory(nsIFile *pluginsDir,
273 : bool aCreatePluginList,
274 : bool *aPluginsChanged);
275 :
276 : nsresult
277 : ScanPluginsDirectoryList(nsISimpleEnumerator *dirEnum,
278 : bool aCreatePluginList,
279 : bool *aPluginsChanged);
280 :
281 : nsresult EnsurePluginLoaded(nsPluginTag* plugin);
282 :
283 : bool IsRunningPlugin(nsPluginTag * plugin);
284 :
285 : // Stores all plugins info into the registry
286 : nsresult WritePluginInfo();
287 :
288 : // Loads all cached plugins info into mCachedPlugins
289 : nsresult ReadPluginInfo();
290 :
291 : // Given a file path, returns the plugins info from our cache
292 : // and removes it from the cache.
293 : void RemoveCachedPluginsInfo(const char *filePath,
294 : nsPluginTag **result);
295 :
296 : // Checks to see if a tag object is in our list of live tags.
297 : bool IsLiveTag(nsIPluginTag* tag);
298 :
299 : nsresult EnsurePrivateDirServiceProvider();
300 :
301 : void OnPluginInstanceDestroyed(nsPluginTag* aPluginTag);
302 :
303 : nsRefPtr<nsPluginTag> mPlugins;
304 : nsRefPtr<nsPluginTag> mCachedPlugins;
305 : nsRefPtr<nsInvalidPluginTag> mInvalidPlugins;
306 : bool mPluginsLoaded;
307 : bool mDontShowBadPluginMessage;
308 : bool mIsDestroyed;
309 :
310 : // set by pref plugin.override_internal_types
311 : bool mOverrideInternalTypes;
312 :
313 : // set by pref plugin.disable
314 : bool mPluginsDisabled;
315 :
316 : // Any instances in this array will have valid plugin objects via GetPlugin().
317 : // When removing an instance it might not die - be sure to null out it's plugin.
318 : nsTArray< nsRefPtr<nsNPAPIPluginInstance> > mInstances;
319 :
320 : nsCOMPtr<nsIFile> mPluginRegFile;
321 : nsCOMPtr<nsIPrefBranch> mPrefService;
322 : #ifdef XP_WIN
323 : nsRefPtr<nsPluginDirServiceProvider> mPrivateDirServiceProvider;
324 : #endif
325 :
326 : nsCOMPtr<nsIEffectiveTLDService> mTLDService;
327 : nsCOMPtr<nsIIDNService> mIDNService;
328 :
329 : // Helpers for ClearSiteData and SiteHasData.
330 : nsresult NormalizeHostname(nsCString& host);
331 : nsresult EnumerateSiteData(const nsACString& domain,
332 : const nsTArray<nsCString>& sites,
333 : InfallibleTArray<nsCString>& result,
334 : bool firstMatchOnly);
335 :
336 : nsWeakPtr mCurrentDocument; // weak reference, we use it to id document only
337 :
338 : static nsIFile *sPluginTempDir;
339 :
340 : // We need to hold a global ptr to ourselves because we register for
341 : // two different CIDs for some reason...
342 : static nsPluginHost* sInst;
343 :
344 : #ifdef MAC_CARBON_PLUGINS
345 : nsCOMPtr<nsITimer> mVisiblePluginTimer;
346 : nsTObserverArray<nsIPluginInstanceOwner*> mVisibleTimerTargets;
347 : nsCOMPtr<nsITimer> mHiddenPluginTimer;
348 : nsTObserverArray<nsIPluginInstanceOwner*> mHiddenTimerTargets;
349 : #endif
350 : };
351 :
352 : class NS_STACK_CLASS PluginDestructionGuard : protected PRCList
353 : {
354 : public:
355 0 : PluginDestructionGuard(nsNPAPIPluginInstance *aInstance)
356 0 : : mInstance(aInstance)
357 : {
358 0 : Init();
359 0 : }
360 :
361 0 : PluginDestructionGuard(NPP npp)
362 0 : : mInstance(npp ? static_cast<nsNPAPIPluginInstance*>(npp->ndata) : nsnull)
363 : {
364 0 : Init();
365 0 : }
366 :
367 : ~PluginDestructionGuard();
368 :
369 : static bool DelayDestroy(nsNPAPIPluginInstance *aInstance);
370 :
371 : protected:
372 0 : void Init()
373 : {
374 0 : NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread");
375 :
376 0 : mDelayedDestroy = false;
377 :
378 0 : PR_INIT_CLIST(this);
379 0 : PR_INSERT_BEFORE(this, &sListHead);
380 0 : }
381 :
382 : nsRefPtr<nsNPAPIPluginInstance> mInstance;
383 : bool mDelayedDestroy;
384 :
385 : static PRCList sListHead;
386 : };
387 :
388 : #endif // nsPluginHost_h_
|