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