#include "content/common/content_navigation_policy.h"
#include <bitset>
#include "base/command_line.h"
#include "base/metrics/field_trial_params.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"
#include "content/common/features.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "net/base/features.h"
namespace content {
bool DeviceHasEnoughMemoryForBackForwardCache() {
if (base::FeatureList::IsEnabled(features::kBackForwardCacheMemoryControls)) {
int default_memory_threshold_mb =
#if BUILDFLAG(IS_ANDROID)
1200;
#else
0;
#endif
int memory_threshold_mb = base::GetFieldTrialParamByFeatureAsInt(
features::kBackForwardCacheMemoryControls,
"memory_threshold_for_back_forward_cache_in_mb",
default_memory_threshold_mb);
return base::SysInfo::AmountOfPhysicalMemory().InMiB() >
memory_threshold_mb;
}
return true;
}
bool IsBackForwardCacheDisabledByCommandLine() {
if (base::CommandLine::InitializedForCurrentProcess() &&
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableBackForwardCache)) {
return true;
}
return false;
}
bool IsBackForwardCacheEnabled() {
bool has_enough_memory = DeviceHasEnoughMemoryForBackForwardCache();
if (!has_enough_memory) {
return false;
}
if (IsBackForwardCacheDisabledByCommandLine())
return false;
if (base::FeatureList::IsEnabled(features::kBackForwardCache)) {
return true;
}
return false;
}
bool CanCrossSiteNavigationsProactivelySwapBrowsingInstances() {
return IsBackForwardCacheEnabled();
}
constexpr base::FeatureParam<RenderDocumentLevel>::Option
render_document_levels[] = {
{RenderDocumentLevel::kCrashedFrame, "crashed-frame"},
{RenderDocumentLevel::kNonLocalRootSubframe, "non-local-root-subframe"},
{RenderDocumentLevel::kSubframe, "subframe"},
{RenderDocumentLevel::kAllFrames, "all-frames"}};
const base::FeatureParam<RenderDocumentLevel> render_document_level{
&features::kRenderDocument, kRenderDocumentLevelParameterName,
#if BUILDFLAG(IS_ANDROID)
RenderDocumentLevel::kAllFrames,
#else
RenderDocumentLevel::kSubframe,
#endif
&render_document_levels};
RenderDocumentLevel GetRenderDocumentLevel() {
if (base::FeatureList::IsEnabled(features::kRenderDocument))
return render_document_level.Get();
return RenderDocumentLevel::kCrashedFrame;
}
std::string GetRenderDocumentLevelName(RenderDocumentLevel level) {
return render_document_level.GetName(level);
}
bool ShouldCreateNewRenderFrameHostOnSameSiteNavigation(
bool is_main_frame,
bool is_local_root,
bool has_committed_any_navigation,
bool must_be_replaced,
bool client_overrides_level) {
if (must_be_replaced) {
return true;
}
if (!has_committed_any_navigation) {
return false;
}
if (client_overrides_level) {
return true;
}
RenderDocumentLevel level = GetRenderDocumentLevel();
if (is_main_frame) {
CHECK(is_local_root);
return level >= RenderDocumentLevel::kAllFrames;
}
if (is_local_root) {
return level >= RenderDocumentLevel::kSubframe;
}
return level >= RenderDocumentLevel::kNonLocalRootSubframe;
}
bool ShouldCreateNewHostForAllFrames() {
return GetRenderDocumentLevel() >= RenderDocumentLevel::kAllFrames;
}
bool ShouldSkipEarlyCommitPendingForCrashedFrame() {
static bool skip_early_commit_pending_for_crashed_frame =
base::FeatureList::IsEnabled(
features::kSkipEarlyCommitPendingForCrashedFrame);
return skip_early_commit_pending_for_crashed_frame;
}
static constexpr base::FeatureParam<NavigationQueueingFeatureLevel>::Option
kNavigationQueueingFeatureLevels[] = {
{NavigationQueueingFeatureLevel::kNone, "none"},
{NavigationQueueingFeatureLevel::kAvoidRedundantCancellations,
"avoid-redundant"},
{NavigationQueueingFeatureLevel::kFull, "full"}};
const base::FeatureParam<NavigationQueueingFeatureLevel>
kNavigationQueueingFeatureLevelParam{
&features::kQueueNavigationsWhileWaitingForCommit, "queueing_level",
NavigationQueueingFeatureLevel::kFull,
&kNavigationQueueingFeatureLevels};
NavigationQueueingFeatureLevel GetNavigationQueueingFeatureLevel() {
if (GetRenderDocumentLevel() >= RenderDocumentLevel::kNonLocalRootSubframe) {
return NavigationQueueingFeatureLevel::kFull;
}
if (base::FeatureList::IsEnabled(
features::kQueueNavigationsWhileWaitingForCommit)) {
return kNavigationQueueingFeatureLevelParam.Get();
}
return NavigationQueueingFeatureLevel::kNone;
}
bool ShouldAvoidRedundantNavigationCancellations() {
return GetNavigationQueueingFeatureLevel() >=
NavigationQueueingFeatureLevel::kAvoidRedundantCancellations;
}
bool ShouldQueueNavigationsWhenPendingCommitRFHExists() {
return GetNavigationQueueingFeatureLevel() ==
NavigationQueueingFeatureLevel::kFull;
}
bool ShouldCreateSiteInstanceForDataUrls() {
return base::FeatureList::IsEnabled(features::kSiteInstanceGroupsForDataUrls);
}
bool ShouldUseDefaultSiteInstanceGroup() {
return GetContentClient()->ShouldAllowDefaultSiteInstanceGroup() &&
base::FeatureList::IsEnabled(features::kDefaultSiteInstanceGroups);
}
}