|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | +from typing import Dict, Optional, cast |
| 17 | + |
| 18 | +from pyee import AsyncIOEventEmitter |
| 19 | + |
| 20 | +from playwright._impl._api_types import Error |
| 21 | +from playwright._impl._connection import Channel |
| 22 | +from playwright._impl._helper import ParsedMessagePayload, parse_error |
| 23 | +from playwright._impl._transport import Transport |
| 24 | + |
| 25 | + |
| 26 | +class JsonPipeTransport(AsyncIOEventEmitter, Transport): |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + loop: asyncio.AbstractEventLoop, |
| 30 | + pipe_channel: Channel, |
| 31 | + ) -> None: |
| 32 | + super().__init__(loop) |
| 33 | + Transport.__init__(self, loop) |
| 34 | + self._stop_requested = False |
| 35 | + self._pipe_channel = pipe_channel |
| 36 | + |
| 37 | + def request_stop(self) -> None: |
| 38 | + self._stop_requested = True |
| 39 | + self._loop.create_task(self._pipe_channel.send("close", {})) |
| 40 | + |
| 41 | + def dispose(self) -> None: |
| 42 | + self.on_error_future.cancel() |
| 43 | + self._stopped_future.cancel() |
| 44 | + |
| 45 | + async def wait_until_stopped(self) -> None: |
| 46 | + await self._stopped_future |
| 47 | + |
| 48 | + async def connect(self) -> None: |
| 49 | + self._stopped_future: asyncio.Future = asyncio.Future() |
| 50 | + |
| 51 | + def handle_message(message: Dict) -> None: |
| 52 | + if not self._stop_requested: |
| 53 | + self.on_message(cast(ParsedMessagePayload, message)) |
| 54 | + |
| 55 | + def handle_closed(error: Optional[Dict]) -> None: |
| 56 | + self.emit("close") |
| 57 | + self.on_error_future.set_exception( |
| 58 | + parse_error(error["error"]) |
| 59 | + if error |
| 60 | + else Error("Playwright connection closed") |
| 61 | + ) |
| 62 | + self._stopped_future.set_result(None) |
| 63 | + |
| 64 | + self._pipe_channel.on( |
| 65 | + "message", |
| 66 | + lambda params: handle_message(params["message"]), |
| 67 | + ) |
| 68 | + self._pipe_channel.on( |
| 69 | + "closed", |
| 70 | + lambda params: handle_closed(params.get("error")), |
| 71 | + ) |
| 72 | + |
| 73 | + async def run(self) -> None: |
| 74 | + await self._stopped_future |
| 75 | + |
| 76 | + def send(self, message: Dict) -> None: |
| 77 | + if self._stop_requested: |
| 78 | + raise Error("Playwright connection closed") |
| 79 | + self._loop.create_task(self._pipe_channel.send("send", {"message": message})) |
0 commit comments