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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/browser/extension_host.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/common/switches.h"
21 #include "net/dns/mock_host_resolver.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h"
24 using content::NavigationController
;
25 using content::WebContents
;
29 class ProcessManagementTest
: public ExtensionBrowserTest
{
31 // This is needed for testing isolated apps, which are still experimental.
32 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
33 ExtensionBrowserTest::SetUpCommandLine(command_line
);
34 command_line
->AppendSwitch(
35 extensions::switches::kEnableExperimentalExtensionApis
);
42 // TODO(nasko): crbug.com/173137
44 #define MAYBE_ProcessOverflow DISABLED_ProcessOverflow
46 #define MAYBE_ProcessOverflow ProcessOverflow
49 // Ensure that an isolated app never shares a process with WebUIs, non-isolated
50 // extensions, and normal webpages. None of these should ever comingle
51 // RenderProcessHosts even if we hit the process limit.
52 IN_PROC_BROWSER_TEST_F(ProcessManagementTest
, MAYBE_ProcessOverflow
) {
53 // Set max renderers to 1 to force running out of processes.
54 content::RenderProcessHost::SetMaxRendererProcessCount(1);
56 host_resolver()->AddRule("*", "127.0.0.1");
57 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
59 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("isolated_apps/app1")));
60 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("isolated_apps/app2")));
61 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII("hosted_app")));
63 LoadExtension(test_data_dir_
.AppendASCII("api_test/app_process")));
65 // The app under test acts on URLs whose host is "localhost",
66 // so the URLs we navigate to must have host "localhost".
67 GURL base_url
= embedded_test_server()->GetURL(
69 GURL::Replacements replace_host
;
70 std::string
host_str("localhost"); // Must stay in scope with replace_host.
71 replace_host
.SetHostStr(host_str
);
72 base_url
= base_url
.ReplaceComponents(replace_host
);
74 // Load an extension before adding tabs.
75 const extensions::Extension
* extension1
= LoadExtension(
76 test_data_dir_
.AppendASCII("api_test/browser_action/basics"));
77 ASSERT_TRUE(extension1
);
78 GURL extension1_url
= extension1
->url();
80 // Create multiple tabs for each type of renderer that might exist.
81 ui_test_utils::NavigateToURL(
82 browser(), base_url
.Resolve("isolated_apps/app1/main.html"));
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
);
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
);
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
);
111 // Load another extension.
112 const extensions::Extension
* extension2
= LoadExtension(
113 test_data_dir_
.AppendASCII("api_test/browser_action/close_background"));
114 ASSERT_TRUE(extension2
);
115 GURL extension2_url
= extension2
->url();
117 // Get tab processes.
118 ASSERT_EQ(9, browser()->tab_strip_model()->count());
119 content::RenderProcessHost
* isolated1_host
=
120 browser()->tab_strip_model()->GetWebContentsAt(0)->GetRenderProcessHost();
121 content::RenderProcessHost
* ntp1_host
=
122 browser()->tab_strip_model()->GetWebContentsAt(1)->GetRenderProcessHost();
123 content::RenderProcessHost
* hosted1_host
=
124 browser()->tab_strip_model()->GetWebContentsAt(2)->GetRenderProcessHost();
125 content::RenderProcessHost
* web1_host
=
126 browser()->tab_strip_model()->GetWebContentsAt(3)->GetRenderProcessHost();
128 content::RenderProcessHost
* isolated2_host
=
129 browser()->tab_strip_model()->GetWebContentsAt(4)->GetRenderProcessHost();
130 content::RenderProcessHost
* ntp2_host
=
131 browser()->tab_strip_model()->GetWebContentsAt(5)->GetRenderProcessHost();
132 content::RenderProcessHost
* hosted2_host
=
133 browser()->tab_strip_model()->GetWebContentsAt(6)->GetRenderProcessHost();
134 content::RenderProcessHost
* web2_host
=
135 browser()->tab_strip_model()->GetWebContentsAt(7)->GetRenderProcessHost();
137 content::RenderProcessHost
* second_isolated1_host
=
138 browser()->tab_strip_model()->GetWebContentsAt(8)->GetRenderProcessHost();
140 // Get extension processes.
141 extensions::ProcessManager
* process_manager
=
142 extensions::ProcessManager::Get(browser()->profile());
143 content::RenderProcessHost
* extension1_host
=
144 process_manager
->GetSiteInstanceForURL(extension1_url
)->GetProcess();
145 content::RenderProcessHost
* extension2_host
=
146 process_manager
->GetSiteInstanceForURL(extension2_url
)->GetProcess();
148 // An isolated app only shares with other instances of itself, not other
149 // isolated apps or anything else.
150 EXPECT_EQ(isolated1_host
, second_isolated1_host
);
151 EXPECT_NE(isolated1_host
, isolated2_host
);
152 EXPECT_NE(isolated1_host
, ntp1_host
);
153 EXPECT_NE(isolated1_host
, hosted1_host
);
154 EXPECT_NE(isolated1_host
, web1_host
);
155 EXPECT_NE(isolated1_host
, extension1_host
);
156 EXPECT_NE(isolated2_host
, ntp1_host
);
157 EXPECT_NE(isolated2_host
, hosted1_host
);
158 EXPECT_NE(isolated2_host
, web1_host
);
159 EXPECT_NE(isolated2_host
, extension1_host
);
161 // Everything else is clannish. WebUI only shares with other WebUI.
162 EXPECT_EQ(ntp1_host
, ntp2_host
);
163 EXPECT_NE(ntp1_host
, hosted1_host
);
164 EXPECT_NE(ntp1_host
, web1_host
);
165 EXPECT_NE(ntp1_host
, extension1_host
);
167 // Hosted apps only share with each other.
168 // Note that hosted2_host's app has the background permission and will use
169 // process-per-site mode, but it should still share with hosted1_host's app.
170 EXPECT_EQ(hosted1_host
, hosted2_host
);
171 EXPECT_NE(hosted1_host
, web1_host
);
172 EXPECT_NE(hosted1_host
, extension1_host
);
174 // Web pages only share with each other.
175 EXPECT_EQ(web1_host
, web2_host
);
176 EXPECT_NE(web1_host
, extension1_host
);
178 // Extensions only share with each other.
179 EXPECT_EQ(extension1_host
, extension2_host
);
184 #define MAYBE_ExtensionProcessBalancing DISABLED_ExtensionProcessBalancing
186 #define MAYBE_ExtensionProcessBalancing ExtensionProcessBalancing
188 // Test to verify that the policy of maximum share of extension processes is
189 // properly enforced.
190 IN_PROC_BROWSER_TEST_F(ProcessManagementTest
, MAYBE_ExtensionProcessBalancing
) {
191 // Set max renderers to 6 so we can expect 2 extension processes to be
193 content::RenderProcessHost::SetMaxRendererProcessCount(6);
195 host_resolver()->AddRule("*", "127.0.0.1");
196 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
198 // The app under test acts on URLs whose host is "localhost",
199 // so the URLs we navigate to must have host "localhost".
200 GURL base_url
= embedded_test_server()->GetURL(
202 GURL::Replacements replace_host
;
203 std::string
host_str("localhost"); // Must stay in scope with replace_host.
204 replace_host
.SetHostStr(host_str
);
205 base_url
= base_url
.ReplaceComponents(replace_host
);
207 ASSERT_TRUE(LoadExtension(
208 test_data_dir_
.AppendASCII("api_test/browser_action/none")));
209 ASSERT_TRUE(LoadExtension(
210 test_data_dir_
.AppendASCII("api_test/browser_action/basics")));
211 ASSERT_TRUE(LoadExtension(
212 test_data_dir_
.AppendASCII("api_test/browser_action/remove_popup")));
213 ASSERT_TRUE(LoadExtension(
214 test_data_dir_
.AppendASCII("api_test/browser_action/add_popup")));
215 ASSERT_TRUE(LoadExtension(
216 test_data_dir_
.AppendASCII("api_test/browser_action/no_icon")));
217 ASSERT_TRUE(LoadExtension(
218 test_data_dir_
.AppendASCII("isolated_apps/app1")));
219 ASSERT_TRUE(LoadExtension(
220 test_data_dir_
.AppendASCII("api_test/management/test")));
222 ui_test_utils::NavigateToURL(
223 browser(), base_url
.Resolve("isolated_apps/app1/main.html"));
225 ui_test_utils::NavigateToURL(
226 browser(), base_url
.Resolve("api_test/management/test/basics.html"));
228 std::set
<int> process_ids
;
229 Profile
* profile
= browser()->profile();
230 extensions::ProcessManager
* epm
= extensions::ProcessManager::Get(profile
);
231 for (extensions::ProcessManager::const_iterator iter
=
232 epm
->background_hosts().begin();
233 iter
!= epm
->background_hosts().end(); ++iter
) {
234 process_ids
.insert((*iter
)->render_process_host()->GetID());
237 // We've loaded 5 extensions with background pages, 1 extension without
238 // background page, and one isolated app. We expect only 2 unique processes
239 // hosting those extensions.
240 extensions::ProcessMap
* process_map
= extensions::ProcessMap::Get(profile
);
242 EXPECT_GE((size_t) 6, process_map
->size());
243 EXPECT_EQ((size_t) 2, process_ids
.size());