[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame^] | 5 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 6 | #include "chrome/browser/extensions/extension_apitest.h" |
| 7 | #include "chrome/browser/extensions/extension_host.h" |
| 8 | #include "chrome/browser/extensions/extension_process_manager.h" |
| 9 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile.h" |
| 12 | #include "chrome/browser/profiles/profile_manager.h" |
| 13 | #include "chrome/browser/ui/browser.h" |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 14 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 15 | #include "chrome/common/chrome_switches.h" |
| 16 | #include "chrome/common/url_constants.h" |
| 17 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 18 | #include "content/public/browser/render_process_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 19 | #include "content/public/browser/render_view_host.h" |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 20 | #include "content/public/browser/site_instance.h" |
| 21 | #include "content/public/browser/web_contents.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 22 | #include "net/dns/mock_host_resolver.h" |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 23 | |
| 24 | using content::NavigationController; |
| 25 | using content::WebContents; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class ProcessManagementTest : public ExtensionBrowserTest { |
| 30 | private: |
| 31 | // This is needed for testing isolated apps, which are still experimental. |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 32 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 33 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 34 | command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | } // namespace |
| 39 | |
[email protected] | bb4162a | 2013-02-03 01:35:12 | [diff] [blame] | 40 | |
[email protected] | 1c6a3f2 | 2013-03-19 09:52:30 | [diff] [blame] | 41 | // TODO(nasko): crbug.com/173137 |
| 42 | #if defined(OS_WIN) |
[email protected] | bb4162a | 2013-02-03 01:35:12 | [diff] [blame] | 43 | #define MAYBE_ProcessOverflow DISABLED_ProcessOverflow |
| 44 | #else |
| 45 | #define MAYBE_ProcessOverflow ProcessOverflow |
| 46 | #endif |
| 47 | |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 48 | // Ensure that an isolated app never shares a process with WebUIs, non-isolated |
| 49 | // extensions, and normal webpages. None of these should ever comingle |
| 50 | // RenderProcessHosts even if we hit the process limit. |
[email protected] | bb4162a | 2013-02-03 01:35:12 | [diff] [blame] | 51 | IN_PROC_BROWSER_TEST_F(ProcessManagementTest, MAYBE_ProcessOverflow) { |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 52 | // Set max renderers to 1 to force running out of processes. |
| 53 | content::RenderProcessHost::SetMaxRendererProcessCount(1); |
| 54 | |
| 55 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 56 | ASSERT_TRUE(test_server()->Start()); |
| 57 | |
| 58 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1"))); |
| 59 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2"))); |
| 60 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("hosted_app"))); |
| 61 | ASSERT_TRUE( |
| 62 | LoadExtension(test_data_dir_.AppendASCII("api_test/app_process"))); |
| 63 | |
| 64 | // The app under test acts on URLs whose host is "localhost", |
| 65 | // so the URLs we navigate to must have host "localhost". |
| 66 | GURL base_url = test_server()->GetURL( |
| 67 | "files/extensions/"); |
| 68 | GURL::Replacements replace_host; |
| 69 | std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 70 | replace_host.SetHostStr(host_str); |
| 71 | base_url = base_url.ReplaceComponents(replace_host); |
| 72 | |
| 73 | // Load an extension before adding tabs. |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 74 | const extensions::Extension* extension1 = LoadExtension( |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 75 | test_data_dir_.AppendASCII("api_test/browser_action/basics")); |
| 76 | ASSERT_TRUE(extension1); |
| 77 | GURL extension1_url = extension1->url(); |
| 78 | |
| 79 | // Create multiple tabs for each type of renderer that might exist. |
| 80 | ui_test_utils::NavigateToURLWithDisposition( |
| 81 | browser(), base_url.Resolve("isolated_apps/app1/main.html"), |
| 82 | CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 83 | ui_test_utils::NavigateToURLWithDisposition( |
| 84 | browser(), GURL(chrome::kChromeUINewTabURL), |
| 85 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 86 | ui_test_utils::NavigateToURLWithDisposition( |
| 87 | browser(), base_url.Resolve("hosted_app/main.html"), |
| 88 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 89 | ui_test_utils::NavigateToURLWithDisposition( |
| 90 | browser(), base_url.Resolve("test_file.html"), |
| 91 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 92 | |
| 93 | ui_test_utils::NavigateToURLWithDisposition( |
| 94 | browser(), base_url.Resolve("isolated_apps/app2/main.html"), |
| 95 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 96 | ui_test_utils::NavigateToURLWithDisposition( |
| 97 | browser(), GURL(chrome::kChromeUINewTabURL), |
| 98 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 99 | ui_test_utils::NavigateToURLWithDisposition( |
| 100 | browser(), base_url.Resolve("api_test/app_process/path1/empty.html"), |
| 101 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 102 | ui_test_utils::NavigateToURLWithDisposition( |
| 103 | browser(), base_url.Resolve("test_file_with_body.html"), |
| 104 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 105 | |
| 106 | // Load another copy of isolated app 1. |
| 107 | ui_test_utils::NavigateToURLWithDisposition( |
| 108 | browser(), base_url.Resolve("isolated_apps/app1/main.html"), |
| 109 | NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 110 | |
| 111 | // Load another extension. |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 112 | const extensions::Extension* extension2 = LoadExtension( |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 113 | test_data_dir_.AppendASCII("api_test/browser_action/close_background")); |
| 114 | ASSERT_TRUE(extension2); |
| 115 | GURL extension2_url = extension2->url(); |
| 116 | |
| 117 | // Get tab processes. |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 118 | ASSERT_EQ(9, browser()->tab_strip_model()->count()); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 119 | content::RenderProcessHost* isolated1_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 120 | browser()->tab_strip_model()->GetWebContentsAt(0)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 121 | content::RenderProcessHost* ntp1_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 122 | browser()->tab_strip_model()->GetWebContentsAt(1)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 123 | content::RenderProcessHost* hosted1_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 124 | browser()->tab_strip_model()->GetWebContentsAt(2)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 125 | content::RenderProcessHost* web1_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 126 | browser()->tab_strip_model()->GetWebContentsAt(3)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 127 | |
| 128 | content::RenderProcessHost* isolated2_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 129 | browser()->tab_strip_model()->GetWebContentsAt(4)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 130 | content::RenderProcessHost* ntp2_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 131 | browser()->tab_strip_model()->GetWebContentsAt(5)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 132 | content::RenderProcessHost* hosted2_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 133 | browser()->tab_strip_model()->GetWebContentsAt(6)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 134 | content::RenderProcessHost* web2_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 135 | browser()->tab_strip_model()->GetWebContentsAt(7)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 136 | |
| 137 | content::RenderProcessHost* second_isolated1_host = |
[email protected] | ee49695 | 2013-01-10 23:17:33 | [diff] [blame] | 138 | browser()->tab_strip_model()->GetWebContentsAt(8)->GetRenderProcessHost(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 139 | |
| 140 | // Get extension processes. |
| 141 | ExtensionProcessManager* process_manager = |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 142 | extensions::ExtensionSystem::Get(browser()->profile())-> |
| 143 | process_manager(); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 144 | content::RenderProcessHost* extension1_host = |
| 145 | process_manager->GetSiteInstanceForURL(extension1_url)->GetProcess(); |
| 146 | content::RenderProcessHost* extension2_host = |
| 147 | process_manager->GetSiteInstanceForURL(extension2_url)->GetProcess(); |
| 148 | |
| 149 | // An isolated app only shares with other instances of itself, not other |
| 150 | // isolated apps or anything else. |
| 151 | EXPECT_EQ(isolated1_host, second_isolated1_host); |
| 152 | EXPECT_NE(isolated1_host, isolated2_host); |
| 153 | EXPECT_NE(isolated1_host, ntp1_host); |
| 154 | EXPECT_NE(isolated1_host, hosted1_host); |
| 155 | EXPECT_NE(isolated1_host, web1_host); |
| 156 | EXPECT_NE(isolated1_host, extension1_host); |
| 157 | EXPECT_NE(isolated2_host, ntp1_host); |
| 158 | EXPECT_NE(isolated2_host, hosted1_host); |
| 159 | EXPECT_NE(isolated2_host, web1_host); |
| 160 | EXPECT_NE(isolated2_host, extension1_host); |
| 161 | |
| 162 | // Everything else is clannish. WebUI only shares with other WebUI. |
| 163 | EXPECT_EQ(ntp1_host, ntp2_host); |
| 164 | EXPECT_NE(ntp1_host, hosted1_host); |
| 165 | EXPECT_NE(ntp1_host, web1_host); |
| 166 | EXPECT_NE(ntp1_host, extension1_host); |
| 167 | |
| 168 | // Hosted apps only share with each other. |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 169 | // Note that hosted2_host's app has the background permission and will use |
| 170 | // process-per-site mode, but it should still share with hosted1_host's app. |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 171 | EXPECT_EQ(hosted1_host, hosted2_host); |
| 172 | EXPECT_NE(hosted1_host, web1_host); |
| 173 | EXPECT_NE(hosted1_host, extension1_host); |
| 174 | |
| 175 | // Web pages only share with each other. |
| 176 | EXPECT_EQ(web1_host, web2_host); |
| 177 | EXPECT_NE(web1_host, extension1_host); |
| 178 | |
| 179 | // Extensions only share with each other. |
| 180 | EXPECT_EQ(extension1_host, extension2_host); |
| 181 | } |
| 182 | |
| 183 | // Test to verify that the policy of maximum share of extension processes is |
| 184 | // properly enforced. |
| 185 | IN_PROC_BROWSER_TEST_F(ProcessManagementTest, ExtensionProcessBalancing) { |
| 186 | // Set max renderers to 6 so we can expect 2 extension processes to be |
| 187 | // allocated. |
| 188 | content::RenderProcessHost::SetMaxRendererProcessCount(6); |
| 189 | |
[email protected] | c3f757f3 | 2012-03-30 22:34:58 | [diff] [blame] | 190 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 191 | ASSERT_TRUE(test_server()->Start()); |
| 192 | |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 193 | // The app under test acts on URLs whose host is "localhost", |
| 194 | // so the URLs we navigate to must have host "localhost". |
| 195 | GURL base_url = test_server()->GetURL( |
| 196 | "files/extensions/"); |
| 197 | GURL::Replacements replace_host; |
| 198 | std::string host_str("localhost"); // Must stay in scope with replace_host. |
| 199 | replace_host.SetHostStr(host_str); |
| 200 | base_url = base_url.ReplaceComponents(replace_host); |
| 201 | |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 202 | ASSERT_TRUE(LoadExtension( |
| 203 | test_data_dir_.AppendASCII("api_test/browser_action/none"))); |
| 204 | ASSERT_TRUE(LoadExtension( |
| 205 | test_data_dir_.AppendASCII("api_test/browser_action/basics"))); |
| 206 | ASSERT_TRUE(LoadExtension( |
| 207 | test_data_dir_.AppendASCII("api_test/browser_action/remove_popup"))); |
| 208 | ASSERT_TRUE(LoadExtension( |
| 209 | test_data_dir_.AppendASCII("api_test/browser_action/add_popup"))); |
| 210 | ASSERT_TRUE(LoadExtension( |
| 211 | test_data_dir_.AppendASCII("api_test/browser_action/no_icon"))); |
| 212 | ASSERT_TRUE(LoadExtension( |
| 213 | test_data_dir_.AppendASCII("isolated_apps/app1"))); |
| 214 | ASSERT_TRUE(LoadExtension( |
| 215 | test_data_dir_.AppendASCII("api_test/management/test"))); |
| 216 | |
| 217 | ui_test_utils::NavigateToURLWithDisposition( |
| 218 | browser(), base_url.Resolve("isolated_apps/app1/main.html"), |
| 219 | CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 220 | |
| 221 | ui_test_utils::NavigateToURLWithDisposition( |
| 222 | browser(), base_url.Resolve("api_test/management/test/basics.html"), |
| 223 | CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 224 | |
| 225 | std::set<int> process_ids; |
[email protected] | 2fc15ae | 2012-05-06 00:01:37 | [diff] [blame] | 226 | Profile* profile = browser()->profile(); |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 227 | ExtensionProcessManager* epm = extensions::ExtensionSystem::Get(profile)-> |
| 228 | process_manager(); |
[email protected] | d1fe135 | 2012-04-26 00:47:32 | [diff] [blame] | 229 | for (ExtensionProcessManager::const_iterator iter = |
| 230 | epm->background_hosts().begin(); |
| 231 | iter != epm->background_hosts().end(); ++iter) { |
| 232 | process_ids.insert((*iter)->render_process_host()->GetID()); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // We've loaded 5 extensions with background pages, 1 extension without |
| 236 | // background page, and one isolated app. We expect only 2 unique processes |
| 237 | // hosting those extensions. |
[email protected] | 06bdd2b | 2012-11-30 18:47:13 | [diff] [blame] | 238 | ExtensionService* service = |
| 239 | extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 240 | |
| 241 | EXPECT_GE((size_t) 6, service->process_map()->size()); |
[email protected] | c3f757f3 | 2012-03-30 22:34:58 | [diff] [blame] | 242 | EXPECT_EQ((size_t) 2, process_ids.size()); |
[email protected] | 76411f41 | 2012-02-22 18:56:06 | [diff] [blame] | 243 | } |