summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/notifications/notification.h
blob: b417545770536f0ba682288d2603991948d294e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_NOTIFICATIONS_NOTIFICATION_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_NOTIFICATIONS_NOTIFICATION_H_

#include "third_party/blink/public/mojom/notifications/notification_service.mojom-blink.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/permissions/permission_status.mojom-blink-forward.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/core/dom/dom_time_stamp.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/vibration/navigator_vibration.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/timer.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"

namespace blink {

class ExecutionContext;
class NotificationOptions;
class NotificationResourcesLoader;
class ScriptState;
class V8NotificationPermissionCallback;
class TimestampTrigger;

class MODULES_EXPORT Notification final
    : public EventTargetWithInlineData,
      public ActiveScriptWrappable<Notification>,
      public ExecutionContextLifecycleObserver,
      public mojom::blink::NonPersistentNotificationListener {
  USING_GARBAGE_COLLECTED_MIXIN(Notification);
  DEFINE_WRAPPERTYPEINFO();

 public:
  // Used for JavaScript instantiations of non-persistent notifications. Will
  // automatically schedule for the notification to be displayed to the user
  // when the developer-provided data is valid.
  static Notification* Create(ExecutionContext* context,
                              const String& title,
                              const NotificationOptions* options,
                              ExceptionState& state);

  // Used for embedder-created persistent notifications. Initializes the state
  // of the notification as either Showing or Closed based on |showing|.
  static Notification* Create(ExecutionContext* context,
                              const String& notification_id,
                              mojom::blink::NotificationDataPtr data,
                              bool showing);

  // The type of notification this instance represents. Non-persistent
  // notifications will have events delivered to their instance, whereas
  // persistent notification will be using a Service Worker.
  enum class Type { kNonPersistent, kPersistent };

  Notification(ExecutionContext* context,
               Type type,
               mojom::blink::NotificationDataPtr data);
  ~Notification() override;

  void close();

  DEFINE_ATTRIBUTE_EVENT_LISTENER(click, kClick)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(show, kShow)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(error, kError)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(close, kClose)

  // NonPersistentNotificationListener interface.
  void OnShow() override;
  void OnClick(OnClickCallback completed_closure) override;
  void OnClose(OnCloseCallback completed_closure) override;

  String title() const;
  String dir() const;
  String lang() const;
  String body() const;
  String tag() const;
  String image() const;
  String icon() const;
  String badge() const;
  NavigatorVibration::VibrationPattern vibrate() const;
  DOMTimeStamp timestamp() const;
  bool renotify() const;
  bool silent() const;
  bool requireInteraction() const;
  ScriptValue data(ScriptState* script_state);
  Vector<v8::Local<v8::Value>> actions(ScriptState* script_state) const;
  TimestampTrigger* showTrigger() const { return show_trigger_; }

  static String PermissionString(mojom::blink::PermissionStatus permission);
  static String permission(ExecutionContext* context);
  static ScriptPromise requestPermission(
      ScriptState* script_state,
      V8NotificationPermissionCallback* deprecated_callback = nullptr);

  static uint32_t maxActions();

  // EventTarget interface.
  ExecutionContext* GetExecutionContext() const final {
    return ExecutionContextLifecycleObserver::GetExecutionContext();
  }
  const AtomicString& InterfaceName() const override;

  // ExecutionContextLifecycleObserver interface.
  void ContextDestroyed() override;

  // ScriptWrappable interface.
  bool HasPendingActivity() const final;

  void Trace(Visitor* visitor) override;

 protected:
  // EventTarget interface.
  DispatchEventResult DispatchEventInternal(Event& event) final;

 private:
  // The current phase of the notification in its lifecycle.
  enum class State { kLoading, kShowing, kClosing, kClosed };

  // Sets the state of the notification in its lifecycle.
  void SetState(State state) { state_ = state; }

  // Sets the notification ID to |notificationId|. This should be done once
  // the notification has shown for non-persistent notifications, and at
  // object initialisation time for persistent notifications.
  void SetNotificationId(const String& notification_id) {
    notification_id_ = notification_id;
  }

  // Sets the token which will be used to both show and close the notification.
  // Should be equal to tag_ if a tag is present, else should be unique.
  void SetToken(const String& token) { token_ = token; }

  // Schedules an asynchronous call to |prepareShow|, allowing the constructor
  // to return so that events can be fired on the notification object.
  void SchedulePrepareShow();

  // Verifies that permission has been granted, then asynchronously starts
  // loading the resources associated with this notification.
  void PrepareShow(TimerBase* timer);

  // Shows the notification through the embedder using the loaded resources.
  void DidLoadResources(NotificationResourcesLoader* loader);

  void DispatchErrorEvent();

  Type type_;
  State state_;

  mojom::blink::NotificationDataPtr data_;

  Member<TimestampTrigger> show_trigger_;

  String notification_id_;

  String token_;

  TaskRunnerTimer<Notification> prepare_show_timer_;

  Member<NotificationResourcesLoader> loader_;

  HeapMojoReceiver<mojom::blink::NonPersistentNotificationListener,
                   Notification>
      listener_receiver_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_NOTIFICATIONS_NOTIFICATION_H_