Avoid calling JavaScript of devtools WebContents too early

It would craete a context that window.location is about:blank
This commit is contained in:
Cheng Zhao 2015-06-05 11:58:27 +08:00
parent 9a9bc5530e
commit 56747e975e
2 changed files with 25 additions and 7 deletions

View file

@ -52,6 +52,8 @@ const int kDevToolsActionTakenBoundary = 100;
const char kDevToolsPanelShownHistogram[] = "DevTools.PanelShown"; const char kDevToolsPanelShownHistogram[] = "DevTools.PanelShown";
const int kDevToolsPanelShownBoundary = 20; const int kDevToolsPanelShownBoundary = 20;
const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4;
void RectToDictionary(const gfx::Rect& bounds, base::DictionaryValue* dict) { void RectToDictionary(const gfx::Rect& bounds, base::DictionaryValue* dict) {
dict->SetInteger("x", bounds.x()); dict->SetInteger("x", bounds.x());
dict->SetInteger("y", bounds.y()); dict->SetInteger("y", bounds.y());
@ -159,6 +161,7 @@ InspectableWebContentsImpl::InspectableWebContentsImpl(
content::WebContents* web_contents) content::WebContents* web_contents)
: web_contents_(web_contents), : web_contents_(web_contents),
can_dock_(true), can_dock_(true),
frontend_loaded_(false),
delegate_(nullptr), delegate_(nullptr),
weak_factory_(this) { weak_factory_(this) {
auto context = static_cast<BrowserContext*>(web_contents_->GetBrowserContext()); auto context = static_cast<BrowserContext*>(web_contents_->GetBrowserContext());
@ -280,6 +283,7 @@ void InspectableWebContentsImpl::CloseWindow() {
} }
void InspectableWebContentsImpl::LoadCompleted() { void InspectableWebContentsImpl::LoadCompleted() {
frontend_loaded_ = true;
} }
void InspectableWebContentsImpl::SetInspectedPageBounds(const gfx::Rect& rect) { void InspectableWebContentsImpl::SetInspectedPageBounds(const gfx::Rect& rect) {
@ -448,9 +452,22 @@ void InspectableWebContentsImpl::HandleMessageFromDevToolsFrontendToBackend(
void InspectableWebContentsImpl::DispatchProtocolMessage( void InspectableWebContentsImpl::DispatchProtocolMessage(
content::DevToolsAgentHost* agent_host, const std::string& message) { content::DevToolsAgentHost* agent_host, const std::string& message) {
std::string code = "DevToolsAPI.dispatchMessage(" + message + ");"; if (!frontend_loaded_)
base::string16 javascript = base::UTF8ToUTF16(code); return;
web_contents()->GetMainFrame()->ExecuteJavaScript(javascript);
if (message.length() < kMaxMessageChunkSize) {
base::string16 javascript = base::UTF8ToUTF16(
"DevToolsAPI.dispatchMessage(" + message + ");");
devtools_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));
CallClientFunction("DevToolsAPI.dispatchMessageChunk",
&message_value, pos ? NULL : &total_size, NULL);
}
} }
void InspectableWebContentsImpl::AgentHostClosed( void InspectableWebContentsImpl::AgentHostClosed(
@ -478,9 +495,9 @@ void InspectableWebContentsImpl::DidFinishLoad(content::RenderFrameHost* render_
} }
void InspectableWebContentsImpl::WebContentsDestroyed() { void InspectableWebContentsImpl::WebContentsDestroyed() {
agent_host_->DetachClient();
Observe(nullptr); Observe(nullptr);
agent_host_ = nullptr; Detach();
frontend_loaded_ = false;
for (const auto& pair : pending_requests_) for (const auto& pair : pending_requests_)
delete pair.first; delete pair.first;

View file

@ -167,6 +167,7 @@ class InspectableWebContentsImpl :
DevToolsContentsResizingStrategy contents_resizing_strategy_; DevToolsContentsResizingStrategy contents_resizing_strategy_;
gfx::Rect devtools_bounds_; gfx::Rect devtools_bounds_;
bool can_dock_; bool can_dock_;
bool frontend_loaded_;
scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_; scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_;