blob: 9436e14c6c69d3dac728ec8d1cc94d5087582bd2 [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
5#include "base/utf_string_conversions.h"
6#include "chrome/browser/automation/automation_util.h"
7#include "chrome/browser/extensions/extension_apitest.h"
8#include "chrome/browser/extensions/extension_host.h"
9#include "chrome/browser/extensions/extension_process_manager.h"
10#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/profiles/profile_manager.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
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]76411f412012-02-22 18:56:0618#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3319#include "content/public/browser/render_view_host.h"
[email protected]76411f412012-02-22 18:56:0620#include "content/public/browser/site_instance.h"
21#include "content/public/browser/web_contents.h"
22#include "net/base/mock_host_resolver.h"
23
24using content::NavigationController;
25using content::WebContents;
26
27namespace {
28
29class ProcessManagementTest : public ExtensionBrowserTest {
30 private:
31 // This is needed for testing isolated apps, which are still experimental.
32 virtual void SetUpCommandLine(CommandLine* command_line) {
33 ExtensionBrowserTest::SetUpCommandLine(command_line);
34 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
35 }
36};
37
38} // namespace
39
40// Ensure that an isolated app never shares a process with WebUIs, non-isolated
41// extensions, and normal webpages. None of these should ever comingle
42// RenderProcessHosts even if we hit the process limit.
43IN_PROC_BROWSER_TEST_F(ProcessManagementTest, ProcessOverflow) {
44 // Set max renderers to 1 to force running out of processes.
45 content::RenderProcessHost::SetMaxRendererProcessCount(1);
46
47 host_resolver()->AddRule("*", "127.0.0.1");
48 ASSERT_TRUE(test_server()->Start());
49
50 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1")));
51 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2")));
52 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("hosted_app")));
53 ASSERT_TRUE(
54 LoadExtension(test_data_dir_.AppendASCII("api_test/app_process")));
55
56 // The app under test acts on URLs whose host is "localhost",
57 // so the URLs we navigate to must have host "localhost".
58 GURL base_url = test_server()->GetURL(
59 "files/extensions/");
60 GURL::Replacements replace_host;
61 std::string host_str("localhost"); // Must stay in scope with replace_host.
62 replace_host.SetHostStr(host_str);
63 base_url = base_url.ReplaceComponents(replace_host);
64
65 // Load an extension before adding tabs.
[email protected]1c321ee52012-05-21 03:02:3466 const extensions::Extension* extension1 = LoadExtension(
[email protected]76411f412012-02-22 18:56:0667 test_data_dir_.AppendASCII("api_test/browser_action/basics"));
68 ASSERT_TRUE(extension1);
69 GURL extension1_url = extension1->url();
70
71 // Create multiple tabs for each type of renderer that might exist.
72 ui_test_utils::NavigateToURLWithDisposition(
73 browser(), base_url.Resolve("isolated_apps/app1/main.html"),
74 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
75 ui_test_utils::NavigateToURLWithDisposition(
76 browser(), GURL(chrome::kChromeUINewTabURL),
77 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
78 ui_test_utils::NavigateToURLWithDisposition(
79 browser(), base_url.Resolve("hosted_app/main.html"),
80 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
81 ui_test_utils::NavigateToURLWithDisposition(
82 browser(), base_url.Resolve("test_file.html"),
83 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
84
85 ui_test_utils::NavigateToURLWithDisposition(
86 browser(), base_url.Resolve("isolated_apps/app2/main.html"),
87 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
88 ui_test_utils::NavigateToURLWithDisposition(
89 browser(), GURL(chrome::kChromeUINewTabURL),
90 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
91 ui_test_utils::NavigateToURLWithDisposition(
92 browser(), base_url.Resolve("api_test/app_process/path1/empty.html"),
93 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
94 ui_test_utils::NavigateToURLWithDisposition(
95 browser(), base_url.Resolve("test_file_with_body.html"),
96 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
97
98 // Load another copy of isolated app 1.
99 ui_test_utils::NavigateToURLWithDisposition(
100 browser(), base_url.Resolve("isolated_apps/app1/main.html"),
101 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
102
103 // Load another extension.
[email protected]1c321ee52012-05-21 03:02:34104 const extensions::Extension* extension2 = LoadExtension(
[email protected]76411f412012-02-22 18:56:06105 test_data_dir_.AppendASCII("api_test/browser_action/close_background"));
106 ASSERT_TRUE(extension2);
107 GURL extension2_url = extension2->url();
108
109 // Get tab processes.
110 ASSERT_EQ(9, browser()->tab_count());
111 content::RenderProcessHost* isolated1_host =
112 browser()->GetWebContentsAt(0)->GetRenderProcessHost();
113 content::RenderProcessHost* ntp1_host =
114 browser()->GetWebContentsAt(1)->GetRenderProcessHost();
115 content::RenderProcessHost* hosted1_host =
116 browser()->GetWebContentsAt(2)->GetRenderProcessHost();
117 content::RenderProcessHost* web1_host =
118 browser()->GetWebContentsAt(3)->GetRenderProcessHost();
119
120 content::RenderProcessHost* isolated2_host =
121 browser()->GetWebContentsAt(4)->GetRenderProcessHost();
122 content::RenderProcessHost* ntp2_host =
123 browser()->GetWebContentsAt(5)->GetRenderProcessHost();
124 content::RenderProcessHost* hosted2_host =
125 browser()->GetWebContentsAt(6)->GetRenderProcessHost();
126 content::RenderProcessHost* web2_host =
127 browser()->GetWebContentsAt(7)->GetRenderProcessHost();
128
129 content::RenderProcessHost* second_isolated1_host =
130 browser()->GetWebContentsAt(8)->GetRenderProcessHost();
131
132 // Get extension processes.
133 ExtensionProcessManager* process_manager =
[email protected]2fc15ae2012-05-06 00:01:37134 browser()->profile()->GetExtensionProcessManager();
[email protected]76411f412012-02-22 18:56:06135 content::RenderProcessHost* extension1_host =
136 process_manager->GetSiteInstanceForURL(extension1_url)->GetProcess();
137 content::RenderProcessHost* extension2_host =
138 process_manager->GetSiteInstanceForURL(extension2_url)->GetProcess();
139
140 // An isolated app only shares with other instances of itself, not other
141 // isolated apps or anything else.
142 EXPECT_EQ(isolated1_host, second_isolated1_host);
143 EXPECT_NE(isolated1_host, isolated2_host);
144 EXPECT_NE(isolated1_host, ntp1_host);
145 EXPECT_NE(isolated1_host, hosted1_host);
146 EXPECT_NE(isolated1_host, web1_host);
147 EXPECT_NE(isolated1_host, extension1_host);
148 EXPECT_NE(isolated2_host, ntp1_host);
149 EXPECT_NE(isolated2_host, hosted1_host);
150 EXPECT_NE(isolated2_host, web1_host);
151 EXPECT_NE(isolated2_host, extension1_host);
152
153 // Everything else is clannish. WebUI only shares with other WebUI.
154 EXPECT_EQ(ntp1_host, ntp2_host);
155 EXPECT_NE(ntp1_host, hosted1_host);
156 EXPECT_NE(ntp1_host, web1_host);
157 EXPECT_NE(ntp1_host, extension1_host);
158
159 // Hosted apps only share with each other.
160 EXPECT_EQ(hosted1_host, hosted2_host);
161 EXPECT_NE(hosted1_host, web1_host);
162 EXPECT_NE(hosted1_host, extension1_host);
163
164 // Web pages only share with each other.
165 EXPECT_EQ(web1_host, web2_host);
166 EXPECT_NE(web1_host, extension1_host);
167
168 // Extensions only share with each other.
169 EXPECT_EQ(extension1_host, extension2_host);
170}
171
172// Test to verify that the policy of maximum share of extension processes is
173// properly enforced.
174IN_PROC_BROWSER_TEST_F(ProcessManagementTest, ExtensionProcessBalancing) {
175 // Set max renderers to 6 so we can expect 2 extension processes to be
176 // allocated.
177 content::RenderProcessHost::SetMaxRendererProcessCount(6);
178
[email protected]c3f757f32012-03-30 22:34:58179 host_resolver()->AddRule("*", "127.0.0.1");
180 ASSERT_TRUE(test_server()->Start());
181
[email protected]76411f412012-02-22 18:56:06182 // The app under test acts on URLs whose host is "localhost",
183 // so the URLs we navigate to must have host "localhost".
184 GURL base_url = test_server()->GetURL(
185 "files/extensions/");
186 GURL::Replacements replace_host;
187 std::string host_str("localhost"); // Must stay in scope with replace_host.
188 replace_host.SetHostStr(host_str);
189 base_url = base_url.ReplaceComponents(replace_host);
190
[email protected]76411f412012-02-22 18:56:06191 ASSERT_TRUE(LoadExtension(
192 test_data_dir_.AppendASCII("api_test/browser_action/none")));
193 ASSERT_TRUE(LoadExtension(
194 test_data_dir_.AppendASCII("api_test/browser_action/basics")));
195 ASSERT_TRUE(LoadExtension(
196 test_data_dir_.AppendASCII("api_test/browser_action/remove_popup")));
197 ASSERT_TRUE(LoadExtension(
198 test_data_dir_.AppendASCII("api_test/browser_action/add_popup")));
199 ASSERT_TRUE(LoadExtension(
200 test_data_dir_.AppendASCII("api_test/browser_action/no_icon")));
201 ASSERT_TRUE(LoadExtension(
202 test_data_dir_.AppendASCII("isolated_apps/app1")));
203 ASSERT_TRUE(LoadExtension(
204 test_data_dir_.AppendASCII("api_test/management/test")));
205
206 ui_test_utils::NavigateToURLWithDisposition(
207 browser(), base_url.Resolve("isolated_apps/app1/main.html"),
208 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
209
210 ui_test_utils::NavigateToURLWithDisposition(
211 browser(), base_url.Resolve("api_test/management/test/basics.html"),
212 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
213
214 std::set<int> process_ids;
[email protected]2fc15ae2012-05-06 00:01:37215 Profile* profile = browser()->profile();
[email protected]76411f412012-02-22 18:56:06216 ExtensionProcessManager* epm = profile->GetExtensionProcessManager();
217
[email protected]d1fe1352012-04-26 00:47:32218 for (ExtensionProcessManager::const_iterator iter =
219 epm->background_hosts().begin();
220 iter != epm->background_hosts().end(); ++iter) {
221 process_ids.insert((*iter)->render_process_host()->GetID());
[email protected]76411f412012-02-22 18:56:06222 }
223
224 // We've loaded 5 extensions with background pages, 1 extension without
225 // background page, and one isolated app. We expect only 2 unique processes
226 // hosting those extensions.
[email protected]c3f757f32012-03-30 22:34:58227 EXPECT_GE((size_t) 6, profile->GetExtensionService()->process_map()->size());
228 EXPECT_EQ((size_t) 2, process_ids.size());
[email protected]76411f412012-02-22 18:56:06229}