event.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #ifndef ATOM_BROWSER_API_EVENT_H_
  5. #define ATOM_BROWSER_API_EVENT_H_
  6. #include "base/optional.h"
  7. #include "content/public/browser/web_contents_observer.h"
  8. #include "electron/atom/common/api/api.mojom.h"
  9. #include "native_mate/handle.h"
  10. #include "native_mate/wrappable.h"
  11. namespace IPC {
  12. class Message;
  13. }
  14. namespace mate {
  15. class Event : public Wrappable<Event>, public content::WebContentsObserver {
  16. public:
  17. using MessageSyncCallback = atom::mojom::ElectronBrowser::MessageSyncCallback;
  18. static Handle<Event> Create(v8::Isolate* isolate);
  19. static void BuildPrototype(v8::Isolate* isolate,
  20. v8::Local<v8::FunctionTemplate> prototype);
  21. // Pass the sender and message to be replied.
  22. void SetSenderAndMessage(content::RenderFrameHost* sender,
  23. base::Optional<MessageSyncCallback> callback);
  24. // event.PreventDefault().
  25. void PreventDefault(v8::Isolate* isolate);
  26. // event.sendReply(array), used for replying synchronous message.
  27. bool SendReply(const base::ListValue& result);
  28. protected:
  29. explicit Event(v8::Isolate* isolate);
  30. ~Event() override;
  31. // content::WebContentsObserver implementations:
  32. void RenderFrameDeleted(content::RenderFrameHost* rfh) override;
  33. void RenderFrameHostChanged(content::RenderFrameHost* old_rfh,
  34. content::RenderFrameHost* new_rfh) override;
  35. void FrameDeleted(content::RenderFrameHost* rfh) override;
  36. private:
  37. // Replyer for the synchronous messages.
  38. content::RenderFrameHost* sender_ = nullptr;
  39. base::Optional<MessageSyncCallback> callback_;
  40. DISALLOW_COPY_AND_ASSIGN(Event);
  41. };
  42. } // namespace mate
  43. #endif // ATOM_BROWSER_API_EVENT_H_