OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/shell/renderer/test_runner/NotificationPresenter.h" |
| 6 |
| 7 #include "content/shell/renderer/test_runner/WebTestDelegate.h" |
| 8 #include "third_party/WebKit/public/platform/Platform.h" |
| 9 #include "third_party/WebKit/public/platform/WebString.h" |
| 10 #include "third_party/WebKit/public/platform/WebURL.h" |
| 11 #include "third_party/WebKit/public/web/WebKit.h" |
| 12 #include "third_party/WebKit/public/web/WebNotificationPermissionCallback.h" |
| 13 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 using namespace blink; |
| 17 using namespace std; |
| 18 |
| 19 namespace WebTestRunner { |
| 20 |
| 21 namespace { |
| 22 |
| 23 WebString identifierForNotification(const WebNotification& notification) |
| 24 { |
| 25 if (notification.isHTML()) |
| 26 return notification.url().spec().utf16(); |
| 27 return notification.title(); |
| 28 } |
| 29 |
| 30 void deferredDisplayDispatch(void* context) |
| 31 { |
| 32 WebNotification* notification = static_cast<WebNotification*>(context); |
| 33 notification->dispatchDisplayEvent(); |
| 34 delete notification; |
| 35 } |
| 36 |
| 37 } |
| 38 |
| 39 NotificationPresenter::NotificationPresenter() |
| 40 : m_delegate(0) |
| 41 { |
| 42 } |
| 43 |
| 44 NotificationPresenter::~NotificationPresenter() |
| 45 { |
| 46 } |
| 47 |
| 48 void NotificationPresenter::grantPermission(const WebString& origin) |
| 49 { |
| 50 m_allowedOrigins.insert(origin.utf8()); |
| 51 } |
| 52 |
| 53 bool NotificationPresenter::simulateClick(const WebString& title) |
| 54 { |
| 55 string id(title.utf8()); |
| 56 if (m_activeNotifications.find(id) == m_activeNotifications.end()) |
| 57 return false; |
| 58 |
| 59 const WebNotification& notification = m_activeNotifications.find(id)->second
; |
| 60 WebNotification eventTarget(notification); |
| 61 eventTarget.dispatchClickEvent(); |
| 62 return true; |
| 63 } |
| 64 |
| 65 void NotificationPresenter::cancelAllActiveNotifications() |
| 66 { |
| 67 while (!m_activeNotifications.empty()) { |
| 68 const WebNotification& notification = m_activeNotifications.begin()->sec
ond; |
| 69 cancel(notification); |
| 70 } |
| 71 } |
| 72 |
| 73 // The output from all these methods matches what DumpRenderTree produces. |
| 74 bool NotificationPresenter::show(const WebNotification& notification) |
| 75 { |
| 76 WebString identifier = identifierForNotification(notification); |
| 77 if (!notification.replaceId().isEmpty()) { |
| 78 string replaceId(notification.replaceId().utf8()); |
| 79 if (m_replacements.find(replaceId) != m_replacements.end()) |
| 80 m_delegate->printMessage(string("REPLACING NOTIFICATION ") + m_repla
cements.find(replaceId)->second + "\n"); |
| 81 |
| 82 m_replacements[replaceId] = identifier.utf8(); |
| 83 } |
| 84 |
| 85 if (notification.isHTML()) |
| 86 m_delegate->printMessage(string("DESKTOP NOTIFICATION: contents at ") +
string(notification.url().spec()) + "\n"); |
| 87 else { |
| 88 m_delegate->printMessage("DESKTOP NOTIFICATION:"); |
| 89 m_delegate->printMessage(notification.direction() == WebTextDirectionRig
htToLeft ? "(RTL)" : ""); |
| 90 m_delegate->printMessage(" icon "); |
| 91 m_delegate->printMessage(notification.iconURL().isEmpty() ? "" : notific
ation.iconURL().spec().data()); |
| 92 m_delegate->printMessage(", title "); |
| 93 m_delegate->printMessage(notification.title().isEmpty() ? "" : notificat
ion.title().utf8().data()); |
| 94 m_delegate->printMessage(", text "); |
| 95 m_delegate->printMessage(notification.body().isEmpty() ? "" : notificati
on.body().utf8().data()); |
| 96 m_delegate->printMessage("\n"); |
| 97 } |
| 98 |
| 99 string id(identifier.utf8()); |
| 100 m_activeNotifications[id] = notification; |
| 101 |
| 102 Platform::current()->callOnMainThread(deferredDisplayDispatch, new WebNotifi
cation(notification)); |
| 103 return true; |
| 104 } |
| 105 |
| 106 void NotificationPresenter::cancel(const WebNotification& notification) |
| 107 { |
| 108 WebString identifier = identifierForNotification(notification); |
| 109 m_delegate->printMessage(string("DESKTOP NOTIFICATION CLOSED: ") + string(id
entifier.utf8()) + "\n"); |
| 110 WebNotification eventTarget(notification); |
| 111 eventTarget.dispatchCloseEvent(false); |
| 112 |
| 113 string id(identifier.utf8()); |
| 114 m_activeNotifications.erase(id); |
| 115 } |
| 116 |
| 117 void NotificationPresenter::objectDestroyed(const blink::WebNotification& notifi
cation) |
| 118 { |
| 119 WebString identifier = identifierForNotification(notification); |
| 120 string id(identifier.utf8()); |
| 121 m_activeNotifications.erase(id); |
| 122 } |
| 123 |
| 124 WebNotificationPresenter::Permission NotificationPresenter::checkPermission(cons
t WebSecurityOrigin& origin) |
| 125 { |
| 126 // Check with the layout test controller |
| 127 WebString originString = origin.toString(); |
| 128 bool allowed = m_allowedOrigins.find(string(originString.utf8())) != m_allow
edOrigins.end(); |
| 129 return allowed ? WebNotificationPresenter::PermissionAllowed |
| 130 : WebNotificationPresenter::PermissionDenied; |
| 131 } |
| 132 |
| 133 void NotificationPresenter::requestPermission( |
| 134 const WebSecurityOrigin& origin, |
| 135 WebNotificationPermissionCallback* callback) |
| 136 { |
| 137 m_delegate->printMessage("DESKTOP NOTIFICATION PERMISSION REQUESTED: " + str
ing(origin.toString().utf8()) + "\n"); |
| 138 callback->permissionRequestComplete(); |
| 139 } |
| 140 |
| 141 } |
OLD | NEW |