forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_devtools_frontend.cc
More file actions
158 lines (133 loc) · 5.21 KB
/
shell_devtools_frontend.cc
File metadata and controls
158 lines (133 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/nw/src/shell_devtools_frontend.h"
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/devtools_http_handler.h"
//#include "content/public/browser/devtools_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/nw/src/nw_shell.h"
#include "content/nw/src/shell_browser_context.h"
#include "content/nw/src/shell_browser_main_parts.h"
#include "content/nw/src/shell_content_browser_client.h"
//#include "content/nw/src/browser/shell_devtools_delegate.h"
#include "net/base/net_util.h"
#include "net/base/filename_util.h"
namespace content {
// This constant should be in sync with
// the constant at devtools_ui_bindings.cc.
const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4;
#if 0
// static
ShellDevToolsFrontend* ShellDevToolsFrontend::Show(
WebContents* inspected_contents) {
Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(),
GURL(),
NULL,
MSG_ROUTING_NONE,
gfx::Size());
ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend(
shell,
DevToolsAgentHost::GetOrCreateFor(
inspected_contents->GetRenderViewHost()));
ShellDevToolsDelegate* delegate = ShellContentBrowserClient::Get()->
shell_browser_main_parts()->devtools_delegate();
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
shell->LoadURL(GetDevToolsPathAsURL());
else
shell->LoadURL(delegate->devtools_http_handler()->GetFrontendURL(NULL));
return devtools_frontend;
}
#endif
void ShellDevToolsFrontend::Focus() {
web_contents()->Focus();
}
void ShellDevToolsFrontend::Close() {
}
ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell,
DevToolsAgentHost* agent_host)
: WebContentsObserver(frontend_shell->web_contents()),
frontend_shell_(frontend_shell),
agent_host_(agent_host) {
}
ShellDevToolsFrontend::~ShellDevToolsFrontend() {
}
void ShellDevToolsFrontend::RenderViewCreated(
RenderViewHost* render_view_host) {
if (!frontend_host_) {
frontend_host_.reset(
DevToolsFrontendHost::Create(web_contents()->GetMainFrame(), this));
agent_host_->AttachClient(this);
}
}
void ShellDevToolsFrontend::WebContentsDestroyed() {
if (agent_host_)
agent_host_->DetachClient();
delete this;
}
void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(
const std::string& message) {
std::string method;
int id = 0;
base::ListValue* params = NULL;
base::DictionaryValue* dict = NULL;
scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message));
if (!parsed_message ||
!parsed_message->GetAsDictionary(&dict) ||
!dict->GetString("method", &method)) {
return;
}
dict->GetList("params", ¶ms);
std::string browser_message;
if (method == "sendMessageToBrowser" && params &&
params->GetSize() == 1 && params->GetString(0, &browser_message)) {
agent_host_->DispatchProtocolMessage(browser_message);
} else if (method == "loadCompleted") {
web_contents()->GetMainFrame()->ExecuteJavaScript(
base::ASCIIToUTF16("DevToolsAPI.setUseSoftMenu(true);"));
} else {
return;
}
dict->GetInteger("id", &id);
if (id) {
std::string code = "DevToolsAPI.embedderMessageAck(" +
base::IntToString(id) + ",\"\");";
base::string16 javascript = base::UTF8ToUTF16(code);
web_contents()->GetMainFrame()->ExecuteJavaScript(javascript);
}
}
void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontendToBackend(
const std::string& message) {
agent_host_->DispatchProtocolMessage(message);
}
void ShellDevToolsFrontend::DispatchProtocolMessage(
DevToolsAgentHost* agent_host, const std::string& message) {
if (message.length() < kMaxMessageChunkSize) {
base::string16 javascript = base::UTF8ToUTF16(
"DevToolsAPI.dispatchMessage(" + message + ");");
web_contents()->GetMainFrame()->ExecuteJavaScript(javascript);
return;
}
base::FundamentalValue total_size(static_cast<int>(message.length()));
for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) {
base::StringValue message_value(message.substr(pos, kMaxMessageChunkSize));
std::string param;
base::JSONWriter::Write(&message_value, ¶m);
std::string code = "DevToolsAPI.dispatchMessageChunk(" + param + ");";
base::string16 javascript = base::UTF8ToUTF16(code);
web_contents()->GetMainFrame()->ExecuteJavaScript(javascript);
}
}
void ShellDevToolsFrontend::AgentHostClosed(
DevToolsAgentHost* agent_host, bool replaced) {
//FIXME
// frontend_shell_->Close();
}
} // namespace content