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 70% |
| copy from content/shell/browser/shell_devtools_frontend.cc |
| copy to content/shell/browser/shell_devtools_bindings.cc |
| index 0bab673f8edd3befd24d4300abcde6cb965df3ed..7a3a50f912147035b7132693c2fa67b9cc3664e4 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> |
| @@ -35,12 +35,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 +51,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 +80,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 +91,13 @@ 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 ShellDevToolsFrontend::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 +106,52 @@ void ShellDevToolsFrontend::InspectElementAt(int x, int y) { |
| } |
| } |
| -void ShellDevToolsFrontend::Close() { |
| - frontend_shell_->Close(); |
| -} |
| - |
| -void ShellDevToolsFrontend::DisconnectFromTarget() { |
| - if (!agent_host_) |
| - return; |
| - agent_host_->DetachClient(this); |
| - agent_host_ = NULL; |
| -} |
| - |
| -ShellDevToolsFrontend::ShellDevToolsFrontend(Shell* frontend_shell, |
| - WebContents* inspected_contents) |
| - : WebContentsObserver(frontend_shell->web_contents()), |
| - frontend_shell_(frontend_shell), |
| +ShellDevToolsBindings::ShellDevToolsBindings(WebContents* devtools_contents, |
| + WebContents* inspected_contents, |
| + ShellDevToolsDelegate* delegate, |
| + const std::string& settings, |
| + const GURL& devtools_url) |
| + : WebContentsObserver(devtools_contents), |
| inspected_contents_(inspected_contents), |
| + delegate_(delegate), |
| inspect_element_at_x_(-1), |
| inspect_element_at_y_(-1), |
| weak_factory_(this) { |
| + SetPreferences(settings); |
| + NavigationController::LoadURLParams params(devtools_url); |
| + params.transition_type = ui::PageTransitionFromInt( |
| + ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
| + web_contents()->GetController().LoadURLWithParams(params); |
| + web_contents()->Focus(); |
| + frontend_host_.reset(DevToolsFrontendHost::Create( |
| + web_contents()->GetMainFrame(), |
| + base::Bind(&ShellDevToolsBindings::HandleMessageFromDevToolsFrontend, |
| + base::Unretained(this)))); |
| } |
| -ShellDevToolsFrontend::~ShellDevToolsFrontend() { |
| +ShellDevToolsBindings::~ShellDevToolsBindings() { |
| for (const auto& pair : pending_requests_) |
| delete pair.first; |
| + if (agent_host_) |
| + agent_host_->DetachClient(this); |
| } |
| -void ShellDevToolsFrontend::RenderViewCreated( |
|
dgozman
2017/03/21 20:57:35
We still need this in case RenderFrameHost for mai
chenwilliam
2017/03/21 23:36:42
Done.
|
| - RenderViewHost* render_view_host) { |
| - if (!frontend_host_) { |
| - frontend_host_.reset(DevToolsFrontendHost::Create( |
| - web_contents()->GetMainFrame(), |
| - base::Bind(&ShellDevToolsFrontend::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() { |
| - if (agent_host_) |
| - agent_host_->DetachClient(this); |
| +void ShellDevToolsBindings::WebContentsDestroyed() { |
| delete this; |
|
dgozman
2017/03/21 20:57:35
Deleting this while being owned via std::unique_pt
chenwilliam
2017/03/21 23:36:43
Done.
|
| } |
| -void ShellDevToolsFrontend::SetPreferences(const std::string& json) { |
| +void ShellDevToolsBindings::SetPreferences(const std::string& json) { |
| preferences_.Clear(); |
| if (json.empty()) |
| return; |
| @@ -206,7 +166,7 @@ void ShellDevToolsFrontend::SetPreferences(const std::string& json) { |
| } |
| } |
| -void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend( |
| +void ShellDevToolsBindings::HandleMessageFromDevToolsFrontend( |
| const std::string& message) { |
| if (!agent_host_) |
| return; |
| @@ -214,8 +174,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 +197,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 +213,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 +228,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 +251,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 +275,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 +299,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 +322,17 @@ 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(); |
| + if (delegate_) |
| + delegate_->Close(); |
| } |
| } // namespace content |