Chromium Code Reviews| Index: content/shell/browser/shell_devtools_bindings.cc |
| diff --git a/content/shell/browser/shell_devtools_frontend.cc b/content/shell/browser/shell_devtools_bindings.cc |
| similarity index 73% |
| copy from content/shell/browser/shell_devtools_frontend.cc |
| copy to content/shell/browser/shell_devtools_bindings.cc |
| index 0bab673f8edd3befd24d4300abcde6cb965df3ed..f497266a25361325a224367c48de0766171cd110 100644 |
| --- a/content/shell/browser/shell_devtools_frontend.cc |
| +++ b/content/shell/browser/shell_devtools_bindings.cc |
| @@ -1,8 +1,8 @@ |
| -// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Copyright 2017 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/shell/browser/shell_devtools_frontend.h" |
| +#include "content/shell/browser/shell_devtools_bindings.h" |
| #include <stddef.h> |
| @@ -17,6 +17,7 @@ |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/render_frame_host.h" |
| #include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/render_widget_host.h" |
|
dgozman
2017/03/16 21:38:07
Do you need this?
chenwilliam
2017/03/17 22:08:26
Removed.
|
| #include "content/public/browser/storage_partition.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/common/content_client.h" |
| @@ -35,12 +36,11 @@ namespace content { |
| namespace { |
| - |
| // ResponseWriter ------------------------------------------------------------- |
| class ResponseWriter : public net::URLFetcherResponseWriter { |
| public: |
| - ResponseWriter(base::WeakPtr<ShellDevToolsFrontend> shell_devtools_, |
| + ResponseWriter(base::WeakPtr<ShellDevToolsBindings> devtools_bindings_, |
| int stream_id); |
| ~ResponseWriter() override; |
| @@ -52,21 +52,18 @@ class ResponseWriter : public net::URLFetcherResponseWriter { |
| int Finish(int net_error, const net::CompletionCallback& callback) override; |
| private: |
| - base::WeakPtr<ShellDevToolsFrontend> shell_devtools_; |
| + base::WeakPtr<ShellDevToolsBindings> devtools_bindings_; |
| int stream_id_; |
| DISALLOW_COPY_AND_ASSIGN(ResponseWriter); |
| }; |
| ResponseWriter::ResponseWriter( |
| - base::WeakPtr<ShellDevToolsFrontend> shell_devtools, |
| + base::WeakPtr<ShellDevToolsBindings> shell_devtools, |
| int stream_id) |
| - : shell_devtools_(shell_devtools), |
| - stream_id_(stream_id) { |
| -} |
| + : devtools_bindings_(shell_devtools), stream_id_(stream_id) {} |
| -ResponseWriter::~ResponseWriter() { |
| -} |
| +ResponseWriter::~ResponseWriter() {} |
| int ResponseWriter::Initialize(const net::CompletionCallback& callback) { |
| return net::OK; |
| @@ -84,9 +81,9 @@ int ResponseWriter::Write(net::IOBuffer* buffer, |
| content::BrowserThread::PostTask( |
| content::BrowserThread::UI, FROM_HERE, |
| - base::Bind(&ShellDevToolsFrontend::CallClientFunction, |
| - shell_devtools_, "DevToolsAPI.streamWrite", |
| - base::Owned(id), base::Owned(chunkValue), nullptr)); |
| + base::Bind(&ShellDevToolsBindings::CallClientFunction, devtools_bindings_, |
| + "DevToolsAPI.streamWrite", base::Owned(id), |
| + base::Owned(chunkValue), nullptr)); |
| return num_bytes; |
| } |
| @@ -95,41 +92,21 @@ int ResponseWriter::Finish(int net_error, |
| return net::OK; |
| } |
| -static GURL GetFrontendURL() { |
| - int port = ShellDevToolsManagerDelegate::GetHttpHandlerPort(); |
| - return GURL( |
| - base::StringPrintf("http://127.0.0.1:%d/devtools/inspector.html", port)); |
| -} |
| - |
| } // namespace |
| // This constant should be in sync with |
| // the constant at devtools_ui_bindings.cc. |
| const size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4; |
| -// static |
| -ShellDevToolsFrontend* ShellDevToolsFrontend::Show( |
| - WebContents* inspected_contents) { |
| - Shell* shell = Shell::CreateNewWindow(inspected_contents->GetBrowserContext(), |
| - GURL(), |
| - NULL, |
| - gfx::Size()); |
| - ShellDevToolsFrontend* devtools_frontend = new ShellDevToolsFrontend( |
| - shell, |
| - inspected_contents); |
| - shell->LoadURL(GetFrontendURL()); |
| - return devtools_frontend; |
| -} |
| - |
| -void ShellDevToolsFrontend::Activate() { |
| - frontend_shell_->ActivateContents(web_contents()); |
| +void ShellDevToolsBindings::Activate() { |
| + web_contents()->GetRenderViewHost()->GetWidget()->Focus(); |
| } |
| -void ShellDevToolsFrontend::Focus() { |
| +void ShellDevToolsBindings::Focus() { |
| web_contents()->Focus(); |
| } |
| -void ShellDevToolsFrontend::InspectElementAt(int x, int y) { |
| +void ShellDevToolsBindings::InspectElementAt(int x, int y) { |
| if (agent_host_) { |
| agent_host_->InspectElement(this, x, y); |
| } else { |
| @@ -138,60 +115,55 @@ void ShellDevToolsFrontend::InspectElementAt(int x, int y) { |
| } |
| } |
| -void ShellDevToolsFrontend::Close() { |
| - frontend_shell_->Close(); |
| -} |
| - |
| -void ShellDevToolsFrontend::DisconnectFromTarget() { |
| +void ShellDevToolsBindings::DisconnectFromTarget() { |
| if (!agent_host_) |
| return; |
| agent_host_->DetachClient(this); |
| agent_host_ = NULL; |
| } |
| -ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell, |
| +ShellDevToolsBindings::ShellDevToolsBindings(WebContents* devtools_contents, |
| WebContents* inspected_contents) |
| - : WebContentsObserver(frontend_shell->web_contents()), |
| - frontend_shell_(frontend_shell), |
| + : WebContentsObserver(devtools_contents), |
| + devtools_contents_(devtools_contents), |
| inspected_contents_(inspected_contents), |
| inspect_element_at_x_(-1), |
| inspect_element_at_y_(-1), |
| - weak_factory_(this) { |
| -} |
| + weak_factory_(this) {} |
| -ShellDevToolsFrontend::~ShellDevToolsFrontend() { |
| +ShellDevToolsBindings::~ShellDevToolsBindings() { |
| for (const auto& pair : pending_requests_) |
| delete pair.first; |
| } |
| -void ShellDevToolsFrontend::RenderViewCreated( |
| +void ShellDevToolsBindings::RenderViewCreated( |
| RenderViewHost* render_view_host) { |
| if (!frontend_host_) { |
| frontend_host_.reset(DevToolsFrontendHost::Create( |
| web_contents()->GetMainFrame(), |
| - base::Bind(&ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend, |
| + base::Bind(&ShellDevToolsBindings::HandleMessageFromDevToolsFrontend, |
| base::Unretained(this)))); |
| } |
| } |
| -void ShellDevToolsFrontend::DocumentAvailableInMainFrame() { |
| +void ShellDevToolsBindings::DocumentAvailableInMainFrame() { |
| agent_host_ = DevToolsAgentHost::GetOrCreateFor(inspected_contents_); |
| agent_host_->AttachClient(this); |
| if (inspect_element_at_x_ != -1) { |
| - agent_host_->InspectElement( |
| - this, inspect_element_at_x_, inspect_element_at_y_); |
| + agent_host_->InspectElement(this, inspect_element_at_x_, |
| + inspect_element_at_y_); |
| inspect_element_at_x_ = -1; |
| inspect_element_at_y_ = -1; |
| } |
| } |
| -void ShellDevToolsFrontend::WebContentsDestroyed() { |
| +void ShellDevToolsBindings::WebContentsDestroyed() { |
| if (agent_host_) |
| agent_host_->DetachClient(this); |
| delete this; |
| } |
| -void ShellDevToolsFrontend::SetPreferences(const std::string& json) { |
| +void ShellDevToolsBindings::SetPreferences(const std::string& json) { |
| preferences_.Clear(); |
| if (json.empty()) |
| return; |
| @@ -206,7 +178,7 @@ void ShellDevToolsFrontend::SetPreferences(const std::string& json) { |
| } |
| } |
| -void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| +void ShellDevToolsBindings::HandleMessageFromDevToolsFrontend( |
| const std::string& message) { |
| if (!agent_host_) |
| return; |
| @@ -214,8 +186,7 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| base::ListValue* params = NULL; |
| base::DictionaryValue* dict = NULL; |
| std::unique_ptr<base::Value> parsed_message = base::JSONReader::Read(message); |
| - if (!parsed_message || |
| - !parsed_message->GetAsDictionary(&dict) || |
| + if (!parsed_message || !parsed_message->GetAsDictionary(&dict) || |
| !dict->GetString("method", &method)) { |
| return; |
| } |
| @@ -238,8 +209,7 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| std::string url; |
| std::string headers; |
| int stream_id; |
| - if (!params->GetString(0, &url) || |
| - !params->GetString(1, &headers) || |
| + if (!params->GetString(0, &url) || !params->GetString(1, &headers) || |
| !params->GetInteger(2, &stream_id)) { |
| return; |
| } |
| @@ -255,10 +225,9 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| net::URLFetcher* fetcher = |
| net::URLFetcher::Create(gurl, net::URLFetcher::GET, this).release(); |
| pending_requests_[fetcher] = request_id; |
| - fetcher->SetRequestContext( |
| - BrowserContext::GetDefaultStoragePartition( |
| - web_contents()->GetBrowserContext())-> |
| - GetURLRequestContext()); |
| + fetcher->SetRequestContext(BrowserContext::GetDefaultStoragePartition( |
| + web_contents()->GetBrowserContext()) |
| + ->GetURLRequestContext()); |
| fetcher->SetExtraRequestHeaders(headers); |
| fetcher->SaveResponseWithWriter( |
| std::unique_ptr<net::URLFetcherResponseWriter>( |
| @@ -271,8 +240,7 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| } else if (method == "setPreference") { |
| std::string name; |
| std::string value; |
| - if (!params->GetString(0, &name) || |
| - !params->GetString(1, &value)) { |
| + if (!params->GetString(0, &name) || !params->GetString(1, &value)) { |
| return; |
| } |
| preferences_.SetStringWithoutPathExpansion(name, value); |
| @@ -295,9 +263,9 @@ void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| SendMessageAck(request_id, nullptr); |
| } |
| -void ShellDevToolsFrontend::DispatchProtocolMessage( |
| - DevToolsAgentHost* agent_host, const std::string& message) { |
| - |
| +void ShellDevToolsBindings::DispatchProtocolMessage( |
| + DevToolsAgentHost* agent_host, |
| + const std::string& message) { |
| if (message.length() < kMaxMessageChunkSize) { |
| std::string param; |
| base::EscapeJSONString(message, true, ¶m); |
| @@ -319,7 +287,7 @@ void ShellDevToolsFrontend::DispatchProtocolMessage( |
| } |
| } |
| -void ShellDevToolsFrontend::OnURLFetchComplete(const net::URLFetcher* source) { |
| +void ShellDevToolsBindings::OnURLFetchComplete(const net::URLFetcher* source) { |
| // TODO(pfeldman): this is a copy of chrome's devtools_ui_bindings.cc. |
| // We should handle some of the commands including this one in content. |
| DCHECK(source); |
| @@ -343,11 +311,10 @@ void ShellDevToolsFrontend::OnURLFetchComplete(const net::URLFetcher* source) { |
| delete source; |
| } |
| -void ShellDevToolsFrontend::CallClientFunction( |
| - const std::string& function_name, |
| - const base::Value* arg1, |
| - const base::Value* arg2, |
| - const base::Value* arg3) { |
| +void ShellDevToolsBindings::CallClientFunction(const std::string& function_name, |
| + const base::Value* arg1, |
| + const base::Value* arg2, |
| + const base::Value* arg3) { |
| std::string javascript = function_name + "("; |
| if (arg1) { |
| std::string json; |
| @@ -367,17 +334,19 @@ void ShellDevToolsFrontend::CallClientFunction( |
| base::UTF8ToUTF16(javascript)); |
| } |
| -void ShellDevToolsFrontend::SendMessageAck(int request_id, |
| +void ShellDevToolsBindings::SendMessageAck(int request_id, |
| const base::Value* arg) { |
| base::Value id_value(request_id); |
| - CallClientFunction("DevToolsAPI.embedderMessageAck", |
| - &id_value, arg, nullptr); |
| + CallClientFunction("DevToolsAPI.embedderMessageAck", &id_value, arg, nullptr); |
| } |
| -void ShellDevToolsFrontend::AgentHostClosed( |
| - DevToolsAgentHost* agent_host, bool replaced) { |
| +void ShellDevToolsBindings::AgentHostClosed(DevToolsAgentHost* agent_host, |
| + bool replaced) { |
| agent_host_ = nullptr; |
| - frontend_shell_->Close(); |
| + WebContentsDelegate* delegate = devtools_contents_->GetDelegate(); |
|
dgozman
2017/03/16 21:38:07
This belongs to ShellDevToolsFrontend. You can not
chenwilliam
2017/03/17 22:08:26
Done.
|
| + if (delegate) { |
| + delegate->CloseContents(devtools_contents_); |
| + } |
| } |
| } // namespace content |