-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathSession.php
More file actions
203 lines (163 loc) · 4.91 KB
/
Session.php
File metadata and controls
203 lines (163 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace ChromeDevtoolsProtocol;
use ChromeDevtoolsProtocol\Exception\ErrorException;
use ChromeDevtoolsProtocol\Exception\RuntimeException;
use ChromeDevtoolsProtocol\Model\Target\CloseTargetRequest;
use ChromeDevtoolsProtocol\Model\Target\DisposeBrowserContextRequest;
use ChromeDevtoolsProtocol\Model\Target\SendMessageToTargetRequest;
/**
* Session is an isolated context in headless browser (i.e. cookies are not shared), like incognito window.
*
* @author Jakub Kulhan <jakub.kulhan@gmail.com>
*/
class Session implements DevtoolsClientInterface, InternalClientInterface
{
use DevtoolsClientTrait;
/** @var DevtoolsClientInterface */
private $browser;
/** @var string */
private $browserContextId;
/** @var string */
private $targetId;
/** @var string */
private $sessionId;
/** @var callable[][] */
private $listeners = [];
/** @var int */
private $messageId = 0;
/** @var object[] */
private $commandResults = [];
/** @var object[][] */
private $eventBuffers = [];
public function __construct(DevtoolsClientInterface $browser, string $browserContextId, string $targetId, string $sessionId)
{
$this->browser = $browser;
$this->targetId = $targetId;
$this->sessionId = $sessionId;
$this->browserContextId = $browserContextId;
}
public function close(): void
{
$ctx = Context::withTimeout(Context::background(), 10);
$this->browser->target()->closeTarget(
$ctx,
CloseTargetRequest::builder()
->setTargetId($this->targetId)
->build()
);
$this->browser->target()->disposeBrowserContext(
$ctx,
DisposeBrowserContextRequest::builder()
->setBrowserContextId($this->browserContextId)
->build()
);
$this->browser->close();
}
/**
* @internal
*/
public function executeCommand(ContextInterface $ctx, string $method, $parameters)
{
$messageId = ++$this->messageId;
$payload = new \stdClass();
$payload->id = $messageId;
$payload->method = $method;
$payload->params = $parameters;
$this->browser->target()->sendMessageToTarget(
$ctx,
SendMessageToTargetRequest::builder()
->setTargetId($this->targetId)
->setSessionId($this->sessionId)
->setMessage(json_encode($payload))
->build()
);
for (; ;) {
$receivedMessage = $this->browser->target()->awaitReceivedMessageFromTarget($ctx);
if ($receivedMessage->targetId === $this->targetId && $receivedMessage->sessionId === $this->sessionId) {
$this->handleMessage(json_decode($receivedMessage->message));
}
if (isset($this->commandResults[$messageId])) {
$result = $this->commandResults[$messageId];
unset($this->commandResults[$messageId]);
return $result;
}
}
}
/**
* @internal
*/
public function awaitEvent(ContextInterface $ctx, string $method)
{
if (!empty($this->eventBuffers[$method])) {
return array_shift($this->eventBuffers[$method])->params;
}
for (; ;) {
$receivedMessage = $this->browser->target()->awaitReceivedMessageFromTarget($ctx);
if ($receivedMessage->targetId === $this->targetId && $receivedMessage->sessionId === $this->sessionId) {
$this->handleMessage(json_decode($receivedMessage->message));
}
if (!empty($this->eventBuffers[$method])) {
return array_shift($this->eventBuffers[$method])->params;
}
}
}
/**
* @internal
*/
private function handleMessage($message)
{
if (isset($message->error)) {
throw new ErrorException($message->error->message, $message->error->code);
} else if (isset($message->method)) {
if (isset($this->listeners[$message->method])) {
foreach ($this->listeners[$message->method] as $callback) {
$callback($message->params);
}
}
if (!isset($this->eventBuffers[$message->method])) {
$this->eventBuffers[$message->method] = [];
}
array_push($this->eventBuffers[$message->method], $message);
} else if (isset($message->id)) {
$this->commandResults[$message->id] = $message->result ?? new \stdClass();
} else {
throw new RuntimeException(sprintf(
"Unhandled message: %s",
json_encode($message, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
));
}
return null;
}
public function addListener(string $method, callable $listener): SubscriptionInterface
{
if (!isset($this->listeners[$method])) {
$this->listeners[$method] = [];
}
$this->listeners[$method][] = $listener;
$callback = function () use ($method, $listener) {
foreach ($this->listeners[$method] as $k => $candidateListener) {
if ($candidateListener === $listener) {
unset($this->listeners[$method][$k]);
break;
}
}
if (empty($this->listeners[$method])) {
unset($this->listeners[$method]);
}
};
return new class($callback) implements SubscriptionInterface
{
/** @var callable */
private $callback;
public function __construct(callable $callable)
{
$this->callback = $callable;
}
public function cancel(): void
{
$callback = $this->callback;
$callback();
}
};
}
}