2017-10-27 01:08:41 +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: */
|
2015-03-19 10:48:28 +03: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/. */
|
|
|
|
|
2016-07-14 03:44:00 +03:00
|
|
|
#include "Presentation.h"
|
|
|
|
|
2016-06-16 10:43:51 +03:00
|
|
|
#include <ctype.h>
|
2016-07-14 03:44:00 +03:00
|
|
|
|
2015-03-19 10:48:28 +03:00
|
|
|
#include "mozilla/dom/PresentationBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2016-05-17 06:18:16 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-03-19 10:48:28 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2016-05-17 06:18:16 +03:00
|
|
|
#include "nsIDocShell.h"
|
2015-03-30 09:27:27 +03:00
|
|
|
#include "nsIPresentationService.h"
|
2016-06-03 14:50:30 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
2016-10-04 01:22:34 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2016-07-14 03:44:00 +03:00
|
|
|
#include "nsSandboxFlags.h"
|
2015-03-19 10:48:28 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2015-09-22 13:36:47 +03:00
|
|
|
#include "PresentationReceiver.h"
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2016-10-04 01:22:34 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2016-10-04 01:22:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Presentation, mWindow, mDefaultRequest,
|
|
|
|
mReceiver)
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2016-10-04 01:22:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Presentation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Presentation)
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2016-10-04 01:22:34 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Presentation)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Presentation> Presentation::Create(
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aWindow) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Presentation> presentation = new Presentation(aWindow);
|
2016-05-17 06:18:16 +03:00
|
|
|
return presentation.forget();
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
Presentation::Presentation(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) {}
|
2015-03-19 10:48:28 +03:00
|
|
|
|
|
|
|
Presentation::~Presentation() {}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* virtual */
|
|
|
|
JSObject* Presentation::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return Presentation_Binding::Wrap(aCx, this, aGivenProto);
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
|
|
|
|
2015-09-14 05:39:57 +03:00
|
|
|
void Presentation::SetDefaultRequest(PresentationRequest* aRequest) {
|
2017-08-09 12:38:30 +03:00
|
|
|
if (nsContentUtils::ShouldResistFingerprinting()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = mWindow ? mWindow->GetExtantDoc() : nullptr;
|
2016-07-14 03:44:00 +03:00
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc->GetSandboxFlags() & SANDBOXED_PRESENTATION) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-14 05:39:57 +03:00
|
|
|
mDefaultRequest = aRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<PresentationRequest> Presentation::GetDefaultRequest() const {
|
2017-08-09 12:38:30 +03:00
|
|
|
if (nsContentUtils::ShouldResistFingerprinting()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationRequest> request = mDefaultRequest;
|
2015-09-14 05:39:57 +03:00
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-22 13:36:47 +03:00
|
|
|
already_AddRefed<PresentationReceiver> Presentation::GetReceiver() {
|
2017-08-09 12:38:30 +03:00
|
|
|
if (nsContentUtils::ShouldResistFingerprinting()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-17 06:18:16 +03:00
|
|
|
// return the same receiver if already created
|
|
|
|
if (mReceiver) {
|
|
|
|
RefPtr<PresentationReceiver> receiver = mReceiver;
|
|
|
|
return receiver.forget();
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:53:31 +03:00
|
|
|
if (!HasReceiverSupport() || !IsInPresentedContent()) {
|
2016-05-17 06:18:16 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-04 01:22:34 +03:00
|
|
|
mReceiver = PresentationReceiver::Create(mWindow);
|
2016-05-17 06:18:16 +03:00
|
|
|
if (NS_WARN_IF(!mReceiver)) {
|
|
|
|
MOZ_ASSERT(mReceiver);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationReceiver> receiver = mReceiver;
|
2015-09-22 13:36:47 +03:00
|
|
|
return receiver.forget();
|
2015-09-10 11:29:08 +03:00
|
|
|
}
|
2016-05-17 06:18:16 +03:00
|
|
|
|
2016-10-31 10:49:32 +03:00
|
|
|
void Presentation::SetStartSessionUnsettled(bool aIsUnsettled) {
|
|
|
|
mStartSessionUnsettled = aIsUnsettled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Presentation::IsStartSessionUnsettled() const {
|
|
|
|
return mStartSessionUnsettled;
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:53:31 +03:00
|
|
|
bool Presentation::HasReceiverSupport() const {
|
|
|
|
if (!mWindow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grant access to browser receiving pages and their same-origin iframes. (App
|
|
|
|
// pages should be controlled by "presentation" permission in app manifests.)
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = mWindow->GetDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-02 00:48:55 +03:00
|
|
|
if (!StaticPrefs::dom_presentation_testing_simulate_receiver() &&
|
2016-10-15 04:46:26 +03:00
|
|
|
!docShell->GetIsInMozBrowser() &&
|
2016-10-27 06:27:41 +03:00
|
|
|
!docShell->GetIsTopLevelContentDocShell()) {
|
2016-11-02 12:53:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString presentationURL;
|
|
|
|
nsContentUtils::GetPresentationURL(docShell, presentationURL);
|
|
|
|
|
|
|
|
if (presentationURL.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> securityManager =
|
|
|
|
nsContentUtils::GetSecurityManager();
|
|
|
|
if (!securityManager) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> presentationURI;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(presentationURI), presentationURL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-25 08:25:05 +03:00
|
|
|
bool isPrivateWin = false;
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = mWindow->GetExtantDoc();
|
2018-09-25 08:25:05 +03:00
|
|
|
if (doc) {
|
|
|
|
isPrivateWin =
|
|
|
|
doc->NodePrincipal()->OriginAttributesRef().mPrivateBrowsingId > 0;
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:53:31 +03:00
|
|
|
nsCOMPtr<nsIURI> docURI = mWindow->GetDocumentURI();
|
|
|
|
return NS_SUCCEEDED(securityManager->CheckSameOriginURI(
|
2018-09-25 08:25:05 +03:00
|
|
|
presentationURI, docURI, false, isPrivateWin));
|
2016-11-02 12:53:31 +03:00
|
|
|
}
|
|
|
|
|
2016-05-17 06:18:16 +03:00
|
|
|
bool Presentation::IsInPresentedContent() const {
|
2016-10-04 01:22:34 +03:00
|
|
|
if (!mWindow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = mWindow->GetDocShell();
|
2016-05-17 06:18:16 +03:00
|
|
|
MOZ_ASSERT(docShell);
|
|
|
|
|
|
|
|
nsAutoString presentationURL;
|
|
|
|
nsContentUtils::GetPresentationURL(docShell, presentationURL);
|
|
|
|
|
|
|
|
return !presentationURL.IsEmpty();
|
|
|
|
}
|
2016-10-04 01:22:34 +03:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|