Commit 0eebabe2 authored by tmarek's avatar tmarek Committed by Commit bot

Expose the full API for attaching devtools clients:

 * propagate information about the success of an attach attempt
 * force-attach support
 * added the IsAttached() method

BUG=546953

Review-Url: https://codereview.chromium.org/2531313002
Cr-Commit-Position: refs/heads/master@{#434956}
parent 64150c96
......@@ -69,11 +69,21 @@ HeadlessDevToolsClientImpl::HeadlessDevToolsClientImpl()
HeadlessDevToolsClientImpl::~HeadlessDevToolsClientImpl() {}
void HeadlessDevToolsClientImpl::AttachToHost(
bool HeadlessDevToolsClientImpl::AttachToHost(
content::DevToolsAgentHost* agent_host) {
DCHECK(!agent_host_);
if (agent_host->AttachClient(this)) {
agent_host_ = agent_host;
return true;
}
return false;
}
void HeadlessDevToolsClientImpl::ForceAttachToHost(
content::DevToolsAgentHost* agent_host) {
DCHECK(!agent_host_);
agent_host_ = agent_host;
agent_host_->AttachClient(this);
agent_host_->ForceAttachClient(this);
}
void HeadlessDevToolsClientImpl::DetachFromHost(
......
......@@ -115,7 +115,8 @@ class HeadlessDevToolsClientImpl : public HeadlessDevToolsClient,
const char* method,
base::Callback<void(const base::Value&)> callback) override;
void AttachToHost(content::DevToolsAgentHost* agent_host);
bool AttachToHost(content::DevToolsAgentHost* agent_host);
void ForceAttachToHost(content::DevToolsAgentHost* agent_host);
void DetachFromHost(content::DevToolsAgentHost* agent_host);
private:
......
......@@ -240,8 +240,15 @@ HeadlessDevToolsTarget* HeadlessWebContentsImpl::GetDevToolsTarget() {
return web_contents()->GetMainFrame()->IsRenderFrameLive() ? this : nullptr;
}
void HeadlessWebContentsImpl::AttachClient(HeadlessDevToolsClient* client) {
HeadlessDevToolsClientImpl::From(client)->AttachToHost(agent_host_.get());
bool HeadlessWebContentsImpl::AttachClient(HeadlessDevToolsClient* client) {
return HeadlessDevToolsClientImpl::From(client)->AttachToHost(
agent_host_.get());
}
void HeadlessWebContentsImpl::ForceAttachClient(
HeadlessDevToolsClient* client) {
HeadlessDevToolsClientImpl::From(client)->ForceAttachToHost(
agent_host_.get());
}
void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) {
......@@ -249,6 +256,11 @@ void HeadlessWebContentsImpl::DetachClient(HeadlessDevToolsClient* client) {
HeadlessDevToolsClientImpl::From(client)->DetachFromHost(agent_host_.get());
}
bool HeadlessWebContentsImpl::IsAttached() {
DCHECK(agent_host_);
return agent_host_->IsAttached();
}
content::WebContents* HeadlessWebContentsImpl::web_contents() const {
return web_contents_.get();
}
......
......@@ -58,8 +58,10 @@ class HeadlessWebContentsImpl : public HeadlessWebContents,
HeadlessDevToolsTarget* GetDevToolsTarget() override;
// HeadlessDevToolsTarget implementation:
void AttachClient(HeadlessDevToolsClient* client) override;
bool AttachClient(HeadlessDevToolsClient* client) override;
void ForceAttachClient(HeadlessDevToolsClient* client) override;
void DetachClient(HeadlessDevToolsClient* client) override;
bool IsAttached() override;
// RenderProcessHostObserver implementation:
void RenderProcessExited(content::RenderProcessHost* host,
......
......@@ -687,4 +687,63 @@ class HeadlessCrashObserverTest : public HeadlessAsyncDevTooledBrowserTest,
HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessCrashObserverTest);
class HeadlessDevToolsClientAttachTest
: public HeadlessAsyncDevTooledBrowserTest {
public:
void RunDevTooledTest() override {
other_devtools_client_ = HeadlessDevToolsClient::Create();
HeadlessDevToolsTarget* devtools_target =
web_contents_->GetDevToolsTarget();
// Try attaching: there's already a client attached.
EXPECT_FALSE(devtools_target->AttachClient(other_devtools_client_.get()));
EXPECT_TRUE(devtools_target->IsAttached());
// Detach the existing client, attach the other client.
devtools_target->DetachClient(devtools_client_.get());
EXPECT_FALSE(devtools_target->IsAttached());
EXPECT_TRUE(devtools_target->AttachClient(other_devtools_client_.get()));
EXPECT_TRUE(devtools_target->IsAttached());
// Now, let's make sure this devtools client works.
other_devtools_client_->GetRuntime()->Evaluate(
"24 * 7", base::Bind(&HeadlessDevToolsClientAttachTest::OnFirstResult,
base::Unretained(this)));
}
void OnFirstResult(std::unique_ptr<runtime::EvaluateResult> result) {
int value;
EXPECT_TRUE(result->GetResult()->HasValue());
EXPECT_TRUE(result->GetResult()->GetValue()->GetAsInteger(&value));
EXPECT_EQ(24 * 7, value);
HeadlessDevToolsTarget* devtools_target =
web_contents_->GetDevToolsTarget();
// Try attach, then force-attach the original client.
EXPECT_FALSE(devtools_target->AttachClient(devtools_client_.get()));
devtools_target->ForceAttachClient(devtools_client_.get());
EXPECT_TRUE(devtools_target->IsAttached());
devtools_client_->GetRuntime()->Evaluate(
"27 * 4", base::Bind(&HeadlessDevToolsClientAttachTest::OnSecondResult,
base::Unretained(this)));
}
void OnSecondResult(std::unique_ptr<runtime::EvaluateResult> result) {
int value;
EXPECT_TRUE(result->GetResult()->HasValue());
EXPECT_TRUE(result->GetResult()->GetValue()->GetAsInteger(&value));
EXPECT_EQ(27 * 4, value);
// If everything worked, this call will not crash, since it
// detaches devtools_client_.
FinishAsynchronousTest();
}
protected:
std::unique_ptr<HeadlessDevToolsClient> other_devtools_client_;
};
HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessDevToolsClientAttachTest);
} // namespace headless
......@@ -20,14 +20,21 @@ class HEADLESS_EXPORT HeadlessDevToolsTarget {
// Attach or detach a client to this target. A client must be attached in
// order to send commands or receive notifications from the target.
//
// A single client may be attached to at most one target at a time. Note that
// currently also only one client may be attached to a single target at a
// time.
// A single client may be attached to at most one target at a time. If
// the target already has a client attached, AttachClient will return false.
// ForceAttachClient will detach any existing connection before attaching
// |client|.
// Note that currently also only one client may be attached to a single target
// at a time.
//
// |client| must outlive this target.
virtual void AttachClient(HeadlessDevToolsClient* client) = 0;
virtual bool AttachClient(HeadlessDevToolsClient* client) = 0;
virtual void ForceAttachClient(HeadlessDevToolsClient* client) = 0;
virtual void DetachClient(HeadlessDevToolsClient* client) = 0;
// Returns true if a devtools client is attached.
virtual bool IsAttached() = 0;
private:
DISALLOW_COPY_AND_ASSIGN(HeadlessDevToolsTarget);
};
......
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