2015-05-03 19:32:37 +00: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: */
|
2013-01-28 11:08:21 +00:00
|
|
|
/* 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"
|
2013-02-15 08:04:11 +00:00
|
|
|
#include "DOMMediaStream.h"
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-06-21 03:15:15 +00:00
|
|
|
#include "mozilla/dom/MediaSource.h"
|
2013-09-04 17:07:21 +00:00
|
|
|
#include "mozilla/dom/URLBinding.h"
|
2013-01-28 11:08:21 +00:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2013-09-04 17:07:21 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsNetCID.h"
|
2014-06-09 16:20:19 +00:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-04 17:07:21 +00:00
|
|
|
#include "nsIURL.h"
|
2015-05-05 17:38:56 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-01-28 11:08:21 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-02-03 16:48:38 +00: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 19:30:27 +00: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
|
|
|
|
|
2015-05-05 17:38:56 +00:00
|
|
|
URL::URL(already_AddRefed<nsIURI> aURI)
|
2013-10-10 06:56:01 +00:00
|
|
|
: mURI(aURI)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:56:42 +00:00
|
|
|
bool
|
Bug 1117172 part 2. Change the non-wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, Codegen.py, and
StructuredClone.cpp. The rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/WrapObject\((JSContext *\* *(?:aCx|cx)),(\s*)(JS::MutableHandle<JSObject\*> aReflector)/WrapObject(\1,\2JS::Handle<JSObject*> aGivenProto,\2\3/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx)), this, aReflector/\1, this, aGivenProto, aReflector/'
2015-03-19 14:13:32 +00:00
|
|
|
URL::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
Bug 1117172 part 2. Change the non-wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, Codegen.py, and
StructuredClone.cpp. The rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/WrapObject\((JSContext *\* *(?:aCx|cx)),(\s*)(JS::MutableHandle<JSObject\*> aReflector)/WrapObject(\1,\2JS::Handle<JSObject*> aGivenProto,\2\3/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx)), this, aReflector/\1, this, aGivenProto, aReflector/'
2015-03-19 14:13:32 +00:00
|
|
|
return URLBinding::Wrap(aCx, this, aGivenProto, aReflector);
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<URL>
|
|
|
|
URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl,
|
|
|
|
URL& aBase, ErrorResult& aRv)
|
|
|
|
{
|
2015-05-05 17:38:56 +00:00
|
|
|
return Constructor(aUrl, aBase.GetURI(), aRv);
|
|
|
|
}
|
2013-09-04 17:07:21 +00:00
|
|
|
|
2015-05-05 17:38:56 +00:00
|
|
|
/* static */ already_AddRefed<URL>
|
|
|
|
URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl,
|
|
|
|
const Optional<nsAString>& aBase, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aBase.WasPassed()) {
|
|
|
|
return Constructor(aUrl, aBase.Value(), aRv);
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 17:38:56 +00:00
|
|
|
return Constructor(aUrl, nullptr, aRv);
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<URL>
|
|
|
|
URL::Constructor(const GlobalObject& aGlobal, const nsAString& aUrl,
|
2015-05-05 17:38:56 +00:00
|
|
|
const nsAString& aBase, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
2015-05-05 17:38:56 +00:00
|
|
|
return Constructor(aUrl, aBase, aRv);
|
|
|
|
}
|
2013-09-04 17:07:21 +00:00
|
|
|
|
2015-05-05 17:38:56 +00:00
|
|
|
/* static */ already_AddRefed<URL>
|
|
|
|
URL::Constructor(const nsAString& aUrl, const nsAString& aBase,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2013-09-04 17:07:21 +00:00
|
|
|
nsCOMPtr<nsIURI> baseUri;
|
2015-05-05 17:38:56 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(baseUri), aBase, nullptr, nullptr,
|
|
|
|
nsContentUtils::GetIOService());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &aBase);
|
2013-09-04 17:07:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:38:56 +00:00
|
|
|
return Constructor(aUrl, baseUri, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<URL>
|
|
|
|
URL::Constructor(const nsAString& aUrl, nsIURI* aBase, ErrorResult& aRv)
|
|
|
|
{
|
2013-09-04 17:07:21 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2015-05-05 17:38:56 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aUrl, nullptr, aBase,
|
|
|
|
nsContentUtils::GetIOService());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &aUrl);
|
2013-09-04 17:07:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:38:56 +00:00
|
|
|
nsRefPtr<URL> url = new URL(uri.forget());
|
2013-09-04 17:07:21 +00:00
|
|
|
return url.forget();
|
|
|
|
}
|
|
|
|
|
2013-01-28 11:08:21 +00:00
|
|
|
void
|
2013-08-23 05:17:08 +00:00
|
|
|
URL::CreateObjectURL(const GlobalObject& aGlobal,
|
2015-05-12 12:09:51 +00:00
|
|
|
Blob& aBlob,
|
2013-01-28 11:08:21 +00:00
|
|
|
const objectURLOptions& aOptions,
|
2015-02-27 19:10:20 +00:00
|
|
|
nsAString& aResult,
|
2013-01-28 11:08:21 +00:00
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
2014-10-08 16:15:22 +00:00
|
|
|
CreateObjectURLInternal(aGlobal, aBlob.Impl(),
|
2012-12-03 16:07:49 +00:00
|
|
|
NS_LITERAL_CSTRING(BLOBURI_SCHEME), aOptions, aResult,
|
|
|
|
aError);
|
2013-01-28 11:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-15 08:04:11 +00:00
|
|
|
URL::CreateObjectURL(const GlobalObject& aGlobal, DOMMediaStream& aStream,
|
2013-01-28 11:08:21 +00:00
|
|
|
const mozilla::dom::objectURLOptions& aOptions,
|
2015-02-27 19:10:20 +00:00
|
|
|
nsAString& aResult,
|
2013-01-28 11:08:21 +00:00
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
2013-10-10 06:56:01 +00:00
|
|
|
CreateObjectURLInternal(aGlobal, &aStream,
|
2012-12-03 16:07:49 +00:00
|
|
|
NS_LITERAL_CSTRING(MEDIASTREAMURI_SCHEME), aOptions,
|
|
|
|
aResult, aError);
|
2013-01-28 11:08:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 03:15:15 +00:00
|
|
|
void
|
|
|
|
URL::CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource,
|
|
|
|
const objectURLOptions& aOptions,
|
2015-02-27 19:10:20 +00:00
|
|
|
nsAString& aResult,
|
2013-06-21 03:15:15 +00:00
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
2013-10-10 06:56:01 +00:00
|
|
|
CreateObjectURLInternal(aGlobal, &aSource,
|
2013-06-21 03:15:15 +00:00
|
|
|
NS_LITERAL_CSTRING(MEDIASOURCEURI_SCHEME), aOptions,
|
|
|
|
aResult, aError);
|
|
|
|
}
|
|
|
|
|
2013-01-28 11:08:21 +00:00
|
|
|
void
|
2013-10-10 06:56:01 +00:00
|
|
|
URL::CreateObjectURLInternal(const GlobalObject& aGlobal, nsISupports* aObject,
|
2013-01-28 11:08:21 +00:00
|
|
|
const nsACString& aScheme,
|
2013-09-04 17:07:21 +00:00
|
|
|
const objectURLOptions& aOptions,
|
2015-05-07 07:05:43 +00:00
|
|
|
nsAString& aResult, ErrorResult& aRv)
|
2013-01-28 11:08:21 +00:00
|
|
|
{
|
2015-05-07 07:05:43 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-13 09:58:00 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get());
|
2013-01-28 11:08:21 +00:00
|
|
|
|
2015-02-27 19:10:20 +00:00
|
|
|
nsAutoCString url;
|
2013-01-28 11:08:21 +00:00
|
|
|
nsresult rv = nsHostObjectProtocolHandler::AddDataEntry(aScheme, aObject,
|
2013-10-10 06:56:01 +00:00
|
|
|
principal, url);
|
2013-01-28 11:08:21 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-05-07 07:05:43 +00:00
|
|
|
aRv.Throw(rv);
|
2013-01-28 11:08:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-07 07:05:43 +00:00
|
|
|
global->RegisterHostObjectURI(url);
|
2013-01-28 11:08:21 +00:00
|
|
|
CopyASCIItoUTF16(url, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-07 07:05:43 +00:00
|
|
|
URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL,
|
|
|
|
ErrorResult& aRv)
|
2013-01-28 11:08:21 +00:00
|
|
|
{
|
2015-05-07 07:05:43 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-13 09:58:00 +00:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal.Get());
|
2013-01-28 11:08:21 +00:00
|
|
|
|
|
|
|
NS_LossyConvertUTF16toASCII asciiurl(aURL);
|
|
|
|
|
2013-10-10 06:56:01 +00:00
|
|
|
nsIPrincipal* urlPrincipal =
|
2013-01-28 11:08:21 +00:00
|
|
|
nsHostObjectProtocolHandler::GetDataEntryPrincipal(asciiurl);
|
2013-10-10 06:56:01 +00:00
|
|
|
|
|
|
|
if (urlPrincipal && principal->Subsumes(urlPrincipal)) {
|
2015-05-07 07:05:43 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
global->UnregisterHostObjectURI(asciiurl);
|
2013-01-28 11:08:21 +00:00
|
|
|
nsHostObjectProtocolHandler::RemoveDataEntry(asciiurl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 17:07:21 +00:00
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetHref(nsAString& aHref, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
aHref.Truncate();
|
|
|
|
|
|
|
|
nsAutoCString href;
|
|
|
|
nsresult rv = mURI->GetSpec(href);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyUTF8toUTF16(href, aHref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetHref(const nsAString& aHref, ErrorResult& aRv)
|
|
|
|
{
|
2015-02-27 19:10:20 +00:00
|
|
|
NS_ConvertUTF16toUTF8 href(aHref);
|
2013-10-16 14:04:26 +00:00
|
|
|
|
|
|
|
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 14:17:03 +00:00
|
|
|
nsAutoString label(aHref);
|
|
|
|
aRv.ThrowTypeError(MSG_INVALID_URL, &label);
|
2013-10-16 14:04:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:14:04 +00:00
|
|
|
mURI = uri;
|
2014-02-03 16:48:38 +00:00
|
|
|
UpdateURLSearchParams();
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
2014-08-31 10:40:11 +00:00
|
|
|
nsContentUtils::GetUTFOrigin(mURI, aOrigin);
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
2015-02-27 19:10:20 +00:00
|
|
|
nsAutoCString protocol;
|
2013-09-04 17:07:21 +00:00
|
|
|
if (NS_SUCCEEDED(mURI->GetScheme(protocol))) {
|
|
|
|
aProtocol.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyASCIItoUTF16(protocol, aProtocol);
|
2014-01-04 15:02:17 +00:00
|
|
|
aProtocol.Append(char16_t(':'));
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
aProtocol.BeginReading(start);
|
|
|
|
aProtocol.EndReading(end);
|
|
|
|
nsAString::const_iterator iter(start);
|
|
|
|
|
|
|
|
FindCharInReadable(':', iter, end);
|
2014-06-09 16:20:19 +00:00
|
|
|
|
|
|
|
// Changing the protocol of a URL, changes the "nature" of the URI
|
|
|
|
// implementation. In order to do this properly, we have to serialize the
|
|
|
|
// existing URL and reparse it in a new object.
|
|
|
|
nsCOMPtr<nsIURI> clone;
|
|
|
|
nsresult rv = mURI->Clone(getter_AddRefs(clone));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !clone) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = clone->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString href;
|
|
|
|
rv = clone->GetSpec(href);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), href);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mURI = uri;
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define URL_GETTER( value, func ) \
|
|
|
|
value.Truncate(); \
|
|
|
|
nsAutoCString tmp; \
|
|
|
|
nsresult rv = mURI->func(tmp); \
|
|
|
|
if (NS_SUCCEEDED(rv)) { \
|
|
|
|
CopyUTF8toUTF16(tmp, value); \
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetUsername(nsAString& aUsername, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
URL_GETTER(aUsername, GetUsername);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetUsername(const nsAString& aUsername, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
mURI->SetUsername(NS_ConvertUTF16toUTF8(aUsername));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetPassword(nsAString& aPassword, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
URL_GETTER(aPassword, GetPassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetPassword(const nsAString& aPassword, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
mURI->SetPassword(NS_ConvertUTF16toUTF8(aPassword));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetHost(nsAString& aHost, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
URL_GETTER(aHost, GetHostPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetHost(const nsAString& aHost, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
mURI->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:27 +00:00
|
|
|
void
|
2014-08-08 00:45:21 +00:00
|
|
|
URL::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
2014-02-03 16:48:38 +00:00
|
|
|
MOZ_ASSERT(mSearchParams);
|
2014-08-08 00:45:21 +00:00
|
|
|
MOZ_ASSERT(mSearchParams == aSearchParams);
|
2013-12-12 19:30:27 +00:00
|
|
|
|
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-03 16:48:38 +00:00
|
|
|
URL::UpdateURLSearchParams()
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
2014-02-03 16:48:38 +00:00
|
|
|
if (!mSearchParams) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-12 19:30:27 +00: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 16:48:38 +00:00
|
|
|
mSearchParams->ParseInput(search, this);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 17:07:21 +00:00
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetHostname(nsAString& aHostname, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
2014-05-29 22:19:09 +00:00
|
|
|
aHostname.Truncate();
|
2014-10-20 10:46:20 +00:00
|
|
|
nsContentUtils::GetHostOrIPv6WithBrackets(mURI, aHostname);
|
2013-09-04 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetHostname(const nsAString& aHostname, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
2014-04-23 14:45:58 +00:00
|
|
|
// nsStandardURL returns NS_ERROR_UNEXPECTED for an empty hostname
|
|
|
|
// The return code is silently ignored
|
2013-09-04 17:07:21 +00:00
|
|
|
mURI->SetHost(NS_ConvertUTF16toUTF8(aHostname));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetPort(nsAString& aPort, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
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
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetPort(const nsAString& aPort, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString portStr(aPort);
|
2013-12-05 21:05:51 +00: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 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mURI->SetPort(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetPathname(nsAString& aPathname, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
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
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetPathname(const nsAString& aPathname, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(mURI));
|
|
|
|
if (!url) {
|
|
|
|
// Ignore failures to be compatible with NS4.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetSearch(nsAString& aSearch, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
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
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetSearch(const nsAString& aSearch, ErrorResult& aRv)
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
|
|
|
SetSearchInternal(aSearch);
|
2014-02-03 16:48:38 +00:00
|
|
|
UpdateURLSearchParams();
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URL::SetSearchInternal(const nsAString& aSearch)
|
2013-09-04 17:07:21 +00: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 19:30:27 +00:00
|
|
|
URLSearchParams*
|
2014-03-19 15:36:46 +00:00
|
|
|
URL::SearchParams()
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
|
|
|
CreateSearchParamsIfNeeded();
|
|
|
|
return mSearchParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-19 15:36:46 +00:00
|
|
|
URL::SetSearchParams(URLSearchParams& aSearchParams)
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
2014-02-03 16:48:38 +00:00
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
2014-02-21 21:54:42 +00:00
|
|
|
}
|
2014-02-03 16:48:38 +00:00
|
|
|
|
2014-02-03 16:48:38 +00:00
|
|
|
// the observer will be cleared using the cycle collector.
|
2014-03-19 15:36:46 +00:00
|
|
|
mSearchParams = &aSearchParams;
|
2014-02-03 16:48:38 +00:00
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
|
2013-12-12 19:30:27 +00:00
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
}
|
|
|
|
|
2013-09-04 17:07:21 +00:00
|
|
|
void
|
2015-02-27 19:10:20 +00:00
|
|
|
URL::GetHash(nsAString& aHash, ErrorResult& aRv) const
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
aHash.Truncate();
|
|
|
|
|
|
|
|
nsAutoCString ref;
|
|
|
|
nsresult rv = mURI->GetRef(ref);
|
|
|
|
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
|
2014-01-04 15:02:17 +00:00
|
|
|
aHash.Assign(char16_t('#'));
|
2015-01-13 00:11:02 +00:00
|
|
|
if (nsContentUtils::EncodeDecodeURLHash()) {
|
2014-11-12 01:14:04 +00:00
|
|
|
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
|
|
|
|
}
|
2013-09-04 17:07:21 +00:00
|
|
|
AppendUTF8toUTF16(ref, aHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-11 23:30:27 +00:00
|
|
|
URL::SetHash(const nsAString& aHash, ErrorResult& aRv)
|
2013-09-04 17:07:21 +00:00
|
|
|
{
|
|
|
|
mURI->SetRef(NS_ConvertUTF16toUTF8(aHash));
|
|
|
|
}
|
|
|
|
|
2013-11-20 16:29:03 +00:00
|
|
|
bool IsChromeURI(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
bool isChrome = false;
|
|
|
|
if (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)))
|
|
|
|
return isChrome;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:27 +00:00
|
|
|
void
|
|
|
|
URL::CreateSearchParamsIfNeeded()
|
|
|
|
{
|
|
|
|
if (!mSearchParams) {
|
|
|
|
mSearchParams = new URLSearchParams();
|
2014-02-03 16:48:38 +00:00
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
UpdateURLSearchParams();
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 11:08:21 +00:00
|
|
|
}
|
|
|
|
}
|