Commit ab489a4d authored by Karandeep Bhatia's avatar Karandeep Bhatia Committed by Commit Bot

Extensions: Don't suspend generated background page requests on system suspend.

This CL changes GeneratedBackgroundPageJob to not suspend file url requests when
the system suspends. There is no reason to suspend requests which don't require
network activity.

This helps solve bugs where extension event listeners are not registered as
the request for extension background pages are aborted on system suspend.

BUG=898494

Change-Id: I12c267322e28aee573c14160a91868b1cbec0a1b
Reviewed-on: https://chromium-review.googlesource.com/c/1352443
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611884}
parent 82d8ee1e
......@@ -14,6 +14,7 @@
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/test/power_monitor_test_base.h"
#include "base/test/test_file_util.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
......@@ -287,6 +288,12 @@ class ExtensionProtocolsTest
content::BrowserContext* browser_context() { return testing_profile_.get(); }
void SimulateSystemSuspendForRequests() {
power_monitor_source_ = new base::PowerMonitorTestSource();
power_monitor_ = std::make_unique<base::PowerMonitor>(
std::unique_ptr<base::PowerMonitorSource>(power_monitor_source_));
}
protected:
scoped_refptr<ContentVerifier> content_verifier_;
......@@ -304,6 +311,11 @@ class ExtensionProtocolsTest
client.CreateInterfacePtr(),
net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS));
if (power_monitor_source_) {
power_monitor_source_->GenerateSuspendEvent();
power_monitor_source_->GenerateResumeEvent();
}
client.RunUntilComplete();
return GetResult(client.response_head(),
client.completion_status().error_code);
......@@ -325,7 +337,18 @@ class ExtensionProtocolsTest
/*is_async=*/false, content::PREVIEWS_OFF,
/*navigation_ui_data*/ nullptr);
request->Start();
base::RunLoop().Run();
if (power_monitor_source_) {
power_monitor_source_->GenerateSuspendEvent();
power_monitor_source_->GenerateResumeEvent();
// PowerMonitorTestSource calls RunLoop().RunUntilIdle() which causes the
// request to be completed.
EXPECT_TRUE(test_delegate_.response_completed());
} else {
base::RunLoop().Run();
}
return GetResult(std::move(request), test_delegate_.request_status());
}
......@@ -352,6 +375,11 @@ class ExtensionProtocolsTest
std::unique_ptr<TestingProfile> testing_profile_;
net::TestDelegate test_delegate_;
std::unique_ptr<content::WebContents> contents_;
std::unique_ptr<base::PowerMonitor> power_monitor_;
// |power_monitor_source_| is owned by |power_monitor_|
base::PowerMonitorTestSource* power_monitor_source_ = nullptr;
};
// Tests that making a chrome-extension request in an incognito context is
......@@ -759,6 +787,31 @@ TEST_P(ExtensionProtocolsTest, MimeTypesForKnownFiles) {
}
}
// Tests that requests for extension resources (including the generated
// background page) are not aborted on system suspend.
TEST_P(ExtensionProtocolsTest, ExtensionRequestsNotAborted) {
// Register a non-incognito extension protocol handler.
SetProtocolHandler(false);
base::FilePath extension_dir =
GetTestPath("common").AppendASCII("background_script");
std::string error;
scoped_refptr<Extension> extension = file_util::LoadExtension(
extension_dir, Manifest::INTERNAL, Extension::NO_FLAGS, &error);
ASSERT_TRUE(extension.get()) << error;
SimulateSystemSuspendForRequests();
// Request the generated background page. Ensure the request completes
// successfully.
EXPECT_EQ(net::OK,
DoRequestOrLoad(extension.get(), kGeneratedBackgroundPageFilename)
.result());
// Request the background.js file. Ensure the request completes successfully.
EXPECT_EQ(net::OK, DoRequestOrLoad(extension.get(), "background.js").result());
}
INSTANTIATE_TEST_CASE_P(Extensions,
ExtensionProtocolsTest,
::testing::ValuesIn(kTestModes));
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
"description": "Extension which has just a background script",
"name": "background_script",
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2,
"version": "0.1"
}
......@@ -129,6 +129,14 @@ class GeneratedBackgroundPageJob : public net::URLRequestSimpleJob {
return net::OK;
}
// base::PowerObserver override:
void OnSuspend() override {
// Unlike URLRequestJob, don't suspend active requests here. Requests for
// generated background pages need not be suspended when the system
// suspends. This is not needed for URLRequestExtensionJob since it inherits
// from URLRequestFileJob, which has the same behavior.
}
void GetResponseInfo(net::HttpResponseInfo* info) override {
*info = response_info_;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment