1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
8 * This is the base class for all link classes.
11 #ifndef mozilla_dom_Link_h__
12 #define mozilla_dom_Link_h__
14 #include "mozilla/IHistory.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "mozilla/dom/URLSearchParams.h"
17 #include "nsIContent.h" // for nsLinkState
27 #define MOZILLA_DOM_LINK_IMPLEMENTATION_IID \
28 { 0xb25edee6, 0xdd35, 0x4f8b, \
29 { 0xab, 0x90, 0x66, 0xd0, 0xbd, 0x3c, 0x22, 0xd5 } }
31 class Link
: public URLSearchParamsObserver
34 NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOM_LINK_IMPLEMENTATION_IID
)
37 * aElement is the element pointer corresponding to this link.
39 explicit Link(Element
* aElement
);
40 virtual void SetLinkState(nsLinkState aState
);
43 * @return NS_EVENT_STATE_VISITED if this link is visited,
44 * NS_EVENT_STATE_UNVISTED if this link is not visited, or 0 if this
45 * link is not actually a link.
47 EventStates
LinkState() const;
50 * @return the URI this link is for, if available.
52 nsIURI
* GetURI() const;
53 virtual nsIURI
* GetURIExternal() const {
58 * Helper methods for modifying and obtaining parts of the URI of the Link.
60 void SetProtocol(const nsAString
&aProtocol
, ErrorResult
& aError
);
61 void SetUsername(const nsAString
&aUsername
, ErrorResult
& aError
);
62 void SetPassword(const nsAString
&aPassword
, ErrorResult
& aError
);
63 void SetHost(const nsAString
&aHost
, ErrorResult
& aError
);
64 void SetHostname(const nsAString
&aHostname
, ErrorResult
& aError
);
65 void SetPathname(const nsAString
&aPathname
, ErrorResult
& aError
);
66 void SetSearch(const nsAString
&aSearch
, ErrorResult
& aError
);
67 void SetSearchParams(mozilla::dom::URLSearchParams
& aSearchParams
);
68 void SetPort(const nsAString
&aPort
, ErrorResult
& aError
);
69 void SetHash(const nsAString
&aHash
, ErrorResult
& aError
);
70 void GetOrigin(nsAString
&aOrigin
, ErrorResult
& aError
);
71 void GetProtocol(nsAString
&_protocol
, ErrorResult
& aError
);
72 void GetUsername(nsAString
&aUsername
, ErrorResult
& aError
);
73 void GetPassword(nsAString
&aPassword
, ErrorResult
& aError
);
74 void GetHost(nsAString
&_host
, ErrorResult
& aError
);
75 void GetHostname(nsAString
&_hostname
, ErrorResult
& aError
);
76 void GetPathname(nsAString
&_pathname
, ErrorResult
& aError
);
77 void GetSearch(nsAString
&_search
, ErrorResult
& aError
);
78 URLSearchParams
* SearchParams();
79 void GetPort(nsAString
&_port
, ErrorResult
& aError
);
80 void GetHash(nsAString
&_hash
, ErrorResult
& aError
);
83 * Invalidates any link caching, and resets the state to the default.
86 * true if ResetLinkState should notify the owning document about style
87 * changes or false if it should not.
89 void ResetLinkState(bool aNotify
, bool aHasHref
);
91 // This method nevers returns a null element.
92 Element
* GetElement() const { return mElement
; }
95 * DNS prefetch has been deferred until later, e.g. page load complete.
97 virtual void OnDNSPrefetchDeferred() { /*do nothing*/ }
100 * DNS prefetch has been submitted to Host Resolver.
102 virtual void OnDNSPrefetchRequested() { /*do nothing*/ }
105 * Checks if DNS Prefetching is ok
108 * Defaults to true; should be overridden for specialised cases
110 virtual bool HasDeferredDNSPrefetchRequest() { return true; }
113 SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
115 bool ElementHasHref() const;
117 // URLSearchParamsObserver
118 void URLSearchParamsUpdated(URLSearchParams
* aSearchParams
) MOZ_OVERRIDE
;
124 * Return true if the link has associated URI.
128 if (HasCachedURI()) {
135 nsIURI
* GetCachedURI() const { return mCachedURI
; }
136 bool HasCachedURI() const { return !!mCachedURI
; }
138 void UpdateURLSearchParams();
142 void Traverse(nsCycleCollectionTraversalCallback
&cb
);
146 * Unregisters from History so this node no longer gets notifications about
147 * changes to visitedness.
149 void UnregisterFromHistory();
151 already_AddRefed
<nsIURI
> GetURIToMutate();
152 void SetHrefAttribute(nsIURI
*aURI
);
154 void CreateSearchParamsIfNeeded();
156 void SetSearchInternal(const nsAString
& aSearch
);
158 mutable nsCOMPtr
<nsIURI
> mCachedURI
;
160 Element
* const mElement
;
162 // Strong reference to History. The link has to unregister before History
164 nsCOMPtr
<IHistory
> mHistory
;
168 bool mNeedsRegistration
;
173 nsRefPtr
<URLSearchParams
> mSearchParams
;
176 NS_DEFINE_STATIC_IID_ACCESSOR(Link
, MOZILLA_DOM_LINK_IMPLEMENTATION_IID
)
179 } // namespace mozilla
181 #endif // mozilla_dom_Link_h__