Skip to content

Commit f779a42

Browse files
committed
use weak references on DispatcherHost
Fix nwjs#1446 Conflicts: src/api/dispatcher_host.cc src/api/dispatcher_host.h
1 parent c012b25 commit f779a42

File tree

14 files changed

+37
-21
lines changed

14 files changed

+37
-21
lines changed

src/api/base/base.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace nwapi {
2727

2828
Base::Base(int id,
29-
DispatcherHost* dispatcher_host,
29+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
3030
const base::DictionaryValue& option)
3131
: id_(id),
3232
dispatcher_host_(dispatcher_host) {

src/api/base/base.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define CONTENT_NW_SRC_API_BASE_BASE_H_
2323

2424
#include "base/basictypes.h"
25+
#include "base/memory/weak_ptr.h"
2526

2627
#include <string>
2728

@@ -37,7 +38,7 @@ class DispatcherHost;
3738
class Base {
3839
public:
3940
Base(int id,
40-
DispatcherHost* dispatcher_host,
41+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
4142
const base::DictionaryValue& option);
4243
virtual ~Base();
4344

@@ -48,11 +49,11 @@ class Base {
4849
base::ListValue* result);
4950

5051
int id() const { return id_; }
51-
DispatcherHost* dispatcher_host() const { return dispatcher_host_; }
52+
DispatcherHost* dispatcher_host() const { return dispatcher_host_.get(); }
5253

5354
private:
5455
int id_;
55-
DispatcherHost* dispatcher_host_;
56+
base::WeakPtr<DispatcherHost> dispatcher_host_;
5657

5758
DISALLOW_COPY_AND_ASSIGN(Base);
5859
};

src/api/clipboard/clipboard.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
namespace nwapi {
3030

3131
Clipboard::Clipboard(int id,
32-
DispatcherHost* dispatcher_host,
32+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
3333
const base::DictionaryValue& option)
3434
: Base(id, dispatcher_host, option) {
3535
}

src/api/clipboard/clipboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace nwapi {
2929
class Clipboard : public Base {
3030
public:
3131
Clipboard(int id,
32-
DispatcherHost* dispatcher_host,
32+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
3333
const base::DictionaryValue& option);
3434
virtual ~Clipboard();
3535

src/api/dispatcher_host.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ int nwapi::DispatcherHost::next_object_id_ = 1;
5050
static std::map<content::RenderViewHost*, DispatcherHost*> g_dispatcher_host_map;
5151

5252
DispatcherHost::DispatcherHost(content::RenderViewHost* render_view_host)
53-
: content::RenderViewHostObserver(render_view_host) {
53+
: content::RenderViewHostObserver(render_view_host),
54+
weak_ptr_factory_(this) {
5455
g_dispatcher_host_map[render_view_host] = this;
5556
}
5657

@@ -122,20 +123,20 @@ void DispatcherHost::OnAllocateObject(int object_id,
122123
<< " option:" << option;
123124

124125
if (type == "Menu") {
125-
objects_registry_.AddWithID(new Menu(object_id, this, option), object_id);
126+
objects_registry_.AddWithID(new Menu(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
126127
} else if (type == "MenuItem") {
127128
objects_registry_.AddWithID(
128-
new MenuItem(object_id, this, option), object_id);
129+
new MenuItem(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
129130
} else if (type == "Tray") {
130-
objects_registry_.AddWithID(new Tray(object_id, this, option), object_id);
131+
objects_registry_.AddWithID(new Tray(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
131132
} else if (type == "Clipboard") {
132133
objects_registry_.AddWithID(
133-
new Clipboard(object_id, this, option), object_id);
134+
new Clipboard(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
134135
} else if (type == "Window") {
135-
objects_registry_.AddWithID(new Window(object_id, this, option), object_id);
136+
objects_registry_.AddWithID(new Window(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
136137
} else {
137138
LOG(ERROR) << "Allocate an object of unknown type: " << type;
138-
objects_registry_.AddWithID(new Base(object_id, this, option), object_id);
139+
objects_registry_.AddWithID(new Base(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
139140
}
140141
}
141142

src/api/dispatcher_host.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "base/basictypes.h"
2525
#include "base/id_map.h"
26+
#include "base/memory/weak_ptr.h"
2627
#include "content/public/browser/render_view_host_observer.h"
2728

2829
#include <string>
@@ -77,6 +78,9 @@ class DispatcherHost : public content::RenderViewHostObserver {
7778
static IDMap<Base, IDMapOwnPointer> objects_registry_;
7879
static int next_object_id_;
7980

81+
// Factory to generate weak pointer
82+
base::WeakPtrFactory<DispatcherHost> weak_ptr_factory_;
83+
8084
// RenderViewHostObserver implementation.
8185
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
8286

src/api/menu/menu.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
namespace nwapi {
2929

3030
Menu::Menu(int id,
31-
DispatcherHost* dispatcher_host,
31+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
3232
const base::DictionaryValue& option)
3333
: Base(id, dispatcher_host, option) {
3434
Create(option);

src/api/menu/menu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class MenuItem;
8181
class Menu : public Base {
8282
public:
8383
Menu(int id,
84-
DispatcherHost* dispatcher_host,
84+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
8585
const base::DictionaryValue& option);
8686
virtual ~Menu();
8787

src/api/menuitem/menuitem.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
namespace nwapi {
3131

3232
MenuItem::MenuItem(int id,
33-
DispatcherHost* dispatcher_host,
33+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
3434
const base::DictionaryValue& option)
3535
: Base(id, dispatcher_host, option) {
3636
Create(option);

src/api/menuitem/menuitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Menu;
4949
class MenuItem : public Base {
5050
public:
5151
MenuItem(int id,
52-
DispatcherHost* dispatcher_host,
52+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
5353
const base::DictionaryValue& option);
5454
virtual ~MenuItem();
5555

src/api/tray/tray.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
namespace nwapi {
2929

3030
Tray::Tray(int id,
31-
DispatcherHost* dispatcher_host,
31+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
3232
const base::DictionaryValue& option)
3333
: Base(id, dispatcher_host, option) {
3434
Create(option);

src/api/tray/tray.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TrayObserver;
4848
class Tray : public Base {
4949
public:
5050
Tray(int id,
51-
DispatcherHost* dispatcher_host,
51+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
5252
const base::DictionaryValue& option);
5353
virtual ~Tray();
5454

src/api/window/window.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ PopulateCookieObject(const net::CanonicalCookie& canonical_cookie) {
162162
namespace nwapi {
163163

164164
Window::Window(int id,
165-
DispatcherHost* dispatcher_host,
165+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
166166
const base::DictionaryValue& option)
167167
: Base(id, dispatcher_host, option),
168168
shell_(content::Shell::FromRenderViewHost(dispatcher_host->
@@ -313,6 +313,8 @@ void Window::CallSync(const std::string& method,
313313
}
314314

315315
void Window::CookieRemove(const base::ListValue& arguments) {
316+
if (!dispatcher_host())
317+
return;
316318
CookieAPIContext* api_context = new CookieAPIContext(dispatcher_host(), arguments);
317319
bool rv = BrowserThread::PostTask(
318320
BrowserThread::IO, FROM_HERE,
@@ -323,6 +325,8 @@ void Window::CookieRemove(const base::ListValue& arguments) {
323325
}
324326

325327
void Window::CookieSet(const base::ListValue& arguments) {
328+
if (!dispatcher_host())
329+
return;
326330
CookieAPIContext* api_context = new CookieAPIContext(dispatcher_host(), arguments);
327331
bool rv = BrowserThread::PostTask(
328332
BrowserThread::IO, FROM_HERE,
@@ -388,6 +392,8 @@ CookieAPIContext::CookieAPIContext(DispatcherHost* dispatcher_host,
388392

389393
void Window::CookieGet(const base::ListValue& arguments, bool get_all) {
390394

395+
if (!dispatcher_host())
396+
return;
391397
CookieAPIContext* api_context = new CookieAPIContext(dispatcher_host(), arguments);
392398

393399
if (get_all) {
@@ -471,6 +477,8 @@ void Window::GetCookieCallback(CookieAPIContext* api_context,
471477
}
472478

473479
void Window::RespondOnUIThread(CookieAPIContext* api_context) {
480+
if (!dispatcher_host())
481+
return;
474482
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
475483
base::ListValue ret;
476484
ret.Append(api_context->result_.release());
@@ -573,6 +581,8 @@ void Window::Observe(
573581
void Window::CookieChanged(
574582
ShellBrowserContext* browser_context,
575583
ChromeCookieDetails* details) {
584+
if (!dispatcher_host())
585+
return;
576586
scoped_ptr<base::ListValue> args(new base::ListValue());
577587
base::DictionaryValue* dict = new base::DictionaryValue();
578588
dict->SetBoolean(kRemovedKey, details->removed);

src/api/window/window.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CookieAPIContext : public base::RefCountedThreadSafe<CookieAPIContext> {
5757
class Window : public Base, public content::NotificationObserver {
5858
public:
5959
Window(int id,
60-
DispatcherHost* dispatcher_host,
60+
const base::WeakPtr<DispatcherHost>& dispatcher_host,
6161
const base::DictionaryValue& option);
6262
virtual ~Window();
6363

0 commit comments

Comments
 (0)