atom_api_notification.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/api/atom_api_notification.h"
  5. #include "atom/browser/api/atom_api_menu.h"
  6. #include "atom/browser/browser.h"
  7. #include "atom/common/native_mate_converters/gfx_converter.h"
  8. #include "atom/common/native_mate_converters/image_converter.h"
  9. #include "atom/common/native_mate_converters/string16_converter.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "brightray/browser/browser_client.h"
  12. #include "native_mate/constructor.h"
  13. #include "native_mate/dictionary.h"
  14. #include "native_mate/object_template_builder.h"
  15. #include "url/gurl.h"
  16. // Must be the last in the includes list.
  17. // See https://github.com/electron/electron/issues/10363
  18. #include "atom/common/node_includes.h"
  19. namespace mate {
  20. template <>
  21. struct Converter<brightray::NotificationAction> {
  22. static bool FromV8(v8::Isolate* isolate,
  23. v8::Local<v8::Value> val,
  24. brightray::NotificationAction* out) {
  25. mate::Dictionary dict;
  26. if (!ConvertFromV8(isolate, val, &dict))
  27. return false;
  28. if (!dict.Get("type", &(out->type))) {
  29. return false;
  30. }
  31. dict.Get("text", &(out->text));
  32. return true;
  33. }
  34. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  35. brightray::NotificationAction val) {
  36. mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
  37. dict.Set("text", val.text);
  38. dict.Set("type", val.type);
  39. return dict.GetHandle();
  40. }
  41. };
  42. } // namespace mate
  43. namespace atom {
  44. namespace api {
  45. Notification::Notification(v8::Isolate* isolate,
  46. v8::Local<v8::Object> wrapper,
  47. mate::Arguments* args) {
  48. InitWith(isolate, wrapper);
  49. presenter_ = brightray::BrowserClient::Get()->GetNotificationPresenter();
  50. mate::Dictionary opts;
  51. if (args->GetNext(&opts)) {
  52. opts.Get("title", &title_);
  53. opts.Get("subtitle", &subtitle_);
  54. opts.Get("body", &body_);
  55. has_icon_ = opts.Get("icon", &icon_);
  56. if (has_icon_) {
  57. opts.Get("icon", &icon_path_);
  58. }
  59. opts.Get("silent", &silent_);
  60. opts.Get("replyPlaceholder", &reply_placeholder_);
  61. opts.Get("hasReply", &has_reply_);
  62. opts.Get("actions", &actions_);
  63. opts.Get("sound", &sound_);
  64. opts.Get("closeButtonText", &close_button_text_);
  65. }
  66. }
  67. Notification::~Notification() {
  68. if (notification_)
  69. notification_->set_delegate(nullptr);
  70. }
  71. // static
  72. mate::WrappableBase* Notification::New(mate::Arguments* args) {
  73. if (!Browser::Get()->is_ready()) {
  74. args->ThrowError("Cannot create Notification before app is ready");
  75. return nullptr;
  76. }
  77. return new Notification(args->isolate(), args->GetThis(), args);
  78. }
  79. // Getters
  80. base::string16 Notification::GetTitle() const {
  81. return title_;
  82. }
  83. base::string16 Notification::GetSubtitle() const {
  84. return subtitle_;
  85. }
  86. base::string16 Notification::GetBody() const {
  87. return body_;
  88. }
  89. bool Notification::GetSilent() const {
  90. return silent_;
  91. }
  92. bool Notification::GetHasReply() const {
  93. return has_reply_;
  94. }
  95. base::string16 Notification::GetReplyPlaceholder() const {
  96. return reply_placeholder_;
  97. }
  98. base::string16 Notification::GetSound() const {
  99. return sound_;
  100. }
  101. std::vector<brightray::NotificationAction> Notification::GetActions() const {
  102. return actions_;
  103. }
  104. base::string16 Notification::GetCloseButtonText() const {
  105. return close_button_text_;
  106. }
  107. // Setters
  108. void Notification::SetTitle(const base::string16& new_title) {
  109. title_ = new_title;
  110. }
  111. void Notification::SetSubtitle(const base::string16& new_subtitle) {
  112. subtitle_ = new_subtitle;
  113. }
  114. void Notification::SetBody(const base::string16& new_body) {
  115. body_ = new_body;
  116. }
  117. void Notification::SetSilent(bool new_silent) {
  118. silent_ = new_silent;
  119. }
  120. void Notification::SetHasReply(bool new_has_reply) {
  121. has_reply_ = new_has_reply;
  122. }
  123. void Notification::SetReplyPlaceholder(const base::string16& new_placeholder) {
  124. reply_placeholder_ = new_placeholder;
  125. }
  126. void Notification::SetSound(const base::string16& new_sound) {
  127. sound_ = new_sound;
  128. }
  129. void Notification::SetActions(
  130. const std::vector<brightray::NotificationAction>& actions) {
  131. actions_ = actions;
  132. }
  133. void Notification::SetCloseButtonText(const base::string16& text) {
  134. close_button_text_ = text;
  135. }
  136. void Notification::NotificationAction(int index) {
  137. Emit("action", index);
  138. }
  139. void Notification::NotificationClick() {
  140. Emit("click");
  141. }
  142. void Notification::NotificationReplied(const std::string& reply) {
  143. Emit("reply", reply);
  144. }
  145. void Notification::NotificationDisplayed() {
  146. Emit("show");
  147. }
  148. void Notification::NotificationDestroyed() {}
  149. void Notification::NotificationClosed() {
  150. Emit("close");
  151. }
  152. void Notification::Close() {
  153. if (notification_) {
  154. notification_->Dismiss();
  155. notification_.reset();
  156. }
  157. }
  158. // Showing notifications
  159. void Notification::Show() {
  160. Close();
  161. if (presenter_) {
  162. notification_ = presenter_->CreateNotification(this);
  163. if (notification_) {
  164. brightray::NotificationOptions options;
  165. options.title = title_;
  166. options.subtitle = subtitle_;
  167. options.msg = body_;
  168. options.icon_url = GURL();
  169. options.icon = icon_.AsBitmap();
  170. options.silent = silent_;
  171. options.has_reply = has_reply_;
  172. options.reply_placeholder = reply_placeholder_;
  173. options.actions = actions_;
  174. options.sound = sound_;
  175. options.close_button_text = close_button_text_;
  176. notification_->Show(options);
  177. }
  178. }
  179. }
  180. bool Notification::IsSupported() {
  181. return !!brightray::BrowserClient::Get()->GetNotificationPresenter();
  182. }
  183. // static
  184. void Notification::BuildPrototype(v8::Isolate* isolate,
  185. v8::Local<v8::FunctionTemplate> prototype) {
  186. prototype->SetClassName(mate::StringToV8(isolate, "Notification"));
  187. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  188. .MakeDestroyable()
  189. .SetMethod("show", &Notification::Show)
  190. .SetMethod("close", &Notification::Close)
  191. .SetProperty("title", &Notification::GetTitle, &Notification::SetTitle)
  192. .SetProperty("subtitle", &Notification::GetSubtitle,
  193. &Notification::SetSubtitle)
  194. .SetProperty("body", &Notification::GetBody, &Notification::SetBody)
  195. .SetProperty("silent", &Notification::GetSilent, &Notification::SetSilent)
  196. .SetProperty("hasReply", &Notification::GetHasReply,
  197. &Notification::SetHasReply)
  198. .SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder,
  199. &Notification::SetReplyPlaceholder)
  200. .SetProperty("sound", &Notification::GetSound, &Notification::SetSound)
  201. .SetProperty("actions", &Notification::GetActions,
  202. &Notification::SetActions)
  203. .SetProperty("closeButtonText", &Notification::GetCloseButtonText,
  204. &Notification::SetCloseButtonText);
  205. }
  206. } // namespace api
  207. } // namespace atom
  208. namespace {
  209. using atom::api::Notification;
  210. void Initialize(v8::Local<v8::Object> exports,
  211. v8::Local<v8::Value> unused,
  212. v8::Local<v8::Context> context,
  213. void* priv) {
  214. v8::Isolate* isolate = context->GetIsolate();
  215. Notification::SetConstructor(isolate, base::Bind(&Notification::New));
  216. mate::Dictionary dict(isolate, exports);
  217. dict.Set("Notification",
  218. Notification::GetConstructor(isolate)->GetFunction());
  219. dict.SetMethod("isSupported", &Notification::IsSupported);
  220. }
  221. } // namespace
  222. NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_common_notification, Initialize)