Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / notifications / Notification.h
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 #ifndef Notification_h
32 #define Notification_h
33
34 #include "bindings/v8/ScriptWrappable.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/events/EventTarget.h"
37 #include "modules/notifications/NotificationClient.h"
38 #include "platform/AsyncMethodRunner.h"
39 #include "platform/heap/Handle.h"
40 #include "platform/text/TextDirection.h"
41 #include "platform/weborigin/KURL.h"
42 #include "wtf/OwnPtr.h"
43 #include "wtf/PassRefPtr.h"
44 #include "wtf/RefCounted.h"
45
46 namespace WebCore {
47
48 class Dictionary;
49 class ExecutionContext;
50 class NotificationPermissionCallback;
51
52 class Notification : public RefCountedGarbageCollected<Notification>, public ScriptWrappable, public ActiveDOMObject, public EventTargetWithInlineData {
53     DEFINE_EVENT_TARGET_REFCOUNTING(RefCountedGarbageCollected<Notification>);
54
55 public:
56     static Notification* create(ExecutionContext*, const String& title, const Dictionary& options);
57
58     virtual ~Notification();
59
60     void close();
61
62     DEFINE_ATTRIBUTE_EVENT_LISTENER(click);
63     DEFINE_ATTRIBUTE_EVENT_LISTENER(show);
64     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
65     DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
66
67     void dispatchShowEvent();
68     void dispatchClickEvent();
69     void dispatchErrorEvent();
70     void dispatchCloseEvent();
71
72     String title() const { return m_title; }
73     String dir() const { return m_dir; }
74     String lang() const { return m_lang; }
75     String body() const { return m_body; }
76     String tag() const { return m_tag; }
77     String icon() const { return m_iconUrl; }
78
79     TextDirection direction() const;
80     KURL iconURL() const { return m_iconUrl; }
81
82     static const String& permissionString(NotificationClient::Permission);
83     static const String& permission(ExecutionContext*);
84     static void requestPermission(ExecutionContext*, PassOwnPtr<NotificationPermissionCallback> = nullptr);
85
86     // EventTarget interface.
87     virtual ExecutionContext* executionContext() const OVERRIDE FINAL { return ActiveDOMObject::executionContext(); }
88     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE FINAL;
89     virtual const AtomicString& interfaceName() const OVERRIDE;
90
91     // ActiveDOMObject interface.
92     virtual void stop() OVERRIDE;
93     virtual bool hasPendingActivity() const OVERRIDE;
94
95     void trace(Visitor*) { }
96
97 private:
98     Notification(const String& title, ExecutionContext*, NotificationClient*);
99
100     // Calling show() may start asynchronous operation. If this object has
101     // a V8 wrapper, hasPendingActivity() prevents the wrapper from being
102     // collected while m_state is Showing, and so this instance stays alive
103     // until the operation completes. Otherwise, you need to hold a ref on this
104     // instance until the operation completes.
105     void show();
106
107     void setDir(const String& dir) { m_dir = dir; }
108     void setLang(const String& lang) { m_lang = lang; }
109     void setBody(const String& body) { m_body = body; }
110     void setIconUrl(KURL iconUrl) { m_iconUrl = iconUrl; }
111     void setTag(const String& tag) { m_tag = tag; }
112
113 private:
114     String m_title;
115     String m_dir;
116     String m_lang;
117     String m_body;
118     String m_tag;
119
120     KURL m_iconUrl;
121
122     enum NotificationState {
123         Idle = 0,
124         Showing = 1,
125         Closed = 2,
126     };
127
128     NotificationState m_state;
129
130     NotificationClient* m_client;
131
132     OwnPtr<AsyncMethodRunner<Notification> > m_asyncRunner;
133 };
134
135 } // namespace WebCore
136
137 #endif // Notification_h