Skip to content

Commit db140a9

Browse files
committed
Wrap devtools window for access from JS
1 parent 05945c3 commit db140a9

File tree

6 files changed

+55
-14
lines changed

6 files changed

+55
-14
lines changed

src/api/dispatcher_host.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void DispatcherHost::OnCallObjectMethod(
152152
<< " arguments:" << arguments;
153153

154154
Base* object = GetApiObject(object_id);
155-
CHECK(object) << "Unknown object: " << object_id
155+
DLOG(WARNING) << "Unknown object: " << object_id
156156
<< " type:" << type
157157
<< " method:" << method
158158
<< " arguments:" << arguments;

src/api/dispatcher_host.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ namespace WebKit {
3636
class WebFrame;
3737
}
3838

39+
namespace content {
40+
class Shell;
41+
}
42+
3943
namespace api {
4044

4145
class Base;
@@ -66,6 +70,8 @@ class DispatcherHost : public content::RenderViewHostObserver {
6670
}
6771

6872
private:
73+
friend class content::Shell;
74+
6975
static IDMap<Base, IDMapOwnPointer> objects_registry_;
7076
static int next_object_id_;
7177

@@ -102,6 +108,8 @@ class DispatcherHost : public content::RenderViewHostObserver {
102108
DISALLOW_COPY_AND_ASSIGN(DispatcherHost);
103109
};
104110

111+
api::DispatcherHost* FindDispatcherHost(content::RenderViewHost* render_view_host);
112+
105113
} // namespace api
106114

107115
#endif // CONTENT_NW_SRC_API_DISPATCHER_HOST_H_

src/api/window/window.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,6 @@ void Window::Call(const std::string& method,
215215
shell_->window()->SetKiosk(false);
216216
} else if (method == "ToggleKioskMode") {
217217
shell_->window()->SetKiosk(!shell_->window()->IsKiosk());
218-
} else if (method == "ShowDevTools") {
219-
std::string jail_id;
220-
bool headless = false;
221-
arguments.GetString(0, &jail_id);
222-
arguments.GetBoolean(1, &headless);
223-
shell_->ShowDevTools(jail_id.c_str(), headless);
224218
} else if (method == "CloseDevTools") {
225219
shell_->CloseDevTools();
226220
}else if (method == "ResizeTo") {
@@ -298,6 +292,16 @@ void Window::CallSync(const std::string& method,
298292
result->AppendInteger(position.y());
299293
} else if (method == "IsDevToolsOpen") {
300294
result->AppendBoolean(shell_->devToolsOpen());
295+
} else if (method == "ShowDevTools") {
296+
std::string jail_id;
297+
bool headless = false;
298+
arguments.GetString(0, &jail_id);
299+
arguments.GetBoolean(1, &headless);
300+
shell_->ShowDevTools(jail_id.c_str(), headless);
301+
int object_id = 0;
302+
if (!headless)
303+
object_id = shell_->WrapDevToolsWindow();
304+
result->AppendInteger(object_id);
301305
} else {
302306
NOTREACHED() << "Invalid call to Window method:" << method
303307
<< " arguments:" << arguments;

src/api/window_bindings.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ native function CallObjectMethodSync();
9696
Window.prototype.on = Window.prototype.addListener = function(ev, callback) {
9797
// Save window id of where the callback is created.
9898
var closure = v8_util.getCreationContext(callback);
99-
if (v8_util.getConstructorName(closure) == 'Window' &&
99+
if (v8_util.getConstructorName(closure) == 'Window' &&
100100
closure.hasOwnProperty('nwDispatcher')) {
101101
v8_util.setHiddenValue(callback, '__nwWindowId',
102102
closure.nwDispatcher.requireNwGui().Window.get().id);
@@ -347,7 +347,14 @@ Window.prototype.showDevTools = function(frm, headless) {
347347
}else{
348348
this._pending_devtools_jail = frm;
349349
}
350-
CallObjectMethod(this, 'ShowDevTools', [id, Boolean(headless)]);
350+
var win_id = CallObjectMethodSync(this, 'ShowDevTools', [id, Boolean(headless)])[0];
351+
var ret;
352+
if (typeof win_id == 'number' && win_id > 0) {
353+
ret = global.__nwWindowsStore[win_id];
354+
if (!ret)
355+
ret = new global.Window(this.window.nwDispatcher.getRoutingIDForCurrentContext(), true, win_id);
356+
return ret;
357+
}
351358
}
352359

353360
Window.prototype.__setDevToolsJail = function(id) {

src/nw_shell.cc

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "content/public/common/renderer_preferences.h"
4545
#include "content/public/common/url_constants.h"
4646
#include "content/nw/src/api/api_messages.h"
47+
#include "content/nw/src/api/dispatcher_host.h"
4748
#include "content/nw/src/api/app/app.h"
4849
#include "content/nw/src/browser/browser_dialogs.h"
4950
#include "content/nw/src/browser/file_select_helper.h"
@@ -144,11 +145,12 @@ Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) {
144145

145146
Shell::Shell(WebContents* web_contents, base::DictionaryValue* manifest)
146147
:
147-
is_devtools_(false),
148-
force_close_(false),
149-
id_(-1),
150-
enable_nodejs_(true),
151-
weak_ptr_factory_(this)
148+
devtools_window_id_(0),
149+
is_devtools_(false),
150+
force_close_(false),
151+
id_(-1),
152+
enable_nodejs_(true),
153+
weak_ptr_factory_(this)
152154
{
153155
// Register shell.
154156
registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
@@ -182,6 +184,9 @@ Shell::~Shell() {
182184

183185
if (is_devtools_ && devtools_owner_.get()) {
184186
devtools_owner_->SendEvent("devtools-closed");
187+
api::DispatcherHost* dhost = api::FindDispatcherHost(devtools_owner_->web_contents_->GetRenderViewHost());
188+
dhost->OnDeallocateObject(devtools_owner_->devtools_window_id_);
189+
devtools_owner_->devtools_window_id_ = 0;
185190
}
186191

187192
for (size_t i = 0; i < windows_.size(); ++i) {
@@ -317,6 +322,20 @@ void Shell::CloseDevTools() {
317322
return;
318323
devtools_window_->window()->Close();
319324
devtools_window_.reset();
325+
devtools_window_id_ = 0;
326+
}
327+
328+
int Shell::WrapDevToolsWindow() {
329+
if (devtools_window_id_)
330+
return devtools_window_id_;
331+
if (!devtools_window_)
332+
return 0;
333+
api::DispatcherHost* dhost = api::FindDispatcherHost(devtools_window_->web_contents_->GetRenderViewHost());
334+
int object_id = dhost->AllocateId();
335+
base::DictionaryValue manifest;
336+
dhost->OnAllocateObject(object_id, "Window", manifest);
337+
devtools_window_id_ = object_id;
338+
return object_id;
320339
}
321340

322341
void Shell::ShowDevTools(const char* jail_id, bool headless) {

src/nw_shell.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class Shell : public WebContentsDelegate,
112112
void PrintCriticalError(const std::string& title,
113113
const std::string& content);
114114

115+
int WrapDevToolsWindow();
115116
// Returns the currently open windows.
116117
static std::vector<Shell*>& windows() { return windows_; }
117118

@@ -198,6 +199,8 @@ class Shell : public WebContentsDelegate,
198199
// Weak potiner to devtools window.
199200
base::WeakPtr<Shell> devtools_window_;
200201
base::WeakPtr<Shell> devtools_owner_;
202+
203+
int devtools_window_id_;
201204
#if 0
202205
ShellDevToolsFrontend* devtools_frontend_;
203206
#endif

0 commit comments

Comments
 (0)