| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
| 7 #include "chrome/browser/extensions/extension_devtools_browsertest.h" | 7 #include "chrome/browser/extensions/extension_devtools_browsertest.h" |
| 8 #include "chrome/browser/extensions/extension_error_reporter.h" | 8 #include "chrome/browser/extensions/extension_error_reporter.h" |
| 9 #include "chrome/browser/extensions/extension_host.h" | 9 #include "chrome/browser/extensions/extension_host.h" |
| 10 #include "chrome/browser/extensions/extension_process_manager.h" | 10 #include "chrome/browser/extensions/extension_process_manager.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_tab_util.h" | 12 #include "chrome/browser/extensions/extension_tab_util.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/tabs/tab_strip_model.h" | 14 #include "chrome/browser/tabs/tab_strip_model.h" |
| 15 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 16 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "content/public/browser/devtools_agent_host_registry.h" | 19 #include "content/public/browser/devtools_agent_host_registry.h" |
| 20 #include "content/public/browser/devtools_client_host.h" | 20 #include "content/public/browser/devtools_client_host.h" |
| 21 #include "content/public/browser/devtools_manager.h" | 21 #include "content/public/browser/devtools_manager.h" |
| 22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 24 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 25 | 25 |
| 26 using content::DevToolsAgentHost; | 26 using content::DevToolsAgentHost; |
| 27 using content::DevToolsAgentHostRegistry; | 27 using content::DevToolsAgentHostRegistry; |
| 28 using content::DevToolsClientHost; | 28 using content::DevToolsClientHost; |
| 29 using content::DevToolsManager; | 29 using content::DevToolsManager; |
| 30 using content::RenderViewHost; |
| 30 using content::WebContents; | 31 using content::WebContents; |
| 31 | 32 |
| 32 // Looks for an ExtensionHost whose URL has the given path component (including | 33 // Looks for a RenderViewHost whose URL has the given path component (including |
| 33 // leading slash). Also verifies that the expected number of hosts are loaded. | 34 // leading slash). Also verifies that the expected number of hosts are loaded. |
| 34 static ExtensionHost* FindHostWithPath(ExtensionProcessManager* manager, | 35 static RenderViewHost* FindHostWithPath(ExtensionProcessManager* manager, |
| 35 const std::string& path, | 36 const std::string& path, |
| 36 int expected_hosts) { | 37 int expected_hosts) { |
| 37 ExtensionHost* host = NULL; | 38 RenderViewHost* host = NULL; |
| 38 int num_hosts = 0; | 39 int num_hosts = 0; |
| 39 for (ExtensionProcessManager::const_iterator iter = manager->begin(); | 40 ExtensionProcessManager::ContentsSet all_contents = manager->GetAllContents(); |
| 40 iter != manager->end(); ++iter) { | 41 for (ExtensionProcessManager::ContentsSet::const_iterator iter = |
| 42 all_contents.begin(); |
| 43 iter != all_contents.end(); ++iter) { |
| 41 if ((*iter)->GetURL().path() == path) { | 44 if ((*iter)->GetURL().path() == path) { |
| 42 EXPECT_FALSE(host); | 45 EXPECT_FALSE(host); |
| 43 host = *iter; | 46 host = (*iter)->GetRenderViewHost(); |
| 44 } | 47 } |
| 45 num_hosts++; | 48 num_hosts++; |
| 46 } | 49 } |
| 47 EXPECT_EQ(expected_hosts, num_hosts); | 50 EXPECT_EQ(expected_hosts, num_hosts); |
| 48 EXPECT_TRUE(host); | 51 EXPECT_TRUE(host); |
| 49 return host; | 52 return host; |
| 50 } | 53 } |
| 51 | 54 |
| 52 // Tests for the experimental timeline extensions API. | 55 // Tests for the experimental timeline extensions API. |
| 53 IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, TimelineApi) { | 56 IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, TimelineApi) { |
| 54 ASSERT_TRUE(LoadExtension( | 57 ASSERT_TRUE(LoadExtension( |
| 55 test_data_dir_.AppendASCII("devtools").AppendASCII("timeline_api"))); | 58 test_data_dir_.AppendASCII("devtools").AppendASCII("timeline_api"))); |
| 56 | 59 |
| 57 // Get the ExtensionHost that is hosting our background page. | 60 // Get the ExtensionHost that is hosting our background page. |
| 58 ExtensionProcessManager* manager = | 61 ExtensionProcessManager* manager = |
| 59 browser()->profile()->GetExtensionProcessManager(); | 62 browser()->profile()->GetExtensionProcessManager(); |
| 60 ExtensionHost* host = FindHostWithPath(manager, "/background.html", 1); | 63 RenderViewHost* host = FindHostWithPath(manager, "/background.html", 1); |
| 61 | 64 |
| 62 // Grab a handle to the DevToolsManager so we can forward messages to it. | 65 // Grab a handle to the DevToolsManager so we can forward messages to it. |
| 63 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); | 66 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); |
| 64 | 67 |
| 65 // Grab the tab_id of whatever tab happens to be first. | 68 // Grab the tab_id of whatever tab happens to be first. |
| 66 WebContents* web_contents = browser()->GetWebContentsAt(0); | 69 WebContents* web_contents = browser()->GetWebContentsAt(0); |
| 67 ASSERT_TRUE(web_contents); | 70 ASSERT_TRUE(web_contents); |
| 68 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | 71 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 69 | 72 |
| 70 // Test setup. | 73 // Test setup. |
| 71 bool result = false; | 74 bool result = false; |
| 72 std::wstring register_listeners_js = base::StringPrintf( | 75 std::wstring register_listeners_js = base::StringPrintf( |
| 73 L"setListenersOnTab(%d)", tab_id); | 76 L"setListenersOnTab(%d)", tab_id); |
| 74 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 77 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 75 host->render_view_host(), L"", register_listeners_js, &result)); | 78 host, L"", register_listeners_js, &result)); |
| 76 EXPECT_TRUE(result); | 79 EXPECT_TRUE(result); |
| 77 | 80 |
| 78 // Setting the events should have caused an ExtensionDevToolsBridge to be | 81 // Setting the events should have caused an ExtensionDevToolsBridge to be |
| 79 // registered for the tab's RenderViewHost. | 82 // registered for the tab's RenderViewHost. |
| 80 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( | 83 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 81 web_contents->GetRenderViewHost()); | 84 web_contents->GetRenderViewHost()); |
| 82 DevToolsClientHost* devtools_client_host = | 85 DevToolsClientHost* devtools_client_host = |
| 83 devtools_manager->GetDevToolsClientHostFor(agent); | 86 devtools_manager->GetDevToolsClientHostFor(agent); |
| 84 ASSERT_TRUE(devtools_client_host); | 87 ASSERT_TRUE(devtools_client_host); |
| 85 | 88 |
| 86 // Test onPageEvent event. | 89 // Test onPageEvent event. |
| 87 result = false; | 90 result = false; |
| 88 | 91 |
| 89 devtools_client_host->DispatchOnInspectorFrontend(""); | 92 devtools_client_host->DispatchOnInspectorFrontend(""); |
| 90 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 93 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 91 host->render_view_host(), L"", L"testReceivePageEvent()", &result)); | 94 host, L"", L"testReceivePageEvent()", &result)); |
| 92 EXPECT_TRUE(result); | 95 EXPECT_TRUE(result); |
| 93 | 96 |
| 94 // Test onTabClose event. | 97 // Test onTabClose event. |
| 95 result = false; | 98 result = false; |
| 96 devtools_manager->UnregisterDevToolsClientHostFor( | 99 devtools_manager->UnregisterDevToolsClientHostFor( |
| 97 DevToolsAgentHostRegistry::GetDevToolsAgentHost( | 100 DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 98 web_contents->GetRenderViewHost())); | 101 web_contents->GetRenderViewHost())); |
| 99 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 102 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 100 host->render_view_host(), L"", L"testReceiveTabCloseEvent()", &result)); | 103 host, L"", L"testReceiveTabCloseEvent()", &result)); |
| 101 EXPECT_TRUE(result); | 104 EXPECT_TRUE(result); |
| 102 } | 105 } |
| 103 | 106 |
| 104 | 107 |
| 105 // Tests that ref counting of listeners from multiple processes works. | 108 // Tests that ref counting of listeners from multiple processes works. |
| 106 IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, ProcessRefCounting) { | 109 IN_PROC_BROWSER_TEST_F(ExtensionDevToolsBrowserTest, ProcessRefCounting) { |
| 107 ASSERT_TRUE(LoadExtension( | 110 ASSERT_TRUE(LoadExtension( |
| 108 test_data_dir_.AppendASCII("devtools").AppendASCII("timeline_api"))); | 111 test_data_dir_.AppendASCII("devtools").AppendASCII("timeline_api"))); |
| 109 | 112 |
| 110 // Get the ExtensionHost that is hosting our background page. | 113 // Get the ExtensionHost that is hosting our background page. |
| 111 ExtensionProcessManager* manager = | 114 ExtensionProcessManager* manager = |
| 112 browser()->profile()->GetExtensionProcessManager(); | 115 browser()->profile()->GetExtensionProcessManager(); |
| 113 ExtensionHost* host_one = FindHostWithPath(manager, "/background.html", 1); | 116 RenderViewHost* host_one = FindHostWithPath(manager, "/background.html", 1); |
| 114 | 117 |
| 115 ASSERT_TRUE(LoadExtension( | 118 ASSERT_TRUE(LoadExtension( |
| 116 test_data_dir_.AppendASCII("devtools").AppendASCII("timeline_api_two"))); | 119 test_data_dir_.AppendASCII("devtools").AppendASCII("timeline_api_two"))); |
| 117 ExtensionHost* host_two = FindHostWithPath(manager, | 120 RenderViewHost* host_two = FindHostWithPath(manager, |
| 118 "/background_two.html", 2); | 121 "/background_two.html", 2); |
| 119 | 122 |
| 120 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); | 123 DevToolsManager* devtools_manager = DevToolsManager::GetInstance(); |
| 121 | 124 |
| 122 // Grab the tab_id of whatever tab happens to be first. | 125 // Grab the tab_id of whatever tab happens to be first. |
| 123 WebContents* web_contents = browser()->GetWebContentsAt(0); | 126 WebContents* web_contents = browser()->GetWebContentsAt(0); |
| 124 ASSERT_TRUE(web_contents); | 127 ASSERT_TRUE(web_contents); |
| 125 int tab_id = ExtensionTabUtil::GetTabId(web_contents); | 128 int tab_id = ExtensionTabUtil::GetTabId(web_contents); |
| 126 | 129 |
| 127 // Test setup. | 130 // Test setup. |
| 128 bool result = false; | 131 bool result = false; |
| 129 std::wstring register_listeners_js = base::StringPrintf( | 132 std::wstring register_listeners_js = base::StringPrintf( |
| 130 L"setListenersOnTab(%d)", tab_id); | 133 L"setListenersOnTab(%d)", tab_id); |
| 131 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 134 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 132 host_one->render_view_host(), L"", register_listeners_js, &result)); | 135 host_one, L"", register_listeners_js, &result)); |
| 133 EXPECT_TRUE(result); | 136 EXPECT_TRUE(result); |
| 134 | 137 |
| 135 // Setting the event listeners should have caused an ExtensionDevToolsBridge | 138 // Setting the event listeners should have caused an ExtensionDevToolsBridge |
| 136 // to be registered for the tab's RenderViewHost. | 139 // to be registered for the tab's RenderViewHost. |
| 137 ASSERT_TRUE(devtools_manager->GetDevToolsClientHostFor( | 140 ASSERT_TRUE(devtools_manager->GetDevToolsClientHostFor( |
| 138 DevToolsAgentHostRegistry::GetDevToolsAgentHost( | 141 DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 139 web_contents->GetRenderViewHost()))); | 142 web_contents->GetRenderViewHost()))); |
| 140 | 143 |
| 141 // Register listeners from the second extension as well. | 144 // Register listeners from the second extension as well. |
| 142 std::wstring script = base::StringPrintf(L"registerListenersForTab(%d)", | 145 std::wstring script = base::StringPrintf(L"registerListenersForTab(%d)", |
| 143 tab_id); | 146 tab_id); |
| 144 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 147 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 145 host_two->render_view_host(), L"", script, &result)); | 148 host_two, L"", script, &result)); |
| 146 EXPECT_TRUE(result); | 149 EXPECT_TRUE(result); |
| 147 | 150 |
| 148 // Removing the listeners from the first extension should leave the bridge | 151 // Removing the listeners from the first extension should leave the bridge |
| 149 // alive. | 152 // alive. |
| 150 result = false; | 153 result = false; |
| 151 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 154 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 152 host_one->render_view_host(), L"", L"unregisterListeners()", &result)); | 155 host_one, L"", L"unregisterListeners()", &result)); |
| 153 EXPECT_TRUE(result); | 156 EXPECT_TRUE(result); |
| 154 ASSERT_TRUE(devtools_manager->GetDevToolsClientHostFor( | 157 ASSERT_TRUE(devtools_manager->GetDevToolsClientHostFor( |
| 155 DevToolsAgentHostRegistry::GetDevToolsAgentHost( | 158 DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 156 web_contents->GetRenderViewHost()))); | 159 web_contents->GetRenderViewHost()))); |
| 157 | 160 |
| 158 // Removing the listeners from the second extension should tear the bridge | 161 // Removing the listeners from the second extension should tear the bridge |
| 159 // down. | 162 // down. |
| 160 result = false; | 163 result = false; |
| 161 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 164 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 162 host_two->render_view_host(), L"", L"unregisterListeners()", &result)); | 165 host_two, L"", L"unregisterListeners()", &result)); |
| 163 EXPECT_TRUE(result); | 166 EXPECT_TRUE(result); |
| 164 ASSERT_FALSE(devtools_manager->GetDevToolsClientHostFor( | 167 ASSERT_FALSE(devtools_manager->GetDevToolsClientHostFor( |
| 165 DevToolsAgentHostRegistry::GetDevToolsAgentHost( | 168 DevToolsAgentHostRegistry::GetDevToolsAgentHost( |
| 166 web_contents->GetRenderViewHost()))); | 169 web_contents->GetRenderViewHost()))); |
| 167 } | 170 } |
| OLD | NEW |