Location.h (4920B)
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_Location_h 8 #define mozilla_dom_Location_h 9 10 #include "js/TypeDecls.h" 11 #include "mozilla/ErrorResult.h" 12 #include "mozilla/LinkedList.h" 13 #include "mozilla/dom/BrowsingContext.h" 14 #include "mozilla/dom/DOMStringList.h" 15 #include "mozilla/dom/LocationBase.h" 16 #include "nsCycleCollectionParticipant.h" 17 #include "nsString.h" 18 #include "nsWrapperCache.h" 19 20 class nsIDocShell; 21 class nsIPrincipal; 22 class nsIURI; 23 class nsPIDOMWindowInner; 24 25 namespace mozilla::dom { 26 27 // Serializes principals to strings for location.ancestorOrigin purposes. 28 nsTArray<nsString> ProduceAncestorOriginsList( 29 const nsTArray<nsCOMPtr<nsIPrincipal>>& aPrincipals); 30 31 //***************************************************************************** 32 // Location: Script "location" object 33 //***************************************************************************** 34 35 class Location final : public nsISupports, 36 public LocationBase, 37 public nsWrapperCache, 38 public LinkedListElement<Location> { 39 public: 40 typedef BrowsingContext::LocationProxy RemoteProxy; 41 42 explicit Location(nsPIDOMWindowInner* aWindow); 43 44 NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL 45 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Location) 46 47 // WebIDL API: 48 void Assign(const nsACString& aUrl, nsIPrincipal& aSubjectPrincipal, 49 ErrorResult& aError); 50 51 MOZ_CAN_RUN_SCRIPT 52 void Reload(JSContext* aCx, bool aForceget, nsIPrincipal& aSubjectPrincipal, 53 ErrorResult& aError); 54 55 void GetHref(nsACString& aHref, nsIPrincipal& aSubjectPrincipal, 56 ErrorResult& aError) { 57 if (!CallerSubsumes(&aSubjectPrincipal)) { 58 aError.Throw(NS_ERROR_DOM_SECURITY_ERR); 59 return; 60 } 61 62 aError = GetHref(aHref); 63 } 64 65 void GetOrigin(nsACString& aOrigin, nsIPrincipal& aSubjectPrincipal, 66 ErrorResult& aError); 67 68 void GetProtocol(nsACString& aProtocol, nsIPrincipal& aSubjectPrincipal, 69 ErrorResult& aError); 70 71 void SetProtocol(const nsACString& aProtocol, nsIPrincipal& aSubjectPrincipal, 72 ErrorResult& aError); 73 74 void GetHost(nsACString& aHost, nsIPrincipal& aSubjectPrincipal, 75 ErrorResult& aError); 76 77 void SetHost(const nsACString& aHost, nsIPrincipal& aSubjectPrincipal, 78 ErrorResult& aError); 79 80 void GetHostname(nsACString& aHostname, nsIPrincipal& aSubjectPrincipal, 81 ErrorResult& aError); 82 83 void SetHostname(const nsACString& aHostname, nsIPrincipal& aSubjectPrincipal, 84 ErrorResult& aError); 85 86 void GetPort(nsACString& aPort, nsIPrincipal& aSubjectPrincipal, 87 ErrorResult& aError); 88 89 void SetPort(const nsACString& aPort, nsIPrincipal& aSubjectPrincipal, 90 ErrorResult& aError); 91 92 void GetPathname(nsACString& aPathname, nsIPrincipal& aSubjectPrincipal, 93 ErrorResult& aError); 94 95 void SetPathname(const nsACString& aPathname, nsIPrincipal& aSubjectPrincipal, 96 ErrorResult& aError); 97 98 void GetSearch(nsACString& aSeach, nsIPrincipal& aSubjectPrincipal, 99 ErrorResult& aError); 100 101 void SetSearch(const nsACString& aSeach, nsIPrincipal& aSubjectPrincipal, 102 ErrorResult& aError); 103 104 void GetHash(nsACString& aHash, nsIPrincipal& aSubjectPrincipal, 105 ErrorResult& aError); 106 107 void SetHash(const nsACString& aHash, nsIPrincipal& aSubjectPrincipal, 108 ErrorResult& aError); 109 110 RefPtr<DOMStringList> GetAncestorOrigins(nsIPrincipal& aSubjectPrincipal, 111 ErrorResult& aRv); 112 113 nsPIDOMWindowInner* GetParentObject() const { return mInnerWindow; } 114 115 virtual JSObject* WrapObject(JSContext* aCx, 116 JS::Handle<JSObject*> aGivenProto) override; 117 118 // Non WebIDL methods: 119 120 nsresult GetHref(nsACString& aHref); 121 122 void ClearCachedValues(); 123 124 protected: 125 ~Location(); 126 127 BrowsingContext* GetBrowsingContext() override; 128 nsIDocShell* GetDocShell() override; 129 130 // In the case of jar: uris, we sometimes want the place the jar was 131 // fetched from as the URI instead of the jar: uri itself. Pass in 132 // true for aGetInnermostURI when that's the case. 133 // Note, this method can return NS_OK with a null value for aURL. This happens 134 // if the docShell is null. 135 nsresult GetURI(nsIURI** aURL, bool aGetInnermostURI = false); 136 137 bool CallerSubsumes(nsIPrincipal* aSubjectPrincipal); 138 139 nsCString mCachedHash; 140 nsCOMPtr<nsPIDOMWindowInner> mInnerWindow; 141 }; 142 143 } // namespace mozilla::dom 144 145 #endif // mozilla_dom_Location_h