Skip to content

Commit 78c2b66

Browse files
committed
Add NoBestEffortTasksTest for extension loading/messaging.
Adds a browser test that loads an extension, sends it a message, and receives a reply; all while BEST_EFFORT tasks are disabled. This is a regression test for http://crbug.com/177163#c112. Note that the test caught a bug in that BEST_EFFORT task priority was being used when the NetworkService was enabled (different code path than normal). This change fixes that as well. Bug: 177163,924416,925117 Change-Id: I9e4e1874e0f03d5be53224845a4f6fa42ffc68b3 Reviewed-on: https://chromium-review.googlesource.com/c/1427468 Reviewed-by: François Doray <[email protected]> Reviewed-by: Avi Drissman <[email protected]> Reviewed-by: Chris Mumford <[email protected]> Commit-Queue: François Doray <[email protected]> Cr-Original-Commit-Position: refs/heads/master@{#626544}(cherry picked from commit 60ea85d) Reviewed-on: https://chromium-review.googlesource.com/c/1444993 Reviewed-by: Yuri Wiitala <[email protected]> Cr-Commit-Position: refs/branch-heads/3683@{#55} Cr-Branched-From: e510299-refs/heads/master@{#625896}
1 parent 105c463 commit 78c2b66

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

chrome/browser/no_best_effort_tasks_browsertest.cc

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,35 @@
44

55
#include "base/base_switches.h"
66
#include "base/command_line.h"
7+
#include "base/files/file_path.h"
78
#include "base/macros.h"
9+
#include "base/path_service.h"
810
#include "base/run_loop.h"
11+
#include "base/strings/string_piece.h"
12+
#include "base/strings/stringprintf.h"
913
#include "build/build_config.h"
14+
#include "build/buildflag.h"
15+
#include "chrome/browser/profiles/profile.h"
1016
#include "chrome/browser/ui/browser.h"
1117
#include "chrome/browser/ui/tabs/tab_strip_model.h"
18+
#include "chrome/common/chrome_paths.h"
1219
#include "chrome/test/base/in_process_browser_test.h"
20+
#include "chrome/test/base/ui_test_utils.h"
1321
#include "content/public/browser/web_contents.h"
1422
#include "content/public/browser/web_contents_observer.h"
23+
#include "content/public/test/browser_test_utils.h"
1524
#include "content/public/test/test_utils.h"
25+
#include "net/dns/mock_host_resolver.h"
1626
#include "testing/gtest/include/gtest/gtest.h"
1727

