1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modules/serviceworkers/NavigatorServiceWorker.h"
#include "core/dom/Document.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Navigator.h"
#include "modules/serviceworkers/ServiceWorkerContainer.h"
namespace blink {
NavigatorServiceWorker::NavigatorServiceWorker(Navigator& navigator) {}
NavigatorServiceWorker* NavigatorServiceWorker::from(Document& document) {
if (!document.frame() || !document.frame()->domWindow())
return nullptr;
Navigator& navigator = *document.frame()->domWindow()->navigator();
return &from(navigator);
}
NavigatorServiceWorker& NavigatorServiceWorker::from(Navigator& navigator) {
NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator);
if (!supplement) {
supplement = new NavigatorServiceWorker(navigator);
provideTo(navigator, supplementName(), supplement);
if (navigator.frame() &&
navigator.frame()
->securityContext()
->getSecurityOrigin()
->canAccessServiceWorkers()) {
// Initialize ServiceWorkerContainer too.
supplement->serviceWorker(navigator.frame(), ASSERT_NO_EXCEPTION);
}
}
return *supplement;
}
NavigatorServiceWorker* NavigatorServiceWorker::toNavigatorServiceWorker(
Navigator& navigator) {
return static_cast<NavigatorServiceWorker*>(
Supplement<Navigator>::from(navigator, supplementName()));
}
const char* NavigatorServiceWorker::supplementName() {
return "NavigatorServiceWorker";
}
ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(
ExecutionContext* executionContext,
Navigator& navigator,
ExceptionState& exceptionState) {
DCHECK(!navigator.frame() ||
executionContext->getSecurityOrigin()->canAccessCheckSuborigins(
navigator.frame()->securityContext()->getSecurityOrigin()));
return NavigatorServiceWorker::from(navigator).serviceWorker(
navigator.frame(), exceptionState);
}
ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(
ExecutionContext* executionContext,
Navigator& navigator,
String& errorMessage) {
DCHECK(!navigator.frame() ||
executionContext->getSecurityOrigin()->canAccessCheckSuborigins(
navigator.frame()->securityContext()->getSecurityOrigin()));
return NavigatorServiceWorker::from(navigator).serviceWorker(
navigator.frame(), errorMessage);
}
ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(
LocalFrame* frame,
ExceptionState& exceptionState) {
String errorMessage;
ServiceWorkerContainer* result = serviceWorker(frame, errorMessage);
if (!errorMessage.isEmpty()) {
DCHECK(!result);
exceptionState.throwSecurityError(errorMessage);
}
return result;
}
ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(
LocalFrame* frame,
String& errorMessage) {
if (frame &&
!frame->securityContext()
->getSecurityOrigin()
->canAccessServiceWorkers()) {
if (frame->securityContext()->isSandboxed(SandboxOrigin)) {
errorMessage =
"Service worker is disabled because the context is sandboxed and "
"lacks the 'allow-same-origin' flag.";
} else if (frame->securityContext()->getSecurityOrigin()->hasSuborigin()) {
errorMessage =
"Service worker is disabled because the context is in a suborigin.";
} else {
errorMessage =
"Access to service workers is denied in this document origin.";
}
return nullptr;
}
if (!m_serviceWorker && frame) {
// We need to create a new ServiceWorkerContainer when the frame
// navigates to a new document. In practice, this happens only when the
// frame navigates from the initial empty page to a new same-origin page.
DCHECK(frame->domWindow());
m_serviceWorker = ServiceWorkerContainer::create(
frame->domWindow()->getExecutionContext(), this);
}
return m_serviceWorker.get();
}
void NavigatorServiceWorker::clearServiceWorker() {
m_serviceWorker = nullptr;
}
DEFINE_TRACE(NavigatorServiceWorker) {
visitor->trace(m_serviceWorker);
Supplement<Navigator>::trace(visitor);
}
} // namespace blink
|