michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "Link.h" michael@0: michael@0: #include "mozilla/EventStates.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "nsIURL.h" michael@0: #include "nsISizeOf.h" michael@0: michael@0: #include "nsEscape.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsString.h" michael@0: #include "mozAutoDocUpdate.h" michael@0: michael@0: #include "mozilla/Services.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: Link::Link(Element *aElement) michael@0: : mElement(aElement) michael@0: , mHistory(services::GetHistoryService()) michael@0: , mLinkState(eLinkState_NotLink) michael@0: , mNeedsRegistration(false) michael@0: , mRegistered(false) michael@0: { michael@0: NS_ABORT_IF_FALSE(mElement, "Must have an element"); michael@0: } michael@0: michael@0: Link::~Link() michael@0: { michael@0: UnregisterFromHistory(); michael@0: } michael@0: michael@0: bool michael@0: Link::ElementHasHref() const michael@0: { michael@0: return ((!mElement->IsSVG() && mElement->HasAttr(kNameSpaceID_None, nsGkAtoms::href)) michael@0: || (!mElement->IsHTML() && mElement->HasAttr(kNameSpaceID_XLink, nsGkAtoms::href))); michael@0: } michael@0: michael@0: void michael@0: Link::SetLinkState(nsLinkState aState) michael@0: { michael@0: NS_ASSERTION(mRegistered, michael@0: "Setting the link state of an unregistered Link!"); michael@0: NS_ASSERTION(mLinkState != aState, michael@0: "Setting state to the currently set state!"); michael@0: michael@0: // Set our current state as appropriate. michael@0: mLinkState = aState; michael@0: michael@0: // Per IHistory interface documentation, we are no longer registered. michael@0: mRegistered = false; michael@0: michael@0: NS_ABORT_IF_FALSE(LinkState() == NS_EVENT_STATE_VISITED || michael@0: LinkState() == NS_EVENT_STATE_UNVISITED, michael@0: "Unexpected state obtained from LinkState()!"); michael@0: michael@0: // Tell the element to update its visited state michael@0: mElement->UpdateState(true); michael@0: } michael@0: michael@0: EventStates michael@0: Link::LinkState() const michael@0: { michael@0: // We are a constant method, but we are just lazily doing things and have to michael@0: // track that state. Cast away that constness! michael@0: Link *self = const_cast(this); michael@0: michael@0: Element *element = self->mElement; michael@0: michael@0: // If we have not yet registered for notifications and need to, michael@0: // due to our href changing, register now! michael@0: if (!mRegistered && mNeedsRegistration && element->IsInDoc()) { michael@0: // Only try and register once. michael@0: self->mNeedsRegistration = false; michael@0: michael@0: nsCOMPtr hrefURI(GetURI()); michael@0: michael@0: // Assume that we are not visited until we are told otherwise. michael@0: self->mLinkState = eLinkState_Unvisited; michael@0: michael@0: // Make sure the href attribute has a valid link (bug 23209). michael@0: // If we have a good href, register with History if available. michael@0: if (mHistory && hrefURI) { michael@0: nsresult rv = mHistory->RegisterVisitedCallback(hrefURI, self); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: self->mRegistered = true; michael@0: michael@0: // And make sure we are in the document's link map. michael@0: element->GetCurrentDoc()->AddStyleRelevantLink(self); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Otherwise, return our known state. michael@0: if (mLinkState == eLinkState_Visited) { michael@0: return NS_EVENT_STATE_VISITED; michael@0: } michael@0: michael@0: if (mLinkState == eLinkState_Unvisited) { michael@0: return NS_EVENT_STATE_UNVISITED; michael@0: } michael@0: michael@0: return EventStates(); michael@0: } michael@0: michael@0: nsIURI* michael@0: Link::GetURI() const michael@0: { michael@0: // If we have this URI cached, use it. michael@0: if (mCachedURI) { michael@0: return mCachedURI; michael@0: } michael@0: michael@0: // Otherwise obtain it. michael@0: Link *self = const_cast(this); michael@0: Element *element = self->mElement; michael@0: mCachedURI = element->GetHrefURI(); michael@0: michael@0: return mCachedURI; michael@0: } michael@0: michael@0: void michael@0: Link::SetProtocol(const nsAString &aProtocol) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: if (!uri) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: nsAString::const_iterator start, end; michael@0: aProtocol.BeginReading(start); michael@0: aProtocol.EndReading(end); michael@0: nsAString::const_iterator iter(start); michael@0: (void)FindCharInReadable(':', iter, end); michael@0: (void)uri->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter))); michael@0: michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetPassword(const nsAString &aPassword) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: if (!uri) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: uri->SetPassword(NS_ConvertUTF16toUTF8(aPassword)); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetUsername(const nsAString &aUsername) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: if (!uri) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: uri->SetUsername(NS_ConvertUTF16toUTF8(aUsername)); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetHost(const nsAString &aHost) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: if (!uri) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: (void)uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost)); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetHostname(const nsAString &aHostname) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: if (!uri) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: (void)uri->SetHost(NS_ConvertUTF16toUTF8(aHostname)); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetPathname(const nsAString &aPathname) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: nsCOMPtr url(do_QueryInterface(uri)); michael@0: if (!url) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: (void)url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname)); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetSearch(const nsAString& aSearch) michael@0: { michael@0: SetSearchInternal(aSearch); michael@0: UpdateURLSearchParams(); michael@0: } michael@0: michael@0: void michael@0: Link::SetSearchInternal(const nsAString& aSearch) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: nsCOMPtr url(do_QueryInterface(uri)); michael@0: if (!url) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: (void)url->SetQuery(NS_ConvertUTF16toUTF8(aSearch)); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetPort(const nsAString &aPort) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: if (!uri) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: nsresult rv; michael@0: nsAutoString portStr(aPort); michael@0: michael@0: // nsIURI uses -1 as default value. michael@0: int32_t port = -1; michael@0: if (!aPort.IsEmpty()) { michael@0: port = portStr.ToInteger(&rv); michael@0: if (NS_FAILED(rv)) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: (void)uri->SetPort(port); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::SetHash(const nsAString &aHash) michael@0: { michael@0: nsCOMPtr uri(GetURIToMutate()); michael@0: if (!uri) { michael@0: // Ignore failures to be compatible with NS4. michael@0: return; michael@0: } michael@0: michael@0: (void)uri->SetRef(NS_ConvertUTF16toUTF8(aHash)); michael@0: SetHrefAttribute(uri); michael@0: } michael@0: michael@0: void michael@0: Link::GetOrigin(nsAString &aOrigin) michael@0: { michael@0: aOrigin.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: return; michael@0: } michael@0: michael@0: nsString origin; michael@0: nsContentUtils::GetUTFNonNullOrigin(uri, origin); michael@0: aOrigin.Assign(origin); michael@0: } michael@0: michael@0: void michael@0: Link::GetProtocol(nsAString &_protocol) michael@0: { michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: _protocol.AssignLiteral("http"); michael@0: } michael@0: else { michael@0: nsAutoCString scheme; michael@0: (void)uri->GetScheme(scheme); michael@0: CopyASCIItoUTF16(scheme, _protocol); michael@0: } michael@0: _protocol.Append(char16_t(':')); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: Link::GetUsername(nsAString& aUsername) michael@0: { michael@0: aUsername.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString username; michael@0: uri->GetUsername(username); michael@0: CopyASCIItoUTF16(username, aUsername); michael@0: } michael@0: michael@0: void michael@0: Link::GetPassword(nsAString &aPassword) michael@0: { michael@0: aPassword.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString password; michael@0: uri->GetPassword(password); michael@0: CopyASCIItoUTF16(password, aPassword); michael@0: } michael@0: michael@0: void michael@0: Link::GetHost(nsAString &_host) michael@0: { michael@0: _host.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: // Do not throw! Not having a valid URI should result in an empty string. michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString hostport; michael@0: nsresult rv = uri->GetHostPort(hostport); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: CopyUTF8toUTF16(hostport, _host); michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::GetHostname(nsAString &_hostname) michael@0: { michael@0: _hostname.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: // Do not throw! Not having a valid URI should result in an empty string. michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString host; michael@0: nsresult rv = uri->GetHost(host); michael@0: // Note that failure to get the host from the URI is not necessarily a bad michael@0: // thing. Some URIs do not have a host. michael@0: if (NS_SUCCEEDED(rv)) { michael@0: CopyUTF8toUTF16(host, _hostname); michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::GetPathname(nsAString &_pathname) michael@0: { michael@0: _pathname.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: nsCOMPtr url(do_QueryInterface(uri)); michael@0: if (!url) { michael@0: // Do not throw! Not having a valid URI or URL should result in an empty michael@0: // string. michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString file; michael@0: nsresult rv = url->GetFilePath(file); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: CopyUTF8toUTF16(file, _pathname); michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::GetSearch(nsAString &_search) michael@0: { michael@0: _search.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: nsCOMPtr url(do_QueryInterface(uri)); michael@0: if (!url) { michael@0: // Do not throw! Not having a valid URI or URL should result in an empty michael@0: // string. michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString search; michael@0: nsresult rv = url->GetQuery(search); michael@0: if (NS_SUCCEEDED(rv) && !search.IsEmpty()) { michael@0: CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, _search); michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::GetPort(nsAString &_port) michael@0: { michael@0: _port.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: // Do not throw! Not having a valid URI should result in an empty string. michael@0: return; michael@0: } michael@0: michael@0: int32_t port; michael@0: nsresult rv = uri->GetPort(&port); michael@0: // Note that failure to get the port from the URI is not necessarily a bad michael@0: // thing. Some URIs do not have a port. michael@0: if (NS_SUCCEEDED(rv) && port != -1) { michael@0: nsAutoString portStr; michael@0: portStr.AppendInt(port, 10); michael@0: _port.Assign(portStr); michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::GetHash(nsAString &_hash) michael@0: { michael@0: _hash.Truncate(); michael@0: michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: // Do not throw! Not having a valid URI should result in an empty michael@0: // string. michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString ref; michael@0: nsresult rv = uri->GetRef(ref); michael@0: if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) { michael@0: NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes! michael@0: _hash.Assign(char16_t('#')); michael@0: AppendUTF8toUTF16(ref, _hash); michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::ResetLinkState(bool aNotify, bool aHasHref) michael@0: { michael@0: nsLinkState defaultState; michael@0: michael@0: // The default state for links with an href is unvisited. michael@0: if (aHasHref) { michael@0: defaultState = eLinkState_Unvisited; michael@0: } else { michael@0: defaultState = eLinkState_NotLink; michael@0: } michael@0: michael@0: // If !mNeedsRegstration, then either we've never registered, or we're michael@0: // currently registered; in either case, we should remove ourself michael@0: // from the doc and the history. michael@0: if (!mNeedsRegistration && mLinkState != eLinkState_NotLink) { michael@0: nsIDocument *doc = mElement->GetCurrentDoc(); michael@0: if (doc && (mRegistered || mLinkState == eLinkState_Visited)) { michael@0: // Tell the document to forget about this link if we've registered michael@0: // with it before. michael@0: doc->ForgetLink(this); michael@0: } michael@0: michael@0: UnregisterFromHistory(); michael@0: } michael@0: michael@0: // If we have an href, we should register with the history. michael@0: mNeedsRegistration = aHasHref; michael@0: michael@0: // If we've cached the URI, reset always invalidates it. michael@0: mCachedURI = nullptr; michael@0: UpdateURLSearchParams(); michael@0: michael@0: // Update our state back to the default. michael@0: mLinkState = defaultState; michael@0: michael@0: // We have to be very careful here: if aNotify is false we do NOT michael@0: // want to call UpdateState, because that will call into LinkState() michael@0: // and try to start off loads, etc. But ResetLinkState is called michael@0: // with aNotify false when things are in inconsistent states, so michael@0: // we'll get confused in that situation. Instead, just silently michael@0: // update the link state on mElement. Since we might have set the michael@0: // link state to unvisited, make sure to update with that state if michael@0: // required. michael@0: if (aNotify) { michael@0: mElement->UpdateState(aNotify); michael@0: } else { michael@0: if (mLinkState == eLinkState_Unvisited) { michael@0: mElement->UpdateLinkState(NS_EVENT_STATE_UNVISITED); michael@0: } else { michael@0: mElement->UpdateLinkState(EventStates()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::UnregisterFromHistory() michael@0: { michael@0: // If we are not registered, we have nothing to do. michael@0: if (!mRegistered) { michael@0: return; michael@0: } michael@0: michael@0: NS_ASSERTION(mCachedURI, "mRegistered is true, but we have no cached URI?!"); michael@0: michael@0: // And tell History to stop tracking us. michael@0: if (mHistory) { michael@0: nsresult rv = mHistory->UnregisterVisitedCallback(mCachedURI, this); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "This should only fail if we misuse the API!"); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: mRegistered = false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: already_AddRefed michael@0: Link::GetURIToMutate() michael@0: { michael@0: nsCOMPtr uri(GetURI()); michael@0: if (!uri) { michael@0: return nullptr; michael@0: } michael@0: nsCOMPtr clone; michael@0: (void)uri->Clone(getter_AddRefs(clone)); michael@0: return clone.forget(); michael@0: } michael@0: michael@0: void michael@0: Link::SetHrefAttribute(nsIURI *aURI) michael@0: { michael@0: NS_ASSERTION(aURI, "Null URI is illegal!"); michael@0: michael@0: nsAutoCString href; michael@0: (void)aURI->GetSpec(href); michael@0: (void)mElement->SetAttr(kNameSpaceID_None, nsGkAtoms::href, michael@0: NS_ConvertUTF8toUTF16(href), true); michael@0: } michael@0: michael@0: size_t michael@0: Link::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t n = 0; michael@0: michael@0: if (mCachedURI) { michael@0: nsCOMPtr iface = do_QueryInterface(mCachedURI); michael@0: if (iface) { michael@0: n += iface->SizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: } michael@0: michael@0: // The following members don't need to be measured: michael@0: // - mElement, because it is a pointer-to-self used to avoid QIs michael@0: // - mHistory, because it is non-owning michael@0: michael@0: return n; michael@0: } michael@0: michael@0: URLSearchParams* michael@0: Link::SearchParams() michael@0: { michael@0: CreateSearchParamsIfNeeded(); michael@0: return mSearchParams; michael@0: } michael@0: michael@0: void michael@0: Link::SetSearchParams(URLSearchParams& aSearchParams) michael@0: { michael@0: if (mSearchParams) { michael@0: mSearchParams->RemoveObserver(this); michael@0: } michael@0: michael@0: mSearchParams = &aSearchParams; michael@0: mSearchParams->AddObserver(this); michael@0: michael@0: nsAutoString search; michael@0: mSearchParams->Serialize(search); michael@0: SetSearchInternal(search); michael@0: } michael@0: michael@0: void michael@0: Link::URLSearchParamsUpdated() michael@0: { michael@0: MOZ_ASSERT(mSearchParams); michael@0: michael@0: nsString search; michael@0: mSearchParams->Serialize(search); michael@0: SetSearchInternal(search); michael@0: } michael@0: michael@0: void michael@0: Link::UpdateURLSearchParams() michael@0: { michael@0: if (!mSearchParams) { michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString search; michael@0: nsCOMPtr uri(GetURI()); michael@0: nsCOMPtr url(do_QueryInterface(uri)); michael@0: if (url) { michael@0: nsresult rv = url->GetQuery(search); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Failed to get the query from a nsIURL."); michael@0: } michael@0: } michael@0: michael@0: mSearchParams->ParseInput(search, this); michael@0: } michael@0: michael@0: void michael@0: Link::CreateSearchParamsIfNeeded() michael@0: { michael@0: if (!mSearchParams) { michael@0: mSearchParams = new URLSearchParams(); michael@0: mSearchParams->AddObserver(this); michael@0: UpdateURLSearchParams(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::Unlink() michael@0: { michael@0: if (mSearchParams) { michael@0: mSearchParams->RemoveObserver(this); michael@0: mSearchParams = nullptr; michael@0: } michael@0: } michael@0: michael@0: void michael@0: Link::Traverse(nsCycleCollectionTraversalCallback &cb) michael@0: { michael@0: Link* tmp = this; michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSearchParams); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla