Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash in BrowserWindow destructor after win.webContents.destroy() #18794

Merged
merged 1 commit into from Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions atom/browser/api/atom_api_browser_window.cc
Expand Up @@ -68,7 +68,7 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
}

web_contents_.Reset(isolate, web_contents.ToV8());
api_web_contents_ = web_contents.get();
api_web_contents_ = web_contents->GetWeakPtr();
api_web_contents_->AddObserver(this);
Observe(api_web_contents_->web_contents());

Expand All @@ -95,7 +95,9 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
}

BrowserWindow::~BrowserWindow() {
api_web_contents_->RemoveObserver(this);
// FIXME This is a hack rather than a proper fix preventing shutdown crashes.
if (api_web_contents_)
api_web_contents_->RemoveObserver(this);
// Note that the OnWindowClosed will not be called after the destructor runs,
// since the window object is managed by the TopLevelWindow class.
if (web_contents())
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_browser_window.h
Expand Up @@ -116,7 +116,7 @@ class BrowserWindow : public TopLevelWindow,
#endif

v8::Global<v8::Value> web_contents_;
api::WebContents* api_web_contents_;
base::WeakPtr<api::WebContents> api_web_contents_;

base::WeakPtrFactory<BrowserWindow> weak_factory_;

Expand Down
12 changes: 8 additions & 4 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -287,7 +287,9 @@ struct WebContents::FrameDispatchHelper {

WebContents::WebContents(v8::Isolate* isolate,
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents), type_(REMOTE) {
: content::WebContentsObserver(web_contents),
type_(REMOTE),
weak_factory_(this) {
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent(),
false);
Init(isolate);
Expand All @@ -298,16 +300,18 @@ WebContents::WebContents(v8::Isolate* isolate,
WebContents::WebContents(v8::Isolate* isolate,
std::unique_ptr<content::WebContents> web_contents,
Type type)
: content::WebContentsObserver(web_contents.get()), type_(type) {
: content::WebContentsObserver(web_contents.get()),
type_(type),
weak_factory_(this) {
DCHECK(type != REMOTE) << "Can't take ownership of a remote WebContents";
auto session = Session::CreateFrom(isolate, GetBrowserContext());
session_.Reset(isolate, session.ToV8());
InitWithSessionAndOptions(isolate, std::move(web_contents), session,
mate::Dictionary::CreateEmpty(isolate));
}

WebContents::WebContents(v8::Isolate* isolate,
const mate::Dictionary& options) {
WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options)
: weak_factory_(this) {
// Read options.
options.Get("backgroundThrottling", &background_throttling_);

Expand Down
4 changes: 4 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -111,6 +111,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

base::WeakPtr<WebContents> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }

// Destroy the managed content::WebContents instance.
//
// Note: The |async| should only be |true| when users are expecting to use the
Expand Down Expand Up @@ -545,6 +547,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
// -1 means no speculative RVH has been committed yet.
int currently_committed_process_id_ = -1;

base::WeakPtrFactory<WebContents> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(WebContents);
};

Expand Down