Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / console / ConsoleView.js
index 6c9ffab..908af50 100644 (file)
  * @implements {WebInspector.Searchable}
  * @implements {WebInspector.TargetManager.Observer}
  * @implements {WebInspector.ViewportControl.Provider}
- * @param {boolean} hideContextSelector
  */
-WebInspector.ConsoleView = function(hideContextSelector)
+WebInspector.ConsoleView = function()
 {
     WebInspector.VBox.call(this);
-    this.registerRequiredCSS("filter.css");
+    this.registerRequiredCSS("ui/filter.css");
 
     this._searchableView = new WebInspector.SearchableView(this);
     this._searchableView.setMinimalSearchQuerySize(0);
@@ -63,9 +62,6 @@ WebInspector.ConsoleView = function(hideContextSelector)
     this._filter = new WebInspector.ConsoleViewFilter(this);
     this._filter.addEventListener(WebInspector.ConsoleViewFilter.Events.FilterChanged, this._updateMessageList.bind(this));
 
-    if (hideContextSelector)
-        this._executionContextSelector.element.classList.add("hidden");
-
     this._filterBar = new WebInspector.FilterBar();
 
     this._preserveLogCheckbox = new WebInspector.StatusBarCheckbox(WebInspector.UIString("Preserve log"));
@@ -92,7 +88,7 @@ WebInspector.ConsoleView = function(hideContextSelector)
 
     this._viewportThrottler = new WebInspector.Throttler(50);
 
-    this._filterStatusMessageElement = document.createElementWithClass("div", "console-message");
+    this._filterStatusMessageElement = createElementWithClass("div", "console-message");
     this._messagesElement.insertBefore(this._filterStatusMessageElement, this._messagesElement.firstChild);
     this._filterStatusTextElement = this._filterStatusMessageElement.createChild("span", "console-info");
     this._filterStatusMessageElement.createTextChild(" ");
@@ -107,6 +103,10 @@ WebInspector.ConsoleView = function(hideContextSelector)
     this._promptElement.id = "console-prompt";
     this._promptElement.spellcheck = false;
 
+    // FIXME: This is a workaround for the selection machinery bug. See crbug.com/410899
+    var selectAllFixer = this._messagesElement.createChild("div", "console-view-fix-select-all");
+    selectAllFixer.textContent = ".";
+
     this._showAllMessagesCheckbox = new WebInspector.StatusBarCheckbox(WebInspector.UIString("Show all messages"));
     this._showAllMessagesCheckbox.inputElement.checked = true;
     this._showAllMessagesCheckbox.inputElement.addEventListener("change", this._updateMessageList.bind(this), false);
@@ -139,28 +139,55 @@ WebInspector.ConsoleView = function(hideContextSelector)
 
     this._registerWithMessageSink();
     WebInspector.targetManager.observeTargets(this);
-    WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
-    WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._onConsoleMessageAdded, this);
-    WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.CommandEvaluated, this._commandEvaluated, this);
     WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this);
     WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this);
+    WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Events.MainFrameNavigated, this._onMainFrameNavigated, this);
+
+    this._initConsoleMessages();
+
+    WebInspector.context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executionContextChangedExternally, this);
+}
 
+WebInspector.ConsoleView.prototype = {
     /**
-     * @param {!WebInspector.ConsoleMessage} message
-     * @this {WebInspector.ConsoleView}
+     * @param {!WebInspector.Event} event
      */
-    function appendMessage(message)
+    _onMainFrameNavigated: function(event)
     {
-         var viewMessage = this._createViewMessage(message);
-         this._consoleMessageAdded(viewMessage);
-    }
+        var frame = /** @type {!WebInspector.ResourceTreeFrame} */(event.data);
+        WebInspector.console.addMessage(WebInspector.UIString("Navigated to %s", frame.url));
+    },
 
-    WebInspector.multitargetConsoleModel.messages().forEach(appendMessage, this);
+    _initConsoleMessages: function()
+    {
+        var mainTarget = WebInspector.targetManager.mainTarget();
+        if (!WebInspector.isWorkerFrontend() && (!mainTarget || !mainTarget.resourceTreeModel.cachedResourcesLoaded())) {
+            WebInspector.targetManager.addModelListener(WebInspector.ResourceTreeModel, WebInspector.ResourceTreeModel.EventTypes.CachedResourcesLoaded, this._onResourceTreeModelLoaded, this);
+            return;
+        }
+        this._fetchMultitargetMessages();
+    },
 
