 Chromium Code Reviews
 Chromium Code Reviews Issue 1701313002:
  Partial implementation of subscription restrictions.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1701313002:
  Partial implementation of subscription restrictions.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: third_party/WebKit/Source/modules/push_messaging/PushManager.cpp | 
| diff --git a/third_party/WebKit/Source/modules/push_messaging/PushManager.cpp b/third_party/WebKit/Source/modules/push_messaging/PushManager.cpp | 
| index 0f5a2ffd66ddd641444c5580ef3b4c3273ccc98d..40ea105fbeecc9c352379818cd8743de4cdd1882 100644 | 
| --- a/third_party/WebKit/Source/modules/push_messaging/PushManager.cpp | 
| +++ b/third_party/WebKit/Source/modules/push_messaging/PushManager.cpp | 
| @@ -34,13 +34,6 @@ WebPushProvider* pushProvider() | 
| return webPushProvider; | 
| } | 
| -WebPushSubscriptionOptions toWebPushSubscriptionOptions(const PushSubscriptionOptions& options) | 
| -{ | 
| - WebPushSubscriptionOptions webOptions; | 
| - webOptions.userVisibleOnly = options.userVisibleOnly(); | 
| - return webOptions; | 
| -} | 
| - | 
| } // namespace | 
| PushManager::PushManager(ServiceWorkerRegistration* registration) | 
| @@ -49,11 +42,54 @@ PushManager::PushManager(ServiceWorkerRegistration* registration) | 
| ASSERT(registration); | 
| } | 
| -ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscriptionOptions& options) | 
| +String PushManager::toString(const ArrayBufferOrArrayBufferView& applicationServerKey, | 
| + 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.
 | 
| +{ | 
| + // Check the validity of the sender info. It must be a 65 byte unencrypted key, | 
| + // which has the byte 0x04 as the first byte as a marker. | 
| + char* input; | 
| + int length; | 
| + if (applicationServerKey.isArrayBuffer()) { | 
| + input = static_cast<char*>( | 
| + applicationServerKey.getAsArrayBuffer()->data()); | 
| + length = applicationServerKey.getAsArrayBuffer()->byteLength(); | 
| + } else if (applicationServerKey.isArrayBufferView()) { | 
| + input = static_cast<char*>( | 
| + applicationServerKey.getAsArrayBufferView()->buffer()->data()); | 
| + length = applicationServerKey.getAsArrayBufferView()->buffer()->byteLength(); | 
| + } else { | 
| + ASSERT_NOT_REACHED(); | 
| + return String(); | 
| + } | 
| + | 
| + if (length == 65 && input[0] == 0x04) | 
| + return WebString::fromUTF8(input, length); | 
| + exceptionState.throwDOMException(InvalidAccessError, | 
| + "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.
 | 
| + return String(); | 
| +} | 
| + | 
| +WebPushSubscriptionOptions PushManager::toWebPushSubscriptionOptions( | 
| + 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.
 | 
| +{ | 
| + WebPushSubscriptionOptions webOptions; | 
| + webOptions.userVisibleOnly = options.userVisibleOnly(); | 
| + if (options.hasApplicationServerKey()) { | 
| + webOptions.applicationServerKey = PushManager::toString(options.applicationServerKey(), | 
| + exceptionState); | 
| + } | 
| + return webOptions; | 
| +} | 
| + | 
| +ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscriptionOptions& options, ExceptionState& exceptionState) | 
| { | 
| if (!m_registration->active()) | 
| return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Subscription failed - no active Service Worker")); | 
| + const WebPushSubscriptionOptions& webOptions = toWebPushSubscriptionOptions(options, exceptionState); | 
| + if (exceptionState.hadException()) | 
| + return ScriptPromise(); | 
| + | 
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 
| ScriptPromise promise = resolver->promise(); | 
| @@ -64,9 +100,9 @@ ScriptPromise PushManager::subscribe(ScriptState* scriptState, const PushSubscri | 
| Document* document = toDocument(scriptState->executionContext()); | 
| if (!document->domWindow() || !document->frame()) | 
| return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "Document is detached from window.")); | 
| - PushController::clientFrom(document->frame()).subscribe(m_registration->webRegistration(), toWebPushSubscriptionOptions(options), new PushSubscriptionCallbacks(resolver, m_registration)); | 
| + PushController::clientFrom(document->frame()).subscribe(m_registration->webRegistration(), webOptions, new PushSubscriptionCallbacks(resolver, m_registration)); | 
| } else { | 
| - pushProvider()->subscribe(m_registration->webRegistration(), toWebPushSubscriptionOptions(options), new PushSubscriptionCallbacks(resolver, m_registration)); | 
| + pushProvider()->subscribe(m_registration->webRegistration(), webOptions, new PushSubscriptionCallbacks(resolver, m_registration)); | 
| } | 
| return promise; | 
| @@ -81,7 +117,7 @@ ScriptPromise PushManager::getSubscription(ScriptState* scriptState) | 
| return promise; | 
| } | 
| -ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushSubscriptionOptions& options) | 
| +ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushSubscriptionOptions& options, ExceptionState& exceptionState) | 
| { | 
| if (scriptState->executionContext()->isDocument()) { | 
| Document* document = toDocument(scriptState->executionContext()); | 
| @@ -92,7 +128,7 @@ ScriptPromise PushManager::permissionState(ScriptState* scriptState, const PushS | 
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 
| ScriptPromise promise = resolver->promise(); | 
| - pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWebPushSubscriptionOptions(options), new PushPermissionStatusCallbacks(resolver)); | 
| + pushProvider()->getPermissionStatus(m_registration->webRegistration(), toWebPushSubscriptionOptions(options, exceptionState), new PushPermissionStatusCallbacks(resolver)); | 
| return promise; | 
| } |