blob: e607d268bdee7ebe5f320be39f5260b30d24e940 [file] [log] [blame]
[email protected]76411f412012-02-22 18:56:061// 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]112158af2013-06-07 23:46:185#include "base/strings/utf_string_conversions.h"
[email protected]76411f412012-02-22 18:56:066#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]be93bba02012-10-24 16:44:0310#include "chrome/browser/extensions/extension_system.h"
[email protected]76411f412012-02-22 18:56:0611#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/profiles/profile_manager.h"
13#include "chrome/browser/ui/browser.h"
[email protected]ee496952013-01-10 23:17:3314#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]76411f412012-02-22 18:56:0615#include "chrome/common/url_constants.h"
16#include "chrome/test/base/ui_test_utils.h"
[email protected]76411f412012-02-22 18:56:0617#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3318#include "content/public/browser/render_view_host.h"
[email protected]76411f412012-02-22 18:56:0619#include "content/public/browser/site_instance.h"
20#include "content/public/browser/web_contents.h"
[email protected]c8d02992013-07-31 22:16:5121#include "extensions/common/switches.h"
[email protected]f2cb3cf2013-03-21 01:40:5322#include "net/dns/mock_host_resolver.h"
[email protected]c1dffe82013-06-26 20:59:0523#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]76411f412012-02-22 18:56:0624
25using content::NavigationController;
26using content::WebContents;
27
28namespace {
29
30class ProcessManagementTest : public ExtensionBrowserTest {
31 private:
32 // This is needed for testing isolated apps, which are still experimental.
[email protected]49aeab62013-02-07 02:53:1133 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
[email protected]76411f412012-02-22 18:56:0634 ExtensionBrowserTest::SetUpCommandLine(command_line);
[email protected]c8d02992013-07-31 22:16:5135 command_line->AppendSwitch(
36 extensions::switches::kEnableExperimentalExtensionApis);
[email protected]76411f412012-02-22 18:56:0637 }
38};
39
40} // namespace
41
[email protected]bb4162a2013-02-03 01:35:1242
[email protected]1c6a3f22013-03-19 09:52:3043// TODO(nasko): crbug.com/173137
44#if defined(OS_WIN)
[email protected]bb4162a2013-02-03 01:35:1245#define MAYBE_ProcessOverflow DISABLED_ProcessOverflow
46#else
47#define MAYBE_ProcessOverflow ProcessOverflow
48#endif
49
[email protected]76411f412012-02-22 18:56:0650// Ensure that an isolated app never shares a process with WebUIs, non-isolated
51// extensions, and normal webpages. None of these should ever comingle
52// RenderProcessHosts even if we hit the process limit.
[email protected]bb4162a2013-02-03 01:35:1253IN_PROC_BROWSER_TEST_F(ProcessManagementTest, MAYBE_ProcessOverflow) {
[email protected]76411f412012-02-22 18:56:0654 // Set max renderers to 1 to force running out of processes.
55 content::RenderProcessHost::SetMaxRendererProcessCount(1);
56
57 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]c1dffe82013-06-26 20:59:0558 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]76411f412012-02-22 18:56:0659
60 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1")));
61 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2")));
62 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("hosted_app")));
63 ASSERT_TRUE(
64 LoadExtension(test_data_dir_.AppendASCII("api_test/app_process")));
65
66 // The app under test acts on URLs whose host is "localhost",
67 // so the URLs we navigate to must have host "localhost".
[email protected]c1dffe82013-06-26 20:59:0568 GURL base_url = embedded_test_server()->GetURL(
69 "/extensions/");
[email protected]76411f412012-02-22 18:56:0670 GURL::Replacements replace_host;
71 std::string host_str("localhost"); // Must stay in scope with replace_host.
72 replace_host.SetHostStr(host_str);
73 base_url = base_url.ReplaceComponents(replace_host);
74
75 // Load an extension before adding tabs.
[email protected]1c321ee52012-05-21 03:02:3476 const extensions::Extension* extension1 = LoadExtension(
[email protected]76411f412012-02-22 18:56:0677 test_data_dir_.AppendASCII("api_test/browser_action/basics"));
78 ASSERT_TRUE(extension1);
79 GURL extension1_url = extension1->url();
80
81 // Create multiple tabs for each type of renderer that might exist.
82 ui_test_utils::NavigateToURLWithDisposition(
83 browser(), base_url.Resolve("isolated_apps/app1/main.html"),
84 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
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]1c321ee52012-05-21 03:02:34114 const extensions::Extension* extension2 = LoadExtension(
[email protected]76411f412012-02-22 18:56:06115 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]ee496952013-01-10 23:17:33120 ASSERT_EQ(9, browser()->tab_strip_model()->count());
[email protected]76411f412012-02-22 18:56:06121 content::RenderProcessHost* isolated1_host =
[email protected]ee496952013-01-10 23:17:33122 browser()->tab_strip_model()->GetWebContentsAt(0)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06123 content::RenderProcessHost* ntp1_host =
[email protected]ee496952013-01-10 23:17:33124 browser()->tab_strip_model()->GetWebContentsAt(1)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06125 content::RenderProcessHost* hosted1_host =
[email protected]ee496952013-01-10 23:17:33126 browser()->tab_strip_model()->GetWebContentsAt(2)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06127 content::RenderProcessHost* web1_host =
[email protected]ee496952013-01-10 23:17:33128 browser()->tab_strip_model()->GetWebContentsAt(3)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06129
130 content::RenderProcessHost* isolated2_host =
[email protected]ee496952013-01-10 23:17:33131 browser()->tab_strip_model()->GetWebContentsAt(4)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06132 content::RenderProcessHost* ntp2_host =
[email protected]ee496952013-01-10 23:17:33133 browser()->tab_strip_model()->GetWebContentsAt(5)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06134 content::RenderProcessHost* hosted2_host =
[email protected]ee496952013-01-10 23:17:33135 browser()->tab_strip_model()->GetWebContentsAt(6)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06136 content::RenderProcessHost* web2_host =
[email protected]ee496952013-01-10 23:17:33137 browser()->tab_strip_model()->GetWebContentsAt(7)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06138
139 content::RenderProcessHost* second_isolated1_host =
[email protected]ee496952013-01-10 23:17:33140 browser()->tab_strip_model()->GetWebContentsAt(8)->GetRenderProcessHost();
[email protected]76411f412012-02-22 18:56:06141
142 // Get extension processes.
143 ExtensionProcessManager* process_manager =
[email protected]be93bba02012-10-24 16:44:03144 extensions::ExtensionSystem::Get(browser()->profile())->
145 process_manager();
[email protected]76411f412012-02-22 18:56:06146 content::RenderProcessHost* extension1_host =
147 process_manager->GetSiteInstanceForURL(extension1_url)->GetProcess();
148 content::RenderProcessHost* extension2_host =
149 process_manager->GetSiteInstanceForURL(extension2_url)->GetProcess();
150
151 // An isolated app only shares with other instances of itself, not other
152 // isolated apps or anything else.
153 EXPECT_EQ(isolated1_host, second_isolated1_host);
154 EXPECT_NE(isolated1_host, isolated2_host);
155 EXPECT_NE(isolated1_host, ntp1_host);
156 EXPECT_NE(isolated1_host, hosted1_host);
157 EXPECT_NE(isolated1_host, web1_host);
158 EXPECT_NE(isolated1_host, extension1_host);
159 EXPECT_NE(isolated2_host, ntp1_host);
160 EXPECT_NE(isolated2_host, hosted1_host);
161 EXPECT_NE(isolated2_host, web1_host);
162 EXPECT_NE(isolated2_host, extension1_host);
163
164 // Everything else is clannish. WebUI only shares with other WebUI.
165 EXPECT_EQ(ntp1_host, ntp2_host);
166 EXPECT_NE(ntp1_host, hosted1_host);
167 EXPECT_NE(ntp1_host, web1_host);
168 EXPECT_NE(ntp1_host, extension1_host);
169
170 // Hosted apps only share with each other.
[email protected]41fb79a52012-06-29 16:34:33171 // Note that hosted2_host's app has the background permission and will use
172 // process-per-site mode, but it should still share with hosted1_host's app.
[email protected]76411f412012-02-22 18:56:06173 EXPECT_EQ(hosted1_host, hosted2_host);
174 EXPECT_NE(hosted1_host, web1_host);
175 EXPECT_NE(hosted1_host, extension1_host);
176
177 // Web pages only share with each other.
178 EXPECT_EQ(web1_host, web2_host);
179 EXPECT_NE(web1_host, extension1_host);
180
181 // Extensions only share with each other.
182 EXPECT_EQ(extension1_host, extension2_host);
183}
184
[email protected]58b6c6992013-06-11 17:29:50185// See
186#if defined(OS_WIN)
187#define MAYBE_ExtensionProcessBalancing DISABLED_ExtensionProcessBalancing
188#else
189#define MAYBE_ExtensionProcessBalancing ExtensionProcessBalancing
190#endif
[email protected]76411f412012-02-22 18:56:06191// Test to verify that the policy of maximum share of extension processes is
192// properly enforced.
[email protected]58b6c6992013-06-11 17:29:50193IN_PROC_BROWSER_TEST_F(ProcessManagementTest, MAYBE_ExtensionProcessBalancing) {
[email protected]76411f412012-02-22 18:56:06194 // Set max renderers to 6 so we can expect 2 extension processes to be
195 // allocated.
196 content::RenderProcessHost::SetMaxRendererProcessCount(6);
197
[email protected]c3f757f32012-03-30 22:34:58198 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]c1dffe82013-06-26 20:59:05199 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
[email protected]c3f757f32012-03-30 22:34:58200
[email protected]76411f412012-02-22 18:56:06201 // The app under test acts on URLs whose host is "localhost",
202 // so the URLs we navigate to must have host "localhost".
[email protected]c1dffe82013-06-26 20:59:05203 GURL base_url = embedded_test_server()->GetURL(
204 "/extensions/");
[email protected]76411f412012-02-22 18:56:06205 GURL::Replacements replace_host;
206 std::string host_str("localhost"); // Must stay in scope with replace_host.
207 replace_host.SetHostStr(host_str);
208 base_url = base_url.ReplaceComponents(replace_host);
209
[email protected]76411f412012-02-22 18:56:06210 ASSERT_TRUE(LoadExtension(
211 test_data_dir_.AppendASCII("api_test/browser_action/none")));
212 ASSERT_TRUE(LoadExtension(
213 test_data_dir_.AppendASCII("api_test/browser_action/basics")));
214 ASSERT_TRUE(LoadExtension(
215 test_data_dir_.AppendASCII("api_test/browser_action/remove_popup")));
216 ASSERT_TRUE(LoadExtension(
217 test_data_dir_.AppendASCII("api_test/browser_action/add_popup")));
218 ASSERT_TRUE(LoadExtension(
219 test_data_dir_.AppendASCII("api_test/browser_action/no_icon")));
220 ASSERT_TRUE(LoadExtension(
221 test_data_dir_.AppendASCII("isolated_apps/app1")));
222 ASSERT_TRUE(LoadExtension(
223 test_data_dir_.AppendASCII("api_test/management/test")));
224
225 ui_test_utils::NavigateToURLWithDisposition(
226 browser(), base_url.Resolve("isolated_apps/app1/main.html"),
227 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
228
229 ui_test_utils::NavigateToURLWithDisposition(
230 browser(), base_url.Resolve("api_test/management/test/basics.html"),
231 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
232
233 std::set<int> process_ids;
[email protected]2fc15ae2012-05-06 00:01:37234 Profile* profile = browser()->profile();
[email protected]be93bba02012-10-24 16:44:03235 ExtensionProcessManager* epm = extensions::ExtensionSystem::Get(profile)->
236 process_manager();
[email protected]d1fe1352012-04-26 00:47:32237 for (ExtensionProcessManager::const_iterator iter =
238 epm->background_hosts().begin();
239 iter != epm->background_hosts().end(); ++iter) {
240 process_ids.insert((*iter)->render_process_host()->GetID());
[email protected]76411f412012-02-22 18:56:06241 }
242
243 // We've loaded 5 extensions with background pages, 1 extension without
244 // background page, and one isolated app. We expect only 2 unique processes
245 // hosting those extensions.
[email protected]06bdd2b2012-11-30 18:47:13246 ExtensionService* service =
247 extensions::ExtensionSystem::Get(profile)->extension_service();
248
249 EXPECT_GE((size_t) 6, service->process_map()->size());
[email protected]c3f757f32012-03-30 22:34:58250 EXPECT_EQ((size_t) 2, process_ids.size());
[email protected]76411f412012-02-22 18:56:06251}