LiveEditSupport.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2012 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. * @param {WebInspector.Workspace} workspace
  33. */
  34. WebInspector.LiveEditSupport = function(workspace)
  35. {
  36. this._workspaceProvider = new WebInspector.SimpleWorkspaceProvider(workspace, WebInspector.projectTypes.LiveEdit);
  37. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
  38. this._debuggerReset();
  39. }
  40. WebInspector.LiveEditSupport.prototype = {
  41. /**
  42. * @param {WebInspector.UISourceCode} uiSourceCode
  43. * @return {WebInspector.UISourceCode}
  44. */
  45. uiSourceCodeForLiveEdit: function(uiSourceCode)
  46. {
  47. var rawLocation = uiSourceCode.uiLocationToRawLocation(0, 0);
  48. var debuggerModelLocation = /** @type {WebInspector.DebuggerModel.Location} */ (rawLocation);
  49. var script = WebInspector.debuggerModel.scriptForId(debuggerModelLocation.scriptId);
  50. var uiLocation = script.rawLocationToUILocation(0, 0);
  51. // FIXME: Support live editing of scripts mapped to some file.
  52. if (uiLocation.uiSourceCode !== uiSourceCode)
  53. return uiLocation.uiSourceCode;
  54. if (this._uiSourceCodeForScriptId[script.scriptId])
  55. return this._uiSourceCodeForScriptId[script.scriptId];
  56. console.assert(!script.isInlineScript());
  57. var liveEditUISourceCode = this._workspaceProvider.addUniqueFileForURL(script.sourceURL, script, true, script.isContentScript);
  58. liveEditUISourceCode.setScriptFile(new WebInspector.LiveEditScriptFile(uiSourceCode, liveEditUISourceCode, script.scriptId));
  59. this._uiSourceCodeForScriptId[script.scriptId] = liveEditUISourceCode;
  60. this._scriptIdForUISourceCode.put(liveEditUISourceCode, script.scriptId);
  61. return liveEditUISourceCode;
  62. },
  63. _debuggerReset: function()
  64. {
  65. /** @type {!Object.<string, WebInspector.UISourceCode>} */
  66. this._uiSourceCodeForScriptId = {};
  67. /** @type {!Map.<WebInspector.UISourceCode, string>} */
  68. this._scriptIdForUISourceCode = new Map();
  69. this._workspaceProvider.reset();
  70. },
  71. }
  72. /**
  73. * @param {?string} error
  74. * @param {DebuggerAgent.SetScriptSourceError=} errorData
  75. * @param {WebInspector.Script=} contextScript
  76. */
  77. WebInspector.LiveEditSupport.logDetailedError = function(error, errorData, contextScript)
  78. {
  79. if (!errorData) {
  80. WebInspector.showErrorMessage(error);
  81. return;
  82. }
  83. var compileError = errorData.compileError;
  84. if (compileError) {
  85. var message = compileError.message;
  86. if (contextScript)
  87. message += " at " + contextScript.sourceURL + ":" + compileError.lineNumber + ":" + compileError.columnNumber;
  88. WebInspector.showErrorMessage(message);
  89. } else {
  90. WebInspector.showErrorMessage("Unknown LiveEdit error: " + JSON.stringify(errorData) + "; " + error);
  91. }
  92. }
  93. /**
  94. * @constructor
  95. * @implements {WebInspector.ScriptFile}
  96. * @extends {WebInspector.Object}
  97. * @param {WebInspector.UISourceCode} uiSourceCode
  98. * @param {WebInspector.UISourceCode} liveEditUISourceCode
  99. * @param {string} scriptId
  100. */
  101. WebInspector.LiveEditScriptFile = function(uiSourceCode, liveEditUISourceCode, scriptId)
  102. {
  103. WebInspector.ScriptFile.call(this);
  104. this._uiSourceCode = uiSourceCode;
  105. this._liveEditUISourceCode = liveEditUISourceCode;
  106. this._scriptId = scriptId;
  107. this._liveEditUISourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
  108. }
  109. WebInspector.LiveEditScriptFile.prototype = {
  110. _workingCopyCommitted: function(event)
  111. {
  112. /**
  113. * @param {?string} error
  114. * @param {DebuggerAgent.SetScriptSourceError=} errorData
  115. */
  116. function innerCallback(error, errorData)
  117. {
  118. if (error) {
  119. var script = WebInspector.debuggerModel.scriptForId(this._scriptId);
  120. WebInspector.LiveEditSupport.logDetailedError(error, errorData, script);
  121. return;
  122. }
  123. }
  124. var script = WebInspector.debuggerModel.scriptForId(this._scriptId);
  125. WebInspector.debuggerModel.setScriptSource(script.scriptId, this._liveEditUISourceCode.workingCopy(), innerCallback.bind(this));
  126. },
  127. /**
  128. * @return {boolean}
  129. */
  130. hasDivergedFromVM: function()
  131. {
  132. return true;
  133. },
  134. /**
  135. * @return {boolean}
  136. */
  137. isDivergingFromVM: function()
  138. {
  139. return false;
  140. },
  141. /**
  142. * @return {boolean}
  143. */
  144. isMergingToVM: function()
  145. {
  146. return false;
  147. },
  148. checkMapping: function()
  149. {
  150. },
  151. __proto__: WebInspector.Object.prototype
  152. }
  153. /** @type {WebInspector.LiveEditSupport} */
  154. WebInspector.liveEditSupport = null;