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_URL_h
8 #define mozilla_dom_URL_h
9 
10 #include "mozilla/dom/URLSearchParams.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsIURI.h"
13 #include "nsString.h"
14 #include "nsWrapperCache.h"
15 
16 class nsISupports;
17 
18 namespace mozilla {
19 
20 class ErrorResult;
21 
22 namespace dom {
23 
24 class Blob;
25 class MediaSource;
26 class GlobalObject;
27 template <typename T>
28 class Optional;
29 
30 class URL final : public URLSearchParamsObserver, public nsWrapperCache {
31  public:
32   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(URL)33   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(URL)
34 
35   explicit URL(nsISupports* aParent) : mParent(aParent) {}
36 
37   // WebIDL methods
GetParentObject()38   nsISupports* GetParentObject() const { return mParent; }
39 
40   JSObject* WrapObject(JSContext* aCx,
41                        JS::Handle<JSObject*> aGivenProto) override;
42 
43   static already_AddRefed<URL> Constructor(const GlobalObject& aGlobal,
44                                            const nsAString& aURL,
45                                            const Optional<nsAString>& aBase,
46                                            ErrorResult& aRv);
47 
48   static already_AddRefed<URL> Constructor(nsISupports* aParent,
49                                            const nsAString& aURL,
50                                            const nsAString& aBase,
51                                            ErrorResult& aRv);
52 
53   static already_AddRefed<URL> Constructor(nsISupports* aParent,
54                                            const nsAString& aURL, nsIURI* aBase,
55                                            ErrorResult& aRv);
56 
57   static void CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
58                               nsAString& aResult, ErrorResult& aRv);
59 
60   static void CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource,
61                               nsAString& aResult, ErrorResult& aRv);
62 
63   static void RevokeObjectURL(const GlobalObject& aGlobal,
64                               const nsAString& aURL, ErrorResult& aRv);
65 
66   static bool IsValidURL(const GlobalObject& aGlobal, const nsAString& aURL,
67                          ErrorResult& aRv);
68 
69   void GetHref(nsAString& aHref) const;
70 
71   void SetHref(const nsAString& aHref, ErrorResult& aRv);
72 
73   void GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const;
74 
75   void GetProtocol(nsAString& aProtocol) const;
76 
77   void SetProtocol(const nsAString& aProtocol, ErrorResult& aRv);
78 
79   void GetUsername(nsAString& aUsername) const;
80 
81   void SetUsername(const nsAString& aUsername);
82 
83   void GetPassword(nsAString& aPassword) const;
84 
85   void SetPassword(const nsAString& aPassword);
86 
87   void GetHost(nsAString& aHost) const;
88 
89   void SetHost(const nsAString& aHost);
90 
91   void GetHostname(nsAString& aHostname) const;
92 
93   void SetHostname(const nsAString& aHostname);
94 
95   void GetPort(nsAString& aPort) const;
96 
97   void SetPort(const nsAString& aPort);
98 
99   void GetPathname(nsAString& aPathname) const;
100 
101   void SetPathname(const nsAString& aPathname);
102 
103   void GetSearch(nsAString& aSearch) const;
104 
105   void SetSearch(const nsAString& aSearch);
106 
107   URLSearchParams* SearchParams();
108 
109   void GetHash(nsAString& aHost) const;
110 
111   void SetHash(const nsAString& aHash);
112 
ToJSON(nsAString & aResult)113   void ToJSON(nsAString& aResult) const { GetHref(aResult); }
114 
115   // URLSearchParamsObserver
116   void URLSearchParamsUpdated(URLSearchParams* aSearchParams) override;
117 
118  private:
119   ~URL() = default;
120 
121   void SetURI(already_AddRefed<nsIURI> aURI);
122 
123   nsIURI* GetURI() const;
124 
125   void UpdateURLSearchParams();
126 
127  private:
128   void SetSearchInternal(const nsAString& aSearch);
129 
130   void CreateSearchParamsIfNeeded();
131 
132   nsCOMPtr<nsISupports> mParent;
133   RefPtr<URLSearchParams> mSearchParams;
134   nsCOMPtr<nsIURI> mURI;
135 };
136 
137 bool IsChromeURI(nsIURI* aURI);
138 
139 }  // namespace dom
140 }  // namespace mozilla
141 
142 #endif /* mozilla_dom_URL_h */
143