FrontendCommandHandler.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. var debugProtocol = require('debug')('node-inspector:protocol:devtools');
  2. var RuntimeAgent = require('./RuntimeAgent').RuntimeAgent,
  3. PageAgent = require('./PageAgent').PageAgent,
  4. NetworkAgent = require('./NetworkAgent').NetworkAgent,
  5. DebuggerAgent = require('./DebuggerAgent').DebuggerAgent;
  6. /**
  7. * @param {Object} config
  8. * @param {FrontendClient} frontendClient
  9. * @param {DebuggerClient} debuggerClient
  10. * @param {BreakEventHandler} breakEventHandler
  11. * @param {ScriptManager} scriptManager
  12. */
  13. function FrontendCommandHandler(config,
  14. frontendClient,
  15. debuggerClient,
  16. breakEventHandler,
  17. scriptManager) {
  18. this._config = config;
  19. this._agents = {};
  20. this._specialCommands = {};
  21. this._frontendClient = frontendClient;
  22. this._debuggerClient = debuggerClient;
  23. this._breakEventHandler = breakEventHandler;
  24. this._scriptManager = scriptManager;
  25. this._initializeRegistry();
  26. this._registerEventHandlers();
  27. this._pauseInitialEvents();
  28. }
  29. FrontendCommandHandler.prototype = {
  30. _initializeRegistry: function() {
  31. this._registerAgent(
  32. 'Debugger',
  33. new DebuggerAgent(
  34. this._config,
  35. this._frontendClient,
  36. this._debuggerClient,
  37. this._breakEventHandler,
  38. this._scriptManager)
  39. );
  40. this._registerAgent('Runtime', new RuntimeAgent(this._config, this._debuggerClient));
  41. this._registerAgent(
  42. 'Page',
  43. new PageAgent(
  44. this._config,
  45. this._debuggerClient,
  46. this._scriptManager)
  47. );
  48. this._registerAgent('Network', new NetworkAgent());
  49. this._registerNoopCommands(
  50. 'Network.enable',
  51. 'Console.enable',
  52. 'Console.clearMessages',
  53. 'Database.enable',
  54. 'DOMStorage.enable',
  55. 'DOM.hideHighlight',
  56. 'Inspector.enable',
  57. 'Page.clearDeviceOrientationOverride',
  58. 'Page.clearGeolocationOverride',
  59. 'Page.setContinuousPaintingEnabled',
  60. 'Page.setEmulatedMedia',
  61. 'Page.setDeviceMetricsOverride',
  62. 'Page.setShowDebugBorders',
  63. 'Page.setShowFPSCounter',
  64. 'Page.setShowScrollBottleneckRects',
  65. 'Page.setShowViewportSizeOnResize',
  66. 'Profiler.enable',
  67. 'CSS.enable'
  68. );
  69. this._registerQuery('CSS.getSupportedCSSProperties', { cssProperties: []});
  70. this._registerQuery('Worker.canInspectWorkers', { result: false });
  71. this._registerQuery('Page.getScriptExecutionStatus', { result: 'enabled' });
  72. },
  73. _registerAgent: function(name, agent) {
  74. this._agents[name] = agent;
  75. },
  76. _registerNoopCommands: function() {
  77. var i, fullMethodName;
  78. for (i = 0; i < arguments.length; i++) {
  79. fullMethodName = arguments[i];
  80. this._specialCommands[fullMethodName] = {};
  81. }
  82. },
  83. _registerQuery: function(fullMethodName, result) {
  84. this._specialCommands[fullMethodName] = { result: result };
  85. },
  86. _registerEventHandlers: function() {
  87. this._frontendClient.on(
  88. 'message',
  89. this._handleFrontendMessage.bind(this));
  90. },
  91. _handleFrontendMessage: function(message) {
  92. debugProtocol('frontend: ' + message);
  93. var command = JSON.parse(message);
  94. this.handleCommand(command);
  95. },
  96. _pauseInitialEvents: function() {
  97. this._frontendClient.pauseEvents();
  98. this._agents.Page.on('resource-tree', function() {
  99. this._frontendClient.resumeEvents();
  100. }.bind(this));
  101. },
  102. handleCommand: function(messageObject) {
  103. var fullMethodName = messageObject.method,
  104. domainAndMethod = fullMethodName.split('.'),
  105. domainName = domainAndMethod[0],
  106. methodName = domainAndMethod[1],
  107. requestId = messageObject.id,
  108. agent,
  109. method;
  110. if (this._specialCommands[fullMethodName]) {
  111. this._handleMethodResult(
  112. requestId,
  113. fullMethodName,
  114. null,
  115. this._specialCommands[fullMethodName].result);
  116. return;
  117. }
  118. agent = this._agents[domainName];
  119. if (!agent) {
  120. this._sendNotImplementedResponse(requestId, fullMethodName);
  121. return;
  122. }
  123. method = agent[methodName];
  124. if (!method || typeof method !== 'function') {
  125. this._sendNotImplementedResponse(requestId, fullMethodName);
  126. return;
  127. }
  128. method.call(agent, messageObject.params, function(error, result) {
  129. this._handleMethodResult(messageObject.id, fullMethodName, error, result);
  130. }.bind(this));
  131. },
  132. _sendNotImplementedResponse: function(requestId, fullMethodName) {
  133. console.log(
  134. 'Received request for a method not implemented:',
  135. fullMethodName
  136. );
  137. this._handleMethodResult(
  138. requestId,
  139. fullMethodName,
  140. new Error('Not implemented.')
  141. );
  142. },
  143. _handleMethodResult: function(requestId, fullMethodName, error, result) {
  144. var response;
  145. if (!requestId) {
  146. if (response !== undefined)
  147. console.log('Warning: discarded result of ' + fullMethodName);
  148. return;
  149. }
  150. this._frontendClient.sendResponse(requestId, fullMethodName, error, result);
  151. }
  152. };
  153. exports.FrontendCommandHandler = FrontendCommandHandler;