Commit d3d86f52 authored by Anand K. Mistry's avatar Anand K. Mistry Committed by Commit Bot

[Extensions Functions] Migrate debugger API to ExtensionFunction

Bug: 634140
Change-Id: I38b8ad3ec7c01c2dedbb65281d7d4541bd553688
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2078371
Commit-Queue: Anand Mistry <amistry@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746576}
parent 6b639f6c
...@@ -360,12 +360,12 @@ void ExtensionDevToolsClientHost::DispatchProtocolMessage( ...@@ -360,12 +360,12 @@ void ExtensionDevToolsClientHost::DispatchProtocolMessage(
EventRouter::Get(profile_)->DispatchEventToExtension(extension_id(), EventRouter::Get(profile_)->DispatchEventToExtension(extension_id(),
std::move(event)); std::move(event));
} else { } else {
DebuggerSendCommandFunction* function = pending_requests_[id].get(); auto it = pending_requests_.find(id);
if (!function) if (it == pending_requests_.end())
return; return;
function->SendResponseBody(dictionary); it->second->SendResponseBody(dictionary);
pending_requests_.erase(id); pending_requests_.erase(it);
} }
} }
...@@ -398,46 +398,49 @@ DebuggerFunction::DebuggerFunction() ...@@ -398,46 +398,49 @@ DebuggerFunction::DebuggerFunction()
: client_host_(NULL) { : client_host_(NULL) {
} }
DebuggerFunction::~DebuggerFunction() { DebuggerFunction::~DebuggerFunction() = default;
}
void DebuggerFunction::FormatErrorMessage(const std::string& format) { std::string DebuggerFunction::FormatErrorMessage(const std::string& format) {
if (debuggee_.tab_id) if (debuggee_.tab_id) {
error_ = ErrorUtils::FormatErrorMessage( return ErrorUtils::FormatErrorMessage(
format, debugger_api_constants::kTabTargetType, format, debugger_api_constants::kTabTargetType,
base::NumberToString(*debuggee_.tab_id)); base::NumberToString(*debuggee_.tab_id));
else if (debuggee_.extension_id) }
error_ = ErrorUtils::FormatErrorMessage( if (debuggee_.extension_id) {
return ErrorUtils::FormatErrorMessage(
format, debugger_api_constants::kBackgroundPageTargetType, format, debugger_api_constants::kBackgroundPageTargetType,
*debuggee_.extension_id); *debuggee_.extension_id);
else }
error_ = ErrorUtils::FormatErrorMessage(
format, debugger_api_constants::kOpaqueTargetType, return ErrorUtils::FormatErrorMessage(
*debuggee_.target_id); format, debugger_api_constants::kOpaqueTargetType, *debuggee_.target_id);
} }
bool DebuggerFunction::InitAgentHost() { bool DebuggerFunction::InitAgentHost(std::string* error) {
if (debuggee_.tab_id) { if (debuggee_.tab_id) {
WebContents* web_contents = nullptr; WebContents* web_contents = nullptr;
bool result = ExtensionTabUtil::GetTabById(*debuggee_.tab_id, GetProfile(), bool result = ExtensionTabUtil::GetTabById(
include_incognito_information(), *debuggee_.tab_id, browser_context(), include_incognito_information(),
&web_contents); &web_contents);
if (result && web_contents) { if (result && web_contents) {
// TODO(rdevlin.cronin) This should definitely be GetLastCommittedURL(). // TODO(rdevlin.cronin) This should definitely be GetLastCommittedURL().
GURL url = web_contents->GetVisibleURL(); GURL url = web_contents->GetVisibleURL();
if (!ExtensionCanAttachToURL(*extension(), url, GetProfile(), &error_)) if (!ExtensionCanAttachToURL(
*extension(), url, Profile::FromBrowserContext(browser_context()),
error)) {
return false; return false;
}
agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents); agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents);
} }
} else if (debuggee_.extension_id) { } else if (debuggee_.extension_id) {
ExtensionHost* extension_host = ExtensionHost* extension_host =
ProcessManager::Get(GetProfile()) ProcessManager::Get(browser_context())
->GetBackgroundHostForExtension(*debuggee_.extension_id); ->GetBackgroundHostForExtension(*debuggee_.extension_id);
if (extension_host) { if (extension_host) {
if (extension()->permissions_data()->IsRestrictedUrl( if (extension()->permissions_data()->IsRestrictedUrl(
extension_host->GetURL(), &error_)) { extension_host->GetURL(), error)) {
return false; return false;
} }
agent_host_ = agent_host_ =
...@@ -447,7 +450,7 @@ bool DebuggerFunction::InitAgentHost() { ...@@ -447,7 +450,7 @@ bool DebuggerFunction::InitAgentHost() {
agent_host_ = DevToolsAgentHost::GetForId(*debuggee_.target_id); agent_host_ = DevToolsAgentHost::GetForId(*debuggee_.target_id);
if (agent_host_.get()) { if (agent_host_.get()) {
if (extension()->permissions_data()->IsRestrictedUrl( if (extension()->permissions_data()->IsRestrictedUrl(
agent_host_->GetURL(), &error_)) { agent_host_->GetURL(), error)) {
agent_host_ = nullptr; agent_host_ = nullptr;
return false; return false;
} }
...@@ -473,24 +476,24 @@ bool DebuggerFunction::InitAgentHost() { ...@@ -473,24 +476,24 @@ bool DebuggerFunction::InitAgentHost() {
DevToolsAgentHost::CreateServerSocketCallback()); DevToolsAgentHost::CreateServerSocketCallback());
} }
} else { } else {
error_ = debugger_api_constants::kInvalidTargetError; *error = debugger_api_constants::kInvalidTargetError;
return false; return false;
} }
if (!agent_host_.get()) { if (!agent_host_.get()) {
FormatErrorMessage(debugger_api_constants::kNoTargetError); *error = FormatErrorMessage(debugger_api_constants::kNoTargetError);
return false; return false;
} }
return true; return true;
} }
bool DebuggerFunction::InitClientHost() { bool DebuggerFunction::InitClientHost(std::string* error) {
if (!InitAgentHost()) if (!InitAgentHost(error))
return false; return false;
client_host_ = FindClientHost(); client_host_ = FindClientHost();
if (!client_host_) { if (!client_host_) {
FormatErrorMessage(debugger_api_constants::kNotAttachedError); *error = FormatErrorMessage(debugger_api_constants::kNotAttachedError);
return false; return false;
} }
...@@ -516,98 +519,93 @@ ExtensionDevToolsClientHost* DebuggerFunction::FindClientHost() { ...@@ -516,98 +519,93 @@ ExtensionDevToolsClientHost* DebuggerFunction::FindClientHost() {
// DebuggerAttachFunction ----------------------------------------------------- // DebuggerAttachFunction -----------------------------------------------------
DebuggerAttachFunction::DebuggerAttachFunction() { DebuggerAttachFunction::DebuggerAttachFunction() = default;
}
DebuggerAttachFunction::~DebuggerAttachFunction() { DebuggerAttachFunction::~DebuggerAttachFunction() = default;
}
bool DebuggerAttachFunction::RunAsync() { ExtensionFunction::ResponseAction DebuggerAttachFunction::Run() {
std::unique_ptr<Attach::Params> params(Attach::Params::Create(*args_)); std::unique_ptr<Attach::Params> params(Attach::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get()); EXTENSION_FUNCTION_VALIDATE(params.get());
CopyDebuggee(&debuggee_, params->target); CopyDebuggee(&debuggee_, params->target);
if (!InitAgentHost()) std::string error;
return false; if (!InitAgentHost(&error))
return RespondNow(Error(error));
if (!DevToolsAgentHost::IsSupportedProtocolVersion( if (!DevToolsAgentHost::IsSupportedProtocolVersion(
params->required_version)) { params->required_version)) {
error_ = ErrorUtils::FormatErrorMessage( return RespondNow(Error(ErrorUtils::FormatErrorMessage(
debugger_api_constants::kProtocolVersionNotSupportedError, debugger_api_constants::kProtocolVersionNotSupportedError,
params->required_version); params->required_version)));
return false;
} }
if (FindClientHost()) { if (FindClientHost()) {
FormatErrorMessage(debugger_api_constants::kAlreadyAttachedError); return RespondNow(Error(
return false; FormatErrorMessage(debugger_api_constants::kAlreadyAttachedError)));
} }
auto host = std::make_unique<ExtensionDevToolsClientHost>( auto host = std::make_unique<ExtensionDevToolsClientHost>(
GetProfile(), agent_host_.get(), extension(), debuggee_); Profile::FromBrowserContext(browser_context()), agent_host_.get(),
extension(), debuggee_);
if (!host->Attach()) { if (!host->Attach()) {
error_ = debugger_api_constants::kRestrictedError; return RespondNow(Error(debugger_api_constants::kRestrictedError));
return false;
} }
host.release(); // An attached client host manages its own lifetime. host.release(); // An attached client host manages its own lifetime.
SendResponse(true); return RespondNow(NoArguments());
return true;
} }
// DebuggerDetachFunction ----------------------------------------------------- // DebuggerDetachFunction -----------------------------------------------------
DebuggerDetachFunction::DebuggerDetachFunction() { DebuggerDetachFunction::DebuggerDetachFunction() = default;
}
DebuggerDetachFunction::~DebuggerDetachFunction() { DebuggerDetachFunction::~DebuggerDetachFunction() = default;
}
bool DebuggerDetachFunction::RunAsync() { ExtensionFunction::ResponseAction DebuggerDetachFunction::Run() {
std::unique_ptr<Detach::Params> params(Detach::Params::Create(*args_)); std::unique_ptr<Detach::Params> params(Detach::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get()); EXTENSION_FUNCTION_VALIDATE(params.get());
CopyDebuggee(&debuggee_, params->target); CopyDebuggee(&debuggee_, params->target);
if (!InitClientHost()) std::string error;
return false; if (!InitClientHost(&error))
return RespondNow(Error(error));
client_host_->RespondDetachedToPendingRequests(); client_host_->RespondDetachedToPendingRequests();
client_host_->Close(); client_host_->Close();
SendResponse(true); return RespondNow(NoArguments());
return true;
} }
// DebuggerSendCommandFunction ------------------------------------------------ // DebuggerSendCommandFunction ------------------------------------------------
DebuggerSendCommandFunction::DebuggerSendCommandFunction() { DebuggerSendCommandFunction::DebuggerSendCommandFunction() = default;
}
DebuggerSendCommandFunction::~DebuggerSendCommandFunction() { DebuggerSendCommandFunction::~DebuggerSendCommandFunction() = default;
}
bool DebuggerSendCommandFunction::RunAsync() { ExtensionFunction::ResponseAction DebuggerSendCommandFunction::Run() {
std::unique_ptr<SendCommand::Params> params( std::unique_ptr<SendCommand::Params> params(
SendCommand::Params::Create(*args_)); SendCommand::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get()); EXTENSION_FUNCTION_VALIDATE(params.get());
CopyDebuggee(&debuggee_, params->target); CopyDebuggee(&debuggee_, params->target);
if (!InitClientHost()) std::string error;
return false; if (!InitClientHost(&error))
return RespondNow(Error(error));
client_host_->SendMessageToBackend(this, params->method, client_host_->SendMessageToBackend(this, params->method,
params->command_params.get()); params->command_params.get());
return true; if (did_respond())
return AlreadyResponded();
return RespondLater();
} }
void DebuggerSendCommandFunction::SendResponseBody( void DebuggerSendCommandFunction::SendResponseBody(
base::DictionaryValue* response) { base::DictionaryValue* response) {
base::Value* error_body; base::Value* error_body;
if (response->Get("error", &error_body)) { if (response->Get("error", &error_body)) {
base::JSONWriter::Write(*error_body, &error_); std::string error;
SendResponse(false); base::JSONWriter::Write(*error_body, &error);
Respond(Error(error));
return; return;
} }
...@@ -616,13 +614,11 @@ void DebuggerSendCommandFunction::SendResponseBody( ...@@ -616,13 +614,11 @@ void DebuggerSendCommandFunction::SendResponseBody(
if (response->GetDictionary("result", &result_body)) if (response->GetDictionary("result", &result_body))
result.additional_properties.Swap(result_body); result.additional_properties.Swap(result_body);
SetResultList(SendCommand::Results::Create(result)); Respond(ArgumentList(SendCommand::Results::Create(result)));
SendResponse(true);
} }
void DebuggerSendCommandFunction::SendDetachedError() { void DebuggerSendCommandFunction::SendDetachedError() {
error_ = debugger_api_constants::kDetachedWhileHandlingError; Respond(Error(debugger_api_constants::kDetachedWhileHandlingError));
SendResponse(false);
} }
// DebuggerGetTargetsFunction ------------------------------------------------- // DebuggerGetTargetsFunction -------------------------------------------------
...@@ -677,27 +673,17 @@ std::unique_ptr<base::DictionaryValue> SerializeTarget( ...@@ -677,27 +673,17 @@ std::unique_ptr<base::DictionaryValue> SerializeTarget(
} // namespace } // namespace
DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() { DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() = default;
}
DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() { DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() = default;
}
bool DebuggerGetTargetsFunction::RunAsync() { ExtensionFunction::ResponseAction DebuggerGetTargetsFunction::Run() {
content::DevToolsAgentHost::List list = DevToolsAgentHost::GetOrCreateAll(); content::DevToolsAgentHost::List list = DevToolsAgentHost::GetOrCreateAll();
base::PostTask(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&DebuggerGetTargetsFunction::SendTargetList, this, list));
return true;
}
void DebuggerGetTargetsFunction::SendTargetList(
const content::DevToolsAgentHost::List& target_list) {
std::unique_ptr<base::ListValue> result(new base::ListValue()); std::unique_ptr<base::ListValue> result(new base::ListValue());
for (size_t i = 0; i < target_list.size(); ++i) for (size_t i = 0; i < list.size(); ++i)
result->Append(SerializeTarget(target_list[i])); result->Append(SerializeTarget(list[i]));
SetResult(std::move(result));
SendResponse(true); return RespondNow(OneArgument(std::move(result)));
} }
} // namespace extensions } // namespace extensions
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/common/extensions/api/debugger.h" #include "chrome/common/extensions/api/debugger.h"
#include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/devtools_agent_host.h"
#include "extensions/browser/extension_function.h"
using extensions::api::debugger::Debuggee; using extensions::api::debugger::Debuggee;
...@@ -26,15 +26,15 @@ class DictionaryValue; ...@@ -26,15 +26,15 @@ class DictionaryValue;
namespace extensions { namespace extensions {
class ExtensionDevToolsClientHost; class ExtensionDevToolsClientHost;
class DebuggerFunction : public ChromeAsyncExtensionFunction { class DebuggerFunction : public ExtensionFunction {
protected: protected:
DebuggerFunction(); DebuggerFunction();
~DebuggerFunction() override; ~DebuggerFunction() override;
void FormatErrorMessage(const std::string& format); std::string FormatErrorMessage(const std::string& format);
bool InitAgentHost(); bool InitAgentHost(std::string* error);
bool InitClientHost(); bool InitClientHost(std::string* error);
ExtensionDevToolsClientHost* FindClientHost(); ExtensionDevToolsClientHost* FindClientHost();
Debuggee debuggee_; Debuggee debuggee_;
...@@ -53,7 +53,7 @@ class DebuggerAttachFunction : public DebuggerFunction { ...@@ -53,7 +53,7 @@ class DebuggerAttachFunction : public DebuggerFunction {
~DebuggerAttachFunction() override; ~DebuggerAttachFunction() override;
// ExtensionFunction: // ExtensionFunction:
bool RunAsync() override; ResponseAction Run() override;
}; };
// Implements the debugger.detach() extension function. // Implements the debugger.detach() extension function.
...@@ -67,7 +67,7 @@ class DebuggerDetachFunction : public DebuggerFunction { ...@@ -67,7 +67,7 @@ class DebuggerDetachFunction : public DebuggerFunction {
~DebuggerDetachFunction() override; ~DebuggerDetachFunction() override;
// ExtensionFunction: // ExtensionFunction:
bool RunAsync() override; ResponseAction Run() override;
}; };
// Implements the debugger.sendCommand() extension function. // Implements the debugger.sendCommand() extension function.
...@@ -83,7 +83,7 @@ class DebuggerSendCommandFunction : public DebuggerFunction { ...@@ -83,7 +83,7 @@ class DebuggerSendCommandFunction : public DebuggerFunction {
~DebuggerSendCommandFunction() override; ~DebuggerSendCommandFunction() override;
// ExtensionFunction: // ExtensionFunction:
bool RunAsync() override; ResponseAction Run() override;
}; };
// Implements the debugger.getTargets() extension function. // Implements the debugger.getTargets() extension function.
...@@ -97,10 +97,7 @@ class DebuggerGetTargetsFunction : public DebuggerFunction { ...@@ -97,10 +97,7 @@ class DebuggerGetTargetsFunction : public DebuggerFunction {
~DebuggerGetTargetsFunction() override; ~DebuggerGetTargetsFunction() override;
// ExtensionFunction: // ExtensionFunction:
bool RunAsync() override; ResponseAction Run() override;
private:
void SendTargetList(const content::DevToolsAgentHost::List& target_list);
}; };
} // namespace extensions } // namespace extensions
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment