forked from chrome-php/chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameManager.php
106 lines (90 loc) · 2.62 KB
/
FrameManager.php
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
<?php
/*
* This file is part of Chrome PHP.
*
* (c) Soufiane Ghzal <sghzal@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace HeadlessChromium;
class FrameManager
{
/**
* @var Page
*/
protected $page;
/**
* @var Frame[]
*/
protected $frames = [];
/**
* @var Frame
*/
protected $mainFrame;
/**
* FrameManager constructor.
*/
public function __construct(Page $page, array $frameTree)
{
$this->page = $page;
if (isset($frameTree['frame'])) {
// TODO parse children frames
$this->frames[$frameTree['frame']['id']] = new Frame($frameTree['frame']);
// associate main frame
$this->mainFrame = $this->frames[$frameTree['frame']['id']];
}
// TODO listen for frame events
// update frame on init
$this->page->getSession()->on('method:Page.lifecycleEvent', function (array $params): void {
if (isset($this->frames[$params['frameId']])) {
$frame = $this->frames[$params['frameId']];
$frame->onLifecycleEvent($params);
}
});
// attach context id to frame
$this->page->getSession()->on('method:Runtime.executionContextCreated', function (array $params): void {
if (isset($params['context']['auxData']['frameId']) && $params['context']['auxData']['isDefault']) {
if ($this->hasFrame($params['context']['auxData']['frameId'])) {
$frame = $this->getFrame($params['context']['auxData']['frameId']);
$frame->setExecutionContextId($params['context']['id']);
}
}
});
// TODO maybe implement Runtime.executionContextDestroyed and Runtime.executionContextsCleared
}
/**
* Checks if the given frame exists.
*
* @param string $frameId
*
* @return bool
*/
public function hasFrame($frameId): bool
{
return \array_key_exists($frameId, $this->frames);
}
/**
* Get a frame given its id.
*
* @param string $frameId
*
* @return Frame
*/
public function getFrame($frameId): Frame
{
if (!isset($this->frames[$frameId])) {
throw new \RuntimeException(\sprintf('No such frame "%s"', $frameId));
}
return $this->frames[$frameId];
}
/**
* Gets the main frame.
*
* @return Frame
*/
public function getMainFrame(): Frame
{
return $this->mainFrame;
}
}