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_Navigator_h
8 #define mozilla_dom_Navigator_h
9 
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/dom/AddonManagerBinding.h"
12 #include "mozilla/dom/BindingDeclarations.h"
13 #include "mozilla/dom/Fetch.h"
14 #include "mozilla/dom/Nullable.h"
15 #include "nsWrapperCache.h"
16 #include "nsHashKeys.h"
17 #include "nsInterfaceHashtable.h"
18 #include "nsString.h"
19 #include "nsTArray.h"
20 #include "mozilla/dom/MediaKeySystemAccessManager.h"
21 
22 class nsPluginArray;
23 class nsMimeTypeArray;
24 class nsPIDOMWindowInner;
25 class nsIDOMNavigatorSystemMessages;
26 class nsIPrincipal;
27 class nsIURI;
28 
29 namespace mozilla {
30 class ErrorResult;
31 
32 namespace dom {
33 class AddonManager;
34 class BodyExtractorBase;
35 class Geolocation;
36 class systemMessageCallback;
37 class MediaDevices;
38 struct MediaStreamConstraints;
39 class WakeLock;
40 class ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams;
41 class ServiceWorkerContainer;
42 class DOMRequest;
43 class CredentialsContainer;
44 class Clipboard;
45 class LockManager;
46 }  // namespace dom
47 namespace webgpu {
48 class Instance;
49 }  // namespace webgpu
50 }  // namespace mozilla
51 
52 //*****************************************************************************
53 // Navigator: Script "navigator" object
54 //*****************************************************************************
55 
56 namespace mozilla::dom {
57 
58 class Permissions;
59 
60 namespace battery {
61 class BatteryManager;
62 }  // namespace battery
63 
64 class Promise;
65 
66 class Gamepad;
67 class GamepadServiceTest;
68 class NavigatorUserMediaSuccessCallback;
69 class NavigatorUserMediaErrorCallback;
70 
71 struct MIDIOptions;
72 
73 nsTArray<uint32_t> SanitizeVibratePattern(const nsTArray<uint32_t>& aPattern);
74 
75 namespace network {
76 class Connection;
77 }  // namespace network
78 
79 class LegacyMozTCPSocket;
80 class VRDisplay;
81 class VRServiceTest;
82 class XRSystem;
83 class StorageManager;
84 class MediaCapabilities;
85 class MediaSession;
86 struct ShareData;
87 class WindowGlobalChild;
88 
89 class Navigator final : public nsISupports, public nsWrapperCache {
90  public:
91   explicit Navigator(nsPIDOMWindowInner* aInnerWindow);
92 
93   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
94   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Navigator)
95 
96   void Invalidate();
GetWindow()97   nsPIDOMWindowInner* GetWindow() const { return mWindow; }
98 
99   size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
100 
101   /**
102    * For use during document.write where our inner window changes.
103    */
104   void SetWindow(nsPIDOMWindowInner* aInnerWindow);
105 
106   /**
107    * Called when the inner window navigates to a new page.
108    */
109   void OnNavigation();
110 
111   void GetProduct(nsAString& aProduct);
112   void GetLanguage(nsAString& aLanguage);
113   void GetAppName(nsAString& aAppName, CallerType aCallerType) const;
114   void GetAppVersion(nsAString& aAppName, CallerType aCallerType,
115                      ErrorResult& aRv) const;
116   void GetPlatform(nsAString& aPlatform, CallerType aCallerType,
117                    ErrorResult& aRv) const;
118   void GetUserAgent(nsAString& aUserAgent, CallerType aCallerType,
119                     ErrorResult& aRv) const;
120   bool OnLine();
121   void CheckProtocolHandlerAllowed(const nsAString& aScheme,
122                                    nsIURI* aHandlerURI, nsIURI* aDocumentURI,
123                                    ErrorResult& aRv);
124   void RegisterProtocolHandler(const nsAString& aScheme, const nsAString& aURL,
125                                ErrorResult& aRv);
126   nsMimeTypeArray* GetMimeTypes(ErrorResult& aRv);
127   nsPluginArray* GetPlugins(ErrorResult& aRv);
128   bool PdfViewerEnabled();
129   Permissions* GetPermissions(ErrorResult& aRv);
130   void GetDoNotTrack(nsAString& aResult);
131   bool GlobalPrivacyControl();
132   Geolocation* GetGeolocation(ErrorResult& aRv);
133   Promise* GetBattery(ErrorResult& aRv);
134 
135   bool CanShare(const ShareData& aData);
136   Promise* Share(const ShareData& aData, ErrorResult& aRv);
137 
138   static void AppName(nsAString& aAppName, nsIPrincipal* aCallerPrincipal,
139                       bool aUsePrefOverriddenValue);
140 
141   static nsresult GetPlatform(nsAString& aPlatform,
142                               nsIPrincipal* aCallerPrincipal,
143                               bool aUsePrefOverriddenValue);
144 
145   static nsresult GetAppVersion(nsAString& aAppVersion,
146                                 nsIPrincipal* aCallerPrincipal,
147                                 bool aUsePrefOverriddenValue);
148 
149   static nsresult GetUserAgent(nsPIDOMWindowInner* aWindow,
150                                nsIPrincipal* aCallerPrincipal,
151                                bool aIsCallerChrome, nsAString& aUserAgent);
152 
153   // Clears the platform cache by calling:
154   // Navigator_Binding::ClearCachedPlatformValue(this);
155   void ClearPlatformCache();
156 
157   // Clears the user agent cache by calling:
158   // Navigator_Binding::ClearCachedUserAgentValue(this);
159   void ClearUserAgentCache();
160 
161   bool Vibrate(uint32_t aDuration);
162   bool Vibrate(const nsTArray<uint32_t>& aDuration);
163   void SetVibrationPermission(bool aPermitted, bool aPersistent);
164   uint32_t MaxTouchPoints(CallerType aCallerType);
165   void GetAppCodeName(nsAString& aAppCodeName, ErrorResult& aRv);
166   void GetOscpu(nsAString& aOscpu, CallerType aCallerType,
167                 ErrorResult& aRv) const;
168   void GetVendorSub(nsAString& aVendorSub);
169   void GetVendor(nsAString& aVendor);
170   void GetProductSub(nsAString& aProductSub);
171   bool CookieEnabled();
172   void GetBuildID(nsAString& aBuildID, CallerType aCallerType,
173                   ErrorResult& aRv) const;
JavaEnabled()174   bool JavaEnabled() { return false; }
175   uint64_t HardwareConcurrency();
TaintEnabled()176   bool TaintEnabled() { return false; }
177 
178   already_AddRefed<LegacyMozTCPSocket> MozTCPSocket();
179   network::Connection* GetConnection(ErrorResult& aRv);
180   MediaDevices* GetMediaDevices(ErrorResult& aRv);
GetExtantMediaDevices()181   MediaDevices* GetExtantMediaDevices() const { return mMediaDevices; };
182 
183   void GetGamepads(nsTArray<RefPtr<Gamepad>>& aGamepads, ErrorResult& aRv);
184   GamepadServiceTest* RequestGamepadServiceTest();
185   already_AddRefed<Promise> GetVRDisplays(ErrorResult& aRv);
186   void FinishGetVRDisplays(bool isWebVRSupportedInwindow, Promise* p);
187   void GetActiveVRDisplays(nsTArray<RefPtr<VRDisplay>>& aDisplays) const;
188   void OnXRPermissionRequestAllow();
189   void OnXRPermissionRequestCancel();
190   VRServiceTest* RequestVRServiceTest();
191   bool IsWebVRContentDetected() const;
192   bool IsWebVRContentPresenting() const;
193   void RequestVRPresentation(VRDisplay& aDisplay);
194   XRSystem* GetXr(ErrorResult& aRv);
195   already_AddRefed<Promise> RequestMIDIAccess(const MIDIOptions& aOptions,
196                                               ErrorResult& aRv);
197 
198   bool SendBeacon(const nsAString& aUrl, const Nullable<fetch::BodyInit>& aData,
199                   ErrorResult& aRv);
200 
201   void MozGetUserMedia(const MediaStreamConstraints& aConstraints,
202                        NavigatorUserMediaSuccessCallback& aOnSuccess,
203                        NavigatorUserMediaErrorCallback& aOnError,
204                        CallerType aCallerType, ErrorResult& aRv);
205 
206   already_AddRefed<ServiceWorkerContainer> ServiceWorker();
207 
208   mozilla::dom::CredentialsContainer* Credentials();
209   dom::Clipboard* Clipboard();
210   webgpu::Instance* Gpu();
211   dom::LockManager* Locks();
212 
213   static bool Webdriver();
214 
215   void GetLanguages(nsTArray<nsString>& aLanguages);
216 
217   StorageManager* Storage();
218 
219   static void GetAcceptLanguages(nsTArray<nsString>& aLanguages);
220 
221   dom::MediaCapabilities* MediaCapabilities();
222   dom::MediaSession* MediaSession();
223 
224   AddonManager* GetMozAddonManager(ErrorResult& aRv);
225 
226   // WebIDL helper methods
227   static bool HasUserMediaSupport(JSContext* /* unused */,
228                                   JSObject* /* unused */);
229   static bool HasShareSupport(JSContext* /* unused */, JSObject* /* unused */);
230 
GetParentObject()231   nsPIDOMWindowInner* GetParentObject() const { return GetWindow(); }
232 
233   virtual JSObject* WrapObject(JSContext* cx,
234                                JS::Handle<JSObject*> aGivenProto) override;
235 
236   // GetWindowFromGlobal returns the inner window for this global, if
237   // any, else null.
238   static already_AddRefed<nsPIDOMWindowInner> GetWindowFromGlobal(
239       JSObject* aGlobal);
240 
241   already_AddRefed<Promise> RequestMediaKeySystemAccess(
242       const nsAString& aKeySystem,
243       const Sequence<MediaKeySystemConfiguration>& aConfig, ErrorResult& aRv);
244 
245   bool HasCreatedMediaSession() const;
246 
247  private:
248   void ValidateShareData(const ShareData& aData, ErrorResult& aRv);
249   RefPtr<MediaKeySystemAccessManager> mMediaKeySystemAccessManager;
250 
251  public:
252   void NotifyVRDisplaysUpdated();
253   void NotifyActiveVRDisplaysChanged();
254 
255  private:
256   virtual ~Navigator();
257 
258   // This enum helps SendBeaconInternal to apply different behaviors to body
259   // types.
260   enum BeaconType { eBeaconTypeBlob, eBeaconTypeArrayBuffer, eBeaconTypeOther };
261 
262   bool SendBeaconInternal(const nsAString& aUrl, BodyExtractorBase* aBody,
263                           BeaconType aType, ErrorResult& aRv);
264 
GetDocShell()265   nsIDocShell* GetDocShell() const {
266     return mWindow ? mWindow->GetDocShell() : nullptr;
267   }
268 
269   RefPtr<nsPluginArray> mPlugins;
270   RefPtr<Permissions> mPermissions;
271   RefPtr<Geolocation> mGeolocation;
272   RefPtr<battery::BatteryManager> mBatteryManager;
273   RefPtr<Promise> mBatteryPromise;
274   RefPtr<network::Connection> mConnection;
275   RefPtr<CredentialsContainer> mCredentials;
276   RefPtr<dom::Clipboard> mClipboard;
277   RefPtr<MediaDevices> mMediaDevices;
278   RefPtr<ServiceWorkerContainer> mServiceWorkerContainer;
279   nsCOMPtr<nsPIDOMWindowInner> mWindow;
280   RefPtr<GamepadServiceTest> mGamepadServiceTest;
281   nsTArray<RefPtr<Promise>> mVRGetDisplaysPromises;
282   RefPtr<VRServiceTest> mVRServiceTest;
283   RefPtr<XRSystem> mXRSystem;
284   nsTArray<uint32_t> mRequestedVibrationPattern;
285   RefPtr<StorageManager> mStorageManager;
286   RefPtr<dom::MediaCapabilities> mMediaCapabilities;
287   RefPtr<dom::MediaSession> mMediaSession;
288   RefPtr<AddonManager> mAddonManager;
289   RefPtr<webgpu::Instance> mWebGpu;
290   RefPtr<Promise> mSharePromise;  // Web Share API related
291   RefPtr<dom::LockManager> mLocks;
292 };
293 
294 }  // namespace mozilla::dom
295 
296 #endif  // mozilla_dom_Navigator_h
297