2013-01-28 03:08:21 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "URL.h"
|
|
|
|
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIDOMFile.h"
|
2013-02-15 00:04:11 -08:00
|
|
|
#include "DOMMediaStream.h"
|
2013-06-20 20:15:15 -07:00
|
|
|
#include "mozilla/dom/MediaSource.h"
|
2013-09-04 10:07:21 -07:00
|
|
|
#include "mozilla/dom/URLBinding.h"
|
2013-01-28 03:08:21 -08:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2013-09-04 10:07:21 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsIURL.h"
|
2013-01-28 03:08:21 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-02-03 08:48:38 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(URL)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(URL)
|
|
|
|
if (tmp->mSearchParams) {
|
|
|
|
tmp->mSearchParams->RemoveObserver(tmp);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSearchParams)
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(URL)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSearchParams)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-12-12 11:30:27 -08:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(URL)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(URL)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(URL)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-10-09 23:56:01 -07:00
|
|
|
URL::URL(nsIURI* aURI)
|
|
|
|
: mURI(aURI)
|
2013-09-04 10:07:21 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
URL::WrapObject(JSContext* aCx)
|
2013-09-04 10:07:21 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return URLBinding::Wrap(aCx, this);
|
2013-09-04 10:07:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<URL>
|
|
|
|
URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl,
|
|
|
|
URL& aBase, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aUrl), nullptr, aBase.GetURI(),
|
|
|
|
getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-18 07:17:03 -07:00
|
|
|
nsAutoString label(aUrl);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &label);
|
2013-09-04 10:07:21 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 23:56:01 -07:00
|
|
|
nsRefPtr<URL> url = new URL(uri);
|
2013-09-04 10:07:21 -07:00
|
|
|
return url.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<URL>
|
|
|
|
URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl,
|
|
|
|
const nsAString& aBase, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> baseUri;
|
|
|
|
rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aBase), nullptr, nullptr,
|
|
|
|
getter_AddRefs(baseUri));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-18 07:17:03 -07:00
|
|
|
nsAutoString label(aBase);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &label);
|
2013-09-04 10:07:21 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aUrl), nullptr, baseUri,
|
|
|
|
getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-18 07:17:03 -07:00
|
|
|
nsAutoString label(aUrl);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &label);
|
2013-09-04 10:07:21 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 23:56:01 -07:00
|
|
|
nsRefPtr<URL> url = new URL(uri);
|
2013-09-04 10:07:21 -07:00
|
|
|
return url.forget();
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:08:21 -08:00
|
|
|
void
|
2013-08-22 22:17:08 -07:00
|
|
|
URL::CreateObjectURL(const GlobalObject& aGlobal,
|
|
|
|
nsIDOMBlob* aBlob,
|
2013-01-28 03:08:21 -08:00
|
|
|
const objectURLOptions& aOptions,
|
2012-12-03 08:07:49 -08:00
|
|
|
nsString& aResult,
|
2013-01-28 03:08:21 -08:00
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
2013-10-09 23:56:01 -07:00
|
|
|
CreateObjectURLInternal(aGlobal, aBlob,
|
2012-12-03 08:07:49 -08:00
|
|
|
NS_LITERAL_CSTRING(BLOBURI_SCHEME), aOptions, aResult,
|
|
|
|
aError);
|
2013-01-28 03:08:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-15 00:04:11 -08:00
|
|
|
URL::CreateObjectURL(const GlobalObject& aGlobal, DOMMediaStream& aStream,
|
2013-01-28 03:08:21 -08:00
|
|
|
const mozilla::dom::objectURLOptions& aOptions,
|
2012-12-03 08:07:49 -08:00
|
|
|
nsString& aResult,
|
2013-01-28 03:08:21 -08:00
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
2013-10-09 23:56:01 -07:00
|
|
|
CreateObjectURLInternal(aGlobal, &aStream,
|
2012-12-03 08:07:49 -08:00
|
|
|
NS_LITERAL_CSTRING(MEDIASTREAMURI_SCHEME), aOptions,
|
|
|
|
aResult, aError);
|
2013-01-28 03:08:21 -08:00
|
|
|
}
|
|
|
|
|
2013-06-20 20:15:15 -07:00
|
|
|
void
|
|
|
|
URL::CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource,
|
|
|
|
const objectURLOptions& aOptions,
|
|
|
|
nsString& aResult,
|
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
2013-10-09 23:56:01 -07:00
|
|
|
CreateObjectURLInternal(aGlobal, &aSource,
|
2013-06-20 20:15:15 -07:00
|
|
|
NS_LITERAL_CSTRING(MEDIASOURCEURI_SCHEME), aOptions,
|
|
|
|
aResult, aError);
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:08:21 -08:00
|
|
|
void
|
2013-10-09 23:56:01 -07:00
|
|
|
URL::CreateObjectURLInternal(const GlobalObject& aGlobal, nsISupports* aObject,
|
2013-01-28 03:08:21 -08:00
|
|
|
const nsACString& aScheme,
|
2013-09-04 10:07:21 -07:00
|
|
|
const objectURLOptions& aOptions,
|
|
|
|
nsString& aResult, ErrorResult& aError)
|
2013-01-28 03:08:21 -08:00
|
|
|
{
|
2014-05-13 02:58:00 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get());
|
2013-01-28 03:08:21 -08:00
|
|
|
|
|
|
|
nsCString url;
|
|
|
|
nsresult rv = nsHostObjectProtocolHandler::AddDataEntry(aScheme, aObject,
|
2013-10-09 23:56:01 -07:00
|
|
|
principal, url);
|
2013-01-28 03:08:21 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aError.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-09 23:56:01 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsGlobalWindow* window = static_cast<nsGlobalWindow*>(w.get());
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
NS_PRECONDITION(window->IsInnerWindow(), "Should be inner window");
|
|
|
|
|
|
|
|
if (!window->GetExtantDoc()) {
|
|
|
|
aError.Throw(NS_ERROR_INVALID_POINTER);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
doc->RegisterHostObjectUri(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:08:21 -08:00
|
|
|
CopyASCIItoUTF16(url, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-03 08:07:49 -08:00
|
|
|
URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL)
|
2013-01-28 03:08:21 -08:00
|
|
|
{
|
2014-05-13 02:58:00 -07:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal.Get());
|
2013-01-28 03:08:21 -08:00
|
|
|
|
|
|
|
NS_LossyConvertUTF16toASCII asciiurl(aURL);
|
|
|
|
|
2013-10-09 23:56:01 -07:00
|
|
|
nsIPrincipal* urlPrincipal =
|
2013-01-28 03:08:21 -08:00
|
|
|
nsHostObjectProtocolHandler::GetDataEntryPrincipal(asciiurl);
|
2013-10-09 23:56:01 -07:00
|
|
|
|
|
|
|
if (urlPrincipal && principal->Subsumes(urlPrincipal)) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsGlobalWindow* window = static_cast<nsGlobalWindow*>(w.get());
|
|
|
|
|
|
|
|
if (window && window->GetExtantDoc()) {
|
2013-01-28 03:08:21 -08:00
|
|
|
window->GetExtantDoc()->UnregisterHostObjectUri(asciiurl);
|
|
|
|
}
|
|
|
|
nsHostObjectProtocolHandler::RemoveDataEntry(asciiurl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:07:21 -07:00
|
|
|
void
|
|
|
|
URL::GetHref(nsString& aHref) const
|
|
|
|
{
|
|
|
|
aHref.Truncate();
|
|
|
|
|
|
|
|
nsAutoCString href;
|
|
|
|
nsresult rv = mURI->GetSpec(href);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(href, aHref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetHref(const nsAString& aHref, ErrorResult& aRv)
|
|
|
|
{
|
2013-10-16 07:04:26 -07:00
|
|
|
nsCString href = NS_ConvertUTF16toUTF8(aHref);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = ioService->NewURI(href, nullptr, nullptr, getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-18 07:17:03 -07:00
|
|
|
nsAutoString label(aHref);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &label);
|
2013-10-16 07:04:26 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:14:04 -07:00
|
|
|
mURI = uri;
|
2014-02-03 08:48:38 -08:00
|
|
|
UpdateURLSearchParams();
|
2013-09-04 10:07:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetOrigin(nsString& aOrigin) const
|
|
|
|
{
|
|
|
|
nsContentUtils::GetUTFNonNullOrigin(mURI, aOrigin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetProtocol(nsString& aProtocol) const
|
|
|
|
{
|
|
|
|
nsCString protocol;
|
|
|
|
if (NS_SUCCEEDED(mURI->GetScheme(protocol))) {
|
|
|
|
aProtocol.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyASCIItoUTF16(protocol, aProtocol);
|
2014-01-04 07:02:17 -08:00
|
|
|
aProtocol.Append(char16_t(':'));
|
2013-09-04 10:07:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetProtocol(const nsAString& aProtocol)
|
|
|
|
{
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
aProtocol.BeginReading(start);
|
|
|
|
aProtocol.EndReading(end);
|
|
|
|
nsAString::const_iterator iter(start);
|
|
|
|
|
|
|
|
FindCharInReadable(':', iter, end);
|
|
|
|
mURI->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define URL_GETTER( value, func ) \
|
|
|
|
value.Truncate(); \
|
|
|
|
nsAutoCString tmp; \
|
|
|
|
nsresult rv = mURI->func(tmp); \
|
|
|
|
if (NS_SUCCEEDED(rv)) { \
|
|
|
|
CopyUTF8toUTF16(tmp, value); \
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetUsername(nsString& aUsername) const
|
|
|
|
{
|
|
|
|
URL_GETTER(aUsername, GetUsername);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetUsername(const nsAString& aUsername)
|
|
|
|
{
|
|
|
|
mURI->SetUsername(NS_ConvertUTF16toUTF8(aUsername));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetPassword(nsString& aPassword) const
|
|
|
|
{
|
|
|
|
URL_GETTER(aPassword, GetPassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetPassword(const nsAString& aPassword)
|
|
|
|
{
|
|
|
|
mURI->SetPassword(NS_ConvertUTF16toUTF8(aPassword));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetHost(nsString& aHost) const
|
|
|
|
{
|
|
|
|
URL_GETTER(aHost, GetHostPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetHost(const nsAString& aHost)
|
|
|
|
{
|
|
|
|
mURI->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
|
|
|
|
}
|
|
|
|
|
2013-12-12 11:30:27 -08:00
|
|
|
void
|
|
|
|
URL::URLSearchParamsUpdated()
|
|
|
|
{
|
2014-02-03 08:48:38 -08:00
|
|
|
MOZ_ASSERT(mSearchParams);
|
2013-12-12 11:30:27 -08:00
|
|
|
|
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-03 08:48:38 -08:00
|
|
|
URL::UpdateURLSearchParams()
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
2014-02-03 08:48:38 -08:00
|
|
|
if (!mSearchParams) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-12 11:30:27 -08:00
|
|
|
|
|
|
|
nsAutoCString search;
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(mURI));
|
|
|
|
if (url) {
|
|
|
|
nsresult rv = url->GetQuery(search);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get the query from a nsIURL.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 08:48:38 -08:00
|
|
|
mSearchParams->ParseInput(search, this);
|
2013-12-12 11:30:27 -08:00
|
|
|
}
|
|
|
|
|
2013-09-04 10:07:21 -07:00
|
|
|
void
|
|
|
|
URL::GetHostname(nsString& aHostname) const
|
|
|
|
{
|
|
|
|
URL_GETTER(aHostname, GetHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetHostname(const nsAString& aHostname)
|
|
|
|
{
|
2014-04-23 07:45:58 -07:00
|
|
|
// nsStandardURL returns NS_ERROR_UNEXPECTED for an empty hostname
|
|
|
|
// The return code is silently ignored
|
2013-09-04 10:07:21 -07:00
|
|
|
mURI->SetHost(NS_ConvertUTF16toUTF8(aHostname));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetPort(nsString& aPort) const
|
|
|
|
{
|
|
|
|
aPort.Truncate();
|
|
|
|
|
|
|
|
int32_t port;
|
|
|
|
nsresult rv = mURI->GetPort(&port);
|
|
|
|
if (NS_SUCCEEDED(rv) && port != -1) {
|
|
|
|
nsAutoString portStr;
|
|
|
|
portStr.AppendInt(port, 10);
|
|
|
|
aPort.Assign(portStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetPort(const nsAString& aPort)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString portStr(aPort);
|
2013-12-05 13:05:51 -08:00
|
|
|
int32_t port = -1;
|
|
|
|
|
|
|
|
// nsIURI uses -1 as default value.
|
|
|
|
if (!portStr.IsEmpty()) {
|
|
|
|
port = portStr.ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-04 10:07:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mURI->SetPort(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetPathname(nsString& aPathname) const
|
|
|
|
{
|
|
|
|
aPathname.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(mURI));
|
|
|
|
if (!url) {
|
|
|
|
// Do not throw! Not having a valid URI or URL should result in an empty
|
|
|
|
// string.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString file;
|
|
|
|
nsresult rv = url->GetFilePath(file);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(file, aPathname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetPathname(const nsAString& aPathname)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(mURI));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::GetSearch(nsString& aSearch) const
|
|
|
|
{
|
|
|
|
aSearch.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(mURI));
|
|
|
|
if (!url) {
|
|
|
|
// Do not throw! Not having a valid URI or URL should result in an empty
|
|
|
|
// string.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString search;
|
|
|
|
nsresult rv = url->GetQuery(search);
|
|
|
|
if (NS_SUCCEEDED(rv) && !search.IsEmpty()) {
|
|
|
|
CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, aSearch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetSearch(const nsAString& aSearch)
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
|
|
|
SetSearchInternal(aSearch);
|
2014-02-03 08:48:38 -08:00
|
|
|
UpdateURLSearchParams();
|
2013-12-12 11:30:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetSearchInternal(const nsAString& aSearch)
|
2013-09-04 10:07:21 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(mURI));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
url->SetQuery(NS_ConvertUTF16toUTF8(aSearch));
|
|
|
|
}
|
|
|
|
|
2013-12-12 11:30:27 -08:00
|
|
|
URLSearchParams*
|
2014-03-19 08:36:46 -07:00
|
|
|
URL::SearchParams()
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
|
|
|
CreateSearchParamsIfNeeded();
|
|
|
|
return mSearchParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-19 08:36:46 -07:00
|
|
|
URL::SetSearchParams(URLSearchParams& aSearchParams)
|
2013-12-12 11:30:27 -08:00
|
|
|
{
|
2014-02-03 08:48:38 -08:00
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
2014-02-21 13:54:42 -08:00
|
|
|
}
|
2014-02-03 08:48:38 -08:00
|
|
|
|
2014-02-03 08:48:38 -08:00
|
|
|
// the observer will be cleared using the cycle collector.
|
2014-03-19 08:36:46 -07:00
|
|
|
mSearchParams = &aSearchParams;
|
2014-02-03 08:48:38 -08:00
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
|
2013-12-12 11:30:27 -08:00
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:07:21 -07:00
|
|
|
void
|
|
|
|
URL::GetHash(nsString& aHash) const
|
|
|
|
{
|
|
|
|
aHash.Truncate();
|
|
|
|
|
|
|
|
nsAutoCString ref;
|
|
|
|
nsresult rv = mURI->GetRef(ref);
|
|
|
|
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
|
|
|
|
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
|
2014-01-04 07:02:17 -08:00
|
|
|
aHash.Assign(char16_t('#'));
|
2013-09-04 10:07:21 -07:00
|
|
|
AppendUTF8toUTF16(ref, aHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetHash(const nsAString& aHash)
|
|
|
|
{
|
|
|
|
mURI->SetRef(NS_ConvertUTF16toUTF8(aHash));
|
|
|
|
}
|
|
|
|
|
2013-11-20 08:29:03 -08:00
|
|
|
bool IsChromeURI(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
bool isChrome = false;
|
|
|
|
if (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)))
|
|
|
|
return isChrome;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-12 11:30:27 -08:00
|
|
|
void
|
|
|
|
URL::CreateSearchParamsIfNeeded()
|
|
|
|
{
|
|
|
|
if (!mSearchParams) {
|
|
|
|
mSearchParams = new URLSearchParams();
|
2014-02-03 08:48:38 -08:00
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
UpdateURLSearchParams();
|
2013-12-12 11:30:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:08:21 -08:00
|
|
|
}
|
|
|
|
}
|