Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / extensions / process_management_browsertest.cc
blob3515540415202613c21363b3fbd7b87517f6f425
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;
27 namespace {
29 class ProcessManagementTest : public ExtensionBrowserTest {
30 private:
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);
39 } // namespace
42 // TODO(nasko): crbug.com/173137
43 #if defined(OS_WIN)
44 #define MAYBE_ProcessOverflow DISABLED_ProcessOverflow
45 #else
46 #define MAYBE_ProcessOverflow ProcessOverflow
47 #endif
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")));
62 ASSERT_TRUE(
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(
68 "/extensions/");
69 GURL::Replacements replace_host;
70 replace_host.SetHostStr("localhost");
71 base_url = base_url.ReplaceComponents(replace_host);
73 // Load an extension before adding tabs.
74 const extensions::Extension* extension1 = LoadExtension(
75 test_data_dir_.AppendASCII("api_test/browser_action/basics"));
76 ASSERT_TRUE(extension1);
77 GURL extension1_url = extension1->url();
79 // Create multiple tabs for each type of renderer that might exist.
80 ui_test_utils::NavigateToURL(
81 browser(), base_url.Resolve("isolated_apps/app1/main.html"));
82 ui_test_utils::NavigateToURLWithDisposition(
83 browser(), GURL(chrome::kChromeUINewTabURL),
84 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
85 ui_test_utils::NavigateToURLWithDisposition(
86 browser(), base_url.Resolve("hosted_app/main.html"),
87 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
88 ui_test_utils::NavigateToURLWithDisposition(
89 browser(), base_url.Resolve("test_file.html"),
90 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
92 ui_test_utils::NavigateToURLWithDisposition(
93 browser(), base_url.Resolve("isolated_apps/app2/main.html"),
94 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
95 ui_test_utils::NavigateToURLWithDisposition(
96 browser(), GURL(chrome::kChromeUINewTabURL),
97 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
98 ui_test_utils::NavigateToURLWithDisposition(
99 browser(), base_url.Resolve("api_test/app_process/path1/empty.html"),
100 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
101 ui_test_utils::NavigateToURLWithDisposition(
102 browser(), base_url.Resolve("test_file_with_body.html"),
103 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
105 // Load another copy of isolated app 1.
106 ui_test_utils::NavigateToURLWithDisposition(
107 browser(), base_url.Resolve("isolated_apps/app1/main.html"),
108 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
110 // Load another extension.
111 const extensions::Extension* extension2 = LoadExtension(
112 test_data_dir_.AppendASCII("api_test/browser_action/close_background"));
113 ASSERT_TRUE(extension2);
114 GURL extension2_url = extension2->url();
116 // Get tab processes.
117 ASSERT_EQ(9, browser()->tab_strip_model()->count());
118 content::RenderProcessHost* isolated1_host =
119 browser()->tab_strip_model()->GetWebContentsAt(0)->GetRenderProcessHost();
120 content::RenderProcessHost* ntp1_host =
121 browser()->tab_strip_model()->GetWebContentsAt(1)->GetRenderProcessHost();
122 content::RenderProcessHost* hosted1_host =
123 browser()->tab_strip_model()->GetWebContentsAt(2)->GetRenderProcessHost();
124 content::RenderProcessHost* web1_host =
125 browser()->tab_strip_model()->GetWebContentsAt(3)->GetRenderProcessHost();
127 content::RenderProcessHost* isolated2_host =
128 browser()->tab_strip_model()->GetWebContentsAt(4)->GetRenderProcessHost();
129 content::RenderProcessHost* ntp2_host =
130 browser()->tab_strip_model()->GetWebContentsAt(5)->GetRenderProcessHost();
131 content::RenderProcessHost* hosted2_host =
132 browser()->tab_strip_model()->GetWebContentsAt(6)->GetRenderProcessHost();
133 content::RenderProcessHost* web2_host =
134 browser()->tab_strip_model()->GetWebContentsAt(7)->GetRenderProcessHost();
136 content::RenderProcessHost* second_isolated1_host =
137 browser()->tab_strip_model()->GetWebContentsAt(8)->GetRenderProcessHost();
139 // Get extension processes.
140 extensions::ProcessManager* process_manager =
141 extensions::ProcessManager::Get(browser()->profile());
142 content::RenderProcessHost* extension1_host =
143 process_manager->GetSiteInstanceForURL(extension1_url)->GetProcess();
144 content::RenderProcessHost* extension2_host =
145 process_manager->GetSiteInstanceForURL(extension2_url)->GetProcess();
147 // An isolated app only shares with other instances of itself, not other
148 // isolated apps or anything else.
149 EXPECT_EQ(isolated1_host, second_isolated1_host);
150 EXPECT_NE(isolated1_host, isolated2_host);
151 EXPECT_NE(isolated1_host, ntp1_host);
152 EXPECT_NE(isolated1_host, hosted1_host);
153 EXPECT_NE(isolated1_host, web1_host);
154 EXPECT_NE(isolated1_host, extension1_host);
155 EXPECT_NE(isolated2_host, ntp1_host);
156 EXPECT_NE(isolated2_host, hosted1_host);
157 EXPECT_NE(isolated2_host, web1_host);
158 EXPECT_NE(isolated2_host, extension1_host);
160 // Everything else is clannish. WebUI only shares with other WebUI.
161 EXPECT_EQ(ntp1_host, ntp2_host);
162 EXPECT_NE(ntp1_host, hosted1_host);
163 EXPECT_NE(ntp1_host, web1_host);
164 EXPECT_NE(ntp1_host, extension1_host);
166 // Hosted apps only share with each other.
167 // Note that hosted2_host's app has the background permission and will use
168 // process-per-site mode, but it should still share with hosted1_host's app.
169 EXPECT_EQ(hosted1_host, hosted2_host);
170 EXPECT_NE(hosted1_host, web1_host);
171 EXPECT_NE(hosted1_host, extension1_host);
173 // Web pages only share with each other.
174 EXPECT_EQ(web1_host, web2_host);
175 EXPECT_NE(web1_host, extension1_host);
177 // Extensions only share with each other.
178 EXPECT_EQ(extension1_host, extension2_host);
181 // See
182 #if defined(OS_WIN)
183 #define MAYBE_ExtensionProcessBalancing DISABLED_ExtensionProcessBalancing
184 #else
185 #define MAYBE_ExtensionProcessBalancing ExtensionProcessBalancing
186 #endif
187 // Test to verify that the policy of maximum share of extension processes is
188 // properly enforced.
189 IN_PROC_BROWSER_TEST_F(ProcessManagementTest, MAYBE_ExtensionProcessBalancing) {
190 // Set max renderers to 6 so we can expect 2 extension processes to be
191 // allocated.
192 content::RenderProcessHost::SetMaxRendererProcessCount(6);
194 host_resolver()->AddRule("*", "127.0.0.1");
195 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
197 // The app under test acts on URLs whose host is "localhost",
198 // so the URLs we navigate to must have host "localhost".
199 GURL base_url = embedded_test_server()->GetURL(
200 "/extensions/");
201 GURL::Replacements replace_host;
202 replace_host.SetHostStr("localhost");
203 base_url = base_url.ReplaceComponents(replace_host);
205 ASSERT_TRUE(LoadExtension(
206 test_data_dir_.AppendASCII("api_test/browser_action/none")));
207 ASSERT_TRUE(LoadExtension(
208 test_data_dir_.AppendASCII("api_test/browser_action/basics")));
209 ASSERT_TRUE(LoadExtension(
210 test_data_dir_.AppendASCII("api_test/browser_action/remove_popup")));
211 ASSERT_TRUE(LoadExtension(
212 test_data_dir_.AppendASCII("api_test/browser_action/add_popup")));
213 ASSERT_TRUE(LoadExtension(
214 test_data_dir_.AppendASCII("api_test/browser_action/no_icon")));
215 ASSERT_TRUE(LoadExtension(
216 test_data_dir_.AppendASCII("isolated_apps/app1")));
217 ASSERT_TRUE(LoadExtension(
218 test_data_dir_.AppendASCII("api_test/management/test")));
220 ui_test_utils::NavigateToURL(
221 browser(), base_url.Resolve("isolated_apps/app1/main.html"));
223 ui_test_utils::NavigateToURL(
224 browser(), base_url.Resolve("api_test/management/test/basics.html"));
226 std::set<int> process_ids;
227 Profile* profile = browser()->profile();
228 extensions::ProcessManager* epm = extensions::ProcessManager::Get(profile);
229 for (extensions::ProcessManager::const_iterator iter =
230 epm->background_hosts().begin();
231 iter != epm->background_hosts().end(); ++iter) {
232 process_ids.insert((*iter)->render_process_host()->GetID());
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.
238 extensions::ProcessMap* process_map = extensions::ProcessMap::Get(profile);
240 EXPECT_GE((size_t) 6, process_map->size());
241 EXPECT_EQ((size_t) 2, process_ids.size());