blob: b73e83bdbd807eed52bf64d9d437bce2d7aeb314 [file] [log] [blame]
[email protected]89f8a5cc2012-03-05 16:33:511// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f85d5412009-03-17 12:05:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dcheng59716272016-04-09 05:19:085#include "content/browser/devtools/devtools_manager.h"
6
7#include <memory>
8
pfeldmana9e7dda2016-08-26 14:35:179#include "base/guid.h"
skyostil95082a62015-06-05 19:53:0710#include "base/location.h"
avib7348942015-12-25 20:57:1011#include "base/macros.h"
gabf64a25e2017-05-12 19:42:5612#include "base/message_loop/message_loop.h"
fdoraye716a902016-07-05 16:05:4913#include "base/run_loop.h"
skyostil95082a62015-06-05 19:53:0714#include "base/single_thread_task_runner.h"
gab30f26df2016-05-11 19:37:5515#include "base/threading/thread_task_runner_handle.h"
[email protected]a43858f2013-06-28 15:18:3716#include "base/time/time.h"
kinuko647819d2014-12-22 09:54:4417#include "content/browser/devtools/shared_worker_devtools_manager.h"
[email protected]bfaf36052011-11-16 07:30:2318#include "content/common/view_messages.h"
dgozman24bfc912014-09-29 11:04:1819#include "content/public/browser/browser_context.h"
[email protected]87f3c082011-10-19 18:07:4420#include "content/public/browser/content_browser_client.h"
[email protected]98f66112012-12-25 12:59:3621#include "content/public/browser/devtools_agent_host.h"
[email protected]cb286af2013-03-26 12:46:4522#include "content/public/browser/devtools_external_agent_proxy.h"
23#include "content/public/browser/devtools_external_agent_proxy_delegate.h"
[email protected]674bc592011-12-20 23:00:4224#include "content/public/browser/web_contents_delegate.h"
dgozman7e41e3ec2017-06-12 21:39:0525#include "content/public/common/browser_side_navigation_policy.h"
Arthur Hemery7de58aa2017-08-30 13:22:2326#include "content/public/test/navigation_simulator.h"
dgozman24bfc912014-09-29 11:04:1827#include "content/public/test/test_utils.h"
[email protected]c6681f32012-06-05 14:43:0128#include "content/test/test_content_browser_client.h"
[email protected]4bfd4612013-12-05 18:12:4829#include "content/test/test_render_view_host.h"
[email protected]4172b082013-02-25 18:07:3430#include "content/test/test_web_contents.h"
[email protected]79ea4862011-02-24 00:46:4431#include "testing/gtest/include/gtest/gtest.h"
[email protected]f85d5412009-03-17 12:05:4832
[email protected]e5fc1632011-08-08 07:51:5333using base::TimeDelta;
34
[email protected]de551c62012-10-24 20:50:0835namespace content {
[email protected]f85d5412009-03-17 12:05:4836namespace {
37
[email protected]b50452f2014-08-18 12:31:4438class TestDevToolsClientHost : public DevToolsAgentHostClient {
[email protected]f85d5412009-03-17 12:05:4839 public:
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2840 TestDevToolsClientHost() : last_sent_message(nullptr), closed_(false) {}
[email protected]f85d5412009-03-17 12:05:4841
dchengc2282aa2014-10-21 12:07:5842 ~TestDevToolsClientHost() override { EXPECT_TRUE(closed_); }
[email protected]f85d5412009-03-17 12:05:4843
[email protected]b50452f2014-08-18 12:31:4444 void Close() {
[email protected]7aa27fd2009-03-23 10:43:5845 EXPECT_FALSE(closed_);
[email protected]f85d5412009-03-17 12:05:4846 close_counter++;
pfeldmanfb8e7472016-06-08 21:13:3747 agent_host_->DetachClient(this);
[email protected]f85d5412009-03-17 12:05:4848 closed_ = true;
49 }
[email protected]b50452f2014-08-18 12:31:4450
Pavel Feldmana344d932017-10-31 20:24:5251 void AgentHostClosed(DevToolsAgentHost* agent_host) override { FAIL(); }
[email protected]7aa27fd2009-03-23 10:43:5852
dchengc2282aa2014-10-21 12:07:5853 void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
54 const std::string& message) override {
[email protected]7aa27fd2009-03-23 10:43:5855 last_sent_message = &message;
56 }
[email protected]f85d5412009-03-17 12:05:4857
[email protected]b50452f2014-08-18 12:31:4458 void InspectAgentHost(DevToolsAgentHost* agent_host) {
59 agent_host_ = agent_host;
60 agent_host_->AttachClient(this);
[email protected]b3b26072012-11-02 11:20:5161 }
62
[email protected]b50452f2014-08-18 12:31:4463 DevToolsAgentHost* agent_host() { return agent_host_.get(); }
64
[email protected]f85d5412009-03-17 12:05:4865 static void ResetCounters() {
[email protected]f85d5412009-03-17 12:05:4866 close_counter = 0;
67 }
68
[email protected]f85d5412009-03-17 12:05:4869 static int close_counter;
70
[email protected]0e12d7d2011-12-01 16:21:4471 const std::string* last_sent_message;
[email protected]7aa27fd2009-03-23 10:43:5872
[email protected]f85d5412009-03-17 12:05:4873 private:
[email protected]f85d5412009-03-17 12:05:4874 bool closed_;
[email protected]b50452f2014-08-18 12:31:4475 scoped_refptr<DevToolsAgentHost> agent_host_;
[email protected]f85d5412009-03-17 12:05:4876
[email protected]7aa27fd2009-03-23 10:43:5877 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost);
[email protected]f85d5412009-03-17 12:05:4878};
79
[email protected]7aa27fd2009-03-23 10:43:5880int TestDevToolsClientHost::close_counter = 0;
[email protected]f85d5412009-03-17 12:05:4881
[email protected]e5fc1632011-08-08 07:51:5382
[email protected]de551c62012-10-24 20:50:0883class TestWebContentsDelegate : public WebContentsDelegate {
[email protected]e5fc1632011-08-08 07:51:5384 public:
[email protected]674bc592011-12-20 23:00:4285 TestWebContentsDelegate() : renderer_unresponsive_received_(false) {}
[email protected]e5fc1632011-08-08 07:51:5386
[email protected]a0262432012-04-13 15:48:0987 // Notification that the contents is hung.
Avi Drissman8920def2018-01-31 19:53:3688 void RendererUnresponsive(WebContents* source,
89 RenderProcessHost* render_process_host) override {
[email protected]e5fc1632011-08-08 07:51:5390 renderer_unresponsive_received_ = true;
91 }
92
93 bool renderer_unresponsive_received() const {
94 return renderer_unresponsive_received_;
95 }
96
97 private:
98 bool renderer_unresponsive_received_;
99};
100
[email protected]f85d5412009-03-17 12:05:48101} // namespace
102
[email protected]c0257382012-03-12 20:15:34103class DevToolsManagerTest : public RenderViewHostImplTestHarness {
dgozman24bfc912014-09-29 11:04:18104 public:
dgozman88f5708052015-04-28 16:31:25105 DevToolsManagerTest() {}
dgozman24bfc912014-09-29 11:04:18106
[email protected]f85d5412009-03-17 12:05:48107 protected:
dchengfa85b152014-10-28 01:13:42108 void SetUp() override {
[email protected]46a32b92012-03-22 13:04:48109 RenderViewHostImplTestHarness::SetUp();
[email protected]7aa27fd2009-03-23 10:43:58110 TestDevToolsClientHost::ResetCounters();
[email protected]f85d5412009-03-17 12:05:48111 }
112};
113
[email protected]7aa27fd2009-03-23 10:43:58114TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) {
[email protected]2d92a632013-03-15 17:12:49115 scoped_refptr<DevToolsAgentHost> agent(
[email protected]b3f957e62014-08-08 10:09:02116 DevToolsAgentHost::GetOrCreateFor(web_contents()));
[email protected]94305ee92013-04-01 14:19:58117 EXPECT_FALSE(agent->IsAttached());
[email protected]f85d5412009-03-17 12:05:48118
[email protected]7aa27fd2009-03-23 10:43:58119 TestDevToolsClientHost client_host;
[email protected]b50452f2014-08-18 12:31:44120 client_host.InspectAgentHost(agent.get());
[email protected]94305ee92013-04-01 14:19:58121 // Test that the connection is established.
122 EXPECT_TRUE(agent->IsAttached());
[email protected]7aa27fd2009-03-23 10:43:58123 EXPECT_EQ(0, TestDevToolsClientHost::close_counter);
[email protected]f85d5412009-03-17 12:05:48124
[email protected]b50452f2014-08-18 12:31:44125 client_host.Close();
[email protected]7aa27fd2009-03-23 10:43:58126 EXPECT_EQ(1, TestDevToolsClientHost::close_counter);
[email protected]94305ee92013-04-01 14:19:58127 EXPECT_FALSE(agent->IsAttached());
[email protected]7aa27fd2009-03-23 10:43:58128}
129
[email protected]a0262432012-04-13 15:48:09130TEST_F(DevToolsManagerTest, NoUnresponsiveDialogInInspectedContents) {
nick16b07652015-04-18 02:35:31131 const GURL url("http://www.google.com");
132 contents()->NavigateAndCommit(url);
[email protected]de551c62012-10-24 20:50:08133 TestRenderViewHost* inspected_rvh = test_rvh();
nick16b07652015-04-18 02:35:31134 EXPECT_TRUE(inspected_rvh->IsRenderViewLive());
[email protected]6934a702011-12-20 00:04:51135 EXPECT_FALSE(contents()->GetDelegate());
[email protected]674bc592011-12-20 23:00:42136 TestWebContentsDelegate delegate;
[email protected]6934a702011-12-20 00:04:51137 contents()->SetDelegate(&delegate);
[email protected]e5fc1632011-08-08 07:51:53138
[email protected]e5fc1632011-08-08 07:51:53139 TestDevToolsClientHost client_host;
[email protected]b3f957e62014-08-08 10:09:02140 scoped_refptr<DevToolsAgentHost> agent_host(DevToolsAgentHost::GetOrCreateFor(
141 WebContents::FromRenderViewHost(inspected_rvh)));
[email protected]b50452f2014-08-18 12:31:44142 client_host.InspectAgentHost(agent_host.get());
[email protected]e5fc1632011-08-08 07:51:53143
144 // Start with a short timeout.
avi3627ecac2015-10-16 17:40:43145 inspected_rvh->GetWidget()->StartHangMonitorTimeout(
Dave Tapuskac6146c52017-12-20 22:48:15146 TimeDelta::FromMilliseconds(10));
[email protected]e5fc1632011-08-08 07:51:53147 // Wait long enough for first timeout and see if it fired.
skyostil95082a62015-06-05 19:53:07148 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
ki.stfu800779242015-10-12 22:46:26149 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
[email protected]dd32b1272013-05-04 14:17:11150 TimeDelta::FromMilliseconds(10));
fdoraye716a902016-07-05 16:05:49151 base::RunLoop().Run();
[email protected]e5fc1632011-08-08 07:51:53152 EXPECT_FALSE(delegate.renderer_unresponsive_received());
153
154 // Now close devtools and check that the notification is delivered.
[email protected]b50452f2014-08-18 12:31:44155 client_host.Close();
[email protected]e5fc1632011-08-08 07:51:53156 // Start with a short timeout.
avi3627ecac2015-10-16 17:40:43157 inspected_rvh->GetWidget()->StartHangMonitorTimeout(
Dave Tapuskac6146c52017-12-20 22:48:15158 TimeDelta::FromMilliseconds(10));
[email protected]e5fc1632011-08-08 07:51:53159 // Wait long enough for first timeout and see if it fired.
skyostil95082a62015-06-05 19:53:07160 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
ki.stfu800779242015-10-12 22:46:26161 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
[email protected]dd32b1272013-05-04 14:17:11162 TimeDelta::FromMilliseconds(10));
fdoraye716a902016-07-05 16:05:49163 base::RunLoop().Run();
[email protected]e5fc1632011-08-08 07:51:53164 EXPECT_TRUE(delegate.renderer_unresponsive_received());
165
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28166 contents()->SetDelegate(nullptr);
[email protected]e5fc1632011-08-08 07:51:53167}
[email protected]bfaf36052011-11-16 07:30:23168
[email protected]cb286af2013-03-26 12:46:45169class TestExternalAgentDelegate: public DevToolsExternalAgentProxyDelegate {
pfeldmana9e7dda2016-08-26 14:35:17170 public:
pfeldman10628762016-09-08 07:59:26171 TestExternalAgentDelegate() {
pfeldmana9e7dda2016-08-26 14:35:17172 }
173 ~TestExternalAgentDelegate() override {
174 expectEvent(1, "Attach");
175 expectEvent(1, "Detach");
176 expectEvent(0, "SendMessageToBackend.message0");
177 expectEvent(1, "SendMessageToBackend.message1");
178 expectEvent(2, "SendMessageToBackend.message2");
179 }
180
181 private:
[email protected]cb286af2013-03-26 12:46:45182 std::map<std::string,int> event_counter_;
183
184 void recordEvent(const std::string& name) {
185 if (event_counter_.find(name) == event_counter_.end())
186 event_counter_[name] = 0;
187 event_counter_[name] = event_counter_[name] + 1;
188 }
189
190 void expectEvent(int count, const std::string& name) {
191 EXPECT_EQ(count, event_counter_[name]);
192 }
193
dchengc2282aa2014-10-21 12:07:58194 void Attach(DevToolsExternalAgentProxy* proxy) override {
[email protected]cb286af2013-03-26 12:46:45195 recordEvent("Attach");
196 };
197
Dmitry Gozmanf7a1c2f2017-09-12 19:37:02198 void Detach(DevToolsExternalAgentProxy* proxy) override {
199 recordEvent("Detach");
200 };
[email protected]cb286af2013-03-26 12:46:45201
pfeldmane7d2e412016-09-23 16:41:51202 std::string GetType() override { return std::string(); }
203 std::string GetTitle() override { return std::string(); }
204 std::string GetDescription() override { return std::string(); }
pfeldmana9e7dda2016-08-26 14:35:17205 GURL GetURL() override { return GURL(); }
206 GURL GetFaviconURL() override { return GURL(); }
pfeldmane7d2e412016-09-23 16:41:51207 std::string GetFrontendURL() override { return std::string(); }
pfeldmana9e7dda2016-08-26 14:35:17208 bool Activate() override { return false; };
pfeldmana9e7dda2016-08-26 14:35:17209 void Reload() override { };
210 bool Close() override { return false; };
pfeldman970883722017-02-08 06:08:53211 base::TimeTicks GetLastActivityTime() override { return base::TimeTicks(); }
pfeldmana9e7dda2016-08-26 14:35:17212
Dmitry Gozmanf7a1c2f2017-09-12 19:37:02213 void SendMessageToBackend(DevToolsExternalAgentProxy* proxy,
214 const std::string& message) override {
[email protected]cb286af2013-03-26 12:46:45215 recordEvent(std::string("SendMessageToBackend.") + message);
216 };
217
[email protected]cb286af2013-03-26 12:46:45218};
219
220TEST_F(DevToolsManagerTest, TestExternalProxy) {
pfeldman10628762016-09-08 07:59:26221 std::unique_ptr<TestExternalAgentDelegate> delegate(
222 new TestExternalAgentDelegate());
[email protected]cb286af2013-03-26 12:46:45223
[email protected]42de6e142014-05-29 21:02:47224 scoped_refptr<DevToolsAgentHost> agent_host =
pfeldman10628762016-09-08 07:59:26225 DevToolsAgentHost::Forward(base::GenerateGUID(), std::move(delegate));
[email protected]cb286af2013-03-26 12:46:45226 EXPECT_EQ(agent_host, DevToolsAgentHost::GetForId(agent_host->GetId()));
227
[email protected]d9eb519e2014-08-12 12:32:08228 TestDevToolsClientHost client_host;
[email protected]b50452f2014-08-18 12:31:44229 client_host.InspectAgentHost(agent_host.get());
pfeldmanfb8e7472016-06-08 21:13:37230 agent_host->DispatchProtocolMessage(&client_host, "message1");
231 agent_host->DispatchProtocolMessage(&client_host, "message2");
232 agent_host->DispatchProtocolMessage(&client_host, "message2");
[email protected]d9eb519e2014-08-12 12:32:08233
[email protected]b50452f2014-08-18 12:31:44234 client_host.Close();
[email protected]cb286af2013-03-26 12:46:45235}
236
[email protected]de551c62012-10-24 20:50:08237} // namespace content