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