notification.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_NOTIFICATIONS_NOTIFICATION_H_
  31. #define THIRD_PARTY_BLINK_RENDERER_MODULES_NOTIFICATIONS_NOTIFICATION_H_
  32. #include "third_party/blink/public/mojom/notifications/notification_service.mojom-blink.h"
  33. #include "third_party/blink/public/mojom/permissions/permission.mojom-blink-forward.h"
  34. #include "third_party/blink/public/mojom/permissions/permission_status.mojom-blink-forward.h"
  35. #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
  36. #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
  37. #include "third_party/blink/renderer/bindings/core/v8/script_value.h"
  38. #include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
  39. #include "third_party/blink/renderer/core/dom/dom_time_stamp.h"
  40. #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
  41. #include "third_party/blink/renderer/modules/event_target_modules.h"
  42. #include "third_party/blink/renderer/modules/modules_export.h"
  43. #include "third_party/blink/renderer/modules/vibration/navigator_vibration.h"
  44. #include "third_party/blink/renderer/platform/heap/handle.h"
  45. #include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
  46. #include "third_party/blink/renderer/platform/timer.h"
  47. #include "third_party/blink/renderer/platform/weborigin/kurl.h"
  48. namespace blink {
  49. class ExecutionContext;
  50. class NotificationOptions;
  51. class NotificationResourcesLoader;
  52. class ScriptState;
  53. class V8NotificationPermissionCallback;
  54. class TimestampTrigger;
  55. class MODULES_EXPORT Notification final
  56. : public EventTargetWithInlineData,
  57. public ActiveScriptWrappable<Notification>,
  58. public ExecutionContextLifecycleObserver,
  59. public mojom::blink::NonPersistentNotificationListener {
  60. DEFINE_WRAPPERTYPEINFO();
  61. public:
  62. // Used for JavaScript instantiations of non-persistent notifications. Will
  63. // automatically schedule for the notification to be displayed to the user
  64. // when the developer-provided data is valid.
  65. static Notification* Create(ExecutionContext* context,
  66. const String& title,
  67. const NotificationOptions* options,
  68. ExceptionState& state);
  69. // Used for embedder-created persistent notifications. Initializes the state
  70. // of the notification as either Showing or Closed based on |showing|.
  71. static Notification* Create(ExecutionContext* context,
  72. const String& notification_id,
  73. mojom::blink::NotificationDataPtr data,
  74. bool showing);
  75. // The type of notification this instance represents. Non-persistent
  76. // notifications will have events delivered to their instance, whereas
  77. // persistent notification will be using a Service Worker.
  78. enum class Type { kNonPersistent, kPersistent };
  79. Notification(ExecutionContext* context,
  80. Type type,
  81. mojom::blink::NotificationDataPtr data);
  82. ~Notification() override;
  83. void close();
  84. DEFINE_ATTRIBUTE_EVENT_LISTENER(click, kClick)
  85. DEFINE_ATTRIBUTE_EVENT_LISTENER(show, kShow)
  86. DEFINE_ATTRIBUTE_EVENT_LISTENER(error, kError)
  87. DEFINE_ATTRIBUTE_EVENT_LISTENER(close, kClose)
  88. // NonPersistentNotificationListener interface.
  89. void OnShow() override;
  90. void OnClick(OnClickCallback completed_closure) override;
  91. void OnClose(OnCloseCallback completed_closure) override;
  92. String title() const;
  93. String dir() const;
  94. String lang() const;
  95. String body() const;
  96. String tag() const;
  97. String image() const;
  98. String icon() const;
  99. String badge() const;
  100. NavigatorVibration::VibrationPattern vibrate() const;
  101. DOMTimeStamp timestamp() const;
  102. bool renotify() const;
  103. bool silent() const;
  104. bool requireInteraction() const;
  105. ScriptValue data(ScriptState* script_state);
  106. Vector<v8::Local<v8::Value>> actions(ScriptState* script_state) const;
  107. TimestampTrigger* showTrigger() const { return show_trigger_; }
  108. static String PermissionString(mojom::blink::PermissionStatus permission);
  109. static String permission(ExecutionContext* context);
  110. static ScriptPromise requestPermission(
  111. ScriptState* script_state,
  112. V8NotificationPermissionCallback* deprecated_callback = nullptr);
  113. static uint32_t maxActions();
  114. // EventTarget interface.
  115. ExecutionContext* GetExecutionContext() const final {
  116. return ExecutionContextLifecycleObserver::GetExecutionContext();
  117. }
  118. const AtomicString& InterfaceName() const override;
  119. // ExecutionContextLifecycleObserver interface.
  120. void ContextDestroyed() override;
  121. // ScriptWrappable interface.
  122. bool HasPendingActivity() const final;
  123. void Trace(Visitor* visitor) const override;
  124. protected:
  125. // EventTarget interface.
  126. DispatchEventResult DispatchEventInternal(Event& event) final;
  127. private:
  128. // The current phase of the notification in its lifecycle.
  129. enum class State { kLoading, kShowing, kClosing, kClosed };
  130. // Sets the state of the notification in its lifecycle.
  131. void SetState(State state) { state_ = state; }
  132. // Sets the notification ID to |notificationId|. This should be done once
  133. // the notification has shown for non-persistent notifications, and at
  134. // object initialisation time for persistent notifications.
  135. void SetNotificationId(const String& notification_id) {
  136. notification_id_ = notification_id;
  137. }
  138. // Sets the token which will be used to both show and close the notification.
  139. // Should be equal to tag_ if a tag is present, else should be unique.
  140. void SetToken(const String& token) { token_ = token; }
  141. // Schedules an asynchronous call to |prepareShow|, allowing the constructor
  142. // to return so that events can be fired on the notification object.
  143. void SchedulePrepareShow();
  144. // Verifies that permission has been granted, then asynchronously starts
  145. // loading the resources associated with this notification.
  146. void PrepareShow(TimerBase* timer);
  147. // Shows the notification through the embedder using the loaded resources.
  148. void DidLoadResources(NotificationResourcesLoader* loader);
  149. void DispatchErrorEvent();
  150. Type type_;
  151. State state_;
  152. mojom::blink::NotificationDataPtr data_;
  153. Member<TimestampTrigger> show_trigger_;
  154. String notification_id_;
  155. String token_;
  156. TaskRunnerTimer<Notification> prepare_show_timer_;
  157. Member<NotificationResourcesLoader> loader_;
  158. HeapMojoReceiver<mojom::blink::NonPersistentNotificationListener,
  159. Notification>
  160. listener_receiver_;
  161. };
  162. } // namespace blink
  163. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_NOTIFICATIONS_NOTIFICATION_H_