Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions bluebox/cdp/monitors/async_storage_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import TYPE_CHECKING, Any, Awaitable, Callable

from bluebox.cdp.monitors.abstract_async_monitor import AbstractAsyncMonitor
from bluebox.data_models.cdp import StorageEvent
from bluebox.data_models.cdp import StorageEvent, StorageEventType
from bluebox.utils.logger import get_logger

if TYPE_CHECKING:
Expand Down Expand Up @@ -251,7 +251,7 @@ async def _handle_get_cookies_reply(self, msg: dict, command_info: dict[str, Any
# emit changes if any
if added_cookies or modified_cookies or removed_cookies:
event = StorageEvent(
type="cookieChange",
type=StorageEventType.COOKIE_CHANGE,
source="native_cdp",
triggered_by=triggered_by,
added=added_cookies,
Expand Down Expand Up @@ -285,7 +285,7 @@ async def _handle_dom_storage_cleared(self, msg: dict) -> None:
del self.session_storage_state[origin]

event = StorageEvent(
type=f"{storage_type}Cleared",
type=StorageEventType.cleared(is_local),
origin=origin,
)
try:
Expand All @@ -312,7 +312,7 @@ async def _handle_dom_storage_removed(self, msg: dict) -> None:
del self.session_storage_state[origin][key]

event = StorageEvent(
type=f"{storage_type}ItemRemoved",
type=StorageEventType.item_removed(is_local),
origin=origin,
key=key,
)
Expand Down Expand Up @@ -343,7 +343,7 @@ async def _handle_dom_storage_added(self, msg: dict) -> None:
self.session_storage_state[origin][key] = new_value

event = StorageEvent(
type=f"{storage_type}ItemAdded",
type=StorageEventType.item_added(is_local),
origin=origin,
key=key,
value=new_value,
Expand Down Expand Up @@ -376,7 +376,7 @@ async def _handle_dom_storage_updated(self, msg: dict) -> None:
self.session_storage_state[origin][key] = new_value

event = StorageEvent(
type=f"{storage_type}ItemUpdated",
type=StorageEventType.item_updated(is_local),
origin=origin,
key=key,
old_value=old_value,
Expand Down Expand Up @@ -408,7 +408,7 @@ async def _handle_indexeddb_added(self, msg: dict) -> None:
"""Handle IndexedDB events."""
params = msg.get("params", {})
event = StorageEvent(
type="indexedDBEvent",
type=StorageEventType.INDEXED_DB_EVENT,
params=params,
)
try:
Expand Down
81 changes: 75 additions & 6 deletions bluebox/data_models/cdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,90 @@ class NetworkTransactionEvent(BaseCDPEvent):

## Storage models

class StorageEventType(StrEnum):
"""Types of browser storage events."""

# Cookie events
INITIAL_COOKIES = "initialCookies"
COOKIE_CHANGE = "cookieChange"

# LocalStorage events
LOCAL_STORAGE_CLEARED = "localStorageCleared"
LOCAL_STORAGE_ITEM_ADDED = "localStorageItemAdded"
LOCAL_STORAGE_ITEM_REMOVED = "localStorageItemRemoved"
LOCAL_STORAGE_ITEM_UPDATED = "localStorageItemUpdated"

# SessionStorage events
SESSION_STORAGE_CLEARED = "sessionStorageCleared"
SESSION_STORAGE_ITEM_ADDED = "sessionStorageItemAdded"
SESSION_STORAGE_ITEM_REMOVED = "sessionStorageItemRemoved"
SESSION_STORAGE_ITEM_UPDATED = "sessionStorageItemUpdated"

# IndexedDB events
INDEXED_DB_EVENT = "indexedDBEvent"

@classmethod
def cookie_types(cls) -> set["StorageEventType"]:
"""Return all cookie-related event types."""
return {cls.INITIAL_COOKIES, cls.COOKIE_CHANGE}

@classmethod
def local_storage_types(cls) -> set["StorageEventType"]:
"""Return all localStorage-related event types."""
return {
cls.LOCAL_STORAGE_CLEARED,
cls.LOCAL_STORAGE_ITEM_ADDED,
cls.LOCAL_STORAGE_ITEM_REMOVED,
cls.LOCAL_STORAGE_ITEM_UPDATED,
}

@classmethod
def session_storage_types(cls) -> set["StorageEventType"]:
"""Return all sessionStorage-related event types."""
return {
cls.SESSION_STORAGE_CLEARED,
cls.SESSION_STORAGE_ITEM_ADDED,
cls.SESSION_STORAGE_ITEM_REMOVED,
cls.SESSION_STORAGE_ITEM_UPDATED,
}

@classmethod
def indexed_db_types(cls) -> set["StorageEventType"]:
"""Return all IndexedDB-related event types."""
return {cls.INDEXED_DB_EVENT}

# Factory methods for DOM storage events
@classmethod
def cleared(cls, is_local: bool) -> "StorageEventType":
"""Get the cleared event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_CLEARED if is_local else cls.SESSION_STORAGE_CLEARED

@classmethod
def item_added(cls, is_local: bool) -> "StorageEventType":
"""Get the item added event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_ITEM_ADDED if is_local else cls.SESSION_STORAGE_ITEM_ADDED

@classmethod
def item_removed(cls, is_local: bool) -> "StorageEventType":
"""Get the item removed event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_ITEM_REMOVED if is_local else cls.SESSION_STORAGE_ITEM_REMOVED

@classmethod
def item_updated(cls, is_local: bool) -> "StorageEventType":
"""Get the item updated event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_ITEM_UPDATED if is_local else cls.SESSION_STORAGE_ITEM_UPDATED


class StorageEvent(BaseCDPEvent):
"""
Model for browser storage monitoring events.
Handles cookies, localStorage, sessionStorage, and IndexedDB events.
"""
model_config = ConfigDict(extra='allow')

type: str = Field(
type: StorageEventType = Field(
...,
description="Type of storage event",
examples=[
"initialCookies", "cookieChange",
"localStorageCleared", "localStorageItemAdded", "sessionStorageItemAdded",
"indexedDBEvent",
]
)
source: str | None = Field(
default=None,
Expand Down
Loading