-    WebInspector.context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executionContextChangedExternally, this);
-}
+    /**
+     * @param {!WebInspector.Event} event
+     */
+    _onResourceTreeModelLoaded: function(event)
+    {
+        var resourceTreeModel = event.target;
+        if (resourceTreeModel.target() !== WebInspector.targetManager.mainTarget())
+            return;
+        WebInspector.targetManager.removeModelListener(WebInspector.ResourceTreeModel, WebInspector.ResourceTreeModel.EventTypes.CachedResourcesLoaded, this._onResourceTreeModelLoaded, this);
+        this._fetchMultitargetMessages();
+    },
+
+    _fetchMultitargetMessages: function()
+    {
+        WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
+        WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._onConsoleMessageAdded, this);
+        WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.CommandEvaluated, this._commandEvaluated, this);
+        WebInspector.multitargetConsoleModel.messages().forEach(this._addConsoleMessage, this);
+    },
 
-WebInspector.ConsoleView.prototype = {
     /**
      * @return {number}
      */
@@ -202,7 +229,7 @@ WebInspector.ConsoleView.prototype = {
     {
         this._viewport.invalidate();
         target.runtimeModel.executionContexts().forEach(this._executionContextCreated, this);
-        if (WebInspector.targetManager.targets().length > 1)
+        if (WebInspector.targetManager.targets().length > 1 && !WebInspector.isWorkerFrontend())
             this._showAllMessagesCheckbox.element.classList.toggle("hidden", false);
     },
 
@@ -285,12 +312,12 @@ WebInspector.ConsoleView.prototype = {
         if (executionContext.isMainWorldContext) {
             if (executionContext.frameId) {
                 var frame = executionContext.target().resourceTreeModel.frameForId(executionContext.frameId);
-                result =  frame ? frame.displayName() : executionContext.name;
+                result =  frame ? frame.displayName() : (executionContext.origin || executionContext.name);
             } else {
-                result = WebInspector.displayNameForURL(executionContext.name)
+                result = WebInspector.displayNameForURL(executionContext.origin) || executionContext.name;
             }
         } else
-            result = "\u00a0\u00a0\u00a0\u00a0" + executionContext.name;
+            result = "\u00a0\u00a0\u00a0\u00a0" + (executionContext.name || executionContext.origin);
 
         var maxLength = 50;
         return result.trimMiddle(maxLength);
@@ -310,10 +337,15 @@ WebInspector.ConsoleView.prototype = {
      */
     _executionContextCreated: function(executionContext)
     {
-        var newOption = document.createElement("option");
+        // FIXME(413886): We never want to show execution context for the main thread of shadow page in service/shared worker frontend.
+        // This check could be removed once we do not send this context to frontend.
+        if (WebInspector.isWorkerFrontend() && executionContext.target() === WebInspector.targetManager.mainTarget())
+            return;
+
+        var newOption = createElement("option");
         newOption.__executionContext = executionContext;
         newOption.text = this._titleFor(executionContext);
-        this._optionByExecutionContext.put(executionContext, newOption);
+        this._optionByExecutionContext.set(executionContext, newOption);
         var sameGroupExists = false;
         var options = this._executionContextSelector.selectElement().options;
         var insertBeforeOption = null;
@@ -354,7 +386,7 @@ WebInspector.ConsoleView.prototype = {
      */
     _clearExecutionContextsForTarget: function(target)
     {
-        var executionContexts = this._optionByExecutionContext.keys();
+        var executionContexts = this._optionByExecutionContext.keysArray();
         for (var i = 0; i < executionContexts.length; ++i) {
             if (executionContexts[i].target() === target)
                 this._executionContextDestroyed(executionContexts[i]);
@@ -440,7 +472,12 @@ WebInspector.ConsoleView.prototype = {
          */
         function invalidateViewport(finishCallback)
         {
-            this._viewport.invalidate();
+            if (this._needsFullUpdate) {
+                this._updateMessageList();
+                delete this._needsFullUpdate;
+            } else {
+                this._viewport.invalidate();
+            }
             finishCallback();
         }
         this._viewportThrottler.schedule(invalidateViewport.bind(this));
@@ -459,9 +496,18 @@ WebInspector.ConsoleView.prototype = {
     },
 
     /**
-     * @param {!WebInspector.ConsoleViewMessage} viewMessage
+     * @param {!WebInspector.Event} event
+     */
+    _onConsoleMessageAdded: function(event)
+    {
+        var message = /** @type {!WebInspector.ConsoleMessage} */ (event.data);
+        this._addConsoleMessage(message);
+    },
+
+    /**
+     * @param {!WebInspector.ConsoleMessage} message
      */
-    _consoleMessageAdded: function(viewMessage)
+    _addConsoleMessage: function(message)
     {
         /**
          * @param {!WebInspector.ConsoleViewMessage} viewMessage1
@@ -472,50 +518,48 @@ WebInspector.ConsoleView.prototype = {
         {
             return WebInspector.ConsoleMessage.timestampComparator(viewMessage1.consoleMessage(), viewMessage2.consoleMessage());
         }
+
+        if (message.type === WebInspector.ConsoleMessage.MessageType.Command || message.type === WebInspector.ConsoleMessage.MessageType.Result)
+            message.timestamp = this._consoleMessages.length ? this._consoleMessages.peekLast().consoleMessage().timestamp : 0;
+        var viewMessage = this._createViewMessage(message);
         var insertAt = insertionIndexForObjectInListSortedByFunction(viewMessage, this._consoleMessages, compareTimestamps, true);
+        var insertedInMiddle = insertAt < this._consoleMessages.length;
         this._consoleMessages.splice(insertAt, 0, viewMessage);
 
-        var message = viewMessage.consoleMessage();
         if (this._urlToMessageCount[message.url])
-            this._urlToMessageCount[message.url]++;
+            ++this._urlToMessageCount[message.url];
         else
             this._urlToMessageCount[message.url] = 1;
 
-        if (this._tryToCollapseMessages(viewMessage, this._visibleViewMessages.peekLast()))
-            return;
-
-        if (this._filter.shouldBeVisible(viewMessage))
-            this._showConsoleMessage(viewMessage)
-        else {
-            this._hiddenByFilterCount++;
+        if (!insertedInMiddle) {
+            this._appendMessageToEnd(viewMessage)
             this._updateFilterStatus();
+        } else {
+            this._needsFullUpdate = true;
         }
-    },
 
-    /**
-     * @param {!WebInspector.Event} event
-     */
-    _onConsoleMessageAdded: function(event)
-    {
-        var message = /** @type {!WebInspector.ConsoleMessage} */ (event.data);
-        this._addConsoleMessage(message);
+        this._scheduleViewportRefresh();
+        this._consoleMessageAddedForTest(viewMessage);
     },
 
     /**
-     * @param {!WebInspector.ConsoleMessage} message
+     * @param {!WebInspector.ConsoleViewMessage} viewMessage
      */
-    _addConsoleMessage: function(message)
-    {
-        var viewMessage = this._createViewMessage(message);
-        this._consoleMessageAdded(viewMessage);
-        this._scheduleViewportRefresh();
-    },
+    _consoleMessageAddedForTest: function(viewMessage) { },
 
     /**
      * @param {!WebInspector.ConsoleViewMessage} viewMessage
      */
-    _showConsoleMessage: function(viewMessage)
+    _appendMessageToEnd: function(viewMessage)
     {
+        if (!this._filter.shouldBeVisible(viewMessage)) {
+            this._hiddenByFilterCount++;
+            return;
+        }
+
+        if (this._tryToCollapseMessages(viewMessage, this._visibleViewMessages.peekLast()))
+            return;
+
         var lastMessage = this._visibleViewMessages.peekLast();
         if (viewMessage.consoleMessage().type === WebInspector.ConsoleMessage.MessageType.EndGroup) {
             if (lastMessage && !this._currentGroup.messagesHidden())
@@ -549,7 +593,7 @@ WebInspector.ConsoleView.prototype = {
         var nestingLevel = this._currentGroup.nestingLevel();
         switch (message.type) {
         case WebInspector.ConsoleMessage.MessageType.Command:
-            return new WebInspector.ConsoleCommand(message, nestingLevel);
+            return new WebInspector.ConsoleCommand(message, this._linkifier, nestingLevel);
         case WebInspector.ConsoleMessage.MessageType.Result:
             return new WebInspector.ConsoleCommandResult(message, this._linkifier, nestingLevel);
         case WebInspector.ConsoleMessage.MessageType.StartGroupCollapsed:
@@ -613,9 +657,9 @@ WebInspector.ConsoleView.prototype = {
         contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Clear console" : "Clear Console"), this._requestClearMessages.bind(this));
 
         var request = consoleMessage ? consoleMessage.request : null;
-        if (request && request.type === WebInspector.resourceTypes.XHR) {
+        if (request && request.resourceType() === WebInspector.resourceTypes.XHR) {
             contextMenu.appendSeparator();
-            contextMenu.appendItem(WebInspector.UIString("Replay XHR"), NetworkAgent.replayXHR.bind(null, request.requestId));
+            contextMenu.appendItem(WebInspector.UIString("Replay XHR"), request.replayXHR.bind(request));
         }
 
         contextMenu.show();
@@ -647,15 +691,8 @@ WebInspector.ConsoleView.prototype = {
             this._visibleViewMessages[i].resetIncrementRepeatCount();
         }
         this._visibleViewMessages = [];
-        for (var i = 0; i < this._consoleMessages.length; ++i) {
-            var viewMessage = this._consoleMessages[i];
-            if (this._tryToCollapseMessages(viewMessage, this._visibleViewMessages.peekLast()))
-                continue;
-            if (this._filter.shouldBeVisible(viewMessage))
-                this._showConsoleMessage(viewMessage);
-            else
-                this._hiddenByFilterCount++;
-        }
+        for (var i = 0; i < this._consoleMessages.length; ++i)
+            this._appendMessageToEnd(this._consoleMessages[i]);
         this._updateFilterStatus();
         this._viewport.invalidate();
     },
@@ -791,7 +828,7 @@ WebInspector.ConsoleView.prototype = {
             if (!wasThrown)
                 message = new WebInspector.ConsoleMessage(target, WebInspector.ConsoleMessage.MessageSource.JS, level, "", WebInspector.ConsoleMessage.MessageType.Result, url, lineNumber, columnNumber, undefined, [result]);
             else
-                message = new WebInspector.ConsoleMessage(target, WebInspector.ConsoleMessage.MessageSource.JS, level, exceptionDetails.text, WebInspector.ConsoleMessage.MessageType.Result, exceptionDetails.url, exceptionDetails.line, exceptionDetails.column, undefined, [WebInspector.UIString("Uncaught"), result], exceptionDetails.stackTrace);
+                message = new WebInspector.ConsoleMessage(target, WebInspector.ConsoleMessage.MessageSource.JS, level, exceptionDetails.text, WebInspector.ConsoleMessage.MessageType.Result, exceptionDetails.url, exceptionDetails.line, exceptionDetails.column, undefined, [WebInspector.UIString("Uncaught"), result], exceptionDetails.stackTrace, undefined, undefined, undefined, undefined, exceptionDetails.scriptId);
             message.setOriginatingMessage(originatingConsoleMessage);
             target.consoleModel.addMessage(message);
         }
@@ -812,13 +849,18 @@ WebInspector.ConsoleView.prototype = {
                 addMessage();
                 return;
             }
-
             var url;
+            var lineNumber;
+            var columnNumber;
             var script = target.debuggerModel.scriptForId(response.location.scriptId);
-            if (script && script.sourceURL)
+            if (script && script.sourceURL) {
                 url = script.sourceURL;
+                // FIXME(WK62725): Debugger line/column are 0-based, while console ones are 1-based.
+                lineNumber = response.location.lineNumber + 1;
+                columnNumber = response.location.columnNumber + 1;
+            }
             // FIXME: this should be using live location.
-            addMessage(url, response.location.lineNumber, response.location.columnNumber);
+            addMessage(url, lineNumber, columnNumber);
         }
     },
 
@@ -863,12 +905,13 @@ WebInspector.ConsoleView.prototype = {
     },
 
     /**
-     * @param {string} query
+     * @param {!WebInspector.SearchableView.SearchConfig} searchConfig
      * @param {boolean} shouldJump
      * @param {boolean=} jumpBackwards
      */
-    performSearch: function(query, shouldJump, jumpBackwards)
+    performSearch: function(searchConfig, shouldJump, jumpBackwards)
     {
+        var query = searchConfig.query;
         this.searchCanceled();
         this._searchableView.updateSearchMatchesCount(0);
         this._searchRegex = createPlainTextSearchRegex(query, "gi");
@@ -900,6 +943,22 @@ WebInspector.ConsoleView.prototype = {
         this._jumpToSearchResult(this._currentSearchResultIndex - 1);
     },
 
+    /**
+     * @return {boolean}
+     */
+    supportsCaseSensitiveSearch: function()
+    {
+        return false;
+    },
+
+    /**
+     * @return {boolean}
+     */
+    supportsRegexSearch: function()
+    {
+        return false;
+    },
+
     _clearCurrentSearchResultHighlight: function()
     {
         if (!this._searchResults)
@@ -958,6 +1017,9 @@ WebInspector.ConsoleViewFilter.prototype = {
         this._levelFilterUI = new WebInspector.NamedBitSetFilterUI(levels, WebInspector.settings.messageLevelFilters);
         this._levelFilterUI.addEventListener(WebInspector.FilterUI.Events.FilterChanged, this._filterChanged, this);
         filterBar.addFilter(this._levelFilterUI);
+        this._hideNetworkMessagesCheckbox = new WebInspector.CheckboxFilterUI("hide-network-messages", WebInspector.UIString("Hide network messages"), true, WebInspector.settings.hideNetworkMessages);
+        this._hideNetworkMessagesCheckbox.addEventListener(WebInspector.FilterUI.Events.FilterChanged, this._filterChanged.bind(this), this);
+        filterBar.addFilter(this._hideNetworkMessagesCheckbox);
     },
 
     _textFilterChanged: function(event)
@@ -1018,6 +1080,9 @@ WebInspector.ConsoleViewFilter.prototype = {
             }
         }
 
+        if (WebInspector.settings.hideNetworkMessages.get() && viewMessage.consoleMessage().source === WebInspector.ConsoleMessage.MessageSource.Network)
+            return false;
+
         if (viewMessage.consoleMessage().isGroupMessage())
             return true;
 
@@ -1045,6 +1110,7 @@ WebInspector.ConsoleViewFilter.prototype = {
         WebInspector.settings.messageURLFilters.set(this._messageURLFilters);
         WebInspector.settings.messageLevelFilters.set({});
         this._view._showAllMessagesCheckbox.inputElement.checked = true;
+        this._hideNetworkMessagesCheckbox.setState(false);
         this._textFilterUI.setValue("");
         this._filterChanged();
     },
@@ -1057,11 +1123,12 @@ WebInspector.ConsoleViewFilter.prototype = {
  * @constructor
  * @extends {WebInspector.ConsoleViewMessage}
  * @param {!WebInspector.ConsoleMessage} message
+ * @param {!WebInspector.Linkifier} linkifier
  * @param {number} nestingLevel
  */
-WebInspector.ConsoleCommand = function(message, nestingLevel)
+WebInspector.ConsoleCommand = function(message, linkifier, nestingLevel)
 {
-    WebInspector.ConsoleViewMessage.call(this, message, null, nestingLevel);
+    WebInspector.ConsoleViewMessage.call(this, message, linkifier, nestingLevel);
 }
 
 WebInspector.ConsoleCommand.prototype = {
@@ -1105,7 +1172,7 @@ WebInspector.ConsoleCommand.prototype = {
     contentElement: function()
     {
         if (!this._element) {
-            this._element = document.createElementWithClass("div", "console-user-command");
+            this._element = createElementWithClass("div", "console-user-command");
             this._element.message = this;
 
             this._formatCommand();
@@ -1116,7 +1183,7 @@ WebInspector.ConsoleCommand.prototype = {
 
     _formatCommand: function()
     {
-        this._formattedCommand = document.createElementWithClass("span", "console-message-text source-code");
+        this._formattedCommand = createElementWithClass("span", "console-message-text source-code");
         this._formattedCommand.textContent = this.text;
     },
 
@@ -1153,6 +1220,7 @@ WebInspector.ConsoleCommandResult.prototype = {
     {
         var element = WebInspector.ConsoleViewMessage.prototype.contentElement.call(this);
         element.classList.add("console-user-command-result");
+        this.updateTimestamp(false);
         return element;
     },