chromium/chrome/browser/extensions/service_worker_lifetime_keepalive_browsertest.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/permissions/permissions_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_management_test_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/test/test_browser_closed_waiter.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/profile_destruction_waiter.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/service_worker_context_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/service_worker_test_helpers.h"
#include "extensions/browser/api/test/test_api.h"
#include "extensions/browser/background_script_executor.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/service_worker/service_worker_keepalive.h"
#include "extensions/browser/service_worker/service_worker_test_utils.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "url/gurl.h"

namespace extensions {

namespace {

constexpr char kTestOpenerExtensionId[] =;
constexpr char kTestOpenerExtensionUrl[] =;
constexpr char kTestOpenerExtensionRelativePath[] =;

constexpr char kTestReceiverExtensionId[] =;
constexpr char kTestReceiverExtensionUrl[] =;
constexpr char kTestReceiverExtensionRelativePath[] =;

constexpr char kPersistentPortConnectedMessage[] =;
constexpr char kPersistentPortDisconnectedMessage[] =;

// Gets a keepalive matcher that enforces the extra data field.
testing::Matcher<ProcessManager::ServiceWorkerKeepaliveData>
GetKeepaliveMatcher(const WorkerId& worker_id,
                    Activity::Type type,
                    const std::string& activity_extra_data) {}

// Gets a keepalive matcher enforcing only the worker ID and activity type.
testing::Matcher<ProcessManager::ServiceWorkerKeepaliveData>
GetKeepaliveMatcher(const WorkerId& worker_id, Activity::Type type) {}

// Returns the number of active external requests to the service worker of
// the specified `extension` in the given `context`.
size_t GetExternalRequestCountForWorker(content::BrowserContext& context,
                                        const Extension& extension) {}

}  // namespace

TestServiceWorkerContextObserver;

class ServiceWorkerLifetimeKeepaliveBrowsertest : public ExtensionApiTest {};

// Loads two extensions that open a persistent port connection between each
// other and tests that their service worker will stop after kRequestTimeout (5
// minutes).
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ServiceWorkersTimeOutWithoutPolicy) {}

// Tests that the service workers will not stop if both extensions are
// allowlisted via policy and the port is not closed.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ServiceWorkersDoNotTimeOutWithPolicy) {}

// Tests that the extended lifetime only lasts as long as there is a persistent
// port connection. If the port is closed (by one of the service workers
// stopping), the other service worker will also stop, even if it received an
// extended lifetime.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ServiceWorkersTimeOutWhenOnlyOneHasExtendedLifetime) {}

// Tests that the service workers will stop if both extensions are allowlisted
// via policy and the port is disconnected.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ServiceWorkersTimeOutWhenPortIsDisconnected) {}

// Tests that certain API functions can keep the service worker alive
// indefinitely.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       KeepalivesForCertainExtensionFunctions) {}

// Test the flow of an extension function resolving after an extension service
// worker has timed out and been terminated.
// Regression test for https://crbug.com/1453534.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ExtensionFunctionGetsResolvedAfterWorkerTermination) {}

// Tests that an active debugger session will keep an extension service worker
// alive past its typical timeout.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       DebuggerAttachKeepsServiceWorkerAlive) {}

// Tests the behavior of the ServiceWorkerKeepalive struct, ensuring it properly
// keeps the service worker alive.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ServiceWorkerKeepaliveUtility) {}

// Tests shutting down the associated browser context while the extension has
// an active keepalive from a message pipe behaves appropriately.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ShutdownWithActiveMessagePipe) {}

// Tests that we can safely shut down a BrowserContext when an extension has
// an active message port to another extension, where each are running in
// split incognito mode.
// Regression test for https://crbug.com/1476316.
IN_PROC_BROWSER_TEST_F(ServiceWorkerLifetimeKeepaliveBrowsertest,
                       ShutdownWithActiveMessagePipe_SplitModeExtension) {}

// Tests that we can safely shut down a BrowserContext when an extension has an
// active keepalive for a service worker that spans between the on- and off-
// the-record profiles between two different extensions.
IN_PROC_BROWSER_TEST_F(
    ServiceWorkerLifetimeKeepaliveBrowsertest,
    ShutdownWithActiveMessagePipe_BetweenExtensionsInDifferentContexts) {}

}  // namespace extensions