Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Unified Diff: content/shell/browser/shell_devtools_bindings.cc

Issue 2756623002: DevTools: extract bindings from ShellDevToolsFrontend (Closed)
Patch Set: fixup Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/shell/browser/shell_devtools_bindings.h ('k') | content/shell/browser/shell_devtools_frontend.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 71%
copy from content/shell/browser/shell_devtools_frontend.cc
copy to content/shell/browser/shell_devtools_bindings.cc
index 4c4eedcfe5f5e395bc52cc8363c8591eee534326..250273d6fc156d147419c9409e30849f578674c6 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>
@@ -39,12 +39,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;
@@ -56,21 +55,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;
@@ -88,9 +84,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;
}
@@ -99,41 +95,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 {
@@ -142,69 +110,60 @@ 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)
+ : WebContentsObserver(devtools_contents),
inspected_contents_(inspected_contents),
+ delegate_(delegate),
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;
+ if (agent_host_)
+ agent_host_->DetachClient(this);
}
-#if !defined(OS_ANDROID)
-void ShellDevToolsFrontend::RenderViewCreated(
+void ShellDevToolsBindings::RenderViewCreated(
RenderViewHost* render_view_host) {
+ CreateFrontendHost();
+}
+
+#if !defined(OS_ANDROID)
+void ShellDevToolsBindings::CreateFrontendHost() {
if (!frontend_host_) {
frontend_host_.reset(DevToolsFrontendHost::Create(
web_contents()->GetMainFrame(),
- base::Bind(&ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend,
+ base::Bind(&ShellDevToolsBindings::HandleMessageFromDevToolsFrontend,
base::Unretained(this))));
}
}
#endif
#if defined(OS_ANDROID)
-void ShellDevToolsFrontend::RenderViewCreated(
- RenderViewHost* render_view_host) {
- // No devtools frontend for android
-}
+void ShellDevToolsBindings::CreateFrontendHost() {}
#endif
-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;
@@ -219,7 +178,7 @@ void ShellDevToolsFrontend::SetPreferences(const std::string& json) {
}
}
-void ShellDevToolsFrontend::HandleMessageFromDevToolsFrontend(
+void ShellDevToolsBindings::HandleMessageFromDevToolsFrontend(
const std::string& message) {
if (!agent_host_)
return;
@@ -227,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;
}
@@ -251,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;
}
@@ -268,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>(
@@ -284,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);
@@ -308,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, &param);
@@ -332,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);
@@ -356,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;
@@ -380,17 +334,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
« no previous file with comments | « content/shell/browser/shell_devtools_bindings.h ('k') | content/shell/browser/shell_devtools_frontend.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698