Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "Presentation.h"
8 :
9 : #include <ctype.h>
10 :
11 : #include "mozilla/dom/PresentationBinding.h"
12 : #include "mozilla/dom/Promise.h"
13 : #include "nsContentUtils.h"
14 : #include "nsCycleCollectionParticipant.h"
15 : #include "nsIDocShell.h"
16 : #include "nsIPresentationService.h"
17 : #include "nsIScriptSecurityManager.h"
18 : #include "nsJSUtils.h"
19 : #include "nsNetUtil.h"
20 : #include "nsPIDOMWindow.h"
21 : #include "nsSandboxFlags.h"
22 : #include "nsServiceManagerUtils.h"
23 : #include "PresentationReceiver.h"
24 :
25 : namespace mozilla {
26 : namespace dom {
27 :
28 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Presentation,
29 : mWindow,
30 : mDefaultRequest, mReceiver)
31 :
32 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(Presentation)
33 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(Presentation)
34 :
35 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Presentation)
36 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
37 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
38 0 : NS_INTERFACE_MAP_END
39 :
40 : /* static */ already_AddRefed<Presentation>
41 0 : Presentation::Create(nsPIDOMWindowInner* aWindow)
42 : {
43 0 : RefPtr<Presentation> presentation = new Presentation(aWindow);
44 0 : return presentation.forget();
45 : }
46 :
47 0 : Presentation::Presentation(nsPIDOMWindowInner* aWindow)
48 0 : : mWindow(aWindow)
49 : {
50 0 : }
51 :
52 0 : Presentation::~Presentation()
53 : {
54 0 : }
55 :
56 : /* virtual */ JSObject*
57 0 : Presentation::WrapObject(JSContext* aCx,
58 : JS::Handle<JSObject*> aGivenProto)
59 : {
60 0 : return PresentationBinding::Wrap(aCx, this, aGivenProto);
61 : }
62 :
63 : void
64 0 : Presentation::SetDefaultRequest(PresentationRequest* aRequest)
65 : {
66 0 : nsCOMPtr<nsIDocument> doc = mWindow ? mWindow->GetExtantDoc() : nullptr;
67 0 : if (NS_WARN_IF(!doc)) {
68 0 : return;
69 : }
70 :
71 0 : if (doc->GetSandboxFlags() & SANDBOXED_PRESENTATION) {
72 0 : return;
73 : }
74 :
75 0 : mDefaultRequest = aRequest;
76 : }
77 :
78 : already_AddRefed<PresentationRequest>
79 0 : Presentation::GetDefaultRequest() const
80 : {
81 0 : RefPtr<PresentationRequest> request = mDefaultRequest;
82 0 : return request.forget();
83 : }
84 :
85 : already_AddRefed<PresentationReceiver>
86 0 : Presentation::GetReceiver()
87 : {
88 : // return the same receiver if already created
89 0 : if (mReceiver) {
90 0 : RefPtr<PresentationReceiver> receiver = mReceiver;
91 0 : return receiver.forget();
92 : }
93 :
94 0 : if (!HasReceiverSupport() || !IsInPresentedContent()) {
95 0 : return nullptr;
96 : }
97 :
98 0 : mReceiver = PresentationReceiver::Create(mWindow);
99 0 : if (NS_WARN_IF(!mReceiver)) {
100 0 : MOZ_ASSERT(mReceiver);
101 0 : return nullptr;
102 : }
103 :
104 0 : RefPtr<PresentationReceiver> receiver = mReceiver;
105 0 : return receiver.forget();
106 : }
107 :
108 : void
109 0 : Presentation::SetStartSessionUnsettled(bool aIsUnsettled)
110 : {
111 0 : mStartSessionUnsettled = aIsUnsettled;
112 0 : }
113 :
114 : bool
115 0 : Presentation::IsStartSessionUnsettled() const
116 : {
117 0 : return mStartSessionUnsettled;
118 : }
119 :
120 : bool
121 0 : Presentation::HasReceiverSupport() const
122 : {
123 0 : if (!mWindow) {
124 0 : return false;
125 : }
126 :
127 : // Grant access to browser receiving pages and their same-origin iframes. (App
128 : // pages should be controlled by "presentation" permission in app manifests.)
129 0 : nsCOMPtr<nsIDocShell> docShell = mWindow->GetDocShell();
130 0 : if (!docShell) {
131 0 : return false;
132 : }
133 :
134 0 : if (!Preferences::GetBool("dom.presentation.testing.simulate-receiver") &&
135 0 : !docShell->GetIsInMozBrowser() &&
136 0 : !docShell->GetIsTopLevelContentDocShell()) {
137 0 : return false;
138 : }
139 :
140 0 : nsAutoString presentationURL;
141 0 : nsContentUtils::GetPresentationURL(docShell, presentationURL);
142 :
143 0 : if (presentationURL.IsEmpty()) {
144 0 : return false;
145 : }
146 :
147 : nsCOMPtr<nsIScriptSecurityManager> securityManager =
148 0 : nsContentUtils::GetSecurityManager();
149 0 : if (!securityManager) {
150 0 : return false;
151 : }
152 :
153 0 : nsCOMPtr<nsIURI> presentationURI;
154 0 : nsresult rv = NS_NewURI(getter_AddRefs(presentationURI), presentationURL);
155 0 : if (NS_FAILED(rv)) {
156 0 : return false;
157 : }
158 :
159 0 : nsCOMPtr<nsIURI> docURI = mWindow->GetDocumentURI();
160 0 : return NS_SUCCEEDED(securityManager->CheckSameOriginURI(presentationURI,
161 : docURI,
162 : false));
163 : }
164 :
165 : bool
166 0 : Presentation::IsInPresentedContent() const
167 : {
168 0 : if (!mWindow) {
169 0 : return false;
170 : }
171 :
172 0 : nsCOMPtr<nsIDocShell> docShell = mWindow->GetDocShell();
173 0 : MOZ_ASSERT(docShell);
174 :
175 0 : nsAutoString presentationURL;
176 0 : nsContentUtils::GetPresentationURL(docShell, presentationURL);
177 :
178 0 : return !presentationURL.IsEmpty();
179 : }
180 :
181 : } // namespace dom
182 : } // namespace mozilla
|