1 | /* |
2 | * Copyright (C) 2009 Google Inc. All rights reserved. |
3 | * Copyright (C) 2009, 2011, 2012, 2016 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * * Neither the name of Google Inc. nor the names of its |
16 | * contributors may be used to endorse or promote products derived from |
17 | * this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (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. |
30 | */ |
31 | |
32 | #pragma once |
33 | |
34 | #if ENABLE(NOTIFICATIONS) |
35 | |
36 | #include "ActiveDOMObject.h" |
37 | #include "EventTarget.h" |
38 | #include "NotificationDirection.h" |
39 | #include "NotificationPermission.h" |
40 | #include "Timer.h" |
41 | #include <wtf/URL.h> |
42 | #include "WritingMode.h" |
43 | |
44 | namespace WebCore { |
45 | |
46 | class Document; |
47 | class NotificationPermissionCallback; |
48 | |
49 | class Notification final : public RefCounted<Notification>, public ActiveDOMObject, public EventTargetWithInlineData { |
50 | WTF_MAKE_ISO_ALLOCATED(Notification); |
51 | public: |
52 | using Permission = NotificationPermission; |
53 | using Direction = NotificationDirection; |
54 | |
55 | struct Options { |
56 | Direction dir; |
57 | String lang; |
58 | String body; |
59 | String tag; |
60 | String icon; |
61 | }; |
62 | static Ref<Notification> create(Document&, const String& title, const Options&); |
63 | |
64 | virtual ~Notification(); |
65 | |
66 | void show(); |
67 | void close(); |
68 | |
69 | const String& title() const { return m_title; } |
70 | Direction dir() const { return m_direction; } |
71 | const String& body() const { return m_body; } |
72 | const String& lang() const { return m_lang; } |
73 | const String& tag() const { return m_tag; } |
74 | const URL& icon() const { return m_icon; } |
75 | |
76 | TextDirection direction() const { return m_direction == Direction::Rtl ? TextDirection::RTL : TextDirection::LTR; } |
77 | |
78 | WEBCORE_EXPORT void dispatchClickEvent(); |
79 | WEBCORE_EXPORT void dispatchCloseEvent(); |
80 | WEBCORE_EXPORT void dispatchErrorEvent(); |
81 | WEBCORE_EXPORT void dispatchShowEvent(); |
82 | |
83 | WEBCORE_EXPORT void finalize(); |
84 | |
85 | static Permission permission(Document&); |
86 | static void requestPermission(Document&, RefPtr<NotificationPermissionCallback>&&); |
87 | |
88 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
89 | |
90 | using RefCounted::ref; |
91 | using RefCounted::deref; |
92 | |
93 | private: |
94 | Notification(Document&, const String& title, const Options&); |
95 | |
96 | EventTargetInterface eventTargetInterface() const final { return NotificationEventTargetInterfaceType; } |
97 | |
98 | // ActiveDOMObject |
99 | const char* activeDOMObjectName() const final; |
100 | bool canSuspendForDocumentSuspension() const final; |
101 | void stop() final; |
102 | |
103 | void refEventTarget() final { ref(); } |
104 | void derefEventTarget() final { deref(); } |
105 | |
106 | String m_title; |
107 | Direction m_direction; |
108 | String m_lang; |
109 | String m_body; |
110 | String m_tag; |
111 | URL m_icon; |
112 | |
113 | enum State { Idle, Showing, Closed }; |
114 | State m_state { Idle }; |
115 | |
116 | std::unique_ptr<Timer> m_taskTimer; |
117 | }; |
118 | |
119 | } // namespace WebCore |
120 | |
121 | #endif // ENABLE(NOTIFICATIONS) |
122 | |