2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-10-14 02:13:41 +04:00
|
|
|
|
|
|
|
#include "Link.h"
|
|
|
|
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-11-15 02:10:07 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-09-04 17:07:38 +03:00
|
|
|
#if defined(MOZ_PLACES)
|
2018-09-01 01:36:35 +03:00
|
|
|
# include "mozilla/places/History.h"
|
2018-09-04 17:07:38 +03:00
|
|
|
#else
|
|
|
|
# include "mozilla/IHistory.h"
|
2017-07-21 06:39:14 +03:00
|
|
|
#endif
|
2009-11-09 21:00:54 +03:00
|
|
|
#include "nsIURL.h"
|
2018-02-26 22:43:45 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2012-02-20 07:51:48 +04:00
|
|
|
#include "nsISizeOf.h"
|
2016-02-26 04:41:00 +03:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIPrefetchService.h"
|
2016-06-08 15:46:50 +03:00
|
|
|
#include "nsStyleLinkElement.h"
|
2009-11-09 21:00:54 +03:00
|
|
|
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2015-12-01 20:22:20 +03:00
|
|
|
#include "nsHTMLDNSPrefetch.h"
|
2009-11-09 21:00:54 +03:00
|
|
|
#include "nsString.h"
|
2009-12-16 03:04:07 +03:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2009-11-09 21:00:53 +03:00
|
|
|
|
2010-08-05 21:07:46 +04:00
|
|
|
#include "mozilla/Services.h"
|
2017-07-10 16:05:56 +03:00
|
|
|
#include "nsAttrValueInlines.h"
|
2018-04-20 12:47:00 +03:00
|
|
|
#include "HTMLLinkElement.h"
|
2009-11-23 21:48:52 +03:00
|
|
|
|
2009-10-14 02:13:41 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-09-04 17:07:38 +03:00
|
|
|
#if defined(MOZ_PLACES)
|
2017-07-21 06:39:14 +03:00
|
|
|
using places::History;
|
|
|
|
#endif
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Link::Link(Element* aElement)
|
2012-04-21 01:48:54 +04:00
|
|
|
: mElement(aElement),
|
2012-12-07 18:35:14 +04:00
|
|
|
mLinkState(eLinkState_NotLink),
|
|
|
|
mNeedsRegistration(false),
|
2012-04-21 01:48:54 +04:00
|
|
|
mRegistered(false),
|
2017-04-15 18:55:05 +03:00
|
|
|
mHasPendingLinkUpdate(false),
|
2017-04-19 13:06:36 +03:00
|
|
|
mInDNSPrefetch(false),
|
2017-07-21 06:39:14 +03:00
|
|
|
mHistory(true) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mElement, "Must have an element");
|
2009-10-14 02:13:41 +04:00
|
|
|
}
|
|
|
|
|
2016-11-11 22:22:01 +03:00
|
|
|
Link::Link()
|
|
|
|
: mElement(nullptr),
|
|
|
|
mLinkState(eLinkState_NotLink),
|
|
|
|
mNeedsRegistration(false),
|
|
|
|
mRegistered(false),
|
2017-04-15 18:55:05 +03:00
|
|
|
mHasPendingLinkUpdate(false),
|
2017-04-19 13:06:36 +03:00
|
|
|
mInDNSPrefetch(false),
|
2017-07-21 06:39:14 +03:00
|
|
|
mHistory(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2009-12-16 03:01:53 +03:00
|
|
|
Link::~Link() {
|
2017-04-19 13:06:36 +03:00
|
|
|
// !mElement is for mock_Link.
|
|
|
|
MOZ_ASSERT(!mElement || !mElement->IsInComposedDoc());
|
|
|
|
if (IsInDNSPrefetch()) {
|
|
|
|
nsHTMLDNSPrefetch::LinkDestroyed(this);
|
|
|
|
}
|
2009-12-16 03:01:53 +03:00
|
|
|
UnregisterFromHistory();
|
|
|
|
}
|
|
|
|
|
2012-12-07 18:35:14 +04:00
|
|
|
bool Link::ElementHasHref() const {
|
2016-06-28 12:22:30 +03:00
|
|
|
return mElement->HasAttr(kNameSpaceID_None, nsGkAtoms::href) ||
|
|
|
|
(!mElement->IsHTMLElement() &&
|
|
|
|
mElement->HasAttr(kNameSpaceID_XLink, nsGkAtoms::href));
|
2012-12-07 18:35:14 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 20:22:20 +03:00
|
|
|
void Link::TryDNSPrefetch() {
|
|
|
|
MOZ_ASSERT(mElement->IsInComposedDoc());
|
|
|
|
if (ElementHasHref() && nsHTMLDNSPrefetch::IsAllowed(mElement->OwnerDoc())) {
|
|
|
|
nsHTMLDNSPrefetch::PrefetchLow(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Link::CancelDNSPrefetch(nsWrapperCache::FlagsType aDeferredFlag,
|
|
|
|
nsWrapperCache::FlagsType aRequestedFlag) {
|
|
|
|
// If prefetch was deferred, clear flag and move on
|
|
|
|
if (mElement->HasFlag(aDeferredFlag)) {
|
|
|
|
mElement->UnsetFlags(aDeferredFlag);
|
|
|
|
// Else if prefetch was requested, clear flag and send cancellation
|
|
|
|
} else if (mElement->HasFlag(aRequestedFlag)) {
|
|
|
|
mElement->UnsetFlags(aRequestedFlag);
|
|
|
|
// Possible that hostname could have changed since binding, but since this
|
|
|
|
// covers common cases, most DNS prefetch requests will be canceled
|
|
|
|
nsHTMLDNSPrefetch::CancelPrefetchLow(this, NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-14 02:13:41 +04:00
|
|
|
void Link::SetLinkState(nsLinkState aState) {
|
2019-08-15 17:10:07 +03:00
|
|
|
MOZ_ASSERT(mRegistered, "Setting the link state of an unregistered Link!");
|
|
|
|
MOZ_ASSERT(mLinkState != aState, "Setting state to the currently set state!");
|
2009-12-16 03:04:07 +03:00
|
|
|
|
|
|
|
// Set our current state as appropriate.
|
2009-10-14 02:13:41 +04:00
|
|
|
mLinkState = aState;
|
2009-11-23 21:48:52 +03:00
|
|
|
|
|
|
|
// Per IHistory interface documentation, we are no longer registered.
|
|
|
|
mRegistered = false;
|
2009-12-16 03:04:07 +03:00
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(LinkState() == NS_EVENT_STATE_VISITED ||
|
|
|
|
LinkState() == NS_EVENT_STATE_UNVISITED,
|
|
|
|
"Unexpected state obtained from LinkState()!");
|
2011-06-01 05:46:57 +04:00
|
|
|
|
|
|
|
// Tell the element to update its visited state
|
|
|
|
mElement->UpdateState(true);
|
2009-10-14 02:13:41 +04:00
|
|
|
}
|
|
|
|
|
2009-11-09 21:00:53 +03:00
|
|
|
EventStates Link::LinkState() const {
|
2009-11-23 21:48:52 +03:00
|
|
|
// We are a constant method, but we are just lazily doing things and have to
|
|
|
|
// track that state. Cast away that constness!
|
2019-08-15 17:10:07 +03:00
|
|
|
//
|
|
|
|
// XXX(emilio): that's evil.
|
2019-05-01 11:47:10 +03:00
|
|
|
Link* self = const_cast<Link*>(this);
|
2009-11-23 21:48:52 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Element* element = self->mElement;
|
2009-11-23 21:48:52 +03:00
|
|
|
|
2012-12-07 18:35:14 +04:00
|
|
|
// If we have not yet registered for notifications and need to,
|
|
|
|
// due to our href changing, register now!
|
2017-07-07 13:06:52 +03:00
|
|
|
if (!mRegistered && mNeedsRegistration && element->IsInComposedDoc() &&
|
|
|
|
!HasPendingLinkUpdate()) {
|
2012-12-07 18:35:14 +04:00
|
|
|
// Only try and register once.
|
|
|
|
self->mNeedsRegistration = false;
|
|
|
|
|
2009-11-23 21:48:52 +03:00
|
|
|
nsCOMPtr<nsIURI> hrefURI(GetURI());
|
|
|
|
|
2012-11-30 09:41:40 +04:00
|
|
|
// Assume that we are not visited until we are told otherwise.
|
|
|
|
self->mLinkState = eLinkState_Unvisited;
|
|
|
|
|
2012-12-07 18:35:14 +04:00
|
|
|
// Make sure the href attribute has a valid link (bug 23209).
|
|
|
|
// If we have a good href, register with History if available.
|
|
|
|
if (mHistory && hrefURI) {
|
2017-07-21 06:39:14 +03:00
|
|
|
#ifdef ANDROID
|
|
|
|
nsCOMPtr<IHistory> history = services::GetHistoryService();
|
2018-09-04 17:07:38 +03:00
|
|
|
#elif defined(MOZ_PLACES)
|
2019-05-01 11:47:10 +03:00
|
|
|
History* history = History::GetService();
|
2018-09-04 17:07:38 +03:00
|
|
|
#else
|
|
|
|
nsCOMPtr<IHistory> history;
|
2017-07-21 06:39:14 +03:00
|
|
|
#endif
|
|
|
|
if (history) {
|
|
|
|
nsresult rv = history->RegisterVisitedCallback(hrefURI, self);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
self->mRegistered = true;
|
|
|
|
|
|
|
|
// And make sure we are in the document's link map.
|
|
|
|
element->GetComposedDoc()->AddStyleRelevantLink(self);
|
|
|
|
}
|
2010-02-24 19:37:03 +03:00
|
|
|
}
|
2009-11-23 21:48:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return our known state.
|
2009-11-09 21:00:53 +03:00
|
|
|
if (mLinkState == eLinkState_Visited) {
|
|
|
|
return NS_EVENT_STATE_VISITED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLinkState == eLinkState_Unvisited) {
|
|
|
|
return NS_EVENT_STATE_UNVISITED;
|
|
|
|
}
|
|
|
|
|
2014-04-03 08:18:36 +04:00
|
|
|
return EventStates();
|
2009-11-09 21:00:53 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIURI* Link::GetURI() const {
|
2009-11-09 21:00:54 +03:00
|
|
|
// If we have this URI cached, use it.
|
2013-08-24 06:42:39 +04:00
|
|
|
if (mCachedURI) {
|
|
|
|
return mCachedURI;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise obtain it.
|
2019-05-01 11:47:10 +03:00
|
|
|
Link* self = const_cast<Link*>(this);
|
|
|
|
Element* element = self->mElement;
|
2013-08-24 06:42:39 +04:00
|
|
|
mCachedURI = element->GetHrefURI();
|
2009-11-09 21:00:54 +03:00
|
|
|
|
2013-08-24 06:42:39 +04:00
|
|
|
return mCachedURI;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetProtocol(const nsAString& aProtocol) {
|
2018-02-26 22:43:47 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2009-11-09 21:00:54 +03:00
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
aProtocol.BeginReading(start);
|
|
|
|
aProtocol.EndReading(end);
|
|
|
|
nsAString::const_iterator iter(start);
|
|
|
|
(void)FindCharInReadable(':', iter, end);
|
2018-02-26 22:43:47 +03:00
|
|
|
nsresult rv = NS_MutateURI(uri)
|
|
|
|
.SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)))
|
|
|
|
.Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetPassword(const nsAString& aPassword) {
|
2018-02-26 22:43:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2013-09-04 21:05:10 +04:00
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:46 +03:00
|
|
|
nsresult rv = NS_MutateURI(uri)
|
|
|
|
.SetPassword(NS_ConvertUTF16toUTF8(aPassword))
|
|
|
|
.Finalize(uri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
2013-09-04 21:05:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetUsername(const nsAString& aUsername) {
|
2018-02-26 22:43:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2013-09-04 21:05:10 +04:00
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:46 +03:00
|
|
|
nsresult rv = NS_MutateURI(uri)
|
|
|
|
.SetUsername(NS_ConvertUTF16toUTF8(aUsername))
|
|
|
|
.Finalize(uri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
2013-09-04 21:05:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetHost(const nsAString& aHost) {
|
2018-02-26 22:43:45 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2009-11-09 21:00:54 +03:00
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:45 +03:00
|
|
|
nsresult rv =
|
|
|
|
NS_MutateURI(uri).SetHostPort(NS_ConvertUTF16toUTF8(aHost)).Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetHostname(const nsAString& aHostname) {
|
2018-02-26 22:43:47 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2009-11-09 21:00:54 +03:00
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:47 +03:00
|
|
|
nsresult rv =
|
|
|
|
NS_MutateURI(uri).SetHost(NS_ConvertUTF16toUTF8(aHostname)).Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetPathname(const nsAString& aPathname) {
|
2018-02-26 22:43:45 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2009-11-09 21:00:54 +03:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:45 +03:00
|
|
|
nsresult rv = NS_MutateURI(uri)
|
|
|
|
.SetFilePath(NS_ConvertUTF16toUTF8(aPathname))
|
|
|
|
.Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetSearch(const nsAString& aSearch) {
|
2018-02-26 22:43:45 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2009-11-09 21:00:54 +03:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2017-08-02 14:43:30 +03:00
|
|
|
auto encoding = mElement->OwnerDoc()->GetDocumentCharacterSet();
|
2018-02-26 22:43:45 +03:00
|
|
|
nsresult rv =
|
|
|
|
NS_MutateURI(uri)
|
|
|
|
.SetQueryWithEncoding(NS_ConvertUTF16toUTF8(aSearch), encoding)
|
|
|
|
.Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetPort(const nsAString& aPort) {
|
2018-02-26 22:43:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2009-11-09 21:00:54 +03:00
|
|
|
if (!uri) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString portStr(aPort);
|
2013-12-06 01:05:51 +04:00
|
|
|
|
|
|
|
// nsIURI uses -1 as default value.
|
|
|
|
int32_t port = -1;
|
|
|
|
if (!aPort.IsEmpty()) {
|
|
|
|
port = portStr.ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:46 +03:00
|
|
|
rv = NS_MutateURI(uri).SetPort(port).Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetHash(const nsAString& aHash) {
|
2018-02-26 22:43:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2011-05-22 05:12:46 +04:00
|
|
|
if (!uri) {
|
2009-11-09 21:00:54 +03:00
|
|
|
// Ignore failures to be compatible with NS4.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:46 +03:00
|
|
|
nsresult rv =
|
|
|
|
NS_MutateURI(uri).SetRef(NS_ConvertUTF16toUTF8(aHash)).Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-09 21:00:54 +03:00
|
|
|
SetHrefAttribute(uri);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetOrigin(nsAString& aOrigin) {
|
2013-09-04 21:05:10 +04:00
|
|
|
aOrigin.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString origin;
|
2014-08-31 14:40:11 +04:00
|
|
|
nsContentUtils::GetUTFOrigin(uri, origin);
|
2013-09-04 21:07:21 +04:00
|
|
|
aOrigin.Assign(origin);
|
2013-09-04 21:05:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetProtocol(nsAString& _protocol) {
|
2009-11-09 21:00:54 +03:00
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
_protocol.AssignLiteral("http");
|
|
|
|
} else {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString scheme;
|
2009-11-09 21:00:54 +03:00
|
|
|
(void)uri->GetScheme(scheme);
|
|
|
|
CopyASCIItoUTF16(scheme, _protocol);
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
_protocol.Append(char16_t(':'));
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetUsername(nsAString& aUsername) {
|
2013-09-04 21:05:10 +04:00
|
|
|
aUsername.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString username;
|
|
|
|
uri->GetUsername(username);
|
|
|
|
CopyASCIItoUTF16(username, aUsername);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetPassword(nsAString& aPassword) {
|
2013-09-04 21:05:10 +04:00
|
|
|
aPassword.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString password;
|
|
|
|
uri->GetPassword(password);
|
|
|
|
CopyASCIItoUTF16(password, aPassword);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetHost(nsAString& _host) {
|
2009-11-09 21:00:54 +03:00
|
|
|
_host.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty string.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString hostport;
|
2009-11-09 21:00:54 +03:00
|
|
|
nsresult rv = uri->GetHostPort(hostport);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(hostport, _host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetHostname(nsAString& _hostname) {
|
2009-11-09 21:00:54 +03:00
|
|
|
_hostname.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty string.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2014-10-20 14:46:20 +04:00
|
|
|
nsContentUtils::GetHostOrIPv6WithBrackets(uri, _hostname);
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetPathname(nsAString& _pathname) {
|
2009-11-09 21:00:54 +03:00
|
|
|
_pathname.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Do not throw! Not having a valid URI or URL should result in an empty
|
|
|
|
// string.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString file;
|
2009-11-09 21:00:54 +03:00
|
|
|
nsresult rv = url->GetFilePath(file);
|
2013-01-04 21:02:13 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(file, _pathname);
|
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetSearch(nsAString& _search) {
|
2009-11-09 21:00:54 +03:00
|
|
|
_search.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (!url) {
|
|
|
|
// Do not throw! Not having a valid URI or URL should result in an empty
|
|
|
|
// string.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString search;
|
2009-11-09 21:00:54 +03:00
|
|
|
nsresult rv = url->GetQuery(search);
|
|
|
|
if (NS_SUCCEEDED(rv) && !search.IsEmpty()) {
|
2018-03-24 22:16:51 +03:00
|
|
|
_search.Assign(u'?');
|
|
|
|
AppendUTF8toUTF16(search, _search);
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetPort(nsAString& _port) {
|
2009-11-09 21:00:54 +03:00
|
|
|
_port.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty string.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port;
|
2009-11-09 21:00:54 +03:00
|
|
|
nsresult rv = uri->GetPort(&port);
|
|
|
|
// Note that failure to get the port from the URI is not necessarily a bad
|
|
|
|
// thing. Some URIs do not have a port.
|
|
|
|
if (NS_SUCCEEDED(rv) && port != -1) {
|
|
|
|
nsAutoString portStr;
|
|
|
|
portStr.AppendInt(port, 10);
|
|
|
|
_port.Assign(portStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::GetHash(nsAString& _hash) {
|
2009-11-09 21:00:54 +03:00
|
|
|
_hash.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri(GetURI());
|
2011-05-22 05:12:46 +04:00
|
|
|
if (!uri) {
|
|
|
|
// Do not throw! Not having a valid URI should result in an empty
|
2009-11-09 21:00:54 +03:00
|
|
|
// string.
|
2013-01-04 21:02:13 +04:00
|
|
|
return;
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString ref;
|
2011-05-22 05:12:46 +04:00
|
|
|
nsresult rv = uri->GetRef(ref);
|
2009-11-09 21:00:54 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
|
2014-01-04 19:02:17 +04:00
|
|
|
_hash.Assign(char16_t('#'));
|
2009-11-09 21:00:54 +03:00
|
|
|
AppendUTF8toUTF16(ref, _hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 18:35:14 +04:00
|
|
|
void Link::ResetLinkState(bool aNotify, bool aHasHref) {
|
|
|
|
nsLinkState defaultState;
|
|
|
|
|
|
|
|
// The default state for links with an href is unvisited.
|
|
|
|
if (aHasHref) {
|
|
|
|
defaultState = eLinkState_Unvisited;
|
|
|
|
} else {
|
|
|
|
defaultState = eLinkState_NotLink;
|
2010-02-24 19:37:03 +03:00
|
|
|
}
|
|
|
|
|
2012-12-07 18:35:14 +04:00
|
|
|
// If !mNeedsRegstration, then either we've never registered, or we're
|
|
|
|
// currently registered; in either case, we should remove ourself
|
|
|
|
// from the doc and the history.
|
|
|
|
if (!mNeedsRegistration && mLinkState != eLinkState_NotLink) {
|
2019-05-01 11:47:10 +03:00
|
|
|
Document* doc = mElement->GetComposedDoc();
|
2012-12-07 18:35:14 +04:00
|
|
|
if (doc && (mRegistered || mLinkState == eLinkState_Visited)) {
|
|
|
|
// Tell the document to forget about this link if we've registered
|
|
|
|
// with it before.
|
|
|
|
doc->ForgetLink(this);
|
|
|
|
}
|
2009-10-14 02:13:41 +04:00
|
|
|
}
|
2009-11-09 21:00:54 +03:00
|
|
|
|
2019-08-15 17:10:07 +03:00
|
|
|
// If we have an href, and we're not a <link>, we should register with the
|
|
|
|
// history.
|
|
|
|
//
|
|
|
|
// FIXME(emilio): Do we really want to allow all MathML elements to be
|
|
|
|
// :visited? That seems not great.
|
|
|
|
mNeedsRegistration = aHasHref && !mElement->IsHTMLElement(nsGkAtoms::link);
|
2012-12-07 18:35:14 +04:00
|
|
|
|
|
|
|
// If we've cached the URI, reset always invalidates it.
|
2017-07-21 06:39:14 +03:00
|
|
|
UnregisterFromHistory();
|
2012-12-07 18:35:14 +04:00
|
|
|
mCachedURI = nullptr;
|
2009-11-23 21:48:52 +03:00
|
|
|
|
2009-11-09 21:00:54 +03:00
|
|
|
// Update our state back to the default.
|
2009-10-14 02:13:41 +04:00
|
|
|
mLinkState = defaultState;
|
2009-11-09 21:00:54 +03:00
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
// We have to be very careful here: if aNotify is false we do NOT
|
|
|
|
// want to call UpdateState, because that will call into LinkState()
|
|
|
|
// and try to start off loads, etc. But ResetLinkState is called
|
|
|
|
// with aNotify false when things are in inconsistent states, so
|
|
|
|
// we'll get confused in that situation. Instead, just silently
|
2012-12-07 18:35:14 +04:00
|
|
|
// update the link state on mElement. Since we might have set the
|
|
|
|
// link state to unvisited, make sure to update with that state if
|
|
|
|
// required.
|
2011-06-01 05:46:57 +04:00
|
|
|
if (aNotify) {
|
|
|
|
mElement->UpdateState(aNotify);
|
|
|
|
} else {
|
2012-12-07 18:35:14 +04:00
|
|
|
if (mLinkState == eLinkState_Unvisited) {
|
|
|
|
mElement->UpdateLinkState(NS_EVENT_STATE_UNVISITED);
|
|
|
|
} else {
|
2014-04-03 08:18:36 +04:00
|
|
|
mElement->UpdateLinkState(EventStates());
|
2012-12-07 18:35:14 +04:00
|
|
|
}
|
2010-02-24 19:37:03 +03:00
|
|
|
}
|
2009-10-14 02:13:41 +04:00
|
|
|
}
|
|
|
|
|
2009-11-23 21:48:52 +03:00
|
|
|
void Link::UnregisterFromHistory() {
|
|
|
|
// If we are not registered, we have nothing to do.
|
|
|
|
if (!mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// And tell History to stop tracking us.
|
2017-07-21 06:39:14 +03:00
|
|
|
if (mHistory && mCachedURI) {
|
|
|
|
#ifdef ANDROID
|
|
|
|
nsCOMPtr<IHistory> history = services::GetHistoryService();
|
2018-09-04 17:07:38 +03:00
|
|
|
#elif defined(MOZ_PLACES)
|
2019-05-01 11:47:10 +03:00
|
|
|
History* history = History::GetService();
|
2018-09-04 17:07:38 +03:00
|
|
|
#else
|
|
|
|
nsCOMPtr<IHistory> history;
|
2017-07-21 06:39:14 +03:00
|
|
|
#endif
|
|
|
|
if (history) {
|
|
|
|
nsresult rv = history->UnregisterVisitedCallback(mCachedURI, this);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"This should only fail if we misuse the API!");
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mRegistered = false;
|
|
|
|
}
|
2012-11-09 13:55:54 +04:00
|
|
|
}
|
2009-11-23 21:48:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void Link::SetHrefAttribute(nsIURI* aURI) {
|
2009-11-09 21:00:54 +03:00
|
|
|
NS_ASSERTION(aURI, "Null URI is illegal!");
|
|
|
|
|
2014-06-09 20:20:19 +04:00
|
|
|
// if we change this code to not reserialize we need to do something smarter
|
|
|
|
// in SetProtocol because changing the protocol of an URI can change the
|
|
|
|
// "nature" of the nsIURL/nsIURI implementation.
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString href;
|
2009-11-09 21:00:54 +03:00
|
|
|
(void)aURI->GetSpec(href);
|
2011-06-01 05:46:57 +04:00
|
|
|
(void)mElement->SetAttr(kNameSpaceID_None, nsGkAtoms::href,
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ConvertUTF8toUTF16(href), true);
|
2009-11-09 21:00:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
size_t Link::SizeOfExcludingThis(mozilla::SizeOfState& aState) const {
|
2012-02-20 07:51:48 +04:00
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
if (mCachedURI) {
|
|
|
|
nsCOMPtr<nsISizeOf> iface = do_QueryInterface(mCachedURI);
|
|
|
|
if (iface) {
|
2017-07-28 13:10:04 +03:00
|
|
|
n += iface->SizeOfIncludingThis(aState.mMallocSizeOf);
|
2012-02-20 07:51:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following members don't need to be measured:
|
|
|
|
// - mElement, because it is a pointer-to-self used to avoid QIs
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2009-10-14 02:13:41 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|