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

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: Added exception failure return and updated more tests 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)
38 {
39 WebPushSubscriptionOptions webOptions;
40 webOptions.userVisibleOnly = options.userVisibleOnly();
41 return webOptions;
42 }
43
44 } // namespace 37 } // namespace
45 38
46 PushManager::PushManager(ServiceWorkerRegistration* registration) 39 PushManager::PushManager(ServiceWorkerRegistration* registration)
47 : m_registration(registration) 40 : m_registration(registration)
48 { 41 {
49 ASSERT(registration); 42 ASSERT(registration);
50 } 43 }
51 44
52 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri ptionOptions& options) 45 String PushManager::toString(const ArrayBufferOrArrayBufferView& applicationServ erKey,
46 ExceptionState& exceptionState)
Peter Beverloo 2016/02/26 16:02:33 nit: align this argument with the first one
harkness 2016/02/26 17:12:04 Done.
47 {
48 // Check the validity of the sender info. It must be a 65 byte unencrypted k ey,
49 // which has the byte 0x04 as the first byte as a marker.
50 char* input;
51 int length;
52 if (applicationServerKey.isArrayBuffer()) {
53 input = static_cast<char*>(
54 applicationServerKey.getAsArrayBuffer()->data());
55 length = applicationServerKey.getAsArrayBuffer()->byteLength();
56 } else if (applicationServerKey.isArrayBufferView()) {
57 input = static_cast<char*>(
58 applicationServerKey.getAsArrayBufferView()->buffer()->data());
59 length = applicationServerKey.getAsArrayBufferView()->buffer()->byteLeng th();
60 } else {
61 ASSERT_NOT_REACHED();
62 return String();
63 }
64
65 if (length == 65 && input[0] == 0x04)
66 return WebString::fromUTF8(input, length);
67 exceptionState.throwDOMException(InvalidAccessError,
68 "The provided applicationServerKey is not valid.");
Peter Beverloo 2016/02/26 16:02:33 micro nit: newline not necessary
harkness 2016/02/26 17:12:04 Done.
69 return String();
70 }
71
72 WebPushSubscriptionOptions PushManager::toWebPushSubscriptionOptions(
73 const PushSubscriptionOptions& options, ExceptionState& exceptionState)
Peter Beverloo 2016/02/26 16:02:33 nit: arguments on different lines, or don't line b
harkness 2016/02/26 17:12:04 Done.
74 {
75 WebPushSubscriptionOptions webOptions;
76 webOptions.userVisibleOnly = options.userVisibleOnly();
77 if (options.hasApplicationServerKey()) {
78 webOptions.applicationServerKey = PushManager::toString(options.applicat ionServerKey(),
79 exceptionState);
80 }
81 return webOptions;
82 }
83
84 ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri ptionOptions& options, ExceptionState& exceptionState)
53 { 85 {
54 if (!m_registration->active()) 86 if (!m_registration->active())
55 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Subscription failed - no active Service Worker")); 87 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Subscription failed - no active Service Worker"));
56 88
89 const WebPushSubscriptionOptions& webOptions = toWebPushSubscriptionOptions( options, exceptionState);
90 if (exceptionState.hadException())
91 return ScriptPromise();
92
57 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 93 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
58 ScriptPromise promise = resolver->promise(); 94 ScriptPromise promise = resolver->promise();
59 95
60 // The document context is the only reasonable context from which to ask the user for permission 96 // 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 97 // to use the Push API. The embedder should persist the permission so that l ater calls in
62 // different contexts can succeed. 98 // different contexts can succeed.
63 if (scriptState->executionContext()->isDocument()) { 99 if (scriptState->executionContext()->isDocument()) {
64 Document* document = toDocument(scriptState->executionContext()); 100 Document* document = toDocument(scriptState->executionContext());
65 if (!document->domWindow() || !document->frame()) 101 if (!document->domWindow() || !document->frame())
66 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); 102 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)); 103 PushController::clientFrom(document->frame()).subscribe(m_registration-> webRegistration(), webOptions, new PushSubscriptionCallbacks(resolver, m_registr ation));
68 } else { 104 } else {
69 pushProvider()->subscribe(m_registration->webRegistration(), toWebPushSu bscriptionOptions(options), new PushSubscriptionCallbacks(resolver, m_registrati on)); 105 pushProvider()->subscribe(m_registration->webRegistration(), webOptions, new PushSubscriptionCallbacks(resolver, m_registration));
70 } 106 }
71 107
72 return promise; 108 return promise;
73 } 109 }
74 110
75 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) 111 ScriptPromise PushManager::getSubscription(ScriptState* scriptState)
76 { 112 {
77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 113 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
78 ScriptPromise promise = resolver->promise(); 114 ScriptPromise promise = resolver->promise();
79 115
80 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS ubscriptionCallbacks(resolver, m_registration)); 116 pushProvider()->getSubscription(m_registration->webRegistration(), new PushS ubscriptionCallbacks(resolver, m_registration));
81 return promise; 117 return promise;
82 } 118 }
83 119
84 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS ubscriptionOptions& options) 120 ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS ubscriptionOptions& options, ExceptionState& exceptionState)
85 { 121 {
86 if (scriptState->executionContext()->isDocument()) { 122 if (scriptState->executionContext()->isDocument()) {
87 Document* document = toDocument(scriptState->executionContext()); 123 Document* document = toDocument(scriptState->executionContext());
88 if (!document->domWindow() || !document->frame()) 124 if (!document->domWindow() || !document->frame())
89 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); 125 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window."));
90 } 126 }
91 127
92 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 128 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
93 ScriptPromise promise = resolver->promise(); 129 ScriptPromise promise = resolver->promise();
94 130
95 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb PushSubscriptionOptions(options), new PushPermissionStatusCallbacks(resolver)); 131 pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWeb PushSubscriptionOptions(options, exceptionState), new PushPermissionStatusCallba cks(resolver));
96 return promise; 132 return promise;
97 } 133 }
98 134
99 DEFINE_TRACE(PushManager) 135 DEFINE_TRACE(PushManager)
100 { 136 {
101 visitor->trace(m_registration); 137 visitor->trace(m_registration);
102 } 138 }
103 139
104 } // namespace blink 140 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698