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 mozilla_dom_MediaDocument_h
8 #define mozilla_dom_MediaDocument_h
9 
10 #include "mozilla/Attributes.h"
11 #include "nsHTMLDocument.h"
12 #include "nsGenericHTMLElement.h"
13 #include "nsIStringBundle.h"
14 #include "nsIThreadRetargetableStreamListener.h"
15 
16 #define NSMEDIADOCUMENT_PROPERTIES_URI \
17   "chrome://global/locale/layout/MediaDocument.properties"
18 
19 #define NSMEDIADOCUMENT_PROPERTIES_URI_en_US \
20   "resource://gre/res/locale/layout/MediaDocument.properties"
21 
22 namespace mozilla {
23 namespace dom {
24 
25 class MediaDocument : public nsHTMLDocument {
26  public:
27   MediaDocument();
28   virtual ~MediaDocument();
29 
30   // Subclasses need to override this.
31   enum MediaDocumentKind MediaDocumentKind() const override = 0;
32 
33   virtual nsresult Init() override;
34 
35   virtual nsresult StartDocumentLoad(const char* aCommand, nsIChannel* aChannel,
36                                      nsILoadGroup* aLoadGroup,
37                                      nsISupports* aContainer,
38                                      nsIStreamListener** aDocListener,
39                                      bool aReset = true) override;
40 
WillIgnoreCharsetOverride()41   virtual bool WillIgnoreCharsetOverride() override { return true; }
42 
43  protected:
44   // Hook to be called once our initial document setup is done.  Subclasses
45   // should call this from SetScriptGlobalObject when setup hasn't been done
46   // yet, a non-null script global is being set, and they have finished whatever
47   // setup work they plan to do for an initial load.
48   void InitialSetupDone();
49 
50   // Check whether initial setup has been done.
InitialSetupHasBeenDone()51   [[nodiscard]] bool InitialSetupHasBeenDone() const {
52     return mDidInitialDocumentSetup;
53   }
54 
55   virtual nsresult CreateSyntheticDocument();
56 
57   friend class MediaDocumentStreamListener;
58   virtual nsresult StartLayout();
59 
60   void GetFileName(nsAString& aResult, nsIChannel* aChannel);
61 
62   nsresult LinkStylesheet(const nsAString& aStylesheet);
63   nsresult LinkScript(const nsAString& aScript);
64 
65   void FormatStringFromName(const char* aName,
66                             const nsTArray<nsString>& aParams,
67                             nsAString& aResult);
68 
69   // |aFormatNames[]| needs to have four elements in the following order:
70   // a format name with neither dimension nor file, a format name with
71   // filename but w/o dimension, a format name with dimension but w/o filename,
72   // a format name with both of them.  For instance, it can have
73   // "ImageTitleWithNeitherDimensionsNorFile", "ImageTitleWithoutDimensions",
74   // "ImageTitleWithDimesions2",  "ImageTitleWithDimensions2AndFile".
75   //
76   // Also see MediaDocument.properties if you want to define format names
77   // for a new subclass. aWidth and aHeight are pixels for |ImageDocument|,
78   // but could be in other units for other 'media', in which case you have to
79   // define format names accordingly.
80   void UpdateTitleAndCharset(const nsACString& aTypeStr, nsIChannel* aChannel,
81                              const char* const* aFormatNames = sFormatNames,
82                              int32_t aWidth = 0, int32_t aHeight = 0,
83                              const nsAString& aStatus = u""_ns);
84 
85   nsCOMPtr<nsIStringBundle> mStringBundle;
86   nsCOMPtr<nsIStringBundle> mStringBundleEnglish;
87   static const char* const sFormatNames[4];
88 
89  private:
90   enum { eWithNoInfo, eWithFile, eWithDim, eWithDimAndFile };
91 
92   // A boolean that indicates whether we did our initial document setup.  This
93   // will be false initially, become true when we finish setting up the document
94   // during initial load and stay true thereafter.
95   bool mDidInitialDocumentSetup;
96 };
97 
98 class MediaDocumentStreamListener : public nsIStreamListener,
99                                     public nsIThreadRetargetableStreamListener {
100  protected:
101   virtual ~MediaDocumentStreamListener();
102 
103  public:
104   explicit MediaDocumentStreamListener(MediaDocument* aDocument);
105   void SetStreamListener(nsIStreamListener* aListener);
106 
107   NS_DECL_THREADSAFE_ISUPPORTS
108 
109   NS_DECL_NSIREQUESTOBSERVER
110 
111   NS_DECL_NSISTREAMLISTENER
112 
113   NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
114 
DropDocumentRef()115   void DropDocumentRef() { mDocument = nullptr; }
116 
117   RefPtr<MediaDocument> mDocument;
118   nsCOMPtr<nsIStreamListener> mNextStream;
119 };
120 
121 }  // namespace dom
122 }  // namespace mozilla
123 
124 #endif /* mozilla_dom_MediaDocument_h */
125