OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved. |
3 * | 4 * |
4 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
6 * met: | 7 * met: |
7 * | 8 * |
8 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 11 * * Redistributions in binary form must reproduce the above |
11 * copyright notice, this list of conditions and the following disclaimer | 12 * copyright notice, this list of conditions and the following disclaimer |
12 * in the documentation and/or other materials provided with the | 13 * in the documentation and/or other materials provided with the |
(...skipping 14 matching lines...) Expand all Loading... |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 * (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 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 30 */ |
30 | 31 |
31 #include "config.h" | 32 #include "config.h" |
32 #include "modules/notifications/Notification.h" | 33 #include "modules/notifications/Notification.h" |
33 | 34 |
34 #include "bindings/v8/Dictionary.h" | 35 #include "bindings/v8/Dictionary.h" |
35 #include "bindings/v8/ScriptWrappable.h" | 36 #include "bindings/v8/ScriptWrappable.h" |
36 #include "core/dom/Document.h" | 37 #include "core/dom/Document.h" |
37 #include "core/page/WindowFocusAllowedIndicator.h" | 38 #include "core/dom/ExecutionContext.h" |
38 #include "modules/notifications/NotificationClient.h" | |
39 #include "modules/notifications/NotificationController.h" | 39 #include "modules/notifications/NotificationController.h" |
40 | 40 |
41 namespace WebCore { | 41 namespace WebCore { |
42 | 42 |
43 PassRefPtrWillBeRawPtr<Notification> Notification::create(ExecutionContext* cont
ext, const String& title, const Dictionary& options) | 43 PassRefPtrWillBeRawPtr<Notification> Notification::create(ExecutionContext* cont
ext, const String& title, const Dictionary& options) |
44 { | 44 { |
45 NotificationClient* client = NotificationController::clientFrom(toDocument(c
ontext)->page()); | 45 NotificationClient* client = NotificationController::clientFrom(toDocument(c
ontext)->page()); |
46 RefPtrWillBeRawPtr<Notification> notification = adoptRefWillBeRefCountedGarb
ageCollected(new Notification(title, context, client)); | 46 RefPtrWillBeRawPtr<Notification> notification = adoptRefWillBeRefCountedGarb
ageCollected(new Notification(context, title, client)); |
47 | 47 |
48 String argument; | 48 String argument; |
49 if (options.get("body", argument)) | 49 if (options.get("body", argument)) |
50 notification->setBody(argument); | 50 notification->setBody(argument); |
51 if (options.get("tag", argument)) | 51 if (options.get("tag", argument)) |
52 notification->setTag(argument); | 52 notification->setTag(argument); |
53 if (options.get("lang", argument)) | 53 if (options.get("lang", argument)) |
54 notification->setLang(argument); | 54 notification->setLang(argument); |
55 if (options.get("dir", argument)) | 55 if (options.get("dir", argument)) |
56 notification->setDir(argument); | 56 notification->setDir(argument); |
57 if (options.get("icon", argument)) { | 57 if (options.get("icon", argument)) { |
58 KURL iconUrl = argument.isEmpty() ? KURL() : context->completeURL(argume
nt); | 58 KURL iconUrl = argument.isEmpty() ? KURL() : context->completeURL(argume
nt); |
59 if (!iconUrl.isEmpty() && iconUrl.isValid()) | 59 if (!iconUrl.isEmpty() && iconUrl.isValid()) |
60 notification->setIconUrl(iconUrl); | 60 notification->setIconUrl(iconUrl); |
61 } | 61 } |
62 | 62 |
63 notification->suspendIfNeeded(); | 63 notification->suspendIfNeeded(); |
64 return notification.release(); | 64 return notification.release(); |
65 } | 65 } |
66 | 66 |
67 Notification::Notification(const String& title, ExecutionContext* context, Notif
icationClient* client) | 67 Notification::Notification(ExecutionContext* context, const String& title, Notif
icationClient* client) |
68 : ActiveDOMObject(context) | 68 : NotificationBase(title, context, client) |
69 , m_title(title) | |
70 , m_dir("auto") | |
71 , m_state(Idle) | |
72 , m_client(client) | |
73 , m_asyncRunner(adoptPtr(new AsyncMethodRunner<Notification>(this, &Notifica
tion::showSoon))) | 69 , m_asyncRunner(adoptPtr(new AsyncMethodRunner<Notification>(this, &Notifica
tion::showSoon))) |
74 { | 70 { |
75 ASSERT(m_client); | |
76 ScriptWrappable::init(this); | 71 ScriptWrappable::init(this); |
77 | 72 |
78 m_asyncRunner->runAsync(); | 73 m_asyncRunner->runAsync(); |
79 } | 74 } |
80 | 75 |
81 Notification::~Notification() | 76 Notification::~Notification() |
82 { | 77 { |
83 } | 78 } |
84 | 79 |
85 void Notification::show() | |
86 { | |
87 // prevent double-showing | |
88 if (m_state == Idle) { | |
89 if (!toDocument(executionContext())->page()) | |
90 return; | |
91 if (NotificationController::from(toDocument(executionContext())->page())
->client()->checkPermission(executionContext()) != NotificationClient::Permissio
nAllowed) { | |
92 dispatchErrorEvent(); | |
93 return; | |
94 } | |
95 if (m_client->show(this)) { | |
96 m_state = Showing; | |
97 } | |
98 } | |
99 } | |
100 | |
101 void Notification::close() | |
102 { | |
103 switch (m_state) { | |
104 case Idle: | |
105 break; | |
106 case Showing: | |
107 m_client->cancel(this); | |
108 break; | |
109 case Closed: | |
110 break; | |
111 } | |
112 } | |
113 | |
114 void Notification::dispatchShowEvent() | |
115 { | |
116 dispatchEvent(Event::create(EventTypeNames::show)); | |
117 } | |
118 | |
119 void Notification::dispatchClickEvent() | |
120 { | |
121 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture); | |
122 WindowFocusAllowedIndicator windowFocusAllowed; | |
123 dispatchEvent(Event::create(EventTypeNames::click)); | |
124 } | |
125 | |
126 void Notification::dispatchErrorEvent() | |
127 { | |
128 dispatchEvent(Event::create(EventTypeNames::error)); | |
129 } | |
130 | |
131 void Notification::dispatchCloseEvent() | |
132 { | |
133 dispatchEvent(Event::create(EventTypeNames::close)); | |
134 m_state = Closed; | |
135 } | |
136 | |
137 TextDirection Notification::direction() const | |
138 { | |
139 // FIXME: Resolve dir()=="auto" against the document. | |
140 return dir() == "rtl" ? RTL : LTR; | |
141 } | |
142 | |
143 const String& Notification::permissionString(NotificationClient::Permission perm
ission) | |
144 { | |
145 DEFINE_STATIC_LOCAL(const String, allowedPermission, ("granted")); | |
146 DEFINE_STATIC_LOCAL(const String, deniedPermission, ("denied")); | |
147 DEFINE_STATIC_LOCAL(const String, defaultPermission, ("default")); | |
148 | |
149 switch (permission) { | |
150 case NotificationClient::PermissionAllowed: | |
151 return allowedPermission; | |
152 case NotificationClient::PermissionDenied: | |
153 return deniedPermission; | |
154 case NotificationClient::PermissionNotAllowed: | |
155 return defaultPermission; | |
156 } | |
157 | |
158 ASSERT_NOT_REACHED(); | |
159 return deniedPermission; | |
160 } | |
161 | |
162 const String& Notification::permission(ExecutionContext* context) | 80 const String& Notification::permission(ExecutionContext* context) |
163 { | 81 { |
164 ASSERT(toDocument(context)->page()); | 82 ASSERT(toDocument(context)->page()); |
165 return permissionString(NotificationController::from(toDocument(context)->pa
ge())->client()->checkPermission(context)); | 83 return permissionString(NotificationController::from(toDocument(context)->pa
ge())->client()->checkPermission(context)); |
166 } | 84 } |
167 | 85 |
168 void Notification::requestPermission(ExecutionContext* context, PassOwnPtr<Notif
icationPermissionCallback> callback) | 86 void Notification::requestPermission(ExecutionContext* context, PassOwnPtr<Notif
icationPermissionCallback> callback) |
169 { | 87 { |
170 ASSERT(toDocument(context)->page()); | 88 ASSERT(toDocument(context)->page()); |
171 NotificationController::from(toDocument(context)->page())->client()->request
Permission(context, callback); | 89 NotificationController::from(toDocument(context)->page())->client()->request
Permission(context, callback); |
172 } | 90 } |
173 | 91 |
174 bool Notification::dispatchEvent(PassRefPtr<Event> event) | |
175 { | |
176 // Do not dispatch if the context is gone. | |
177 if (!executionContext()) | |
178 return false; | |
179 | |
180 return EventTarget::dispatchEvent(event); | |
181 } | |
182 | |
183 const AtomicString& Notification::interfaceName() const | 92 const AtomicString& Notification::interfaceName() const |
184 { | 93 { |
185 return EventTargetNames::Notification; | 94 return EventTargetNames::Notification; |
186 } | 95 } |
187 | 96 |
188 void Notification::stop() | 97 void Notification::stop() |
189 { | 98 { |
190 if (m_client) | 99 NotificationBase::stop(); |
191 m_client->notificationObjectDestroyed(this); | |
192 | |
193 if (m_asyncRunner) | 100 if (m_asyncRunner) |
194 m_asyncRunner->stop(); | 101 m_asyncRunner->stop(); |
195 | |
196 m_client = 0; | |
197 m_state = Closed; | |
198 } | 102 } |
199 | 103 |
200 bool Notification::hasPendingActivity() const | 104 bool Notification::hasPendingActivity() const |
201 { | 105 { |
202 return m_state == Showing || (m_asyncRunner && m_asyncRunner->isActive()); | 106 return NotificationBase::hasPendingActivity() || (m_asyncRunner && m_asyncRu
nner->isActive()); |
203 } | 107 } |
204 | 108 |
205 void Notification::showSoon() | 109 void Notification::showSoon() |
206 { | 110 { |
207 ASSERT(executionContext()->isDocument()); | 111 ASSERT(executionContext()->isDocument()); |
208 show(); | 112 show(); |
209 } | 113 } |
210 | 114 |
211 } // namespace WebCore | 115 } // namespace WebCore |
OLD | NEW |