Contains the state for a CDP session. Generally you should not instantiate this object yourself; you should call :meth:`CdpConnection.open_session`.
| 325 | |
| 326 | |
| 327 | class CdpSession(CdpBase): |
| 328 | """Contains the state for a CDP session. |
| 329 | |
| 330 | Generally you should not instantiate this object yourself; you should call |
| 331 | :meth:`CdpConnection.open_session`. |
| 332 | """ |
| 333 | |
| 334 | def __init__(self, ws, session_id, target_id): |
| 335 | """Constructor. |
| 336 | |
| 337 | Args: |
| 338 | ws: trio_websocket.WebSocketConnection |
| 339 | session_id: devtools.target.SessionID |
| 340 | target_id: devtools.target.TargetID |
| 341 | """ |
| 342 | super().__init__(ws, session_id, target_id) |
| 343 | |
| 344 | self._dom_enable_count = 0 |
| 345 | self._dom_enable_lock = trio.Lock() |
| 346 | self._page_enable_count = 0 |
| 347 | self._page_enable_lock = trio.Lock() |
| 348 | |
| 349 | @asynccontextmanager |
| 350 | async def dom_enable(self): |
| 351 | """Context manager that executes ``dom.enable()`` when it enters and then calls ``dom.disable()``. |
| 352 | |
| 353 | This keeps track of concurrent callers and only disables DOM |
| 354 | events when all callers have exited. |
| 355 | """ |
| 356 | global devtools |
| 357 | async with self._dom_enable_lock: |
| 358 | self._dom_enable_count += 1 |
| 359 | if self._dom_enable_count == 1: |
| 360 | await self.execute(devtools.dom.enable()) |
| 361 | |
| 362 | yield |
| 363 | |
| 364 | async with self._dom_enable_lock: |
| 365 | self._dom_enable_count -= 1 |
| 366 | if self._dom_enable_count == 0: |
| 367 | await self.execute(devtools.dom.disable()) |
| 368 | |
| 369 | @asynccontextmanager |
| 370 | async def page_enable(self): |
| 371 | """Context manager executes ``page.enable()`` when it enters and then calls ``page.disable()`` when it exits. |
| 372 | |
| 373 | This keeps track of concurrent callers and only disables page |
| 374 | events when all callers have exited. |
| 375 | """ |
| 376 | global devtools |
| 377 | async with self._page_enable_lock: |
| 378 | self._page_enable_count += 1 |
| 379 | if self._page_enable_count == 1: |
| 380 | await self.execute(devtools.page.enable()) |
| 381 | |
| 382 | yield |
| 383 | |
| 384 | async with self._page_enable_lock: |