Make sure BrowserContext is destroyed on exit
This commit is contained in:
parent
5eb0bedbbc
commit
e2bd1abce6
8 changed files with 47 additions and 16 deletions
|
@ -233,16 +233,16 @@ void SetProxyInIO(net::URLRequestContextGetter* getter,
|
||||||
Session::Session(AtomBrowserContext* browser_context)
|
Session::Session(AtomBrowserContext* browser_context)
|
||||||
: browser_context_(browser_context) {
|
: browser_context_(browser_context) {
|
||||||
AttachAsUserData(browser_context);
|
AttachAsUserData(browser_context);
|
||||||
|
|
||||||
// Observe DownloadManger to get download notifications.
|
// Observe DownloadManger to get download notifications.
|
||||||
auto download_manager =
|
content::BrowserContext::GetDownloadManager(browser_context)->
|
||||||
content::BrowserContext::GetDownloadManager(browser_context);
|
AddObserver(this);
|
||||||
download_manager->AddObserver(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Session::~Session() {
|
Session::~Session() {
|
||||||
auto download_manager =
|
content::BrowserContext::GetDownloadManager(browser_context())->
|
||||||
content::BrowserContext::GetDownloadManager(browser_context());
|
RemoveObserver(this);
|
||||||
download_manager->RemoveObserver(this);
|
Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::OnDownloadCreated(content::DownloadManager* manager,
|
void Session::OnDownloadCreated(content::DownloadManager* manager,
|
||||||
|
@ -257,6 +257,14 @@ void Session::OnDownloadCreated(content::DownloadManager* manager,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Session::IsDestroyed() const {
|
||||||
|
return !browser_context_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Session::Destroy() {
|
||||||
|
browser_context_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
||||||
new ResolveProxyHelper(browser_context(), url, callback);
|
new ResolveProxyHelper(browser_context(), url, callback);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,11 +51,15 @@ class Session: public mate::TrackableObject<Session>,
|
||||||
void OnDownloadCreated(content::DownloadManager* manager,
|
void OnDownloadCreated(content::DownloadManager* manager,
|
||||||
content::DownloadItem* item) override;
|
content::DownloadItem* item) override;
|
||||||
|
|
||||||
// mate::Wrappable implementations:
|
// mate::Wrappable:
|
||||||
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||||
v8::Isolate* isolate) override;
|
v8::Isolate* isolate) override;
|
||||||
|
bool IsDestroyed() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// mate::TrackableObject:
|
||||||
|
void Destroy() override;
|
||||||
|
|
||||||
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
|
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
|
||||||
void ClearCache(const net::CompletionCallback& callback);
|
void ClearCache(const net::CompletionCallback& callback);
|
||||||
void ClearStorageData(mate::Arguments* args);
|
void ClearStorageData(mate::Arguments* args);
|
||||||
|
@ -63,6 +67,7 @@ class Session: public mate::TrackableObject<Session>,
|
||||||
void SetDownloadPath(const base::FilePath& path);
|
void SetDownloadPath(const base::FilePath& path);
|
||||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||||
|
|
||||||
|
// Cached object for cookies API.
|
||||||
v8::Global<v8::Value> cookies_;
|
v8::Global<v8::Value> cookies_;
|
||||||
|
|
||||||
scoped_refptr<AtomBrowserContext> browser_context_;
|
scoped_refptr<AtomBrowserContext> browser_context_;
|
||||||
|
|
|
@ -48,7 +48,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
static mate::Handle<WebContents> Create(
|
static mate::Handle<WebContents> Create(
|
||||||
v8::Isolate* isolate, const mate::Dictionary& options);
|
v8::Isolate* isolate, const mate::Dictionary& options);
|
||||||
|
|
||||||
void Destroy();
|
// mate::TrackableObject:
|
||||||
|
void Destroy() override;
|
||||||
|
|
||||||
bool IsAlive() const;
|
bool IsAlive() const;
|
||||||
int GetID() const;
|
int GetID() const;
|
||||||
bool Equal(const WebContents* web_contents) const;
|
bool Equal(const WebContents* web_contents) const;
|
||||||
|
|
|
@ -221,7 +221,10 @@ bool Window::IsDestroyed() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::Destroy() {
|
void Window::Destroy() {
|
||||||
window_->CloseContents(nullptr);
|
if (window_) {
|
||||||
|
window_->CloseContents(nullptr);
|
||||||
|
window_.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::Close() {
|
void Window::Close() {
|
||||||
|
|
|
@ -78,8 +78,10 @@ class Window : public mate::TrackableObject<Window>,
|
||||||
bool IsDestroyed() const override;
|
bool IsDestroyed() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// mate::TrackableObject:
|
||||||
|
void Destroy() override;
|
||||||
|
|
||||||
// APIs for NativeWindow.
|
// APIs for NativeWindow.
|
||||||
void Destroy();
|
|
||||||
void Close();
|
void Close();
|
||||||
bool IsClosed();
|
bool IsClosed();
|
||||||
void Focus();
|
void Focus();
|
||||||
|
|
|
@ -29,7 +29,9 @@ class IDUserData : public base::SupportsUserData::Data {
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TrackableObjectBase::TrackableObjectBase()
|
TrackableObjectBase::TrackableObjectBase()
|
||||||
: weak_map_id_(0), wrapped_(nullptr) {
|
: weak_map_id_(0), wrapped_(nullptr), weak_factory_(this) {
|
||||||
|
RegisterDestructionCallback(
|
||||||
|
base::Bind(&TrackableObjectBase::Destroy, weak_factory_.GetWeakPtr()));
|
||||||
}
|
}
|
||||||
|
|
||||||
TrackableObjectBase::~TrackableObjectBase() {
|
TrackableObjectBase::~TrackableObjectBase() {
|
||||||
|
@ -61,8 +63,9 @@ int32_t TrackableObjectBase::GetIDFromWrappedClass(base::SupportsUserData* w) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void TrackableObjectBase::RegisterDestructionCallback(void (*c)()) {
|
void TrackableObjectBase::RegisterDestructionCallback(
|
||||||
atom::AtomBrowserMainParts::Get()->RegisterDestructionCallback(base::Bind(c));
|
const base::Closure& closure) {
|
||||||
|
atom::AtomBrowserMainParts::Get()->RegisterDestructionCallback(closure);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
|
@ -9,7 +9,9 @@
|
||||||
|
|
||||||
#include "atom/browser/api/event_emitter.h"
|
#include "atom/browser/api/event_emitter.h"
|
||||||
#include "atom/common/id_weak_map.h"
|
#include "atom/common/id_weak_map.h"
|
||||||
|
#include "base/bind.h"
|
||||||
#include "base/memory/scoped_ptr.h"
|
#include "base/memory/scoped_ptr.h"
|
||||||
|
#include "base/memory/weak_ptr.h"
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
class SupportsUserData;
|
class SupportsUserData;
|
||||||
|
@ -28,6 +30,9 @@ class TrackableObjectBase : public mate::EventEmitter {
|
||||||
// Wrap TrackableObject into a class that SupportsUserData.
|
// Wrap TrackableObject into a class that SupportsUserData.
|
||||||
void AttachAsUserData(base::SupportsUserData* wrapped);
|
void AttachAsUserData(base::SupportsUserData* wrapped);
|
||||||
|
|
||||||
|
// Subclasses should implement this to destroy their native types.
|
||||||
|
virtual void Destroy() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
~TrackableObjectBase() override;
|
~TrackableObjectBase() override;
|
||||||
|
|
||||||
|
@ -39,12 +44,14 @@ class TrackableObjectBase : public mate::EventEmitter {
|
||||||
|
|
||||||
// Register a callback that should be destroyed before JavaScript environment
|
// Register a callback that should be destroyed before JavaScript environment
|
||||||
// gets destroyed.
|
// gets destroyed.
|
||||||
static void RegisterDestructionCallback(void (*callback)());
|
static void RegisterDestructionCallback(const base::Closure& closure);
|
||||||
|
|
||||||
int32_t weak_map_id_;
|
int32_t weak_map_id_;
|
||||||
base::SupportsUserData* wrapped_;
|
base::SupportsUserData* wrapped_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
base::WeakPtrFactory<TrackableObjectBase> weak_factory_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(TrackableObjectBase);
|
DISALLOW_COPY_AND_ASSIGN(TrackableObjectBase);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -85,7 +92,8 @@ class TrackableObject : public TrackableObjectBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
TrackableObject() {
|
TrackableObject() {
|
||||||
RegisterDestructionCallback(&TrackableObject<T>::ReleaseAllWeakReferences);
|
RegisterDestructionCallback(
|
||||||
|
base::Bind(&TrackableObject<T>::ReleaseAllWeakReferences));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes this instance from the weak map.
|
// Removes this instance from the weak map.
|
||||||
|
|
2
vendor/brightray
vendored
2
vendor/brightray
vendored
|
@ -1 +1 @@
|
||||||
Subproject commit 251521cb8c51670dbd08ab9fd2dd45e249cbb596
|
Subproject commit 8d64120b51b48be46eaa419957b965c0ccfc6c8f
|
Loading…
Add table
Add a link
Reference in a new issue