#ifndef CONTENT_BROWSER_BTM_BTM_UTILS_H_
#define CONTENT_BROWSER_BTM_BTM_UTILS_H_
#include <optional>
#include <ostream>
#include <string_view>
#include "base/files/file_path.h"
#include "base/strings/cstring_view.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/public/browser/btm_redirect_info.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/page.h"
#include "content/public/browser/render_frame_host.h"
#include "services/network/public/mojom/cookie_access_observer.mojom.h"
#include "url/gurl.h"
namespace base {
class TimeDelta;
}
namespace url {
class Origin;
}
namespace content {
class BrowserContext;
CONTENT_EXPORT base::cstring_view BtmCookieModeToString(BtmCookieMode mode);
CONTENT_EXPORT base::cstring_view BtmRedirectTypeToString(BtmRedirectType type);
CONTENT_EXPORT base::cstring_view BtmDataAccessTypeToString(
BtmDataAccessType type);
using CookieOperation = network::mojom::CookieAccessDetails::Type;
const base::FilePath::CharType kBtmFilename[] = FILE_PATH_LITERAL("DIPS");
CONTENT_EXPORT base::FilePath GetBtmFilePath(BrowserContext* context);
inline BtmDataAccessType ToBtmDataAccessType(CookieOperation op) {
return (op == CookieOperation::kChange ? BtmDataAccessType::kWrite
: BtmDataAccessType::kRead);
}
CONTENT_EXPORT std::ostream& operator<<(std::ostream& os,
BtmDataAccessType access_type);
constexpr BtmDataAccessType operator|(BtmDataAccessType lhs,
BtmDataAccessType rhs) {
return static_cast<BtmDataAccessType>(static_cast<int>(lhs) |
static_cast<int>(rhs));
}
inline BtmDataAccessType& operator|=(BtmDataAccessType& lhs,
BtmDataAccessType rhs) {
return (lhs = lhs | rhs);
}
BtmCookieMode GetBtmCookieMode(bool is_otr);
std::string_view GetHistogramSuffix(BtmCookieMode mode);
std::ostream& operator<<(std::ostream& os, BtmCookieMode mode);
enum class BtmEventRemovalType {
kNone = 0,
kHistory = 1 << 0,
kStorage = 1 << 1,
kAll = kHistory | kStorage
};
constexpr BtmEventRemovalType operator|(BtmEventRemovalType lhs,
BtmEventRemovalType rhs) {
return static_cast<BtmEventRemovalType>(static_cast<int>(lhs) |
static_cast<int>(rhs));
}
constexpr BtmEventRemovalType operator&(BtmEventRemovalType lhs,
BtmEventRemovalType rhs) {
return static_cast<BtmEventRemovalType>(static_cast<int>(lhs) &
static_cast<int>(rhs));
}
constexpr BtmEventRemovalType& operator|=(BtmEventRemovalType& lhs,
BtmEventRemovalType rhs) {
return lhs = lhs | rhs;
}
constexpr BtmEventRemovalType& operator&=(BtmEventRemovalType& lhs,
BtmEventRemovalType rhs) {
return lhs = lhs & rhs;
}
std::string_view GetHistogramPiece(BtmRedirectType type);
CONTENT_EXPORT std::ostream& operator<<(std::ostream& os, BtmRedirectType type);
using TimestampRange = std::optional<std::pair<base::Time, base::Time>>;
CONTENT_EXPORT bool UpdateTimestampRange(TimestampRange& range,
base::Time time);
CONTENT_EXPORT bool IsNullOrWithin(const TimestampRange& inner,
const TimestampRange& outer);
std::ostream& operator<<(std::ostream& os, TimestampRange type);
struct StateValue {
TimestampRange user_activation_times;
TimestampRange bounce_times;
TimestampRange web_authn_assertion_times;
};
struct PopupsStateValue {
uint64_t access_id;
base::Time last_popup_time;
bool is_current_interaction;
bool is_authentication_interaction;
};
struct PopupWithTime {
std::string opener_site;
std::string popup_site;
base::Time last_popup_time;
};
inline bool operator==(const StateValue& lhs, const StateValue& rhs) {
return std::tie(lhs.user_activation_times, lhs.bounce_times,
lhs.web_authn_assertion_times) ==
std::tie(rhs.user_activation_times, rhs.bounce_times,
rhs.web_authn_assertion_times);
}
CONTENT_EXPORT int64_t BucketizeBtmBounceDelay(base::TimeDelta delta);
CONTENT_EXPORT std::string GetSiteForBtm(const GURL& url);
CONTENT_EXPORT std::string GetSiteForBtm(const url::Origin& origin);
bool HasSameSiteIframe(WebContents* web_contents, const GURL& url);
CONTENT_EXPORT bool HasCHIPS(
const net::CookieAccessResultList& cookie_access_result_list);
inline bool IsInPrimaryPageIFrame(NavigationHandle& navigation_handle) {
return navigation_handle.GetParentFrame()
? navigation_handle.GetParentFrame()->GetPage().IsPrimary()
: false;
}
inline bool IsSameSiteForBtm(const GURL& url1, const GURL& url2) {
return GetSiteForBtm(url1) == GetSiteForBtm(url2);
}
inline bool IsInPrimaryPage(NavigationHandle& navigation_handle) {
return navigation_handle.GetParentFrame()
? navigation_handle.GetParentFrame()->GetPage().IsPrimary()
: navigation_handle.IsInPrimaryMainFrame();
}
inline bool IsInPrimaryPage(RenderFrameHost& rfh) {
return rfh.GetPage().IsPrimary();
}
inline const GURL& GetFirstPartyURL(NavigationHandle& navigation_handle) {
return navigation_handle.GetParentFrame()
? navigation_handle.GetParentFrame()
->GetMainFrame()
->GetLastCommittedURL()
: navigation_handle.GetURL();
}
inline const GURL& GetFirstPartyURL(RenderFrameHost& rfh) {
return rfh.GetMainFrame()->GetLastCommittedURL();
}
inline constexpr base::TimeDelta kBtmTimestampUpdateInterval = base::Minutes(1);
[[nodiscard]] CONTENT_EXPORT bool UpdateTimestamp(
std::optional<base::Time>& last_time,
base::Time now);
enum class BtmInteractionType {
Authentication = 0,
UserActivation = 1,
NoInteraction = 2,
};
enum class BtmRecordedEvent {
kUserActivation,
kWebAuthnAssertion,
};
enum class BtmRedirectCategory {
kNoCookies_NoEngagement = 0,
kReadCookies_NoEngagement = 1,
kWriteCookies_NoEngagement = 2,
kReadWriteCookies_NoEngagement = 3,
kNoCookies_HasEngagement = 4,
kReadCookies_HasEngagement = 5,
kWriteCookies_HasEngagement = 6,
kReadWriteCookies_HasEngagement = 7,
kUnknownCookies_NoEngagement = 8,
kUnknownCookies_HasEngagement = 9,
kMaxValue = kUnknownCookies_HasEngagement,
};
enum class BtmErrorCode {
kRead_None = 0,
kRead_OpenEndedRange_NullStart = 1,
kRead_OpenEndedRange_NullEnd = 2,
kRead_BounceTimesIsntSupersetOfStatefulBounces = 3,
kRead_EmptySite_InDb = 4,
kRead_EmptySite_NotInDb = 5,
kWrite_None = 6,
kWrite_EmptySite = 7,
kMaxValue = kWrite_EmptySite,
};
enum class BtmDeletionAction {
kDisallowed = 0,
kExceptedAs1p = 1,
kExceptedAs3p = 2,
kEnforced = 3,
kIgnored = 4,
kExcepted = 5,
kMaxValue = kExcepted,
};
enum class BtmDatabaseTable {
kBounces = 1,
kPopups = 2,
kMaxValue = kPopups,
};
}
#endif