• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 #include "NotificationPresenter.h"
33 
34 #include "WebKit.h"
35 #include "WebKitClient.h"
36 #include "WebNotification.h"
37 #include "WebNotificationPermissionCallback.h"
38 #include "WebSecurityOrigin.h"
39 #include "WebString.h"
40 #include "WebURL.h"
41 #include "googleurl/src/gurl.h"
42 #include <wtf/text/CString.h>
43 #include <wtf/text/WTFString.h>
44 
45 using namespace WebKit;
46 
identifierForNotification(const WebNotification & notification)47 static WebString identifierForNotification(const WebNotification& notification)
48 {
49     if (notification.isHTML())
50         return notification.url().spec().utf16();
51     return notification.title();
52 }
53 
deferredDisplayDispatch(void * context)54 static void deferredDisplayDispatch(void* context)
55 {
56     WebNotification* notification = static_cast<WebNotification*>(context);
57     notification->dispatchDisplayEvent();
58     delete notification;
59 }
60 
grantPermission(const WebString & origin)61 void NotificationPresenter::grantPermission(const WebString& origin)
62 {
63     // Make sure it's in the form of an origin.
64     GURL url(origin);
65     m_allowedOrigins.add(WTF::String(url.GetOrigin().spec().c_str()));
66 }
67 
simulateClick(const WebString & title)68 bool NotificationPresenter::simulateClick(const WebString& title)
69 {
70     WTF::String id(title.data(), title.length());
71     if (m_activeNotifications.find(id) == m_activeNotifications.end())
72         return false;
73 
74     const WebNotification& notification = m_activeNotifications.find(id)->second;
75     WebNotification eventTarget(notification);
76     eventTarget.dispatchClickEvent();
77     return true;
78 }
79 
80 // The output from all these methods matches what DumpRenderTree produces.
show(const WebNotification & notification)81 bool NotificationPresenter::show(const WebNotification& notification)
82 {
83     WebString identifier = identifierForNotification(notification);
84     if (!notification.replaceId().isEmpty()) {
85         WTF::String replaceId(notification.replaceId().data(), notification.replaceId().length());
86         if (m_replacements.find(replaceId) != m_replacements.end())
87             printf("REPLACING NOTIFICATION %s\n",
88                    m_replacements.find(replaceId)->second.utf8().data());
89 
90         m_replacements.set(replaceId, WTF::String(identifier.data(), identifier.length()));
91     }
92 
93     if (notification.isHTML()) {
94         printf("DESKTOP NOTIFICATION: contents at %s\n",
95                notification.url().spec().data());
96     } else {
97         printf("DESKTOP NOTIFICATION:%s icon %s, title %s, text %s\n",
98                notification.direction() == WebTextDirectionRightToLeft ? "(RTL)" : "",
99                notification.iconURL().isEmpty() ? "" :
100                notification.iconURL().spec().data(),
101                notification.title().isEmpty() ? "" :
102                notification.title().utf8().data(),
103                notification.body().isEmpty() ? "" :
104                notification.body().utf8().data());
105     }
106 
107     WTF::String id(identifier.data(), identifier.length());
108     m_activeNotifications.set(id, notification);
109 
110     webKitClient()->callOnMainThread(deferredDisplayDispatch, new WebNotification(notification));
111     return true;
112 }
113 
cancel(const WebNotification & notification)114 void NotificationPresenter::cancel(const WebNotification& notification)
115 {
116     WebString identifier = identifierForNotification(notification);
117     printf("DESKTOP NOTIFICATION CLOSED: %s\n", identifier.utf8().data());
118     WebNotification eventTarget(notification);
119     eventTarget.dispatchCloseEvent(false);
120 
121     WTF::String id(identifier.data(), identifier.length());
122     m_activeNotifications.remove(id);
123 }
124 
objectDestroyed(const WebKit::WebNotification & notification)125 void NotificationPresenter::objectDestroyed(const WebKit::WebNotification& notification)
126 {
127     WebString identifier = identifierForNotification(notification);
128     WTF::String id(identifier.data(), identifier.length());
129     m_activeNotifications.remove(id);
130 }
131 
checkPermission(const WebURL & url)132 WebNotificationPresenter::Permission NotificationPresenter::checkPermission(const WebURL& url)
133 {
134     // Check with the layout test controller
135     WTF::String origin = WTF::String(static_cast<GURL>(url).GetOrigin().spec().c_str());
136     bool allowed = m_allowedOrigins.find(origin) != m_allowedOrigins.end();
137     return allowed ? WebNotificationPresenter::PermissionAllowed
138         : WebNotificationPresenter::PermissionDenied;
139 }
140 
requestPermission(const WebSecurityOrigin & origin,WebNotificationPermissionCallback * callback)141 void NotificationPresenter::requestPermission(
142     const WebSecurityOrigin& origin,
143     WebNotificationPermissionCallback* callback)
144 {
145     printf("DESKTOP NOTIFICATION PERMISSION REQUESTED: %s\n",
146            origin.toString().utf8().data());
147     callback->permissionRequestComplete();
148 }
149