blob: 0f0deeb8713814b769a5393ba8f382f3ef607315 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 The Chromium Authors
Nasko Oskov8a73f7d2019-09-27 22:29:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Daniel Cheng0d360f552020-12-01 23:11:425#include "base/strings/utf_string_conversions.h"
Nasko Oskov8a73f7d2019-09-27 22:29:326#include "content/browser/child_process_security_policy_impl.h"
7#include "content/browser/renderer_host/render_process_host_impl.h"
David Sandersee132412025-06-25 17:50:298#include "content/public/browser/web_contents.h"
Peter Kasting919ce652020-05-07 10:22:369#include "content/public/test/browser_test.h"
Nasko Oskov8a73f7d2019-09-27 22:29:3210#include "content/public/test/browser_test_utils.h"
11#include "content/public/test/content_browser_test.h"
12#include "content/public/test/content_browser_test_utils.h"
13#include "content/shell/browser/shell.h"
Lukasz Anforowiczbb5ab432020-07-27 20:18:0614#include "testing/gmock/include/gmock/gmock.h"
Nasko Oskov8a73f7d2019-09-27 22:29:3215
16namespace content {
17
Daniel Cheng0d360f552020-12-01 23:11:4218class ProcessInternalsWebUiBrowserTest : public ContentBrowserTest {
19 protected:
20 // Executing javascript in the WebUI requires using an isolated world in which
21 // to execute the script because WebUI has a default CSP policy denying
22 // "eval()", which is what EvalJs uses under the hood.
23 bool ExecJsInWebUI(const std::string& script) {
Dave Tapuska327c06c92022-06-13 20:31:5124 return ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), script,
Daniel Cheng0d360f552020-12-01 23:11:4225 EXECUTE_SCRIPT_DEFAULT_OPTIONS, 1 /* world_id */);
26 }
27};
Nasko Oskov8a73f7d2019-09-27 22:29:3228
29// This test verifies that loading of the process-internals WebUI works
30// correctly and the process rendering it has no WebUI bindings.
31IN_PROC_BROWSER_TEST_F(ProcessInternalsWebUiBrowserTest, NoProcessBindings) {
32 GURL url("chrome://process-internals/#web-contents");
33 EXPECT_TRUE(NavigateToURL(shell(), url));
34
35 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
Emily Andrewsd15fd762024-12-10 20:41:5436 shell()
37 ->web_contents()
38 ->GetPrimaryMainFrame()
39 ->GetProcess()
40 ->GetDeprecatedID()));
Nasko Oskov8a73f7d2019-09-27 22:29:3241
42 // Execute script to ensure the page has loaded correctly and was successful
43 // at retrieving data from the browser process.
44 // Note: This requires using an isolated world in which to execute the
45 // script because WebUI has a default CSP policy denying "eval()", which is
46 // what EvalJs uses under the hood.
Lukasz Anforowiczbb5ab432020-07-27 20:18:0647 std::string page_contents =
Dave Tapuska327c06c92022-06-13 20:31:5148 EvalJs(shell()->web_contents()->GetPrimaryMainFrame(),
49 "document.body.innerHTML", EXECUTE_SCRIPT_DEFAULT_OPTIONS,
50 1 /* world_id */)
Lukasz Anforowiczbb5ab432020-07-27 20:18:0651 .ExtractString();
52
53 // Crude verification that the page had the right content.
54 EXPECT_THAT(page_contents, ::testing::HasSubstr("Process Internals"));
Nasko Oskov8a73f7d2019-09-27 22:29:3255}
56
Daniel Cheng0d360f552020-12-01 23:11:4257IN_PROC_BROWSER_TEST_F(ProcessInternalsWebUiBrowserTest,
58 MojoJsBindingsCorrectlyScoped) {
Jan Wilken Dörrie8aeb5742021-03-23 19:27:0259 const std::u16string passed_title = u"passed";
Daniel Cheng0d360f552020-12-01 23:11:4260
61 GURL url("chrome://process-internals/#web-contents");
62 EXPECT_TRUE(NavigateToURL(shell(), url));
63 {
64 TitleWatcher sent_title_watcher(shell()->web_contents(), passed_title);
65 EXPECT_TRUE(
66 ExecJsInWebUI("document.title = window.Mojo? 'passed' : 'failed';"));
67 EXPECT_EQ(passed_title, sent_title_watcher.WaitAndGetTitle());
68 }
69
70 EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
71 {
72 TitleWatcher sent_title_watcher(shell()->web_contents(), passed_title);
73 EXPECT_TRUE(
74 ExecJsInWebUI("document.title = window.Mojo? 'failed' : 'passed';"));
75 EXPECT_EQ(passed_title, sent_title_watcher.WaitAndGetTitle());
76 }
77}
78
Nasko Oskov8a73f7d2019-09-27 22:29:3279} // namespace content