1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsPluginArray_h___
8 #define nsPluginArray_h___
9 
10 #include "nsTArray.h"
11 #include "nsWeakReference.h"
12 #include "nsIObserver.h"
13 #include "nsWrapperCache.h"
14 #include "nsPIDOMWindow.h"
15 
16 class nsPluginElement;
17 class nsMimeType;
18 class nsIInternalPluginTag;
19 
20 class nsPluginArray final : public nsIObserver,
21                             public nsSupportsWeakReference,
22                             public nsWrapperCache
23 {
24 public:
25   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
26   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsPluginArray,
27                                                          nsIObserver)
28 
29   // nsIObserver
30   NS_DECL_NSIOBSERVER
31 
32   explicit nsPluginArray(nsPIDOMWindowInner* aWindow);
33   nsPIDOMWindowInner* GetParentObject() const;
34   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
35 
36   // nsPluginArray registers itself as an observer with a weak reference.
37   // This can't be done in the constructor, because at that point its
38   // refcount is 0 (and it gets destroyed upon registration). So, Init()
39   // must be called after construction.
40   void Init();
41   void Invalidate();
42 
43   void GetMimeTypes(nsTArray<RefPtr<nsMimeType>>& aMimeTypes);
44   void GetCTPMimeTypes(nsTArray<RefPtr<nsMimeType>>& aMimeTypes);
45 
46   static void NotifyHiddenPluginTouched(nsPluginElement* aElement);
47 
48   // PluginArray WebIDL methods
49 
50   nsPluginElement* Item(uint32_t aIndex);
51   nsPluginElement* NamedItem(const nsAString& aName);
52   void Refresh(bool aReloadDocuments);
53   nsPluginElement* IndexedGetter(uint32_t aIndex, bool &aFound);
54   nsPluginElement* NamedGetter(const nsAString& aName, bool &aFound);
55   uint32_t Length();
56   void GetSupportedNames(nsTArray<nsString>& aRetval);
57 
58 private:
59   virtual ~nsPluginArray();
60 
61   bool AllowPlugins() const;
62   void EnsurePlugins();
63 
64   nsCOMPtr<nsPIDOMWindowInner> mWindow;
65   nsTArray<RefPtr<nsPluginElement> > mPlugins;
66   /* A separate list of click-to-play plugins that we don't tell content
67    * about but keep track of so we can still prompt the user to click to play.
68    */
69   nsTArray<RefPtr<nsPluginElement> > mCTPPlugins;
70 };
71 
72 class nsPluginElement final : public nsISupports,
73                               public nsWrapperCache
74 {
75 public:
76   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
77   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsPluginElement)
78 
79   nsPluginElement(nsPIDOMWindowInner* aWindow,
80                   nsIInternalPluginTag* aPluginTag);
81 
82   nsPIDOMWindowInner* GetParentObject() const;
83   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
84 
PluginTag()85   nsIInternalPluginTag* PluginTag() const
86   {
87     return mPluginTag;
88   }
89 
90   // Plugin WebIDL methods
91 
92   void GetDescription(nsString& retval) const;
93   void GetFilename(nsString& retval) const;
94   void GetVersion(nsString& retval) const;
95   void GetName(nsString& retval) const;
96   nsMimeType* Item(uint32_t index);
97   nsMimeType* NamedItem(const nsAString& name);
98   nsMimeType* IndexedGetter(uint32_t index, bool &found);
99   nsMimeType* NamedGetter(const nsAString& name, bool &found);
100   uint32_t Length();
101   void GetSupportedNames(nsTArray<nsString>& retval);
102 
103   nsTArray<RefPtr<nsMimeType> >& MimeTypes();
104 
105 protected:
106   ~nsPluginElement();
107 
108   void EnsurePluginMimeTypes();
109 
110   nsCOMPtr<nsPIDOMWindowInner> mWindow;
111   nsCOMPtr<nsIInternalPluginTag> mPluginTag;
112   nsTArray<RefPtr<nsMimeType> > mMimeTypes;
113 };
114 
115 #endif /* nsPluginArray_h___ */
116