blob: fc115a5078c120eb25263c91f4b3e744b76b8195 [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
avia2f4804a2015-12-24 23:11:135#include <stddef.h>
6
Lei Zhangfe5b86932019-02-01 17:26:597#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:188#include "base/strings/utf_string_conversions.h"
avia2f4804a2015-12-24 23:11:139#include "build/build_config.h"
naskod1aaa762017-03-18 02:14:4510#include "chrome/browser/chrome_notification_types.h"
[email protected]76411f412012-02-22 18:56:0611#include "chrome/browser/extensions/extension_apitest.h"
[email protected]76411f412012-02-22 18:56:0612#include "chrome/browser/extensions/extension_service.h"
13#include "chrome/browser/profiles/profile.h"
14#include "chrome/browser/profiles/profile_manager.h"
naskod1aaa762017-03-18 02:14:4515#include "chrome/browser/sessions/session_tab_helper.h"
[email protected]76411f412012-02-22 18:56:0616#include "chrome/browser/ui/browser.h"
[email protected]ee496952013-01-10 23:17:3317#include "chrome/browser/ui/tabs/tab_strip_model.h"
lukasza2a130dc2016-10-06 00:50:4318#include "chrome/common/chrome_switches.h"
[email protected]76411f412012-02-22 18:56:0619#include "chrome/common/url_constants.h"
20#include "chrome/test/base/ui_test_utils.h"
naskod1aaa762017-03-18 02:14:4521#include "content/public/browser/notification_service.h"
lukasza1d025732016-09-28 21:36:0822#include "content/public/browser/render_frame_host.h"
[email protected]76411f412012-02-22 18:56:0623#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3324#include "content/public/browser/render_view_host.h"
[email protected]76411f412012-02-22 18:56:0625#include "content/public/browser/site_instance.h"
26#include "content/public/browser/web_contents.h"
Alex Moshchukca171372017-11-28 16:11:3127#include "content/public/common/content_switches.h"
lukasza1d025732016-09-28 21:36:0828#include "content/public/test/browser_test_utils.h"
29#include "content/public/test/test_navigation_observer.h"
Lukasz Anforowicz1df5d87d2018-03-02 01:30:4730#include "content/public/test/test_utils.h"
[email protected]22401dc2014-03-21 01:38:5731#include "extensions/browser/extension_host.h"
[email protected]98b6d942013-11-10 00:34:0732#include "extensions/browser/process_manager.h"
lukasza2a130dc2016-10-06 00:50:4333#include "extensions/browser/process_map.h"
Devlin Croninf1afce252017-08-19 01:21:4834#include "extensions/common/manifest_handlers/web_accessible_resources_info.h"
[email protected]c8d02992013-07-31 22:16:5135#include "extensions/common/switches.h"
[email protected]f2cb3cf2013-03-21 01:40:5336#include "net/dns/mock_host_resolver.h"
[email protected]c1dffe82013-06-26 20:59:0537#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]76411f412012-02-22 18:56:0638
39using content::NavigationController;
40using content::WebContents;
41
Devlin Croninf1afce252017-08-19 01:21:4842namespace extensions {
43
[email protected]76411f412012-02-22 18:56:0644namespace {
45
Nick Carterbf6264a52018-04-06 02:39:3346bool IsExtensionProcessSharingAllowed() {
47 // TODO(nick): Currently, process sharing is allowed even in
48 // --site-per-process. Lock this down. https://crbug.com/766267
49 return true;
50}
51
[email protected]76411f412012-02-22 18:56:0652class ProcessManagementTest : public ExtensionBrowserTest {
53 private:
54 // This is needed for testing isolated apps, which are still experimental.
avi3ef9ec9e2014-12-22 22:50:1755 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]76411f412012-02-22 18:56:0656 ExtensionBrowserTest::SetUpCommandLine(command_line);
[email protected]c8d02992013-07-31 22:16:5157 command_line->AppendSwitch(
58 extensions::switches::kEnableExperimentalExtensionApis);
[email protected]76411f412012-02-22 18:56:0659 }
jambb11ed742017-05-01 17:27:5960
61 void SetUpOnMainThread() override {
62 ExtensionBrowserTest::SetUpOnMainThread();
63 host_resolver()->AddRule("*", "127.0.0.1");
64 }
[email protected]76411f412012-02-22 18:56:0665};
66
lukasza2a130dc2016-10-06 00:50:4367class ChromeWebStoreProcessTest : public ExtensionBrowserTest {
68 public:
69 const GURL& gallery_url() { return gallery_url_; }
70
lukasza2a130dc2016-10-06 00:50:4371 // Overrides location of Chrome Web Store gallery to a test controlled URL.
72 void SetUpCommandLine(base::CommandLine* command_line) override {
73 ExtensionBrowserTest::SetUpCommandLine(command_line);
74
75 ASSERT_TRUE(embedded_test_server()->Start());
76 gallery_url_ =
77 embedded_test_server()->GetURL("chrome.webstore.test.com", "/");
Devlin Croninf1afce252017-08-19 01:21:4878 command_line->AppendSwitchASCII(::switches::kAppsGalleryURL,
lukasza2a130dc2016-10-06 00:50:4379 gallery_url_.spec());
80 }
81
Alex Moshchukca171372017-11-28 16:11:3182 private:
lukasza2a130dc2016-10-06 00:50:4383 void SetUpOnMainThread() override {
84 host_resolver()->AddRule("*", "127.0.0.1");
85 }
86
87 GURL gallery_url_;
88};
89
Alex Moshchukca171372017-11-28 16:11:3190class ChromeWebStoreInIsolatedOriginTest : public ChromeWebStoreProcessTest {
91 public:
92 ChromeWebStoreInIsolatedOriginTest() {}
93
94 void SetUpCommandLine(base::CommandLine* command_line) override {
95 ChromeWebStoreProcessTest::SetUpCommandLine(command_line);
96
97 // Mark the Chrome Web Store URL as an isolated origin.
98 command_line->AppendSwitchASCII(::switches::kIsolateOrigins,
99 gallery_url().spec());
100 }
101
102 private:
103 DISALLOW_COPY_AND_ASSIGN(ChromeWebStoreInIsolatedOriginTest);
104};
105
[email protected]76411f412012-02-22 18:56:06106} // namespace
107
[email protected]bb4162a2013-02-03 01:35:12108
[email protected]1c6a3f22013-03-19 09:52:30109// TODO(nasko): crbug.com/173137
110#if defined(OS_WIN)
[email protected]bb4162a2013-02-03 01:35:12111#define MAYBE_ProcessOverflow DISABLED_ProcessOverflow
112#else
113#define MAYBE_ProcessOverflow ProcessOverflow
114#endif
115
[email protected]76411f412012-02-22 18:56:06116// Ensure that an isolated app never shares a process with WebUIs, non-isolated
117// extensions, and normal webpages. None of these should ever comingle
118// RenderProcessHosts even if we hit the process limit.
[email protected]bb4162a2013-02-03 01:35:12119IN_PROC_BROWSER_TEST_F(ProcessManagementTest, MAYBE_ProcessOverflow) {
[email protected]76411f412012-02-22 18:56:06120 // Set max renderers to 1 to force running out of processes.
121 content::RenderProcessHost::SetMaxRendererProcessCount(1);
122
svaldeza01f7d92015-11-18 17:47:56123 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]76411f412012-02-22 18:56:06124
125 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app1")));
126 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("isolated_apps/app2")));
127 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("hosted_app")));
128 ASSERT_TRUE(
129 LoadExtension(test_data_dir_.AppendASCII("api_test/app_process")));
130
131 // The app under test acts on URLs whose host is "localhost",
132 // so the URLs we navigate to must have host "localhost".
[email protected]c1dffe82013-06-26 20:59:05133 GURL base_url = embedded_test_server()->GetURL(
134 "/extensions/");
[email protected]76411f412012-02-22 18:56:06135 GURL::Replacements replace_host;
mgiuca77752c32015-02-05 07:31:18136 replace_host.SetHostStr("localhost");
[email protected]76411f412012-02-22 18:56:06137 base_url = base_url.ReplaceComponents(replace_host);
138
139 // Load an extension before adding tabs.
[email protected]1c321ee52012-05-21 03:02:34140 const extensions::Extension* extension1 = LoadExtension(
[email protected]76411f412012-02-22 18:56:06141 test_data_dir_.AppendASCII("api_test/browser_action/basics"));
142 ASSERT_TRUE(extension1);
143 GURL extension1_url = extension1->url();
144
145 // Create multiple tabs for each type of renderer that might exist.
thestig53986dc2014-12-16 06:09:18146 ui_test_utils::NavigateToURL(
147 browser(), base_url.Resolve("isolated_apps/app1/main.html"));
[email protected]76411f412012-02-22 18:56:06148 ui_test_utils::NavigateToURLWithDisposition(
149 browser(), GURL(chrome::kChromeUINewTabURL),
nick3b04f322016-08-31 19:29:19150 WindowOpenDisposition::NEW_FOREGROUND_TAB,
151 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06152 ui_test_utils::NavigateToURLWithDisposition(
153 browser(), base_url.Resolve("hosted_app/main.html"),
nick3b04f322016-08-31 19:29:19154 WindowOpenDisposition::NEW_FOREGROUND_TAB,
155 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06156 ui_test_utils::NavigateToURLWithDisposition(
157 browser(), base_url.Resolve("test_file.html"),
nick3b04f322016-08-31 19:29:19158 WindowOpenDisposition::NEW_FOREGROUND_TAB,
159 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06160
161 ui_test_utils::NavigateToURLWithDisposition(
162 browser(), base_url.Resolve("isolated_apps/app2/main.html"),
nick3b04f322016-08-31 19:29:19163 WindowOpenDisposition::NEW_FOREGROUND_TAB,
164 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06165 ui_test_utils::NavigateToURLWithDisposition(
166 browser(), GURL(chrome::kChromeUINewTabURL),
nick3b04f322016-08-31 19:29:19167 WindowOpenDisposition::NEW_FOREGROUND_TAB,
168 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06169 ui_test_utils::NavigateToURLWithDisposition(
170 browser(), base_url.Resolve("api_test/app_process/path1/empty.html"),
nick3b04f322016-08-31 19:29:19171 WindowOpenDisposition::NEW_FOREGROUND_TAB,
172 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06173 ui_test_utils::NavigateToURLWithDisposition(
174 browser(), base_url.Resolve("test_file_with_body.html"),
nick3b04f322016-08-31 19:29:19175 WindowOpenDisposition::NEW_FOREGROUND_TAB,
176 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06177
178 // Load another copy of isolated app 1.
179 ui_test_utils::NavigateToURLWithDisposition(
180 browser(), base_url.Resolve("isolated_apps/app1/main.html"),
nick3b04f322016-08-31 19:29:19181 WindowOpenDisposition::NEW_FOREGROUND_TAB,
182 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
[email protected]76411f412012-02-22 18:56:06183
184 // Load another extension.
[email protected]1c321ee52012-05-21 03:02:34185 const extensions::Extension* extension2 = LoadExtension(
[email protected]76411f412012-02-22 18:56:06186 test_data_dir_.AppendASCII("api_test/browser_action/close_background"));
187 ASSERT_TRUE(extension2);
188 GURL extension2_url = extension2->url();
189
190 // Get tab processes.
[email protected]ee496952013-01-10 23:17:33191 ASSERT_EQ(9, browser()->tab_strip_model()->count());
Lukasz Anforowicz3f0b84ca2017-09-28 13:02:37192 content::RenderProcessHost* isolated1_host = browser()
193 ->tab_strip_model()
194 ->GetWebContentsAt(0)
195 ->GetMainFrame()
196 ->GetProcess();
197 content::RenderProcessHost* ntp1_host = browser()
198 ->tab_strip_model()
199 ->GetWebContentsAt(1)
200 ->GetMainFrame()
201 ->GetProcess();
202 content::RenderProcessHost* hosted1_host = browser()
203 ->tab_strip_model()
204 ->GetWebContentsAt(2)
205 ->GetMainFrame()
206 ->GetProcess();
207 content::RenderProcessHost* web1_host = browser()
208 ->tab_strip_model()
209 ->GetWebContentsAt(3)
210 ->GetMainFrame()
211 ->GetProcess();
[email protected]76411f412012-02-22 18:56:06212
Lukasz Anforowicz3f0b84ca2017-09-28 13:02:37213 content::RenderProcessHost* isolated2_host = browser()
214 ->tab_strip_model()
215 ->GetWebContentsAt(4)
216 ->GetMainFrame()
217 ->GetProcess();
218 content::RenderProcessHost* ntp2_host = browser()
219 ->tab_strip_model()
220 ->GetWebContentsAt(5)
221 ->GetMainFrame()
222 ->GetProcess();
223 content::RenderProcessHost* hosted2_host = browser()
224 ->tab_strip_model()
225 ->GetWebContentsAt(6)
226 ->GetMainFrame()
227 ->GetProcess();
228 content::RenderProcessHost* web2_host = browser()
229 ->tab_strip_model()
230 ->GetWebContentsAt(7)
231 ->GetMainFrame()
232 ->GetProcess();
[email protected]76411f412012-02-22 18:56:06233
Lukasz Anforowicz3f0b84ca2017-09-28 13:02:37234 content::RenderProcessHost* second_isolated1_host = browser()
235 ->tab_strip_model()
236 ->GetWebContentsAt(8)
237 ->GetMainFrame()
238 ->GetProcess();
[email protected]76411f412012-02-22 18:56:06239
240 // Get extension processes.
[email protected]98b6d942013-11-10 00:34:07241 extensions::ProcessManager* process_manager =
reillyg0ea3fa902014-10-28 15:30:23242 extensions::ProcessManager::Get(browser()->profile());
[email protected]76411f412012-02-22 18:56:06243 content::RenderProcessHost* extension1_host =
244 process_manager->GetSiteInstanceForURL(extension1_url)->GetProcess();
245 content::RenderProcessHost* extension2_host =
246 process_manager->GetSiteInstanceForURL(extension2_url)->GetProcess();
247
248 // An isolated app only shares with other instances of itself, not other
249 // isolated apps or anything else.
250 EXPECT_EQ(isolated1_host, second_isolated1_host);
251 EXPECT_NE(isolated1_host, isolated2_host);
252 EXPECT_NE(isolated1_host, ntp1_host);
253 EXPECT_NE(isolated1_host, hosted1_host);
254 EXPECT_NE(isolated1_host, web1_host);
255 EXPECT_NE(isolated1_host, extension1_host);
256 EXPECT_NE(isolated2_host, ntp1_host);
257 EXPECT_NE(isolated2_host, hosted1_host);
258 EXPECT_NE(isolated2_host, web1_host);
259 EXPECT_NE(isolated2_host, extension1_host);
260
261 // Everything else is clannish. WebUI only shares with other WebUI.
262 EXPECT_EQ(ntp1_host, ntp2_host);
263 EXPECT_NE(ntp1_host, hosted1_host);
264 EXPECT_NE(ntp1_host, web1_host);
265 EXPECT_NE(ntp1_host, extension1_host);
266
267 // Hosted apps only share with each other.
[email protected]41fb79a52012-06-29 16:34:33268 // Note that hosted2_host's app has the background permission and will use
269 // process-per-site mode, but it should still share with hosted1_host's app.
[email protected]76411f412012-02-22 18:56:06270 EXPECT_EQ(hosted1_host, hosted2_host);
271 EXPECT_NE(hosted1_host, web1_host);
272 EXPECT_NE(hosted1_host, extension1_host);
273
274 // Web pages only share with each other.
275 EXPECT_EQ(web1_host, web2_host);
276 EXPECT_NE(web1_host, extension1_host);
277
Nick Carterbf6264a52018-04-06 02:39:33278 if (IsExtensionProcessSharingAllowed()) {
Lukasz Anforowicz1df5d87d2018-03-02 01:30:47279 // Extensions only share with each other ...
280 EXPECT_EQ(extension1_host, extension2_host);
281 } else {
Nick Carterbf6264a52018-04-06 02:39:33282 // Unless extensions are not allowed to share, even with each other.
Lukasz Anforowicz1df5d87d2018-03-02 01:30:47283 EXPECT_NE(extension1_host, extension2_host);
284 }
[email protected]76411f412012-02-22 18:56:06285}
286
[email protected]58b6c6992013-06-11 17:29:50287// See
288#if defined(OS_WIN)
289#define MAYBE_ExtensionProcessBalancing DISABLED_ExtensionProcessBalancing
290#else
291#define MAYBE_ExtensionProcessBalancing ExtensionProcessBalancing
292#endif
[email protected]76411f412012-02-22 18:56:06293// Test to verify that the policy of maximum share of extension processes is
294// properly enforced.
[email protected]58b6c6992013-06-11 17:29:50295IN_PROC_BROWSER_TEST_F(ProcessManagementTest, MAYBE_ExtensionProcessBalancing) {
[email protected]76411f412012-02-22 18:56:06296 // Set max renderers to 6 so we can expect 2 extension processes to be
297 // allocated.
298 content::RenderProcessHost::SetMaxRendererProcessCount(6);
299
svaldeza01f7d92015-11-18 17:47:56300 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]c3f757f32012-03-30 22:34:58301
[email protected]76411f412012-02-22 18:56:06302 // The app under test acts on URLs whose host is "localhost",
303 // so the URLs we navigate to must have host "localhost".
[email protected]c1dffe82013-06-26 20:59:05304 GURL base_url = embedded_test_server()->GetURL(
305 "/extensions/");
[email protected]76411f412012-02-22 18:56:06306 GURL::Replacements replace_host;
mgiuca77752c32015-02-05 07:31:18307 replace_host.SetHostStr("localhost");
[email protected]76411f412012-02-22 18:56:06308 base_url = base_url.ReplaceComponents(replace_host);
309
[email protected]76411f412012-02-22 18:56:06310 ASSERT_TRUE(LoadExtension(
311 test_data_dir_.AppendASCII("api_test/browser_action/none")));
312 ASSERT_TRUE(LoadExtension(
313 test_data_dir_.AppendASCII("api_test/browser_action/basics")));
314 ASSERT_TRUE(LoadExtension(
315 test_data_dir_.AppendASCII("api_test/browser_action/remove_popup")));
316 ASSERT_TRUE(LoadExtension(
317 test_data_dir_.AppendASCII("api_test/browser_action/add_popup")));
318 ASSERT_TRUE(LoadExtension(
319 test_data_dir_.AppendASCII("api_test/browser_action/no_icon")));
320 ASSERT_TRUE(LoadExtension(
321 test_data_dir_.AppendASCII("isolated_apps/app1")));
322 ASSERT_TRUE(LoadExtension(
323 test_data_dir_.AppendASCII("api_test/management/test")));
324
Lukasz Anforowicz1df5d87d2018-03-02 01:30:47325 // TODO(lukasza): It might be worth it to navigate to actual
326 // chrome-extension:// URIs below (not to HTTP URIs) to make sure the 1/3rd
327 // of process limit also applies to normal tabs (not just to background pages
328 // and scripts).
danakjfb16d7b62018-10-25 21:51:04329 content::RenderProcessHost* first_renderer = ui_test_utils::NavigateToURL(
thestig53986dc2014-12-16 06:09:18330 browser(), base_url.Resolve("isolated_apps/app1/main.html"));
danakjfb16d7b62018-10-25 21:51:04331 content::RenderProcessHostWatcher first_renderer_watcher(
332 first_renderer,
333 content::RenderProcessHostWatcher::WATCH_FOR_HOST_DESTRUCTION);
334
335 content::RenderProcessHost* second_renderer = ui_test_utils::NavigateToURL(
thestig53986dc2014-12-16 06:09:18336 browser(), base_url.Resolve("api_test/management/test/basics.html"));
[email protected]76411f412012-02-22 18:56:06337
338 std::set<int> process_ids;
[email protected]2fc15ae2012-05-06 00:01:37339 Profile* profile = browser()->profile();
reillyg0ea3fa902014-10-28 15:30:23340 extensions::ProcessManager* epm = extensions::ProcessManager::Get(profile);
rdevlin.cronin6ae04a012015-04-03 20:19:40341 for (extensions::ExtensionHost* host : epm->background_hosts())
342 process_ids.insert(host->render_process_host()->GetID());
[email protected]76411f412012-02-22 18:56:06343
Lukasz Anforowicz1df5d87d2018-03-02 01:30:47344 // We've loaded 5 extensions with background pages
345 // (api_test/browser_action/*), 1 extension without background page
Nick Carterbf6264a52018-04-06 02:39:33346 // (api_test/management), and one isolated app. With extension process
347 // sharing, we expect only 2 unique processes hosting the background
348 // pages/scripts of these extensions (which extension gets assigned to which
349 // process is randomized). If extension process sharing is disabled, there is
350 // no process limit, and each of the 5 background pages/scripts will be hosted
351 // in a separate process.
352 if (IsExtensionProcessSharingAllowed())
Lukasz Anforowicz1df5d87d2018-03-02 01:30:47353 EXPECT_EQ(2u, process_ids.size());
354 else
355 EXPECT_EQ(5u, process_ids.size());
[email protected]06bdd2b2012-11-30 18:47:13356
danakjfb16d7b62018-10-25 21:51:04357 if (first_renderer != second_renderer) {
358 // Wait for the first renderer to be torn down before verifying the number
359 // of processes, else we race with the teardown here (specifically the
Yuzu Saijoc0636d22019-07-12 05:42:10360 // UnfreezableFrameMsg_SwapOut -> FrameHostMsg_SwapOut_ACK round trip).
danakjfb16d7b62018-10-25 21:51:04361 first_renderer_watcher.Wait();
362 }
363
Lukasz Anforowicz1df5d87d2018-03-02 01:30:47364 // ProcessMap will always have exactly 5 entries - one for each of the
365 // extensions with a background page (api_test/browser_action/*). There won't
366 // be any additional entries, since 1) the isolated app will be associated
367 // with a separate content::BrowserContext and 2) the navigation to
368 // api_test/management/test/basics.html navigates to a file: URI (not to a
369 // chrome-extension: URI).
370 extensions::ProcessMap* process_map = extensions::ProcessMap::Get(profile);
371 EXPECT_EQ(5u, process_map->size());
[email protected]76411f412012-02-22 18:56:06372}
lukasza1d025732016-09-28 21:36:08373
374IN_PROC_BROWSER_TEST_F(ProcessManagementTest,
375 NavigateExtensionTabToWebViaPost) {
lukasza1d025732016-09-28 21:36:08376 ASSERT_TRUE(embedded_test_server()->Start());
377
378 // Load an extension.
379 const extensions::Extension* extension = LoadExtension(
380 test_data_dir_.AppendASCII("api_test/browser_action/popup_with_form"));
381 ASSERT_TRUE(extension);
382
383 // Navigate a tab to an extension page.
384 GURL extension_url = extension->GetResourceURL("popup.html");
385 ui_test_utils::NavigateToURL(browser(), extension_url);
386 WebContents* web_contents =
387 browser()->tab_strip_model()->GetActiveWebContents();
388 EXPECT_EQ(extension_url, web_contents->GetLastCommittedURL());
389 content::RenderProcessHost* old_process_host =
390 web_contents->GetMainFrame()->GetProcess();
391
392 // Note that the |setTimeout| call below is needed to make sure
393 // ExecuteScriptAndExtractBool returns *after* a scheduled navigation has
394 // already started.
395 GURL web_url(embedded_test_server()->GetURL("foo.com", "/title1.html"));
396 std::string navigation_starting_script =
397 "var form = document.getElementById('form');\n"
398 "form.action = '" + web_url.spec() + "';\n"
399 "form.submit();\n"
400 "setTimeout(\n"
401 " function() { window.domAutomationController.send(true); },\n"
402 " 0);\n";
403
404 // Try to trigger navigation to a webpage from within the tab.
405 bool ignored_script_result = false;
406 content::TestNavigationObserver nav_observer(web_contents, 1);
407 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
408 web_contents, navigation_starting_script, &ignored_script_result));
409
410 // Verify that the navigation succeeded.
411 nav_observer.Wait();
412 EXPECT_EQ(web_url, web_contents->GetLastCommittedURL());
413
414 // Verify that the navigation transferred the contents to another renderer
415 // process.
naskoabed2a52017-05-03 05:10:17416 content::RenderProcessHost* new_process_host =
417 web_contents->GetMainFrame()->GetProcess();
418 EXPECT_NE(old_process_host, new_process_host);
lukasza1d025732016-09-28 21:36:08419}
lukasza2a130dc2016-10-06 00:50:43420
421IN_PROC_BROWSER_TEST_F(ChromeWebStoreProcessTest,
422 NavigateWebTabToChromeWebStoreViaPost) {
423 // Navigate a tab to a web page with a form.
424 GURL web_url = embedded_test_server()->GetURL("foo.com", "/form.html");
425 ui_test_utils::NavigateToURL(browser(), web_url);
426 WebContents* web_contents =
427 browser()->tab_strip_model()->GetActiveWebContents();
428 EXPECT_EQ(web_url, web_contents->GetLastCommittedURL());
429 content::RenderProcessHost* old_process_host =
430 web_contents->GetMainFrame()->GetProcess();
431
432 // Calculate an URL that is 1) relative to the fake (i.e. test-controlled)
433 // Chrome Web Store gallery URL and 2) resolves to something that
434 // embedded_test_server can actually serve (e.g. title1.html test file).
435 GURL::Replacements replace_path;
436 replace_path.SetPathStr("/title1.html");
437 GURL cws_web_url = gallery_url().ReplaceComponents(replace_path);
438
439 // Note that the |setTimeout| call below is needed to make sure
440 // ExecuteScriptAndExtractBool returns *after* a scheduled navigation has
441 // already started.
442 std::string navigation_starting_script =
443 "var form = document.getElementById('form');\n"
444 "form.action = '" + cws_web_url.spec() + "';\n"
445 "form.submit();\n"
446 "setTimeout(\n"
447 " function() { window.domAutomationController.send(true); },\n"
448 " 0);\n";
449
450 // Trigger a renderer-initiated POST navigation (via the form) to a Chrome Web
451 // Store gallery URL (which will commit into a chrome-extension://cws-app-id).
452 bool ignored_script_result = false;
453 content::TestNavigationObserver nav_observer(web_contents, 1);
nasko9729c202016-10-31 19:52:29454
lukasza2a130dc2016-10-06 00:50:43455 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
456 web_contents, navigation_starting_script, &ignored_script_result));
457
naskoabed2a52017-05-03 05:10:17458 // The expectation is that the store will be properly put in its own process,
459 // otherwise the renderer process is going to be terminated.
lukasza2a130dc2016-10-06 00:50:43460 // Verify that the navigation succeeded.
461 nav_observer.Wait();
462 EXPECT_EQ(cws_web_url, web_contents->GetLastCommittedURL());
463
464 // Verify that we really have the Chrome Web Store app loaded in the Web
465 // Contents.
466 content::RenderProcessHost* new_process_host =
467 web_contents->GetMainFrame()->GetProcess();
468 EXPECT_TRUE(extensions::ProcessMap::Get(profile())->Contains(
469 extensions::kWebStoreAppId, new_process_host->GetID()));
470
471 // Verify that Chrome Web Store is isolated in a separate renderer process.
nasko9729c202016-10-31 19:52:29472 EXPECT_NE(old_process_host, new_process_host);
lukasza2a130dc2016-10-06 00:50:43473}
naskod1aaa762017-03-18 02:14:45474
Alex Moshchukca171372017-11-28 16:11:31475// Check that navigations to the Chrome Web Store succeed when the Chrome Web
476// Store URL's origin is set as an isolated origin via the
477// --isolate-origins flag. See https://crbug.com/788837.
478IN_PROC_BROWSER_TEST_F(ChromeWebStoreInIsolatedOriginTest,
479 NavigationLoadsChromeWebStore) {
480 // Sanity check that a SiteInstance for a Chrome Web Store URL requires a
481 // dedicated process.
482 content::BrowserContext* context = browser()->profile();
483 scoped_refptr<content::SiteInstance> cws_site_instance =
484 content::SiteInstance::CreateForURL(context, gallery_url());
485 EXPECT_TRUE(cws_site_instance->RequiresDedicatedProcess());
486
487 // Navigate to Chrome Web Store and check that it's loaded successfully.
488 ui_test_utils::NavigateToURL(browser(), gallery_url());
489 WebContents* web_contents =
490 browser()->tab_strip_model()->GetActiveWebContents();
491 EXPECT_EQ(gallery_url(), web_contents->GetLastCommittedURL());
492
493 // Verify that the Chrome Web Store hosted app is really loaded.
494 content::RenderProcessHost* render_process_host =
495 web_contents->GetMainFrame()->GetProcess();
496 EXPECT_TRUE(extensions::ProcessMap::Get(profile())->Contains(
497 extensions::kWebStoreAppId, render_process_host->GetID()));
498}
499
naskod1aaa762017-03-18 02:14:45500// This test verifies that blocked navigations to extensions pages do not
501// overwrite process-per-site map inside content/.
jambb11ed742017-05-01 17:27:59502IN_PROC_BROWSER_TEST_F(ProcessManagementTest,
naskod1aaa762017-03-18 02:14:45503 NavigateToBlockedExtensionPageInNewTab) {
naskod1aaa762017-03-18 02:14:45504 ASSERT_TRUE(embedded_test_server()->Start());
505
506 // Load an extension, which will block a request for a specific page in it.
507 const extensions::Extension* extension = LoadExtension(
508 test_data_dir_.AppendASCII("web_request_site_process_registration"));
509 ASSERT_TRUE(extension);
510
511 WebContents* web_contents =
512 browser()->tab_strip_model()->GetActiveWebContents();
513 GURL blocked_url(extension->GetResourceURL("/blocked.html"));
514
515 // Navigating to the blocked extension URL should be done through a redirect,
516 // otherwise it will result in an OpenURL IPC from the renderer process, which
517 // will initiate a navigation through the browser process.
518 GURL redirect_url(
519 embedded_test_server()->GetURL("/server-redirect?" + blocked_url.spec()));
520
521 // Navigate the current tab to the test page in the extension, which will
522 // create the extension process and register the webRequest blocking listener.
523 ui_test_utils::NavigateToURL(browser(),
524 extension->GetResourceURL("/test.html"));
525
526 // Open a new tab to about:blank, which will result in a new SiteInstance
527 // without an explicit site URL set.
528 ui_test_utils::NavigateToURLWithDisposition(
529 browser(), GURL(url::kAboutBlankURL),
530 WindowOpenDisposition::NEW_FOREGROUND_TAB,
531 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
532 WebContents* new_web_contents =
533 browser()->tab_strip_model()->GetActiveWebContents();
534
535 // Navigate the new tab to an extension URL that will be blocked by
536 // webRequest. It must be a renderer-initiated navigation. It also uses a
537 // redirect, otherwise the regular renderer process will send an OpenURL
538 // IPC to the browser due to the chrome-extension:// URL.
539 std::string script =
540 base::StringPrintf("location.href = '%s';", redirect_url.spec().c_str());
541 content::TestNavigationObserver observer(new_web_contents);
542 EXPECT_TRUE(content::ExecuteScript(new_web_contents, script));
543 observer.Wait();
544
545 EXPECT_EQ(observer.last_navigation_url(), blocked_url);
546 EXPECT_FALSE(observer.last_navigation_succeeded());
547
548 // Very subtle check for content/ internal functionality :(.
549 // When a navigation is blocked, it still commits an error page. Since
550 // extensions use the process-per-site model, each extension URL is registered
551 // in a map from URL to a process. Creating a brand new SiteInstance for the
552 // extension URL should always result in a SiteInstance that has a process and
553 // the process is the same for all SiteInstances. This allows us to verify
554 // that the site-to-process map for the extension hasn't been overwritten by
555 // the process of the |blocked_url|.
556 scoped_refptr<content::SiteInstance> new_site_instance =
557 content::SiteInstance::CreateForURL(web_contents->GetBrowserContext(),
558 extension->GetResourceURL(""));
559 EXPECT_TRUE(new_site_instance->HasProcess());
560 EXPECT_EQ(new_site_instance->GetProcess(),
561 web_contents->GetSiteInstance()->GetProcess());
Charlie Reise2c2c492018-06-15 21:34:04562
563 // Ensure that reloading a blocked error page completes.
564 content::TestNavigationObserver reload_observer(new_web_contents);
565 new_web_contents->GetController().Reload(content::ReloadType::NORMAL, false);
566 reload_observer.Wait();
567 EXPECT_EQ(reload_observer.last_navigation_url(), blocked_url);
568 EXPECT_FALSE(reload_observer.last_navigation_succeeded());
naskod1aaa762017-03-18 02:14:45569}
Devlin Croninf1afce252017-08-19 01:21:48570
571// Check that whether we can access the window object of a window.open()'d url
572// to an extension is the same regardless of whether the extension is installed.
573// https://crbug.com/598265.
574IN_PROC_BROWSER_TEST_F(
575 ProcessManagementTest,
576 TestForkingBehaviorForUninstalledAndNonAccessibleExtensions) {
577 ASSERT_TRUE(embedded_test_server()->Start());
578 const Extension* extension =
579 LoadExtension(test_data_dir_.AppendASCII("simple_with_icon"));
580 ASSERT_TRUE(extension);
581 ASSERT_FALSE(
582 WebAccessibleResourcesInfo::HasWebAccessibleResources(extension));
583
584 const GURL installed_extension = extension->url();
585 const GURL nonexistent_extension("chrome-extension://" +
586 std::string(32, 'a') + "/");
587 EXPECT_NE(installed_extension, nonexistent_extension);
588
589 ui_test_utils::NavigateToURL(
590 browser(), embedded_test_server()->GetURL("example.com", "/empty.html"));
591 content::WebContents* web_contents =
592 browser()->tab_strip_model()->GetActiveWebContents();
593 auto can_access_window = [this, web_contents](const GURL& url) {
594 bool can_access = false;
595 const char kOpenNewWindow[] = "window.newWin = window.open('%s');";
596 const char kGetAccess[] =
597 R"(
598 {
599 let canAccess = false;
600 try {
601 window.newWin.document;
602 canAccess = true;
603 } catch (e) {
604 canAccess = false;
605 }
606 window.newWin.close();
607 window.domAutomationController.send(canAccess);
608 }
609 )";
610 EXPECT_TRUE(content::ExecuteScript(
611 web_contents, base::StringPrintf(kOpenNewWindow, url.spec().c_str())));
612
613 // WaitForLoadStop() will return false on a 404, but that can happen if we
614 // navigate to a blocked or nonexistent extension page.
615 ignore_result(content::WaitForLoadStop(
616 browser()->tab_strip_model()->GetActiveWebContents()));
617
618 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(web_contents, kGetAccess,
619 &can_access));
620
621 return can_access;
622 };
623
624 bool can_access_installed = can_access_window(installed_extension);
625 bool can_access_nonexistent = can_access_window(nonexistent_extension);
626 // Behavior for installed and nonexistent extensions should be equivalent.
627 // We don't care much about what the result is (since if it can access it,
628 // it's about:blank); only that the result is safe.
629 EXPECT_EQ(can_access_installed, can_access_nonexistent);
630}
631
632} // namespace extensions