1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/Link.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 et : 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/** 1.11 + * This is the base class for all link classes. 1.12 + */ 1.13 + 1.14 +#ifndef mozilla_dom_Link_h__ 1.15 +#define mozilla_dom_Link_h__ 1.16 + 1.17 +#include "mozilla/IHistory.h" 1.18 +#include "mozilla/MemoryReporting.h" 1.19 +#include "mozilla/dom/URLSearchParams.h" 1.20 +#include "nsIContent.h" 1.21 + 1.22 +namespace mozilla { 1.23 + 1.24 +class EventStates; 1.25 + 1.26 +namespace dom { 1.27 + 1.28 +class Element; 1.29 + 1.30 +#define MOZILLA_DOM_LINK_IMPLEMENTATION_IID \ 1.31 +{ 0xb25edee6, 0xdd35, 0x4f8b, \ 1.32 + { 0xab, 0x90, 0x66, 0xd0, 0xbd, 0x3c, 0x22, 0xd5 } } 1.33 + 1.34 +class Link : public URLSearchParamsObserver 1.35 +{ 1.36 +public: 1.37 + NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOM_LINK_IMPLEMENTATION_IID) 1.38 + 1.39 + /** 1.40 + * aElement is the element pointer corresponding to this link. 1.41 + */ 1.42 + Link(Element* aElement); 1.43 + virtual void SetLinkState(nsLinkState aState); 1.44 + 1.45 + /** 1.46 + * @return NS_EVENT_STATE_VISITED if this link is visited, 1.47 + * NS_EVENT_STATE_UNVISTED if this link is not visited, or 0 if this 1.48 + * link is not actually a link. 1.49 + */ 1.50 + EventStates LinkState() const; 1.51 + 1.52 + /** 1.53 + * @return the URI this link is for, if available. 1.54 + */ 1.55 + nsIURI* GetURI() const; 1.56 + virtual nsIURI* GetURIExternal() const { 1.57 + return GetURI(); 1.58 + } 1.59 + 1.60 + /** 1.61 + * Helper methods for modifying and obtaining parts of the URI of the Link. 1.62 + */ 1.63 + void SetProtocol(const nsAString &aProtocol); 1.64 + void SetUsername(const nsAString &aUsername); 1.65 + void SetPassword(const nsAString &aPassword); 1.66 + void SetHost(const nsAString &aHost); 1.67 + void SetHostname(const nsAString &aHostname); 1.68 + void SetPathname(const nsAString &aPathname); 1.69 + void SetSearch(const nsAString &aSearch); 1.70 + void SetSearchParams(mozilla::dom::URLSearchParams& aSearchParams); 1.71 + void SetPort(const nsAString &aPort); 1.72 + void SetHash(const nsAString &aHash); 1.73 + void GetOrigin(nsAString &aOrigin); 1.74 + void GetProtocol(nsAString &_protocol); 1.75 + void GetUsername(nsAString &aUsername); 1.76 + void GetPassword(nsAString &aPassword); 1.77 + void GetHost(nsAString &_host); 1.78 + void GetHostname(nsAString &_hostname); 1.79 + void GetPathname(nsAString &_pathname); 1.80 + void GetSearch(nsAString &_search); 1.81 + URLSearchParams* SearchParams(); 1.82 + void GetPort(nsAString &_port); 1.83 + void GetHash(nsAString &_hash); 1.84 + 1.85 + /** 1.86 + * Invalidates any link caching, and resets the state to the default. 1.87 + * 1.88 + * @param aNotify 1.89 + * true if ResetLinkState should notify the owning document about style 1.90 + * changes or false if it should not. 1.91 + */ 1.92 + void ResetLinkState(bool aNotify, bool aHasHref); 1.93 + 1.94 + // This method nevers returns a null element. 1.95 + Element* GetElement() const { return mElement; } 1.96 + 1.97 + /** 1.98 + * DNS prefetch has been deferred until later, e.g. page load complete. 1.99 + */ 1.100 + virtual void OnDNSPrefetchDeferred() { /*do nothing*/ } 1.101 + 1.102 + /** 1.103 + * DNS prefetch has been submitted to Host Resolver. 1.104 + */ 1.105 + virtual void OnDNSPrefetchRequested() { /*do nothing*/ } 1.106 + 1.107 + /** 1.108 + * Checks if DNS Prefetching is ok 1.109 + * 1.110 + * @returns boolean 1.111 + * Defaults to true; should be overridden for specialised cases 1.112 + */ 1.113 + virtual bool HasDeferredDNSPrefetchRequest() { return true; } 1.114 + 1.115 + virtual size_t 1.116 + SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.117 + 1.118 + bool ElementHasHref() const; 1.119 + 1.120 + // URLSearchParamsObserver 1.121 + void URLSearchParamsUpdated() MOZ_OVERRIDE; 1.122 + 1.123 +protected: 1.124 + virtual ~Link(); 1.125 + 1.126 + /** 1.127 + * Return true if the link has associated URI. 1.128 + */ 1.129 + bool HasURI() const 1.130 + { 1.131 + if (HasCachedURI()) { 1.132 + return true; 1.133 + } 1.134 + 1.135 + return !!GetURI(); 1.136 + } 1.137 + 1.138 + nsIURI* GetCachedURI() const { return mCachedURI; } 1.139 + bool HasCachedURI() const { return !!mCachedURI; } 1.140 + 1.141 + void UpdateURLSearchParams(); 1.142 + 1.143 + // CC methods 1.144 + void Unlink(); 1.145 + void Traverse(nsCycleCollectionTraversalCallback &cb); 1.146 + 1.147 +private: 1.148 + /** 1.149 + * Unregisters from History so this node no longer gets notifications about 1.150 + * changes to visitedness. 1.151 + */ 1.152 + void UnregisterFromHistory(); 1.153 + 1.154 + already_AddRefed<nsIURI> GetURIToMutate(); 1.155 + void SetHrefAttribute(nsIURI *aURI); 1.156 + 1.157 + void CreateSearchParamsIfNeeded(); 1.158 + 1.159 + void SetSearchInternal(const nsAString& aSearch); 1.160 + 1.161 + mutable nsCOMPtr<nsIURI> mCachedURI; 1.162 + 1.163 + Element * const mElement; 1.164 + 1.165 + // Strong reference to History. The link has to unregister before History 1.166 + // can disappear. 1.167 + nsCOMPtr<IHistory> mHistory; 1.168 + 1.169 + uint16_t mLinkState; 1.170 + 1.171 + bool mNeedsRegistration; 1.172 + 1.173 + bool mRegistered; 1.174 + 1.175 +protected: 1.176 + nsRefPtr<URLSearchParams> mSearchParams; 1.177 +}; 1.178 + 1.179 +NS_DEFINE_STATIC_IID_ACCESSOR(Link, MOZILLA_DOM_LINK_IMPLEMENTATION_IID) 1.180 + 1.181 +} // namespace dom 1.182 +} // namespace mozilla 1.183 + 1.184 +#endif // mozilla_dom_Link_h__