Commit 07722172 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[WebLayer] Part 2: Add WL BackgroundSync classes.

This does the remaining work to add BackgroundSync support.
An end to end browsertest has been added to verify the registration +
sync event firing.

Also, manually tested using the site:
https://webplatformapis.com/one_off_sync/one_off_sync.html

Note that WebLayer doesn't yet wake the browser up to fire sync
events, as the only user of WebLayer doesn't need this functionality.

Bug: 1087486
Change-Id: Iceb8c3a4cbcf4f0f8611905b07c950ac069eb6da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2476282Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820155}
parent 4a9339fb
......@@ -9,18 +9,65 @@
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "content/public/browser/background_sync_parameters.h"
#include "content/public/test/background_sync_test_util.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "weblayer/browser/background_sync/background_sync_controller_factory.h"
#include "weblayer/browser/background_sync/background_sync_delegate_impl.h"
#include "weblayer/browser/host_content_settings_map_factory.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/weblayer_browser_test.h"
#include "weblayer/test/weblayer_browser_test_utils.h"
namespace {
const char kExampleUrl[] = "https://www.example.com/";
const char kTag[] = "test_tag";
} // namespace
namespace weblayer {
class BackgroundSyncBrowserTest : public WebLayerBrowserTest {};
class BackgroundSyncBrowserTest : public WebLayerBrowserTest {
public:
BackgroundSyncBrowserTest() = default;
~BackgroundSyncBrowserTest() override = default;
void SetUpOnMainThread() override {
sync_event_received_ = std::make_unique<base::RunLoop>();
content::background_sync_test_util::SetIgnoreNetworkChanges(
/* ignore= */ true);
https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
https_server_->RegisterRequestHandler(base::BindRepeating(
&BackgroundSyncBrowserTest::HandleRequest, base::Unretained(this)));
https_server_->AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("weblayer/test/data")));
ASSERT_TRUE(https_server_->Start());
}
// Intercepts all requests.
std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
const net::test_server::HttpRequest& request) {
if (request.GetURL().query() == "syncreceived") {
if (sync_event_received_)
sync_event_received_->Quit();
}
// The default handlers will take care of this request.
return nullptr;
}
protected:
content::WebContents* web_contents() {
return static_cast<TabImpl*>(shell()->tab())->web_contents();
}
std::unique_ptr<base::RunLoop> sync_event_received_;
std::unique_ptr<net::EmbeddedTestServer> https_server_;
};
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, GetBackgroundSyncController) {
EXPECT_TRUE(BackgroundSyncControllerFactory::GetForBrowserContext(
......@@ -102,6 +149,14 @@ IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, NormalProfile) {
EXPECT_FALSE(delegate->IsProfileOffTheRecord());
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, SyncEventFired) {
content::background_sync_test_util::SetOnline(web_contents(), false);
NavigateAndWaitForCompletion(
https_server_->GetURL("/background_sync_browsertest.html"), shell());
content::background_sync_test_util::SetOnline(web_contents(), true);
sync_event_received_->Run();
}
class IncognitoBackgroundSyncBrowserTest : public BackgroundSyncBrowserTest {
public:
IncognitoBackgroundSyncBrowserTest() { SetShellStartsInIncognitoMode(); }
......
......@@ -5,6 +5,7 @@
#include "weblayer/browser/browser_context_impl.h"
#include "base/threading/thread_restrictions.h"
#include "components/background_sync/background_sync_controller_impl.h"
#include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
#include "components/client_hints/browser/client_hints.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
......@@ -33,6 +34,7 @@
#include "content/public/browser/download_request_utils.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/storage_partition.h"
#include "weblayer/browser/background_sync/background_sync_controller_factory.h"
#include "weblayer/browser/browsing_data_remover_delegate.h"
#include "weblayer/browser/browsing_data_remover_delegate_factory.h"
#include "weblayer/browser/client_hints_factory.h"
......@@ -197,7 +199,7 @@ BrowserContextImpl::GetBackgroundFetchDelegate() {
content::BackgroundSyncController*
BrowserContextImpl::GetBackgroundSyncController() {
return nullptr;
return BackgroundSyncControllerFactory::GetForBrowserContext(this);
}
content::BrowsingDataRemoverDelegate*
......
......@@ -5,6 +5,7 @@
#include "weblayer/browser/permissions/permission_manager_factory.h"
#include "build/build_config.h"
#include "components/background_sync/background_sync_permission_context.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/permissions/permission_context_base.h"
......@@ -81,6 +82,8 @@ permissions::PermissionManager::PermissionContextMap CreatePermissionContexts(
std::make_unique<SafePermissionContext>(
browser_context, ContentSettingsType::MEDIASTREAM_CAMERA,
blink::mojom::FeaturePolicyFeature::kCamera);
permission_contexts[ContentSettingsType::BACKGROUND_SYNC] =
std::make_unique<BackgroundSyncPermissionContext>(browser_context);
// For now, all requests are denied. As features are added, their permission
// contexts can be added here instead of DeniedPermissionContext.
......
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>BackgroundSyncBrowserTest helper page</title>
<script>
window.onload = async () => {
let registration = await navigator.serviceWorker.register('/background_sync_sw.js');
navigator.serviceWorker.ready.then((registration) => {
registration.sync.register('test-tag');
});
}
</script>
</head>
<body>
BackgroundSyncBrowserTest helper page
</body>
</html>
// Copyright 2020 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.
'use strict';
// Service Worker initialization listeners.
self.addEventListener('install', event => event.waitUntil(skipWaiting()));
self.addEventListener('activate', event => event.waitUntil(clients.claim()));
self.addEventListener('sync', event => {
event.waitUntil(fetch(
'/background_sync_browsertest.html?syncreceived'));
});
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