|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=2 et : |
|
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 /** |
|
8 * This is the base class for all link classes. |
|
9 */ |
|
10 |
|
11 #ifndef mozilla_dom_Link_h__ |
|
12 #define mozilla_dom_Link_h__ |
|
13 |
|
14 #include "mozilla/IHistory.h" |
|
15 #include "mozilla/MemoryReporting.h" |
|
16 #include "mozilla/dom/URLSearchParams.h" |
|
17 #include "nsIContent.h" |
|
18 |
|
19 namespace mozilla { |
|
20 |
|
21 class EventStates; |
|
22 |
|
23 namespace dom { |
|
24 |
|
25 class Element; |
|
26 |
|
27 #define MOZILLA_DOM_LINK_IMPLEMENTATION_IID \ |
|
28 { 0xb25edee6, 0xdd35, 0x4f8b, \ |
|
29 { 0xab, 0x90, 0x66, 0xd0, 0xbd, 0x3c, 0x22, 0xd5 } } |
|
30 |
|
31 class Link : public URLSearchParamsObserver |
|
32 { |
|
33 public: |
|
34 NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOM_LINK_IMPLEMENTATION_IID) |
|
35 |
|
36 /** |
|
37 * aElement is the element pointer corresponding to this link. |
|
38 */ |
|
39 Link(Element* aElement); |
|
40 virtual void SetLinkState(nsLinkState aState); |
|
41 |
|
42 /** |
|
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. |
|
46 */ |
|
47 EventStates LinkState() const; |
|
48 |
|
49 /** |
|
50 * @return the URI this link is for, if available. |
|
51 */ |
|
52 nsIURI* GetURI() const; |
|
53 virtual nsIURI* GetURIExternal() const { |
|
54 return GetURI(); |
|
55 } |
|
56 |
|
57 /** |
|
58 * Helper methods for modifying and obtaining parts of the URI of the Link. |
|
59 */ |
|
60 void SetProtocol(const nsAString &aProtocol); |
|
61 void SetUsername(const nsAString &aUsername); |
|
62 void SetPassword(const nsAString &aPassword); |
|
63 void SetHost(const nsAString &aHost); |
|
64 void SetHostname(const nsAString &aHostname); |
|
65 void SetPathname(const nsAString &aPathname); |
|
66 void SetSearch(const nsAString &aSearch); |
|
67 void SetSearchParams(mozilla::dom::URLSearchParams& aSearchParams); |
|
68 void SetPort(const nsAString &aPort); |
|
69 void SetHash(const nsAString &aHash); |
|
70 void GetOrigin(nsAString &aOrigin); |
|
71 void GetProtocol(nsAString &_protocol); |
|
72 void GetUsername(nsAString &aUsername); |
|
73 void GetPassword(nsAString &aPassword); |
|
74 void GetHost(nsAString &_host); |
|
75 void GetHostname(nsAString &_hostname); |
|
76 void GetPathname(nsAString &_pathname); |
|
77 void GetSearch(nsAString &_search); |
|
78 URLSearchParams* SearchParams(); |
|
79 void GetPort(nsAString &_port); |
|
80 void GetHash(nsAString &_hash); |
|
81 |
|
82 /** |
|
83 * Invalidates any link caching, and resets the state to the default. |
|
84 * |
|
85 * @param aNotify |
|
86 * true if ResetLinkState should notify the owning document about style |
|
87 * changes or false if it should not. |
|
88 */ |
|
89 void ResetLinkState(bool aNotify, bool aHasHref); |
|
90 |
|
91 // This method nevers returns a null element. |
|
92 Element* GetElement() const { return mElement; } |
|
93 |
|
94 /** |
|
95 * DNS prefetch has been deferred until later, e.g. page load complete. |
|
96 */ |
|
97 virtual void OnDNSPrefetchDeferred() { /*do nothing*/ } |
|
98 |
|
99 /** |
|
100 * DNS prefetch has been submitted to Host Resolver. |
|
101 */ |
|
102 virtual void OnDNSPrefetchRequested() { /*do nothing*/ } |
|
103 |
|
104 /** |
|
105 * Checks if DNS Prefetching is ok |
|
106 * |
|
107 * @returns boolean |
|
108 * Defaults to true; should be overridden for specialised cases |
|
109 */ |
|
110 virtual bool HasDeferredDNSPrefetchRequest() { return true; } |
|
111 |
|
112 virtual size_t |
|
113 SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
|
114 |
|
115 bool ElementHasHref() const; |
|
116 |
|
117 // URLSearchParamsObserver |
|
118 void URLSearchParamsUpdated() MOZ_OVERRIDE; |
|
119 |
|
120 protected: |
|
121 virtual ~Link(); |
|
122 |
|
123 /** |
|
124 * Return true if the link has associated URI. |
|
125 */ |
|
126 bool HasURI() const |
|
127 { |
|
128 if (HasCachedURI()) { |
|
129 return true; |
|
130 } |
|
131 |
|
132 return !!GetURI(); |
|
133 } |
|
134 |
|
135 nsIURI* GetCachedURI() const { return mCachedURI; } |
|
136 bool HasCachedURI() const { return !!mCachedURI; } |
|
137 |
|
138 void UpdateURLSearchParams(); |
|
139 |
|
140 // CC methods |
|
141 void Unlink(); |
|
142 void Traverse(nsCycleCollectionTraversalCallback &cb); |
|
143 |
|
144 private: |
|
145 /** |
|
146 * Unregisters from History so this node no longer gets notifications about |
|
147 * changes to visitedness. |
|
148 */ |
|
149 void UnregisterFromHistory(); |
|
150 |
|
151 already_AddRefed<nsIURI> GetURIToMutate(); |
|
152 void SetHrefAttribute(nsIURI *aURI); |
|
153 |
|
154 void CreateSearchParamsIfNeeded(); |
|
155 |
|
156 void SetSearchInternal(const nsAString& aSearch); |
|
157 |
|
158 mutable nsCOMPtr<nsIURI> mCachedURI; |
|
159 |
|
160 Element * const mElement; |
|
161 |
|
162 // Strong reference to History. The link has to unregister before History |
|
163 // can disappear. |
|
164 nsCOMPtr<IHistory> mHistory; |
|
165 |
|
166 uint16_t mLinkState; |
|
167 |
|
168 bool mNeedsRegistration; |
|
169 |
|
170 bool mRegistered; |
|
171 |
|
172 protected: |
|
173 nsRefPtr<URLSearchParams> mSearchParams; |
|
174 }; |
|
175 |
|
176 NS_DEFINE_STATIC_IID_ACCESSOR(Link, MOZILLA_DOM_LINK_IMPLEMENTATION_IID) |
|
177 |
|
178 } // namespace dom |
|
179 } // namespace mozilla |
|
180 |
|
181 #endif // mozilla_dom_Link_h__ |