Esempio n. 1
0
    def _dispatch(self, msg: ParsedMessagePayload) -> None:

        id = msg.get("id")
        if id:
            callback = self._callbacks.pop(id)
            error = msg.get("error")
            if error:
                parsed_error = parse_error(error)
                parsed_error.stack = callback.stack_trace
                callback.future.set_exception(parsed_error)
            else:
                result = self._replace_guids_with_channels(msg.get("result"))
                callback.future.set_result(result)
            return

        guid = msg["guid"]
        method = msg.get("method")
        params = msg["params"]
        if method == "__create__":
            scope = self._scopes[guid]
            scope.create_remote_object(params["type"], params["guid"],
                                       params["initializer"])
            return

        object = self._objects[guid]
        object._channel.emit(method, self._replace_guids_with_channels(params))
Esempio n. 2
0
    def _dispatch(self, msg: ParsedMessagePayload) -> None:
        id = msg.get("id")
        if id:
            callback = self._callbacks.pop(id)
            error = msg.get("error")
            if error:
                parsed_error = parse_error(error["error"])  # type: ignore
                parsed_error.stack = callback.stack_trace
                callback.future.set_exception(parsed_error)
            else:
                result = self._replace_guids_with_channels(msg.get("result"))
                callback.future.set_result(result)
            return

        guid = msg["guid"]
        method = msg.get("method")
        params = msg["params"]
        if method == "__create__":
            parent = self._objects[guid]
            self._create_remote_object(parent, params["type"], params["guid"],
                                       params["initializer"])
            return
        if method == "__dispose__":
            self._objects[guid]._dispose()
            return

        object = self._objects[guid]
        try:
            if self._is_sync:
                for listener in object._channel.listeners(method):
                    g = greenlet(listener)
                    g.switch(self._replace_guids_with_channels(params))
            else:
                object._channel.emit(method,
                                     self._replace_guids_with_channels(params))
        except Exception:
            print(
                "Error dispatching the event",
                "".join(traceback.format_exception(*sys.exc_info())),
            )
