LayerTreeModel.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (C) 2013 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.Object}
  33. */
  34. WebInspector.LayerTreeModel = function()
  35. {
  36. WebInspector.Object.call(this);
  37. this._layersById = {};
  38. InspectorBackend.registerLayerTreeDispatcher(new WebInspector.LayerTreeDispatcher(this));
  39. LayerTreeAgent.enable();
  40. this._needsRefresh = true;
  41. }
  42. WebInspector.LayerTreeModel.Events = {
  43. LayerTreeChanged: "LayerTreeChanged",
  44. }
  45. WebInspector.LayerTreeModel.prototype = {
  46. dispose: function()
  47. {
  48. LayerTreeAgent.disable();
  49. },
  50. /**
  51. * @return {?WebInspector.Layer}
  52. */
  53. root: function()
  54. {
  55. return this._root;
  56. },
  57. /**
  58. * @return {?WebInspector.Layer}
  59. */
  60. contentRoot: function()
  61. {
  62. return this._contentRoot;
  63. },
  64. /**
  65. * @param {function(WebInspector.Layer)} callback
  66. * @param {WebInspector.Layer=} root
  67. * @return {boolean}
  68. */
  69. forEachLayer: function(callback, root)
  70. {
  71. if (!root) {
  72. root = this.root();
  73. if (!root)
  74. return false;
  75. }
  76. return callback(root) || root.children().some(this.forEachLayer.bind(this, callback));
  77. },
  78. /**
  79. * @param {function()=} callback
  80. */
  81. requestLayers: function(callback)
  82. {
  83. delete this._delayedRequestLayersTimer;
  84. if (!callback)
  85. callback = function() {}
  86. if (!this._needsRefresh) {
  87. callback();
  88. return;
  89. }
  90. if (this._pendingRequestLayersCallbacks) {
  91. this._pendingRequestLayersCallbacks.push(callback);
  92. return;
  93. }
  94. this._pendingRequestLayersCallbacks = [];
  95. this._pendingRequestLayersCallbacks.push(callback);
  96. function onGetLayers(error, layers)
  97. {
  98. this._root = null;
  99. this._contentRoot = null;
  100. if (error) {
  101. console.error("LayerTreeAgent.getLayers(): " + error);
  102. layers = [];
  103. }
  104. this._repopulate(layers);
  105. for (var i = 0; i < this._pendingRequestLayersCallbacks.length; ++i)
  106. this._pendingRequestLayersCallbacks[i]();
  107. delete this._pendingRequestLayersCallbacks;
  108. }
  109. function onDocumentAvailable()
  110. {
  111. LayerTreeAgent.getLayers(undefined, onGetLayers.bind(this))
  112. }
  113. WebInspector.domAgent.requestDocument(onDocumentAvailable.bind(this));
  114. },
  115. /**
  116. * @param {string} id
  117. * @return {WebInspector.Layer?}
  118. */
  119. layerById: function(id)
  120. {
  121. return this._layersById[id];
  122. },
  123. /**
  124. * @param{Array.<LayerTreeAgent.Layer>} payload
  125. */
  126. _repopulate: function(payload)
  127. {
  128. var oldLayersById = this._layersById;
  129. this._layersById = {};
  130. for (var i = 0; i < payload.length; ++i) {
  131. var layer = oldLayersById[payload[i].layerId];
  132. if (layer)
  133. layer._reset(payload[i]);
  134. else
  135. layer = new WebInspector.Layer(payload[i]);
  136. this._layersById[layer.id()] = layer;
  137. var parentId = layer.parentId();
  138. if (!this._contentRoot && layer.nodeId())
  139. this._contentRoot = layer;
  140. if (parentId) {
  141. var parent = this._layersById[parentId];
  142. if (!parent)
  143. console.assert(parent, "missing parent " + parentId + " for layer " + layer.id());
  144. parent.addChild(layer);
  145. } else {
  146. if (this._root)
  147. console.assert(false, "Multiple root layers");
  148. this._root = layer;
  149. }
  150. }
  151. this.dispatchEventToListeners(WebInspector.LayerTreeModel.Events.LayerTreeChanged);
  152. },
  153. _layerTreeChanged: function()
  154. {
  155. if (this._delayedRequestLayersTimer)
  156. return;
  157. this._needsRefresh = true;
  158. this._delayedRequestLayersTimer = setTimeout(this.requestLayers.bind(this), 100);
  159. },
  160. __proto__: WebInspector.Object.prototype
  161. }
  162. /**
  163. * @constructor
  164. * @param {LayerTreeAgent.Layer} layerPayload
  165. */
  166. WebInspector.Layer = function(layerPayload)
  167. {
  168. this._reset(layerPayload);
  169. }
  170. WebInspector.Layer.prototype = {
  171. /**
  172. * @return {string}
  173. */
  174. id: function()
  175. {
  176. return this._layerPayload.layerId;
  177. },
  178. /**
  179. * @return {string?}
  180. */
  181. parentId: function()
  182. {
  183. return this._layerPayload.parentLayerId;
  184. },
  185. /**
  186. * @return {WebInspector.Layer}
  187. */
  188. parent: function()
  189. {
  190. return this._parent;
  191. },
  192. /**
  193. * @return {boolean}
  194. */
  195. isRoot: function()
  196. {
  197. return !this.parentId();
  198. },
  199. /**
  200. * @return {Array.<WebInspector.Layer>}
  201. */
  202. children: function()
  203. {
  204. return this._children;
  205. },
  206. /**
  207. * @param {WebInspector.Layer} child
  208. */
  209. addChild: function(child)
  210. {
  211. if (child._parent)
  212. console.assert(false, "Child already has a parent");
  213. this._children.push(child);
  214. child._parent = this;
  215. },
  216. /**
  217. * @return {DOMAgent.NodeId?}
  218. */
  219. nodeId: function()
  220. {
  221. return this._layerPayload.nodeId;
  222. },
  223. /**
  224. * @return {DOMAgent.NodeId?}
  225. */
  226. nodeIdForSelfOrAncestor: function()
  227. {
  228. for (var layer = this; layer; layer = layer._parent) {
  229. var nodeId = layer._layerPayload.nodeId;
  230. if (nodeId)
  231. return nodeId;
  232. }
  233. return null;
  234. },
  235. /**
  236. * @return {number}
  237. */
  238. offsetX: function()
  239. {
  240. return this._layerPayload.offsetX;
  241. },
  242. /**
  243. * @return {number}
  244. */
  245. offsetY: function()
  246. {
  247. return this._layerPayload.offsetY;
  248. },
  249. /**
  250. * @return {number}
  251. */
  252. width: function()
  253. {
  254. return this._layerPayload.width;
  255. },
  256. /**
  257. * @return {number}
  258. */
  259. height: function()
  260. {
  261. return this._layerPayload.height;
  262. },
  263. /**
  264. * @return {Array.<number>}
  265. */
  266. transform: function()
  267. {
  268. return this._layerPayload.transform;
  269. },
  270. /**
  271. * @return {Array.<number>}
  272. */
  273. anchorPoint: function()
  274. {
  275. return [
  276. this._layerPayload.anchorX || 0,
  277. this._layerPayload.anchorY || 0,
  278. this._layerPayload.anchorZ || 0,
  279. ];
  280. },
  281. /**
  282. * @return {boolean}
  283. */
  284. invisible: function()
  285. {
  286. return this._layerPayload.invisible;
  287. },
  288. /**
  289. * @return {number}
  290. */
  291. paintCount: function()
  292. {
  293. return this._layerPayload.paintCount;
  294. },
  295. /**
  296. * @param {function(Array.<string>?)} callback
  297. */
  298. requestCompositingReasons: function(callback)
  299. {
  300. /**
  301. * @param {?string} error
  302. * @param {?Array.<string>} compositingReasons
  303. */
  304. function callbackWrapper(error, compositingReasons)
  305. {
  306. if (error) {
  307. console.error("LayerTreeAgent.reasonsForCompositingLayer(): " + error);
  308. callback(null);
  309. return;
  310. }
  311. callback(compositingReasons);
  312. }
  313. LayerTreeAgent.compositingReasons(this.id(), callbackWrapper.bind(this));
  314. },
  315. /**
  316. * @param {LayerTreeAgent.Layer} layerPayload
  317. */
  318. _reset: function(layerPayload)
  319. {
  320. this._children = [];
  321. this._parent = null;
  322. this._layerPayload = layerPayload;
  323. }
  324. }
  325. /**
  326. * @constructor
  327. * @implements {LayerTreeAgent.Dispatcher}
  328. * @param {WebInspector.LayerTreeModel} layerTreeModel
  329. */
  330. WebInspector.LayerTreeDispatcher = function(layerTreeModel)
  331. {
  332. this._layerTreeModel = layerTreeModel;
  333. }
  334. WebInspector.LayerTreeDispatcher.prototype = {
  335. layerTreeDidChange: function()
  336. {
  337. this._layerTreeModel._layerTreeChanged();
  338. }
  339. }