From c76b0b70c14e7f2b9da7fdde521afaf5f9d36f84 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 1 Aug 2019 00:20:28 +0200 Subject: [PATCH] refactor: remove content::WebContentsObserver from Event (#19251) --- shell/browser/api/event.cc | 32 ++---------------------------- shell/browser/api/event.h | 15 +++----------- shell/browser/api/event_emitter.cc | 2 +- 3 files changed, 6 insertions(+), 43 deletions(-) diff --git a/shell/browser/api/event.cc b/shell/browser/api/event.cc index b13b49b1d2e6..fdc76d8cc7a5 100644 --- a/shell/browser/api/event.cc +++ b/shell/browser/api/event.cc @@ -6,10 +6,7 @@ #include -#include "content/public/browser/render_frame_host.h" -#include "content/public/browser/web_contents.h" #include "native_mate/object_template_builder.h" -#include "shell/common/native_mate_converters/string16_converter.h" #include "shell/common/native_mate_converters/value_converter.h" namespace mate { @@ -20,34 +17,9 @@ Event::Event(v8::Isolate* isolate) { Event::~Event() {} -void Event::SetSenderAndMessage(content::RenderFrameHost* sender, - base::Optional callback) { - DCHECK(!sender_); +void Event::SetCallback(base::Optional callback) { DCHECK(!callback_); - sender_ = sender; callback_ = std::move(callback); - - Observe(content::WebContents::FromRenderFrameHost(sender)); -} - -void Event::RenderFrameDeleted(content::RenderFrameHost* rfh) { - if (sender_ != rfh) - return; - sender_ = nullptr; - callback_.reset(); -} - -void Event::RenderFrameHostChanged(content::RenderFrameHost* old_rfh, - content::RenderFrameHost* new_rfh) { - if (sender_ && sender_ == old_rfh) - sender_ = new_rfh; -} - -void Event::FrameDeleted(content::RenderFrameHost* rfh) { - if (sender_ != rfh) - return; - sender_ = nullptr; - callback_.reset(); } void Event::PreventDefault(v8::Isolate* isolate) { @@ -58,7 +30,7 @@ void Event::PreventDefault(v8::Isolate* isolate) { } bool Event::SendReply(const base::Value& result) { - if (!callback_ || sender_ == nullptr) + if (!callback_) return false; std::move(*callback_).Run(result.Clone()); diff --git a/shell/browser/api/event.h b/shell/browser/api/event.h index 9ae2a87c26cb..236e1e0cc1c0 100644 --- a/shell/browser/api/event.h +++ b/shell/browser/api/event.h @@ -6,7 +6,6 @@ #define SHELL_BROWSER_API_EVENT_H_ #include "base/optional.h" -#include "content/public/browser/web_contents_observer.h" #include "electron/shell/common/api/api.mojom.h" #include "native_mate/handle.h" #include "native_mate/wrappable.h" @@ -17,7 +16,7 @@ class Message; namespace mate { -class Event : public Wrappable, public content::WebContentsObserver { +class Event : public Wrappable { public: using MessageSyncCallback = electron::mojom::ElectronBrowser::MessageSyncCallback; @@ -26,9 +25,8 @@ class Event : public Wrappable, public content::WebContentsObserver { static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); - // Pass the sender and message to be replied. - void SetSenderAndMessage(content::RenderFrameHost* sender, - base::Optional callback); + // Pass the callback to be invoked. + void SetCallback(base::Optional callback); // event.PreventDefault(). void PreventDefault(v8::Isolate* isolate); @@ -41,15 +39,8 @@ class Event : public Wrappable, public content::WebContentsObserver { explicit Event(v8::Isolate* isolate); ~Event() override; - // content::WebContentsObserver implementations: - void RenderFrameDeleted(content::RenderFrameHost* rfh) override; - void RenderFrameHostChanged(content::RenderFrameHost* old_rfh, - content::RenderFrameHost* new_rfh) override; - void FrameDeleted(content::RenderFrameHost* rfh) override; - private: // Replyer for the synchronous messages. - content::RenderFrameHost* sender_ = nullptr; base::Optional callback_; DISALLOW_COPY_AND_ASSIGN(Event); diff --git a/shell/browser/api/event_emitter.cc b/shell/browser/api/event_emitter.cc index 50d3914fbbb2..e304143bdacb 100644 --- a/shell/browser/api/event_emitter.cc +++ b/shell/browser/api/event_emitter.cc @@ -55,7 +55,7 @@ v8::Local CreateJSEvent( if (use_native_event) { mate::Handle native_event = mate::Event::Create(isolate); - native_event->SetSenderAndMessage(sender, std::move(callback)); + native_event->SetCallback(std::move(callback)); event = v8::Local::Cast(native_event.ToV8()); } else { event = CreateEventObject(isolate);