Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: sky/framework/inspector/page-agent.sky

Issue 732413004: Add support for Page.getResourceTree (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: less frustration Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 <import src="/sky/framework/xmlhttprequest.sky" as="XMLHttpRequest" /> 1 <import src="/sky/framework/xmlhttprequest.sky" as="XMLHttpRequest" />
2 <script> 2 <script>
3 function Page(delegate) { 3 function Page(delegate) {
4 this.delegate_ = delegate; 4 this.delegate_ = delegate;
5 } 5 }
6 6
7 Page.prototype.enable = function() { 7 Page.prototype.enable = function() {
8 }; 8 };
9 9
10 Page.prototype.canScreencast = function() { 10 Page.prototype.canScreencast = function() {
(...skipping 15 matching lines...) Expand all
26 'content' : request.responseText, 26 'content' : request.responseText,
27 }; 27 };
28 this.delegate_.sendResponse(message_id, message); 28 this.delegate_.sendResponse(message_id, message);
29 }.bind(this); 29 }.bind(this);
30 request.open("GET", params.url); 30 request.open("GET", params.url);
31 request.send(); 31 request.send();
32 32
33 return this.delegate_.ASYNC_RESPONSE; 33 return this.delegate_.ASYNC_RESPONSE;
34 }; 34 };
35 35
36 Page.prototype._treeOrderWalk = function(root, visit) {
esprehn 2014/11/18 23:23:58 You can actually just do querySelectorAll("*") and
37 // HTML defines tree-order as a pre-order depth-first walk.
38 var node = root;
39 var parent_stack = [];
esprehn 2014/11/18 23:23:58 JS vars should really be camelCase.
40 while (parent_stack.length || node) {
41 if (node) {
42 visit(node);
43 if (node.nextSibling)
44 parent_stack.push(node.nextSibling);
45 node = node.firstChild;
46 } else
47 node = parent_stack.pop();
48 }
49 }
50
51 Page.prototype._getElementsByTagName = function(root, tag_name) {
esprehn 2014/11/18 23:23:58 Just use querySelectorAll()
52 var elements = [];
53 this._treeOrderWalk(root, function(node) {
54 if (node.tagName == tag_name)
55 elements.push(node);
56 });
57 return elements;
58 }
59
60 // ES6 has Array.from which theoretically does this.
61 Page.prototype._arrayFromIterator = function(iterator) {
62 var a = [];
63 for (var e of iterator)
esprehn 2014/11/18 23:23:58 You could also do Array.prototype.slice.call(itera
64 a.push(e);
65 return a;
66 }
67
68 Page.prototype._resourcesMapForSubtree = function(root_doc) {
69 var resource_map = new Map;
70 function _addResource(url, type, mimeType) {
esprehn 2014/11/18 23:23:58 This requires allocating a new function each time
71 var absolute_url = new URL(url, document.URL);
72 if (resource_map.has(absolute_url))
esprehn 2014/11/18 23:23:58 String first, new object means this is always fals
73 return;
74 resource_map.set(absolute_url, {
75 'url': String(absolute_url),
esprehn 2014/11/18 23:23:58 no quotes needed on any of these
76 'type': type,
77 'mimeType': mimeType,
78 })
79 }
80
81 var pending_docs = [root_doc];
82 while (pending_docs.length) {
83 var doc = pending_docs.pop();
84 var imports = this._getElementsByTagName(doc, 'import');
esprehn 2014/11/18 23:23:58 querySelectorAll("import,img") will only walk the
85 imports.forEach(function(imp) {
86 // FIXME: It's no longer possible to walk into an import. :(
87 // pending_docs.push(imp.import);
88 _addResource(imp.getAttribute('src'), 'Document', 'text/html');
89 });
90 var images = this._getElementsByTagName(doc, 'img');
91 images.forEach(function(img) {
92 // FIXME: Can't determine the type of an image from the DOM.
93 _addResource(img.getAttribute('src'), 'Image', 'image/unknown');
94 });
95 }
96 return resource_map;
97 }
98
36 Page.prototype.getResourceTree = function() { 99 Page.prototype.getResourceTree = function() {
37 // Unclear if this is all needed, but if we don't return something here 100 // Unclear if this is all needed, but if we don't return something here
38 // the inspector hits an exception in WebInspector.ResourceTreeModel. 101 // the inspector hits an exception in WebInspector.ResourceTreeModel.
102 var resources_iter = this._resourcesMapForSubtree(document).values();
39 return { 103 return {
40 "frameTree": { 104 "frameTree": {
41 "frame": { 105 "frame": {
42 "id": "1", 106 "id": "1",
43 "loaderId": "1", 107 "loaderId": "1",
44 "url": document.URL, 108 "url": String(document.URL),
45 "mimeType": "text/html", 109 "mimeType": "text/html",
46 "securityOrigin": document.URL, 110 "securityOrigin": String(document.URL),
47 }, 111 },
48 "resources": [], // FIXME 112 "resources": this._arrayFromIterator(resources_iter),
49 } 113 }
50 }; 114 };
51 }; 115 };
52 116
53 module.exports = Page; 117 module.exports = Page;
54 </script> 118 </script>
OLDNEW
« no previous file with comments | « sky/engine/web/ChromeClientImpl.cpp ('k') | sky/tests/inspector/page-agent-get-resource-tree.sky » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698