ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptState* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const String& title, const NotificationOptions& options, ExceptionState& exceptionState) { ExecutionContext* executionContext = scriptState->executionContext(); // If context object's active worker is null, reject promise with a TypeError exception. if (!serviceWorkerRegistration.active()) return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "No active registration available on the ServiceWorkerRegistration.")); // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps. if (Notification::checkPermission(executionContext) != WebNotificationPermissionAllowed) return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "No notification permission has been granted for this origin.")); if (options.hasVibrate() && options.silent()) return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "Silent notifications must not specify vibration patterns.")); // FIXME: Unify the code path here with the Notification.create() function. Vector<char> dataAsWireBytes; if (options.hasData()) { RefPtr<SerializedScriptValue> data = SerializedScriptValueFactory::instance().create(options.data().isolate(), options.data(), nullptr, exceptionState); if (exceptionState.hadException()) return exceptionState.reject(scriptState); data->toWireBytes(dataAsWireBytes); } RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); ScriptPromise promise = resolver->promise(); // FIXME: Do the appropriate CORS checks on the icon URL. KURL iconUrl; if (options.hasIcon() && !options.icon().isEmpty()) { iconUrl = executionContext->completeURL(options.icon()); if (!iconUrl.isValid()) iconUrl = KURL(); } WebNotificationData::Direction dir = options.dir() == "rtl" ? WebNotificationData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight; NavigatorVibration::VibrationPattern vibrate = NavigatorVibration::sanitizeVibrationPattern(options.vibrate()); WebNotificationData notification(title, dir, options.lang(), options.body(), options.tag(), iconUrl, vibrate, options.silent(), dataAsWireBytes); WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, void>(resolver); SecurityOrigin* origin = executionContext->securityOrigin(); ASSERT(origin); WebNotificationManager* notificationManager = Platform::current()->notificationManager(); ASSERT(notificationManager); notificationManager->showPersistent(WebSerializedOrigin(*origin), notification, serviceWorkerRegistration.webRegistration(), callbacks); return promise; }
ScriptPromise ServiceWorkerRegistrationNotifications::showNotification(ScriptState* scriptState, ServiceWorkerRegistration& serviceWorkerRegistration, const String& title, const NotificationOptions& options) { ExecutionContext* executionContext = scriptState->executionContext(); // If context object's active worker is null, reject promise with a TypeError exception. if (!serviceWorkerRegistration.active()) return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "No active registration available on the ServiceWorkerRegistration.")); // If permission for notification's origin is not "granted", reject promise with a TypeError exception, and terminate these substeps. if (Notification::checkPermission(executionContext) != WebNotificationPermissionAllowed) return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "No notification permission has been granted for this origin.")); RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); ScriptPromise promise = resolver->promise(); // FIXME: Do the appropriate CORS checks on the icon URL. // FIXME: Determine the text direction based on the options dictionary. KURL iconUrl; if (options.hasIcon() && !options.icon().isEmpty()) { iconUrl = executionContext->completeURL(options.icon()); if (!iconUrl.isValid()) iconUrl = KURL(); } WebNotificationData notification(title, WebNotificationData::DirectionLeftToRight, options.lang(), options.body(), options.tag(), iconUrl); WebNotificationShowCallbacks* callbacks = new CallbackPromiseAdapter<void, void>(resolver); SecurityOrigin* origin = executionContext->securityOrigin(); ASSERT(origin); WebNotificationManager* notificationManager = Platform::current()->notificationManager(); ASSERT(notificationManager); notificationManager->showPersistent(WebSerializedOrigin(*origin), notification, serviceWorkerRegistration.webRegistration(), callbacks); return promise; }