| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 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 "modules/notifications/NotificationBase.h" | |
| 33 | |
| 34 #include "core/dom/Document.h" | |
| 35 #include "core/page/WindowFocusAllowedIndicator.h" | |
| 36 #include "modules/notifications/NotificationController.h" | |
| 37 | |
| 38 namespace WebCore { | |
| 39 | |
| 40 NotificationBase::NotificationBase(const String& title, ExecutionContext* contex
t, NotificationClient* client) | |
| 41 : ActiveDOMObject(context) | |
| 42 , m_title(title) | |
| 43 , m_dir("auto") | |
| 44 , m_state(Idle) | |
| 45 , m_client(client) | |
| 46 { | |
| 47 ASSERT(m_client); | |
| 48 } | |
| 49 | |
| 50 NotificationBase::~NotificationBase() | |
| 51 { | |
| 52 } | |
| 53 | |
| 54 void NotificationBase::show() | |
| 55 { | |
| 56 // prevent double-showing | |
| 57 if (m_state == Idle) { | |
| 58 if (!toDocument(executionContext())->page()) | |
| 59 return; | |
| 60 if (NotificationController::from(toDocument(executionContext())->page())
->client()->checkPermission(executionContext()) != NotificationClient::Permissio
nAllowed) { | |
| 61 dispatchErrorEvent(); | |
| 62 return; | |
| 63 } | |
| 64 if (m_client->show(this)) { | |
| 65 m_state = Showing; | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void NotificationBase::close() | |
| 71 { | |
| 72 switch (m_state) { | |
| 73 case Idle: | |
| 74 break; | |
| 75 case Showing: | |
| 76 m_client->cancel(this); | |
| 77 break; | |
| 78 case Closed: | |
| 79 break; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void NotificationBase::dispatchShowEvent() | |
| 84 { | |
| 85 dispatchEvent(Event::create(EventTypeNames::show)); | |
| 86 } | |
| 87 | |
| 88 void NotificationBase::dispatchClickEvent() | |
| 89 { | |
| 90 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture); | |
| 91 WindowFocusAllowedIndicator windowFocusAllowed; | |
| 92 dispatchEvent(Event::create(EventTypeNames::click)); | |
| 93 } | |
| 94 | |
| 95 void NotificationBase::dispatchErrorEvent() | |
| 96 { | |
| 97 dispatchEvent(Event::create(EventTypeNames::error)); | |
| 98 } | |
| 99 | |
| 100 void NotificationBase::dispatchCloseEvent() | |
| 101 { | |
| 102 dispatchEvent(Event::create(EventTypeNames::close)); | |
| 103 m_state = Closed; | |
| 104 } | |
| 105 | |
| 106 TextDirection NotificationBase::direction() const | |
| 107 { | |
| 108 // FIXME: Resolve dir()=="auto" against the document. | |
| 109 return dir() == "rtl" ? RTL : LTR; | |
| 110 } | |
| 111 | |
| 112 const String& NotificationBase::permissionString(NotificationClient::Permission
permission) | |
| 113 { | |
| 114 DEFINE_STATIC_LOCAL(const String, allowedPermission, ("granted")); | |
| 115 DEFINE_STATIC_LOCAL(const String, deniedPermission, ("denied")); | |
| 116 DEFINE_STATIC_LOCAL(const String, defaultPermission, ("default")); | |
| 117 | |
| 118 switch (permission) { | |
| 119 case NotificationClient::PermissionAllowed: | |
| 120 return allowedPermission; | |
| 121 case NotificationClient::PermissionDenied: | |
| 122 return deniedPermission; | |
| 123 case NotificationClient::PermissionNotAllowed: | |
| 124 return defaultPermission; | |
| 125 } | |
| 126 | |
| 127 ASSERT_NOT_REACHED(); | |
| 128 return deniedPermission; | |
| 129 } | |
| 130 | |
| 131 bool NotificationBase::dispatchEvent(PassRefPtr<Event> event) | |
| 132 { | |
| 133 // Do not dispatch if the context is gone. | |
| 134 if (!executionContext()) | |
| 135 return false; | |
| 136 | |
| 137 return EventTarget::dispatchEvent(event); | |
| 138 } | |
| 139 | |
| 140 void NotificationBase::stop() | |
| 141 { | |
| 142 if (m_client) | |
| 143 m_client->notificationObjectDestroyed(this); | |
| 144 | |
| 145 m_client = 0; | |
| 146 m_state = Closed; | |
| 147 } | |
| 148 | |
| 149 bool NotificationBase::hasPendingActivity() const | |
| 150 { | |
| 151 return m_state == Showing; | |
| 152 } | |
| 153 | |
| 154 } // namespace WebCore | |
| OLD | NEW |