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

Unified Diff: Source/modules/notifications/NotificationData.cpp

Issue 1260793007: Generalize validation of developer input for Web Notifications (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: Source/modules/notifications/NotificationData.cpp
diff --git a/Source/modules/notifications/NotificationData.cpp b/Source/modules/notifications/NotificationData.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..783d4cf031e8f4a638b213f78365281110083928
--- /dev/null
+++ b/Source/modules/notifications/NotificationData.cpp
@@ -0,0 +1,88 @@
+// Copyright 2015 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 "config.h"
+#include "modules/notifications/NotificationData.h"
+
+#include "bindings/core/v8/ExceptionState.h"
+#include "bindings/core/v8/SerializedScriptValue.h"
+#include "bindings/core/v8/SerializedScriptValueFactory.h"
+#include "core/dom/ExecutionContext.h"
+#include "modules/notifications/Notification.h"
+#include "modules/notifications/NotificationOptions.h"
+#include "modules/vibration/NavigatorVibration.h"
+#include "platform/weborigin/KURL.h"
+
+namespace blink {
+
+WebNotificationData createWebNotificationData(ExecutionContext* executionContext, const String& title, const NotificationOptions& options, ExceptionState& exceptionState)
+{
+ // If silent is true, the notification must not have a vibration pattern.
+ if (options.hasVibrate() && options.silent()) {
+ exceptionState.throwTypeError("Silent notifications must not specify vibration patterns.");
+ return WebNotificationData();
+ }
+
+ WebNotificationData webData;
+
+ // TODO(peter): Require a title to be given for notifications?
+ webData.title = title;
+
+ // TODO(peter): WebNotificationData should be able to reflect "auto" as well.
+ webData.direction = options.dir() == "rtl" ? WebNotificationData::DirectionRightToLeft : WebNotificationData::DirectionLeftToRight;
+
+ webData.lang = options.lang();
+ webData.body = options.body();
+ webData.tag = options.tag();
+
+ KURL iconUrl;
+
+ // TODO(peter): Apply the appropriate CORS checks on the |iconUrl|.
+ if (options.hasIcon() && !options.icon().isEmpty()) {
+ iconUrl = executionContext->completeURL(options.icon());
+ if (!iconUrl.isValid())
+ iconUrl = KURL();
+ }
+
+ webData.icon = iconUrl;
+ webData.vibrate = NavigatorVibration::sanitizeVibrationPattern(options.vibrate());
+ webData.silent = options.silent();
+
+ if (options.hasData()) {
+ RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptValueFactory::instance().create(options.data().isolate(), options.data(), nullptr, exceptionState);
+ if (exceptionState.hadException())
+ return WebNotificationData();
+
+ Vector<char> serializedData;
+ serializedScriptValue->toWireBytes(serializedData);
+
+ webData.data = serializedData;
+ }
+
+ Vector<WebNotificationAction> actions;
+
+ const size_t maxActions = Notification::maxActions();
johnme 2015/08/03 15:41:36 Nit: seems more legible if you don't inline this.
Peter Beverloo 2015/08/03 17:29:30 Acknowledged.
+ for (const NotificationAction& action : options.actions()) {
+ if (action.title().isEmpty()) {
+ exceptionState.throwTypeError("Notification action titles must not be empty.");
+ return WebNotificationData();
+ }
+
+ // TODO(johnme): Ensure that |action.action()| isn't empty.
johnme 2015/08/03 15:41:36 https://codereview.chromium.org/1263043003 has bee
Peter Beverloo 2015/08/03 17:29:30 Done.
+
+ if (actions.size() >= maxActions)
+ continue;
+
+ WebNotificationAction webAction;
+ webAction.title = action.title();
+
+ actions.append(webAction);
+ }
+
+ webData.actions = actions;
+
+ return webData;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698