28+
#if BUILDFLAG(ENABLE_EXTENSIONS)
29+
#include "chrome/browser/extensions/unpacked_installer.h"
30+
#include "extensions/browser/extension_registry.h"
31+
#include "extensions/browser/extension_system.h"
32+
#include "extensions/browser/test_extension_registry_observer.h"
33+
#include "extensions/common/extension.h"
34+
#endif
35+
1836
namespace {
1937

2038
class RunLoopUntilLoadedAndPainted : public content::WebContentsObserver {
@@ -63,11 +81,25 @@ class NoBestEffortTasksTest : public InProcessBrowserTest {
6381
private:
6482
void SetUpCommandLine(base::CommandLine* command_line) override {
6583
command_line->AppendSwitch(switches::kDisableBackgroundTasks);
84+
InProcessBrowserTest::SetUpCommandLine(command_line);
85+
}
86+
87+
void SetUpOnMainThread() override {
88+
// Redirect all DNS requests back to localhost (to the embedded test
89+
// server).
90+
host_resolver()->AddRule("*", "127.0.0.1");
91+
InProcessBrowserTest::SetUpOnMainThread();
6692
}
6793

6894
DISALLOW_COPY_AND_ASSIGN(NoBestEffortTasksTest);
6995
};
7096

97+
#if BUILDFLAG(ENABLE_EXTENSIONS)
98+
constexpr base::StringPiece kExtensionId = "ddchlicdkolnonkihahngkmmmjnjlkkf";
99+
constexpr base::TimeDelta kSendMessageRetryPeriod =
100+
base::TimeDelta::FromMilliseconds(250);
101+
#endif
102+
71103
} // namespace
72104

73105
// Verify that it is possible to load and paint the initial about:blank page
@@ -106,3 +138,74 @@ IN_PROC_BROWSER_TEST_F(NoBestEffortTasksTest, LoadAndPaintFromNetwork) {
106138
RunLoopUntilLoadedAndPainted run_until_loaded_and_painted(web_contents);
107139
run_until_loaded_and_painted.Run();
108140
}
141+
142+
// Verify that an extension can be loaded and perform basic messaging without
143+
// running BEST_EFFORT tasks. Regression test for http://crbug.com/177163#c112.
144+
#if BUILDFLAG(ENABLE_EXTENSIONS)
145+
IN_PROC_BROWSER_TEST_F(NoBestEffortTasksTest, LoadExtensionAndSendMessages) {
146+
ASSERT_TRUE(embedded_test_server()->Start());
147+
148+
// Load the extension, waiting until the ExtensionRegistry reports that its
149+
// renderer has been started.
150+
base::FilePath extension_dir;
151+
const bool have_test_data_dir =
152+
base::PathService::Get(chrome::DIR_TEST_DATA, &extension_dir);
153+
ASSERT_TRUE(have_test_data_dir);
154+
extension_dir = extension_dir.AppendASCII("extensions")
155+
.AppendASCII("no_best_effort_tasks_test_extension");
156+
extensions::UnpackedInstaller::Create(
157+
extensions::ExtensionSystem::Get(browser()->profile())
158+
->extension_service())
159+
->Load(extension_dir);
160+
auto* const extension =
161+
extensions::TestExtensionRegistryObserver(
162+
extensions::ExtensionRegistry::Get(browser()->profile()))
163+
.WaitForExtensionReady();
164+
ASSERT_TRUE(extension);
165+
ASSERT_EQ(kExtensionId, extension->id());
166+
167+
// Navigate to a test page, waiting until complete. Note that the hostname
168+
// here must match the pattern found in the extension's manifest file, or it
169+
// will not be able to send/receive messaging from the test web page (due to
170+
// extension permissions).
171+
ui_test_utils::NavigateToURL(
172+
browser(),
173+
embedded_test_server()->GetURL("fake.chromium.org", "/empty.html"));
174+
175+
// Execute JavaScript in the test page, to send a ping message to the
176+
// extension and await the reply. The chrome.runtime.sendMessage() operation
177+
// can fail if the extension's background page hasn't finished running yet
178+
// (i.e., there is no message listener yet). Thus, use a retry loop.
179+
const std::string request_reply_javascript = base::StringPrintf(
180+
"new Promise((resolve, reject) => {\n"
181+
" chrome.runtime.sendMessage(\n"
182+
" '%s',\n"
183+
" {ping: true},\n"
184+
" response => {\n"
185+
" if (response) {\n"
186+
" resolve(response);\n"
187+
" } else {\n"
188+
" reject(chrome.runtime.lastError.message);\n"
189+
" }\n"
190+
" });\n"
191+
"})",
192+
extension->id().c_str());
193+
for (;;) {
194+
const auto result =
195+
content::EvalJs(browser()->tab_strip_model()->GetActiveWebContents(),
196+
request_reply_javascript);
197+
if (result.error.empty()) {
198+
LOG(INFO) << "Got a response from the extension.";
199+
EXPECT_TRUE(result.value.FindBoolKey("pong").value_or(false));
200+
break;
201+
}
202+
// An error indicates the extension's message listener isn't up yet. Wait a
203+
// little before trying again.
204+
LOG(INFO) << "Waiting for the extension's message listener...";
205+
base::RunLoop run_loop;
206+
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
207+
FROM_HERE, run_loop.QuitClosure(), kSendMessageRetryPeriod);
208+
run_loop.Run();
209+
}
210+
}
211+
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2019 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+
chrome.runtime.onMessageExternal.addListener((request, from, sendResponse) => {
6+
if (request.ping) {
7+
console.info("Got ping, sending pong...");
8+
sendResponse({pong: true});
9+
}
10+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8xv6iO+j4kzj1HiBL93+XVJH/CRyAQMUHS/Z0l8nCAzaAFkW/JsNwxJqQhrZspnxLqbQxNncXs6g6bsXAwKHiEs+LSs+bIv0Gc/2ycZdhXJ8GhEsSMakog5dpQd1681c2gLK/8CrAoewE/0GIKhaFcp7a2iZlGh4Am6fgMKy0iQIDAQAB",
3+
"name": "NoBestEffortTasksTest extension",
4+
"version": "1",
5+
"manifest_version": 2,
6+
"description": "Responds to ping messages.",
7+
"background": {
8+
"scripts": ["background.js"],
9+
"persistent": true
10+
},
11+
"externally_connectable": {
12+
"matches": ["*://*.chromium.org/*"]
13+
}
14+
}

content/browser/file_url_loader_factory.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,11 @@ void CreateFileURLLoader(
851851
network::mojom::URLLoaderClientPtr client,
852852
std::unique_ptr<FileURLLoaderObserver> observer,
853853
scoped_refptr<net::HttpResponseHeaders> extra_response_headers) {
854+
// TODO(crbug.com/924416): Re-evaluate how TaskPriority is set here and in
855+
// other file URL-loading-related code. Some callers require USER_VISIBLE
856+
// (i.e., BEST_EFFORT is not enough).
854857
auto task_runner = base::CreateSequencedTaskRunnerWithTraits(
855-
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
858+
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
856859
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
857860
task_runner->PostTask(
858861
FROM_HERE,
@@ -868,10 +871,12 @@ std::unique_ptr<network::mojom::URLLoaderFactory> CreateFileURLLoaderFactory(
868871
const base::FilePath& profile_path,
869872
scoped_refptr<const SharedCorsOriginAccessList>
870873
shared_cors_origin_access_list) {
874+
// TODO(crbug.com/924416): Re-evaluate TaskPriority: Should the caller provide
875+
// it?
871876
return std::make_unique<content::FileURLLoaderFactory>(
872877
profile_path, shared_cors_origin_access_list,
873878
base::CreateSequencedTaskRunnerWithTraits(
874-
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
879+
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
875880
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
876881
}
877882

0 commit comments

Comments
 (0)