Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: third_party/WebKit/Source/modules/push_messaging/PushManager.cpp

Issue 1701313002: Partial implementation of subscription restrictions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix module export build issue Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/push_messaging/PushManager.h" 5 #include "modules/push_messaging/PushManager.h"
6 6
7 #include "bindings/core/v8/ScriptPromise.h" 7 #include "bindings/core/v8/ScriptPromise.h"
8 #include "bindings/core/v8/ScriptPromiseResolver.h" 8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "bindings/core/v8/ScriptState.h" 9 #include "bindings/core/v8/ScriptState.h"
10 #include "core/dom/DOMException.h" 10 #include "core/dom/DOMException.h"
(...skipping 16 matching lines...) Expand all
27 namespace blink { 27 namespace blink {
28 namespace { 28 namespace {
29 29
30 WebPushProvider* pushProvider() 30 WebPushProvider* pushProvider()
31 { 31 {
32 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); 32 WebPushProvider* webPushProvider = Platform::current()->pushProvider();
33 ASSERT(webPushProvider); 33 ASSERT(webPushProvider);
34 return webPushProvider; 34 return webPushProvider;
35 } 35 }
36 36
37 WebPushSubscriptionOptions toWebPushSubscriptionOptions(const PushSubscriptionOp tions& options) 37 String bufferSourceToString(const ArrayBufferOrArrayBufferView& applicationServe rKey, ExceptionState& exceptionState)
38 { 38 {
39 WebPushSubscriptionOptions webOptions; 39 // Check the validity of the sender info. It must be a 65 byte unencrypted k ey,
40 webOptions.userVisibleOnly = options.userVisibleOnly(); 40 // which has the byte 0x04 as the first byte as a marker.
41 return webOptions; 41 char* input;
42 int length;
43 if (applicationServerKey.isArrayBuffer()) {
44 input = static_cast<char*>(
45 applicationServerKey.getAsArrayBuffer()->data());
46 length = applicationServerKey.getAsArrayBuffer()->byteLength();
47 } else if (applicationServerKey.isArrayBufferView()) {
48 input = static_cast<char*>(
49 applicationServerKey.getAsArrayBufferView()->buffer()->data());
50 length = applicationServerKey.getAsArrayBufferView()->buffer()->byteLeng th();
51 } else {
52 ASSERT_NOT_REACHED();
53 return String();
54 }
55
56 if (length == 65 && input[0] == 0x04)
57 return WebString::fromUTF8(input, length);
58 exceptionState.throwDOMException(InvalidAccessError, "The provided applicati onServerKey is not valid.");
59 return String();
42 } 60 }
43 61
44 } // namespace 62 } // namespace
45 63
46 PushManager::PushManager(ServiceWorkerRegistration* registration) 64 PushManager::PushManager(ServiceWorkerRegistration* registration)
47 : m_registration(registration) 65 : m_registration(registration)
48 { 66 {
49 ASSERT(registration); 67 ASSERT(registration);
50 } 68 }
51 69
52 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri ptionOptions& options) 70 WebPushSubscriptionOptions PushManager::toWebPushSubscriptionOptions(const PushS ubscriptionOptions& options, ExceptionState& exceptionState)
71 {
72 WebPushSubscriptionOptions webOptions;
73 webOptions.userVisibleOnly = options.userVisibleOnly();
74 if (options.hasApplicationServerKey()) {
75 webOptions.applicationServerKey = bufferSourceToString(options.applicati onServerKey(),
76 exceptionState);
77 }
78 return webOptions;
79 }
80
81 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri ptionOptions& options, ExceptionState& exceptionState)
53 { 82 {
54 if (!m_registration->active()) 83 if (!m_registration->active())
55 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Subscription failed - no active Service Worker")); 84 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Subscription failed - no active Service Worker"));
56 85
86 const WebPushSubscriptionOptions& webOptions = toWebPushSubscriptionOptions( options, exceptionState);
87 if (exceptionState.hadException())
88 return ScriptPromise();
89
57 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 90 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
58 ScriptPromise promise = resolver->promise(); 91 ScriptPromise promise = resolver->promise();
59 92
60 // The document context is the only reasonable context from which to ask the user for permission 93 // The document context is the only reasonable context from which to ask the user for permission
61 // to use the Push API. The embedder should persist the permission so that l ater calls in 94 // to use the Push API. The embedder should persist the permission so that l ater calls in
62 // different contexts can succeed. 95 // different contexts can succeed.
63 if (scriptState->executionContext()->isDocument()) { 96 if (scriptState->executionContext()->isDocument()) {
64 Document* document = toDocument(scriptState->executionContext()); 97 Document* document = toDocument(scriptState->executionContext());
65 if (!document->domWindow() || !document->frame()) 98 if (!document->domWindow() || !document->frame())
66 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); 99 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window."));
67 PushController::clientFrom(document->frame()).subscribe(m_registration-> webRegistration(), toWebPushSubscriptionOptions(options), new PushSubscriptionCa llbacks(resolver, m_registration)); 100 PushController::clientFrom(document->frame()).subscribe(m_registration-> webRegistration(), webOptions, new PushSubscriptionCallbacks(resolver, m_registr ation));
68 } else { 101 } else {
69 pushProvider()->subscribe(m_registration->webRegistration(), toWebPushSu bscriptionOptions(options), new PushSubscriptionCallbacks(resolver, m_registrati on)); 102 pushProvider()->subscribe(m_registration->webRegistration(), webOptions, new PushSubscriptionCallbacks(resolver, m_registration));
70 } 103 }
71 104
72 return promise; 105 return promise;
73 } 106 }
74 107
75 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) 108 ScriptPromise PushManager::getSubscription(ScriptState* scriptState)
76 { 109 {
77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 110 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
78 ScriptPromise promise = resolver->promise(); 111 ScriptPromise promise = resolver->promise();
79 112
80 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS ubscriptionCallbacks(resolver, m_registration)); 113 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS ubscriptionCallbacks(resolver, m_registration));
81 return promise; 114 return promise;
82 } 115 }
83 116
84 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS ubscriptionOptions& options) 117 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS ubscriptionOptions& options, ExceptionState& exceptionState)
85 { 118 {
86 if (scriptState->executionContext()->isDocument()) { 119 if (scriptState->executionContext()->isDocument()) {
87 Document* document = toDocument(scriptState->executionContext()); 120 Document* document = toDocument(scriptState->executionContext());
88 if (!document->domWindow() || !document->frame()) 121 if (!document->domWindow() || !document->frame())
89 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); 122 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window."));
90 } 123 }
91 124
92 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 125 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
93 ScriptPromise promise = resolver->promise(); 126 ScriptPromise promise = resolver->promise();
94 127
95 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb PushSubscriptionOptions(options), new PushPermissionStatusCallbacks(resolver)); 128 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb PushSubscriptionOptions(options, exceptionState), new PushPermissionStatusCallba cks(resolver));
96 return promise; 129 return promise;
97 } 130 }
98 131
99 DEFINE_TRACE(PushManager) 132 DEFINE_TRACE(PushManager)
100 { 133 {
101 visitor->trace(m_registration); 134 visitor->trace(m_registration);
102 } 135 }
103 136
104 } // namespace blink 137 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698