OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved. | |
4 * | 3 * |
5 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
7 * met: | 6 * met: |
8 * | 7 * |
9 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
11 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
12 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
13 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
(...skipping 14 matching lines...) Expand all Loading... |
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ | 29 */ |
31 | 30 |
32 #include "config.h" | 31 #include "config.h" |
33 #include "modules/notifications/Notification.h" | 32 #include "modules/notifications/Notification.h" |
34 | 33 |
35 #include "bindings/v8/Dictionary.h" | 34 #include "bindings/v8/Dictionary.h" |
36 #include "bindings/v8/ScriptWrappable.h" | 35 #include "bindings/v8/ScriptWrappable.h" |
37 #include "core/dom/Document.h" | 36 #include "core/dom/Document.h" |
38 #include "core/dom/ExecutionContext.h" | 37 #include "core/page/WindowFocusAllowedIndicator.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(context, title, client)); | 46 RefPtrWillBeRawPtr<Notification> notification = adoptRefWillBeRefCountedGarb
ageCollected(new Notification(title, context, 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(ExecutionContext* context, const String& title, Notif
icationClient* client) | 67 Notification::Notification(const String& title, ExecutionContext* context, Notif
icationClient* client) |
68 : NotificationBase(title, context, client) | 68 : ActiveDOMObject(context) |
| 69 , m_title(title) |
| 70 , m_dir("auto") |
| 71 , m_state(Idle) |
| 72 , m_client(client) |
69 , m_asyncRunner(adoptPtr(new AsyncMethodRunner<Notification>(this, &Notifica
tion::showSoon))) | 73 , m_asyncRunner(adoptPtr(new AsyncMethodRunner<Notification>(this, &Notifica
tion::showSoon))) |
70 { | 74 { |
| 75 ASSERT(m_client); |
71 ScriptWrappable::init(this); | 76 ScriptWrappable::init(this); |
72 | 77 |
73 m_asyncRunner->runAsync(); | 78 m_asyncRunner->runAsync(); |
74 } | 79 } |
75 | 80 |
76 Notification::~Notification() | 81 Notification::~Notification() |
77 { | 82 { |
78 } | 83 } |
79 | 84 |
| 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 |
80 const String& Notification::permission(ExecutionContext* context) | 162 const String& Notification::permission(ExecutionContext* context) |
81 { | 163 { |
82 ASSERT(toDocument(context)->page()); | 164 ASSERT(toDocument(context)->page()); |
83 return permissionString(NotificationController::from(toDocument(context)->pa
ge())->client()->checkPermission(context)); | 165 return permissionString(NotificationController::from(toDocument(context)->pa
ge())->client()->checkPermission(context)); |
84 } | 166 } |
85 | 167 |
86 void Notification::requestPermission(ExecutionContext* context, PassOwnPtr<Notif
icationPermissionCallback> callback) | 168 void Notification::requestPermission(ExecutionContext* context, PassOwnPtr<Notif
icationPermissionCallback> callback) |
87 { | 169 { |
88 ASSERT(toDocument(context)->page()); | 170 ASSERT(toDocument(context)->page()); |
89 NotificationController::from(toDocument(context)->page())->client()->request
Permission(context, callback); | 171 NotificationController::from(toDocument(context)->page())->client()->request
Permission(context, callback); |
90 } | 172 } |
91 | 173 |
| 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 |
92 const AtomicString& Notification::interfaceName() const | 183 const AtomicString& Notification::interfaceName() const |
93 { | 184 { |
94 return EventTargetNames::Notification; | 185 return EventTargetNames::Notification; |
95 } | 186 } |
96 | 187 |
97 void Notification::stop() | 188 void Notification::stop() |
98 { | 189 { |
99 NotificationBase::stop(); | 190 if (m_client) |
| 191 m_client->notificationObjectDestroyed(this); |
| 192 |
100 if (m_asyncRunner) | 193 if (m_asyncRunner) |
101 m_asyncRunner->stop(); | 194 m_asyncRunner->stop(); |
| 195 |
| 196 m_client = 0; |
| 197 m_state = Closed; |
102 } | 198 } |
103 | 199 |
104 bool Notification::hasPendingActivity() const | 200 bool Notification::hasPendingActivity() const |
105 { | 201 { |
106 return NotificationBase::hasPendingActivity() || (m_asyncRunner && m_asyncRu
nner->isActive()); | 202 return m_state == Showing || (m_asyncRunner && m_asyncRunner->isActive()); |
107 } | 203 } |
108 | 204 |
109 void Notification::showSoon() | 205 void Notification::showSoon() |
110 { | 206 { |
111 ASSERT(executionContext()->isDocument()); | 207 ASSERT(executionContext()->isDocument()); |
112 show(); | 208 show(); |
113 } | 209 } |
114 | 210 |
115 } // namespace WebCore | 211 } // namespace WebCore |
OLD | NEW |