#include <optional>
#include <string_view>
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/numerics/safe_conversions.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/page_load_metrics/observers/service_worker_page_load_metrics_observer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_chrome_web_ui_controller_factory.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/favicon/content/content_favicon_driver.h"
#include "components/favicon/core/favicon_driver_observer.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/nacl/common/buildflags.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/page.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/webui_config.h"
#include "content/public/browser/webui_config_map.h"
#include "content/public/common/content_features.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_frame_navigation_observer.h"
#include "net/dns/mock_host_resolver.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 "ppapi/shared_impl/ppapi_switches.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/public/common/messaging/string_message_codec.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/origin.h"
PageLoadMetricsTestWaiter;
namespace chrome_service_worker_browser_test {
const char kInstallAndWaitForActivatedPage[] = …;
const char kInstallAndWaitForActivatedPageWithModuleScript[] = …;
template <typename T>
static void ExpectResultAndRun(T expected,
base::OnceClosure continuation,
T actual) { … }
class ChromeServiceWorkerTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
CanShutDownWithRegisteredServiceWorker) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
CanCloseIncognitoWindowWithServiceWorkerController) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
FailRegisterServiceWorkerWhenJSDisabled) { … }
IN_PROC_BROWSER_TEST_F(
ChromeServiceWorkerTest,
FallbackMainResourceRequestWhenJSDisabledForClassicServiceWorker) { … }
IN_PROC_BROWSER_TEST_F(
ChromeServiceWorkerTest,
FallbackMainResourceRequestWhenJSDisabledForModuleServiceWorker) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
StartServiceWorkerAndDispatchMessage) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
StartServiceWorkerWithModuleScriptAndDispatchMessage) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, SubresourceCountUKM) { … }
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#define MAYBE_StaticRoutingAPISubresourceHistogramTest …
#else
#define MAYBE_StaticRoutingAPISubresourceHistogramTest …
#endif
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
MAYBE_StaticRoutingAPISubresourceHistogramTest) { … }
#if BUILDFLAG(IS_MAC)
#define MAYBE_SubresourceCountUMA …
#else
#define MAYBE_SubresourceCountUMA …
#endif
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, MAYBE_SubresourceCountUMA) { … }
class ChromeServiceWorkerFetchTest : public ChromeServiceWorkerTest { … };
class FaviconUpdateWaiter : public favicon::FaviconDriverObserver { … };
class ChromeServiceWorkerLinkFetchTest : public ChromeServiceWorkerFetchTest { … };
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerLinkFetchTest, ManifestSameOrigin) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerLinkFetchTest,
ManifestSameOriginUseCredentials) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerLinkFetchTest, ManifestOtherOrigin) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerLinkFetchTest,
ManifestOtherOriginUseCredentials) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerLinkFetchTest, FaviconSameOrigin) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerLinkFetchTest, FaviconOtherOrigin) { … }
#if BUILDFLAG(ENABLE_NACL)
class ChromeServiceWorkerFetchPPAPITest : public ChromeServiceWorkerFetchTest {
public:
ChromeServiceWorkerFetchPPAPITest(const ChromeServiceWorkerFetchPPAPITest&) =
delete;
ChromeServiceWorkerFetchPPAPITest& operator=(
const ChromeServiceWorkerFetchPPAPITest&) = delete;
protected:
ChromeServiceWorkerFetchPPAPITest() {}
~ChromeServiceWorkerFetchPPAPITest() override {}
void SetUpCommandLine(base::CommandLine* command_line) override {
ChromeServiceWorkerFetchTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnableNaCl);
}
void SetUpOnMainThread() override {
base::FilePath document_root;
ASSERT_TRUE(ui_test_utils::GetRelativeBuildDirectory(&document_root));
embedded_test_server()->AddDefaultHandlers(
document_root.Append(FILE_PATH_LITERAL("nacl_test_data"))
.Append(FILE_PATH_LITERAL("pnacl")));
ChromeServiceWorkerFetchTest::SetUpOnMainThread();
test_page_url_ = GetURL("/pnacl_url_loader.html");
}
std::string GetNavigationRequestString(const std::string& fragment) const {
return RequestString(test_page_url_ + fragment, "navigate", "include", "");
}
std::string ExecutePNACLUrlLoaderTest(const std::string& mode) {
content::DOMMessageQueue message_queue;
EXPECT_TRUE(content::ExecJs(
browser()->tab_strip_model()->GetActiveWebContents(),
base::StringPrintf("reportOnFetch = false;"
"var iframe = document.createElement('iframe');"
"iframe.src='%s#%s';"
"document.body.appendChild(iframe);",
test_page_url_.c_str(), mode.c_str())));
std::string json;
EXPECT_TRUE(message_queue.WaitForMessage(&json));
base::Value result =
base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS).value();
EXPECT_TRUE(result.is_string());
EXPECT_EQ(base::StringPrintf("OnOpen%s", mode.c_str()), result.GetString());
return EvalJs(browser()->tab_strip_model()->GetActiveWebContents(),
"reportRequests();")
.ExtractString();
}
private:
std::string test_page_url_;
};
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerFetchPPAPITest,
DISABLED_NotInterceptedByServiceWorker) {
const std::string fragment =
"NotIntercepted";
EXPECT_EQ(GetNavigationRequestString("#" + fragment),
ExecutePNACLUrlLoaderTest(fragment));
}
#endif
class ChromeServiceWorkerNavigationHintTest : public ChromeServiceWorkerTest { … };
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest, Started) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest,
StartedModuleScript) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest, AlreadyRunning) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest,
AlreadyRunningModuleScript) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest,
NoServiceWorkerRegistration) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest,
NoActiveServiceWorkerVersion) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest, NoFetchHandler) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationHintTest,
NoFetchHandlerModuleScript) { … }
class StaticURLDataSource : public content::URLDataSource { … };
class StaticWebUIController : public content::WebUIController { … };
class TestWebUIConfig : public content::WebUIConfig { … };
class ChromeWebUIServiceWorkerTest : public ChromeServiceWorkerTest { … };
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerTest,
DisallowChromeSchemeInJavaScript) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerTest, DisallowChromeScheme) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerTest,
DisallowChromeUntrustedSchemeInJavaScript) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerTest,
DisllowChromeUntrustedScheme) { … }
class ChromeWebUIServiceWorkerFlagTest : public ChromeWebUIServiceWorkerTest { … };
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerFlagTest,
DisallowChromeSchemeInJavaScript) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerFlagTest,
DisallowChromeUntrustedScheme) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerFlagTest, AllowChromeScheme) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerFlagTest,
DisallowChromeUntrustedSchemeInJavaScript) { … }
class ChromeWebUIServiceWorkerUntrustedFlagTest
: public ChromeWebUIServiceWorkerTest { … };
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerUntrustedFlagTest,
DisallowChromeSchemeInJavaScript) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerUntrustedFlagTest,
DisallowChromeScheme) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerUntrustedFlagTest,
AllowChromeUntrustedScheme) { … }
IN_PROC_BROWSER_TEST_F(ChromeWebUIServiceWorkerUntrustedFlagTest,
DisallowChromeUntrustedSchemeInJavaScript) { … }
enum class ServicifiedFeatures { … };
class ChromeServiceWorkerNavigationPreloadTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationPreloadTest,
TopFrameWithThirdPartyBlocking) { … }
IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerNavigationPreloadTest,
SubFrameWithThirdPartyBlocking) { … }
}