Home | History | Annotate | Download | only in sdk
      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 /**
     32  * @constructor
     33  * @param {!WebInspector.Workspace} workspace
     34  */
     35 WebInspector.LiveEditSupport = function(workspace)
     36 {
     37     this._workspace = workspace;
     38     this._projectId = "liveedit:";
     39     this._projectDelegate = new WebInspector.DebuggerProjectDelegate(workspace, this._projectId, WebInspector.projectTypes.LiveEdit);
     40     WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
     41     this._debuggerReset();
     42 }
     43 
     44 WebInspector.LiveEditSupport.prototype = {
     45     /**
     46      * @param {!WebInspector.UISourceCode} uiSourceCode
     47      * @return {!WebInspector.UISourceCode}
     48      */
     49     uiSourceCodeForLiveEdit: function(uiSourceCode)
     50     {
     51         var rawLocation = uiSourceCode.uiLocationToRawLocation(WebInspector.targetManager.targets()[0], 0, 0);
     52         var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (rawLocation);
     53         var script = debuggerModelLocation.script();
     54         var uiLocation = script.rawLocationToUILocation(0, 0);
     55 
     56         // FIXME: Support live editing of scripts mapped to some file.
     57         if (uiLocation.uiSourceCode !== uiSourceCode)
     58             return uiLocation.uiSourceCode;
     59         if (this._uiSourceCodeForScriptId[script.scriptId])
     60             return this._uiSourceCodeForScriptId[script.scriptId];
     61 
     62         console.assert(!script.isInlineScript());
     63         var path = this._projectDelegate.addScript(script);
     64         var liveEditUISourceCode = this._workspace.uiSourceCode(this._projectId, path);
     65 
     66         liveEditUISourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
     67         this._uiSourceCodeForScriptId[script.scriptId] = liveEditUISourceCode;
     68         this._scriptIdForUISourceCode.put(liveEditUISourceCode, script.scriptId);
     69         return liveEditUISourceCode;
     70     },
     71 
     72     _debuggerReset: function()
     73     {
     74         /** @type {!Object.<string, !WebInspector.UISourceCode>} */
     75         this._uiSourceCodeForScriptId = {};
     76         /** @type {!Map.<!WebInspector.UISourceCode, string>} */
     77         this._scriptIdForUISourceCode = new Map();
     78         this._projectDelegate.reset();
     79     },
     80 
     81     /**
     82      * @param {!WebInspector.Event} event
     83      */
     84     _workingCopyCommitted: function(event)
     85     {
     86         var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.target);
     87         var scriptId = /** @type {string} */ (this._scriptIdForUISourceCode.get(uiSourceCode));
     88         WebInspector.debuggerModel.setScriptSource(scriptId, uiSourceCode.workingCopy(), innerCallback);
     89 
     90         /**
     91          * @param {?string} error
     92          * @param {!DebuggerAgent.SetScriptSourceError=} errorData
     93          */
     94         function innerCallback(error, errorData)
     95         {
     96             if (error) {
     97                 var script = WebInspector.debuggerModel.scriptForId(scriptId);
     98                 WebInspector.LiveEditSupport.logDetailedError(error, errorData, script);
     99                 return;
    100             }
    101             WebInspector.LiveEditSupport.logSuccess();
    102         }
    103     }
    104 }
    105 
    106 /**
    107  * @param {?string} error
    108  * @param {!DebuggerAgent.SetScriptSourceError=} errorData
    109  * @param {!WebInspector.Script=} contextScript
    110  */
    111 WebInspector.LiveEditSupport.logDetailedError = function(error, errorData, contextScript)
    112 {
    113     var warningLevel = WebInspector.ConsoleMessage.MessageLevel.Warning;
    114     if (!errorData) {
    115         if (error)
    116             WebInspector.messageSink.addMessage(WebInspector.UIString("LiveEdit failed: %s", error), warningLevel);
    117         return;
    118     }
    119     var compileError = errorData.compileError;
    120     if (compileError) {
    121         var location = contextScript ? WebInspector.UIString(" at %s:%d:%d", contextScript.sourceURL, compileError.lineNumber, compileError.columnNumber) : "";
    122         var message = WebInspector.UIString("LiveEdit compile failed: %s%s", compileError.message, location);
    123         WebInspector.messageSink.addErrorMessage(message);
    124     } else {
    125         WebInspector.messageSink.addMessage(WebInspector.UIString("Unknown LiveEdit error: %s; %s", JSON.stringify(errorData), error), warningLevel);
    126     }
    127 }
    128 
    129 WebInspector.LiveEditSupport.logSuccess = function()
    130 {
    131     WebInspector.messageSink.addMessage(WebInspector.UIString("Recompilation and update succeeded."));
    132 }
    133 
    134 /** @type {!WebInspector.LiveEditSupport} */
    135 WebInspector.liveEditSupport;
    136