Esempio n. 3
0
    def __init__(
        self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
    ) -> None:
        super().__init__(parent, type, guid, initializer)
        self.accessibility = Accessibility(self._channel)
        self.keyboard = Keyboard(self._channel)
        self.mouse = Mouse(self._channel)

        self._main_frame: Frame = from_channel(initializer["mainFrame"])
        self._main_frame._page = self
        self._frames = [self._main_frame]
        self._viewport_size = initializer.get("viewportSize")
        self._is_closed = False
        self._workers: List[Worker] = []
        self._bindings: Dict[str, Any] = {}
        self._pending_wait_for_events: List[PendingWaitEvent] = []
        self._routes: List[RouteHandlerEntry] = []
        self._owned_context: Optional["BrowserContext"] = None
        self._timeout_settings: TimeoutSettings = TimeoutSettings(None)

        self._channel.on(
            "bindingCall",
            lambda params: self._on_binding(from_channel(params["binding"])),
        )
        self._channel.on("close", lambda _: self._on_close())
        self._channel.on(
            "console",
            lambda params: self.emit(
                Page.Events.Console, from_channel(params["message"])
            ),
        )
        self._channel.on("crash", lambda _: self._on_crash())
        self._channel.on(
            "dialog",
            lambda params: self.emit(
                Page.Events.Dialog, from_channel(params["dialog"])
            ),
        )
        self._channel.on(
            "domcontentloaded", lambda _: self.emit(Page.Events.DOMContentLoaded)
        )
        self._channel.on(
            "download",
            lambda params: self.emit(
                Page.Events.Download, from_channel(params["download"])
            ),
        )
        self._channel.on(
            "fileChooser",
            lambda params: self.emit(
                Page.Events.FileChooser,
                FileChooser(
                    self, from_channel(params["element"]), params["isMultiple"]
                ),
            ),
        )
        self._channel.on(
            "frameAttached",
            lambda params: self._on_frame_attached(from_channel(params["frame"])),
        )
        self._channel.on(
            "frameDetached",
            lambda params: self._on_frame_detached(from_channel(params["frame"])),
        )
        self._channel.on("load", lambda _: self.emit(Page.Events.Load))
        self._channel.on(
            "pageError",
            lambda params: self.emit(
                Page.Events.PageError, parse_error(params["error"]["error"])
            ),
        )
        self._channel.on(
            "popup",
            lambda params: self.emit(Page.Events.Popup, from_channel(params["page"])),
        )
        self._channel.on(
            "request",
            lambda params: self.emit(
                Page.Events.Request, from_channel(params["request"])
            ),
        )
        self._channel.on(
            "requestFailed",
            lambda params: self._on_request_failed(
                from_channel(params["request"]), params["failureText"]
            ),
        )
        self._channel.on(
            "requestFinished",
            lambda params: self.emit(
                Page.Events.RequestFinished, from_channel(params["request"])
            ),
        )
        self._channel.on(
            "response",
            lambda params: self.emit(
                Page.Events.Response, from_channel(params["response"])
            ),
        )
        self._channel.on(
            "route",
            lambda params: self._on_route(
                from_channel(params["route"]), from_channel(params["request"])
            ),
        )
        self._channel.on(
            "worker", lambda params: self._on_worker(from_channel(params["worker"]))
        )
    def __init__(self, scope: ConnectionScope, guid: str,
                 initializer: Dict) -> None:
        super().__init__(scope, guid, initializer)
        self.accessibility = Accessibility(self._channel)
        self.keyboard = Keyboard(self._channel)
        self.mouse = Mouse(self._channel)

        self._main_frame: Frame = from_channel(initializer["mainFrame"])
        self._main_frame._page = self
        self._frames = [self._main_frame]
        self._viewport_size = initializer["viewportSize"]
        self._is_closed = False
        self._workers: List[Worker] = list()
        self._bindings: Dict[str, Any] = dict()
        self._pending_wait_for_events: List[PendingWaitEvent] = list()
        self._routes: List[RouteHandlerEntry] = list()
        self._owned_context: Optional["BrowserContext"] = None

        self._channel.on(
            "bindingCall",
            lambda binding_call: self._on_binding(from_channel(binding_call)),
        )
        self._channel.on("close", lambda _: self._on_close())
        self._channel.on(
            "console",
            lambda message: self.emit(Page.Events.Console, from_channel(message
                                                                        )),
        )
        self._channel.on("crash", lambda _: self._on_crash())
        self._channel.on(
            "dialog",
            lambda dialog: self.emit(Page.Events.Dialog, from_channel(dialog)))
        self._channel.on("domcontentloaded",
                         lambda _: self.emit(Page.Events.DOMContentLoaded))
        self._channel.on(
            "download",
            lambda download: self.emit(Page.Events.Download,
                                       from_channel(download)),
        )
        self._channel.on(
            "fileChooser",
            lambda params: self.emit(
                Page.Events.FileChooser,
                FileChooser(self, from_channel(params["element"]), params[
                    "isMultiple"]),
            ),
        )
        self._channel.on(
            "frameAttached",
            lambda frame: self._on_frame_attached(from_channel(frame)))
        self._channel.on(
            "frameDetached",
            lambda frame: self._on_frame_detached(from_channel(frame)))
        self._channel.on(
            "frameNavigated",
            lambda params: self._on_frame_navigated(
                from_channel(params["frame"]), params["url"], params["name"]),
        )
        self._channel.on("load", lambda _: self.emit(Page.Events.Load))
        self._channel.on(
            "pageError",
            lambda params: self.emit(Page.Events.PageError,
                                     parse_error(params["error"])),
        )
        self._channel.on(
            "popup",
            lambda popup: self.emit(Page.Events.Popup, from_channel(popup)))
        self._channel.on(
            "request",
            lambda request: self.emit(Page.Events.Request, from_channel(request
                                                                        )),
        )
        self._channel.on(
            "requestFailed",
            lambda params: self._on_request_failed(
                from_channel(params["request"]), params["failureText"]),
        )
        self._channel.on(
            "requestFinished",
            lambda request: self.emit(Page.Events.RequestFinished,
                                      from_channel(request)),
        )
        self._channel.on(
            "response",
            lambda response: self.emit(Page.Events.Response,
                                       from_channel(response)),
        )
        self._channel.on(
            "route",
            lambda params: self._on_route(from_channel(params["route"]),
                                          from_channel(params["request"])),
        )
        self._channel.on("worker",
                         lambda worker: self._on_worker(from_channel(worker)))