HeapSnapshotWorkerDispatcher.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2011 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. */
  33. WebInspector.HeapSnapshotWorkerDispatcher = function(globalObject, postMessage)
  34. {
  35. this._objects = [];
  36. this._global = globalObject;
  37. this._postMessage = postMessage;
  38. }
  39. WebInspector.HeapSnapshotWorkerDispatcher.prototype = {
  40. _findFunction: function(name)
  41. {
  42. var path = name.split(".");
  43. var result = this._global;
  44. for (var i = 0; i < path.length; ++i)
  45. result = result[path[i]];
  46. return result;
  47. },
  48. /**
  49. * @param{string} name
  50. * @param{*} data
  51. */
  52. sendEvent: function(name, data)
  53. {
  54. this._postMessage({eventName: name, data: data});
  55. },
  56. dispatchMessage: function(event)
  57. {
  58. var data = event.data;
  59. var response = {callId: data.callId};
  60. try {
  61. switch (data.disposition) {
  62. case "create": {
  63. var constructorFunction = this._findFunction(data.methodName);
  64. this._objects[data.objectId] = new constructorFunction(this);
  65. break;
  66. }
  67. case "dispose": {
  68. delete this._objects[data.objectId];
  69. break;
  70. }
  71. case "getter": {
  72. var object = this._objects[data.objectId];
  73. var result = object[data.methodName];
  74. response.result = result;
  75. break;
  76. }
  77. case "factory": {
  78. var object = this._objects[data.objectId];
  79. var result = object[data.methodName].apply(object, data.methodArguments);
  80. if (result)
  81. this._objects[data.newObjectId] = result;
  82. response.result = !!result;
  83. break;
  84. }
  85. case "method": {
  86. var object = this._objects[data.objectId];
  87. response.result = object[data.methodName].apply(object, data.methodArguments);
  88. break;
  89. }
  90. }
  91. } catch (e) {
  92. response.error = e.toString();
  93. response.errorCallStack = e.stack;
  94. if (data.methodName)
  95. response.errorMethodName = data.methodName;
  96. }
  97. this._postMessage(response);
  98. }
  99. };