#include <cmath>
#include <map>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "base/containers/contains.h"
#include "base/json/json_reader.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_base.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "base/test/with_feature_override.h"
#include "base/values.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "components/content_settings/core/browser/content_settings_pref_provider.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_partition_key.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/privacy_sandbox_attestations.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/scoped_privacy_sandbox_attestations.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
#include "components/privacy_sandbox/privacy_sandbox_test_util.h"
#include "components/services/storage/shared_storage/shared_storage_manager.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/shared_storage_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_select_url_fenced_frame_config_observer.h"
#include "content/public/test/test_shared_storage_header_observer.h"
#include "net/base/schemeful_site.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/fenced_frame/fenced_frame_utils.h"
#include "third_party/blink/public/common/shared_storage/shared_storage_utils.h"
#include "url/url_constants.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#else
#include "chrome/browser/ui/browser.h"
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "extensions/test/test_extension_dir.h"
#endif
namespace storage {
namespace {
OperationResult;
const auto& SetOperation = …;
const auto& AppendOperation = …;
const auto& ClearOperation = …;
constexpr char kMainHost[] = …;
constexpr char kSimplePagePath[] = …;
constexpr char kTitle1Path[] = …;
constexpr char kCrossOriginHost[] = …;
constexpr char kThirdOriginHost[] = …;
constexpr char kFourthOriginHost[] = …;
constexpr char kRemainingBudgetPrefix[] = …;
constexpr char kErrorTypeHistogram[] = …;
constexpr char kEntriesQueuedCountHistogram[] = …;
constexpr char kReceivedEntriesBenchmarksHistogram[] = …;
constexpr char kIteratedEntriesBenchmarksHistogram[] = …;
constexpr char kTimingDocumentAddModuleHistogram[] = …;
constexpr char kTimingDocumentRunHistogram[] = …;
constexpr char kTimingDocumentSelectUrlHistogram[] = …;
constexpr char kTimingDocumentAppendHistogram[] = …;
constexpr char kTimingDocumentSetHistogram[] = …;
constexpr char kTimingDocumentDeleteHistogram[] = …;
constexpr char kTimingDocumentClearHistogram[] = …;
constexpr char kTimingWorkletAppendHistogram[] = …;
constexpr char kTimingWorkletSetHistogram[] = …;
constexpr char kTimingWorkletGetHistogram[] = …;
constexpr char kTimingWorkletLengthHistogram[] = …;
constexpr char kTimingWorkletDeleteHistogram[] = …;
constexpr char kTimingWorkletClearHistogram[] = …;
constexpr char kTimingWorkletKeysHistogram[] = …;
constexpr char kTimingWorkletEntriesHistogram[] = …;
constexpr char kWorkletNumPerPageHistogram[] = …;
constexpr char kSelectUrlCallsPerPageHistogram[] = …;
constexpr char kTimingRemainingBudgetHistogram[] = …;
constexpr char kPrivateAggregationHostPipeResultHistogram[] = …;
constexpr char
kPrivateAggregationHostTimeToGenerateReportRequestWithContextIdHistogram[] = …;
const double kBudgetAllowed = …;
enum class EnforcementAndEnrollmentStatus { … };
#if BUILDFLAG(IS_ANDROID)
base::FilePath GetChromeTestDataDir() {
return base::FilePath(FILE_PATH_LITERAL("chrome/test/data"));
}
#endif
base::RepeatingCallback<
bool(const content::WebContentsConsoleObserver::Message& message)>
MakeFilter(std::vector<std::string> possible_last_messages) { … }
std::string GetSharedStorageDisabledErrorMessage() { … }
std::string GetSharedStorageSelectURLDisabledErrorMessage() { … }
std::string GetSharedStorageAddModuleDisabledErrorMessage() { … }
void DelayBy(base::TimeDelta delta) { … }
void WaitForHistograms(std::vector<std::string> histogram_names) { … }
int GetSampleCountForHistogram(const std::string& histogram_name) { … }
void WaitForHistogramsWithSampleCounts(
std::vector<std::tuple<std::string, int>> histogram_names_and_counts) { … }
content::RenderFrameHost* LastChild(content::RenderFrameHost* parent_rfh) { … }
content::RenderFrameHost* CreateIframe(content::RenderFrameHost* parent_rfh,
const GURL& url) { … }
privacy_sandbox::PrivacySandboxAttestationsMap
MakeSharedStoragePrivacySandboxAttestationsMap(
const std::vector<GURL>& enrollee_urls,
bool enroll_for_private_aggregation = false) { … }
class MockChromeContentBrowserClient : public ChromeContentBrowserClient { … };
}
class SharedStorageChromeBrowserTestBase : public PlatformBrowserTest { … };
class SharedStorageChromeBrowserTest
: public SharedStorageChromeBrowserTestBase,
public testing::WithParamInterface<bool> { … };
SharedStorageChromeBrowserParams;
#endif
class SharedStoragePrefBrowserTest
: public SharedStorageChromeBrowserTestBase,
public testing::WithParamInterface<SharedStorageChromeBrowserParams> { … };
namespace {
std::string DescribePrefBrowserTestParams(
const testing::TestParamInfo<SharedStoragePrefBrowserTest::ParamType>&
info) { … }
}
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, AddModule) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, RunOperation) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, RunURLSelectionOperation) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, Set) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, Append) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, Delete) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, Clear) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletSet) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletAppend) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletDelete) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletClear) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletGet) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletKeys) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletEntries) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletLength) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrefBrowserTest, WorkletRemainingBudget) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
WorkletKeysEntries_AllIterated) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
#if BUILDFLAG(IS_ANDROID) && !defined(NDEBUG)
DISABLED_WorkletKeys_PartiallyIterated
#else
WorkletKeys_PartiallyIterated
#endif
) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
#if BUILDFLAG(IS_ANDROID) && !defined(NDEBUG)
DISABLED_WorkletEntries_PartiallyIterated
#else
WorkletEntries_PartiallyIterated
#endif
) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
WorkletKeysEntries_AllIteratedLessThanTenKeys) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
WorkletKeysEntries_PartiallyIteratedLessThanTenKeys) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
WorkletKeysEntries_AllIteratedNoKeys) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
AddModule_InvalidScriptUrlError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
AddModule_LoadFailureError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
AddModule_UnexpectedRedirectError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
AddModule_EmptyResultError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
AddModule_MultipleAddModuleError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, Run_NotLoadedError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, Run_NotRegisteredError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, Run_FunctionError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, Run_ScriptError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
Run_UnexpectedCustomDataError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
SelectUrl_NotLoadedError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
SelectUrl_NotRegisteredError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
SelectUrl_FunctionError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, SelectUrl_ScriptError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
SelectUrl_UnexpectedCustomDataError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
SelectUrl_OutOfRangeError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
SelectUrl_ReturnValueToIntError) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletScriptDataOrigin_PrefsError_PrivacySandbox) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletScriptDataOrigin_PrefsError_SiteSettings) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletScriptDataOrigin_AttestationError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginWorklet_SelectUrl_PrefsError_PrivacySandbox) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginWorklet_SelectUrl_PrefsError_SiteSettings) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginWorklet_Run_PrefsError_PrivacySandbox) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginWorklet_Run_PrefsError_SiteSettings) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletScriptDataOrigin_NetworkError_NoAllowOriginResponseHeader) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletScriptDataOrigin_NetworkError_NoCrossOriginWorkletResponseHeader) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletScriptDataOrigin_NetworkError_404) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletScriptDataOrigin_Success) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_PrefsError_PrivacySandbox) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_PrefsError_SiteSettings) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_NoAllowOriginResponseHeader_Failure) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_NoCrossOriginWorkletResponseHeader_Success) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletContextDataOrigin_PrefsError_PrivacySandbox) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletContextDataOrigin_PrefsError_SiteSettings) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletContextDataOrigin_NoAllowOriginResponseHeader_Failure) { … }
IN_PROC_BROWSER_TEST_P(
SharedStorageChromeBrowserTest,
CrossOriginWorkletScript_CreateWorkletContextDataOrigin_NoCrossOriginWorkletResponseHeader_Success) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginScript_AddModule_PrefsError_PrivacySandbox) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginScript_AddModule_PrefsError_SiteSettings) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginScript_AddModule_AttestationError) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginScript_AddModule_NetworkError_MissingHeader) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginScript_AddModule_NetworkError_404) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginScript_AddModule_Success) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginWorklet_SelectURL_Success) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
CrossOriginWorklet_Run_Success) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, DocumentTiming) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, WorkletTiming) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest, WorkletNumPerPage_Two) { … }
IN_PROC_BROWSER_TEST_P(SharedStorageChromeBrowserTest,
DISABLED_WorkletNumPerPage_Three) { … }
INSTANTIATE_TEST_SUITE_P(…);
class SharedStorageFencedFrameChromeBrowserTest
: public SharedStorageChromeBrowserTestBase { … };
IN_PROC_BROWSER_TEST_F(SharedStorageFencedFrameChromeBrowserTest,
FencedFrameNavigateTop_BudgetWithdrawal) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageFencedFrameChromeBrowserTest,
TwoFencedFrames_DifferentURNs_EachNavigateOnce_BudgetWithdrawalTwice) { … }
class SharedStoragePrivateAggregationChromeBrowserTest
: public SharedStorageChromeBrowserTestBase,
public testing::WithParamInterface<EnforcementAndEnrollmentStatus> { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(SharedStoragePrivateAggregationChromeBrowserTest,
ContributeToHistogramViaRun) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrivateAggregationChromeBrowserTest,
ContributeToHistogramViaSelectURL) { … }
IN_PROC_BROWSER_TEST_P(SharedStoragePrivateAggregationChromeBrowserTest,
WithContextId_NoPrivateAggregationJS) { … }
class SharedStorageHeaderPrefBrowserTest : public SharedStoragePrefBrowserTest { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(SharedStorageHeaderPrefBrowserTest, Basic) { … }
class SharedStorageChromeNoParamsBrowserTest
: public SharedStorageChromeBrowserTestBase { … };
class SharedStorageChromeCrossOriginScriptDisabledBrowserTest
: public SharedStorageChromeNoParamsBrowserTest { … };
IN_PROC_BROWSER_TEST_F(SharedStorageChromeCrossOriginScriptDisabledBrowserTest,
AddModule_CrossOriginScriptError) { … }
class SharedStorageChromeContextOriginByDefaultDisabledBrowserTest
: public SharedStorageChromeNoParamsBrowserTest { … };
IN_PROC_BROWSER_TEST_F(
SharedStorageChromeContextOriginByDefaultDisabledBrowserTest,
CrossOriginWorkletScript_CreateWorklet_PrefsError_PrivacySandbox) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageChromeContextOriginByDefaultDisabledBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_PrefsError_SiteSettings) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageChromeContextOriginByDefaultDisabledBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_AttestationError) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageChromeContextOriginByDefaultDisabledBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_NetworkError__NoAllowOriginResponseHeader) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageChromeContextOriginByDefaultDisabledBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_NetworkError__NoCrossOriginWorkletResponseHeader) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageChromeContextOriginByDefaultDisabledBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_NetworkError_404) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageChromeContextOriginByDefaultDisabledBrowserTest,
CrossOriginWorkletScript_CreateWorkletDefaultDataOrigin_Success) { … }
#if BUILDFLAG(ENABLE_EXTENSIONS)
namespace {
constexpr char kBackgroundJSTemplate[] = …;
constexpr char kModulePath[] = …;
constexpr char kModuleMessage[] = …;
constexpr char kModuleTemplate[] = …;
constexpr char kLoadFailedMessagePrefix[] = …;
}
class SharedStorageExtensionBrowserTest
: public extensions::ExtensionBrowserTest { … };
IN_PROC_BROWSER_TEST_F(
SharedStorageExtensionBrowserTest,
CrossOrigin_ExtensionRemovesRequestHeader_CORSRetainsRequestHeader_NoResponseHeader_Failure) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageExtensionBrowserTest,
CrossOrigin_ExtensionRemovesRequestHeader_CORSRetainsRequestHeader_ResponseHeader_Success) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageExtensionBrowserTest,
CrossOrigin_ExtensionModifiesRequestHeader_CORSRetainsRequestHeader_NoResponseHeader_Failure) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageExtensionBrowserTest,
CrossOrigin_ExtensionModifiesRequestHeader_CORSRetainsRequestHeader_ResponseHeader_Success) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageExtensionBrowserTest,
SameOrigin_ExtensionAddsRequestHeader_CORSRetainsRequestHeader_NoResponseHeader_Success) { … }
IN_PROC_BROWSER_TEST_F(
SharedStorageExtensionBrowserTest,
SameOrigin_ExtensionAddsRequestHeader_CORSRetainsRequestHeader_ResponseHeader_Success) { … }
#endif
}