Add ExtendedWebContentsObserver

This commit is contained in:
Cheng Zhao 2018-02-22 15:57:03 +09:00
parent a3124e8873
commit a25b49a127
4 changed files with 25 additions and 4 deletions

View file

@ -134,6 +134,7 @@ void BrowserWindow::Init(v8::Isolate* isolate,
web_contents_.Reset(isolate, web_contents.ToV8());
api_web_contents_ = web_contents.get();
Observe(web_contents->web_contents());
api_web_contents_->AddObserver(this);
// Keep a copy of the options for later use.
mate::Dictionary(isolate, web_contents->GetWrapper()).Set(
@ -175,6 +176,8 @@ BrowserWindow::~BrowserWindow() {
if (!window_->IsClosed())
window_->CloseContents(nullptr);
api_web_contents_->RemoveObserver(this);
// Destroy the native window in next tick because the native code might be
// iterating all windows.
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, window_.release());
@ -222,6 +225,9 @@ bool BrowserWindow::OnMessageReceived(const IPC::Message& message,
return handled;
}
void BrowserWindow::OnRendererResponsive() {
}
void BrowserWindow::WillCloseWindow(bool* prevent_default) {
*prevent_default = Emit("close");
}

View file

@ -10,16 +10,13 @@
#include <string>
#include <vector>
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/native_window.h"
#include "atom/browser/native_window_observer.h"
#include "atom/common/api/atom_api_native_image.h"
#include "atom/common/key_weak_map.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/web_contents_observer.h"
#include "native_mate/handle.h"
#include "native_mate/persistent_dictionary.h"
#include "ui/gfx/image/image.h"
class GURL;
@ -40,6 +37,7 @@ namespace api {
class BrowserWindow : public mate::TrackableObject<BrowserWindow>,
public content::WebContentsObserver,
public ExtendedWebContentsObserver,
public NativeWindowObserver {
public:
static mate::WrappableBase* New(mate::Arguments* args);
@ -67,6 +65,9 @@ class BrowserWindow : public mate::TrackableObject<BrowserWindow>,
bool OnMessageReceived(const IPC::Message& message,
content::RenderFrameHost* rfh) override;
// ExtendedWebContentsObserver:
void OnRendererResponsive() override;
// NativeWindowObserver:
void WillCloseWindow(bool* prevent_default) override;
void WillDestroyNativeObject() override;

View file

@ -642,6 +642,8 @@ void WebContents::RendererResponsive(content::WebContents* source) {
Emit("responsive");
if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) && owner_window())
owner_window()->RendererResponsive(source);
for (ExtendedWebContentsObserver& observer : observers_)
observer.OnRendererResponsive();
}
bool WebContents::HandleContextMenu(const content::ContextMenuParams& params) {

View file

@ -13,6 +13,7 @@
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/common_web_contents_delegate.h"
#include "atom/browser/ui/autofill_popup.h"
#include "base/observer_list.h"
#include "content/common/cursors/webcursor.h"
#include "content/public/browser/keyboard_event_processing_result.h"
#include "content/public/browser/web_contents.h"
@ -52,6 +53,7 @@ namespace api {
// Certain events are only in WebContentsDelegate, provide our own Observer to
// dispatch those events.
class ExtendedWebContentsObserver {
public:
virtual void OnRendererResponsive() {}
};
@ -238,6 +240,13 @@ class WebContents : public mate::TrackableObject<WebContents>,
WebContentsZoomController* GetZoomController() { return zoom_controller_; }
void AddObserver(ExtendedWebContentsObserver* obs) {
observers_.AddObserver(obs);
}
void RemoveObserver(ExtendedWebContentsObserver* obs) {
observers_.RemoveObserver(obs);
}
protected:
WebContents(v8::Isolate* isolate,
content::WebContents* web_contents,
@ -428,6 +437,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Whether to enable devtools.
bool enable_devtools_;
// Observers of this WebContents.
base::ObserverList<ExtendedWebContentsObserver> observers_;
DISALLOW_COPY_AND_ASSIGN(WebContents);
};