Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / CPUProfileView.js
index 4678ac3..8e2ab10 100644 (file)
@@ -32,7 +32,7 @@ WebInspector.CPUProfileView = function(profileHeader)
 {
     WebInspector.View.call(this);
 
-    this.element.classList.add("profile-view");
+    this.element.classList.add("cpu-profile-view");
 
     this.showSelfTimeAsPercent = WebInspector.settings.createSetting("cpuProfilerShowSelfTimeAsPercent", true);
     this.showTotalTimeAsPercent = WebInspector.settings.createSetting("cpuProfilerShowTotalTimeAsPercent", true);
@@ -413,9 +413,7 @@ WebInspector.CPUProfileView.prototype = {
         var script = WebInspector.debuggerModel.scriptForId(node.scriptId)
         if (!script)
             return;
-        var uiLocation = script.rawLocationToUILocation(node.lineNumber);
-        if (uiLocation)
-            uiLocation.reveal();
+        WebInspector.Revealer.reveal(script.rawLocationToUILocation(node.lineNumber));
     },
 
     _changeView: function()
@@ -640,7 +638,6 @@ WebInspector.CPUProfileType = function()
 {
     WebInspector.ProfileType.call(this, WebInspector.CPUProfileType.TypeId, WebInspector.UIString("Collect JavaScript CPU Profile"));
     this._recording = false;
-    this._nextProfileId = 1;
 
     this._nextAnonymousConsoleProfileNumber = 1;
     this._anonymousConsoleProfileIdToTitle = {};
@@ -721,8 +718,7 @@ WebInspector.CPUProfileType.prototype = {
             delete this._anonymousConsoleProfileIdToTitle[protocolId];
         }
 
-        var id = this._nextProfileId++;
-        var profile = new WebInspector.CPUProfileHeader(this, resolvedTitle, id);
+        var profile = new WebInspector.CPUProfileHeader(this, resolvedTitle);
         profile.setProtocolProfile(cpuProfile);
         this.addProfile(profile);
 
@@ -731,10 +727,12 @@ WebInspector.CPUProfileType.prototype = {
         var a = messageElement.createChild("span", "link");
         a.title = resolvedTitle;
         a.textContent = resolvedTitle;
-        a.addEventListener("click", onClick.bind(this), true);
-        function onClick(event)
+        a.addEventListener("click", onClick.bind(this, profile.uid), true);
+        function onClick(profileUid, event)
         {
-            WebInspector.showPanel("profiles").showProfile(WebInspector.CPUProfileType.TypeId, id);
+            var profile = WebInspector.ProfileTypeRegistry.instance.cpuProfileType.getProfile(profileUid);
+            if (profile)
+                WebInspector.showPanel("profiles").showProfile(profile);
         }
         messageElement.createTextChild("' finished.");
 
@@ -778,10 +776,9 @@ WebInspector.CPUProfileType.prototype = {
     {
         if (this._profileBeingRecorded)
             return;
-        var id = this._nextProfileId++;
-        this._profileBeingRecorded = new WebInspector.CPUProfileHeader(this, WebInspector.UIString("Recording\u2026"), id);
+        this._profileBeingRecorded = new WebInspector.CPUProfileHeader(this);
         this.addProfile(this._profileBeingRecorded);
-
+        this._profileBeingRecorded.updateStatus(WebInspector.UIString("Recording\u2026"));
         this._recording = true;
         WebInspector.cpuProfilerModel.setRecording(true);
         WebInspector.userMetrics.ProfilesCPUProfileTaken.record();
@@ -803,13 +800,10 @@ WebInspector.CPUProfileType.prototype = {
             if (!this._profileBeingRecorded)
                 return;
             this._profileBeingRecorded.setProtocolProfile(profile);
-
-            var title = WebInspector.UIString("Profile %d", this._profileBeingRecorded.uid);
-            this._profileBeingRecorded.title = title;
-            this._profileBeingRecorded.sidebarElement.mainTitle = title;
+            this._profileBeingRecorded.updateStatus("");
             var recordedProfile = this._profileBeingRecorded;
             this._profileBeingRecorded = null;
-            WebInspector.panels.profiles._showProfile(recordedProfile);
+            WebInspector.panels.profiles.showProfile(recordedProfile);
         }
         ProfilerAgent.stop(didStopProfiling.bind(this));
     },
@@ -827,11 +821,9 @@ WebInspector.CPUProfileType.prototype = {
     /**
      * @override
      */
-    removeProfile: function(profile)
+    profileBeingRecordedRemoved: function()
     {
-        if (this._profileBeingRecorded === profile)
-            this.stopRecordingProfile();
-        WebInspector.ProfileType.prototype.removeProfile.call(this, profile);
+        this.stopRecordingProfile();
     },
 
     __proto__: WebInspector.ProfileType.prototype
@@ -843,12 +835,11 @@ WebInspector.CPUProfileType.prototype = {
  * @implements {WebInspector.OutputStream}
  * @implements {WebInspector.OutputStreamDelegate}
  * @param {!WebInspector.CPUProfileType} type
- * @param {string} title
- * @param {number=} uid
+ * @param {string=} title
  */
-WebInspector.CPUProfileHeader = function(type, title, uid)
+WebInspector.CPUProfileHeader = function(type, title)
 {
-    WebInspector.ProfileHeader.call(this, type, title, uid);
+    WebInspector.ProfileHeader.call(this, type, title || WebInspector.UIString("Profile %d", type._nextProfileUid));
     this._tempFile = null;
 }
 
@@ -856,7 +847,7 @@ WebInspector.CPUProfileHeader.prototype = {
     onTransferStarted: function()
     {
         this._jsonifiedProfile = "";
-        this.sidebarElement.subtitle = WebInspector.UIString("Loading\u2026 %s", Number.bytesToString(this._jsonifiedProfile.length));
+        this.updateStatus(WebInspector.UIString("Loading\u2026 %s", Number.bytesToString(this._jsonifiedProfile.length)));
     },
 
     /**
@@ -864,15 +855,15 @@ WebInspector.CPUProfileHeader.prototype = {
      */
     onChunkTransferred: function(reader)
     {
-        this.sidebarElement.subtitle = WebInspector.UIString("Loading\u2026 %d\%", Number.bytesToString(this._jsonifiedProfile.length));
+        this.updateStatus(WebInspector.UIString("Loading\u2026 %d\%", Number.bytesToString(this._jsonifiedProfile.length)));
     },
 
     onTransferFinished: function()
     {
-        this.sidebarElement.subtitle = WebInspector.UIString("Parsing\u2026");
+        this.updateStatus(WebInspector.UIString("Parsing\u2026"));
         this._profile = JSON.parse(this._jsonifiedProfile);
         this._jsonifiedProfile = null;
-        this.sidebarElement.subtitle = WebInspector.UIString("Loaded");
+        this.updateStatus(WebInspector.UIString("Loaded"));
 
         if (this._profileType._profileBeingRecorded === this)
             this._profileType._profileBeingRecorded = null;
@@ -883,18 +874,20 @@ WebInspector.CPUProfileHeader.prototype = {
      */
     onError: function(reader, e)
     {
+        var subtitle;
         switch(e.target.error.code) {
         case e.target.error.NOT_FOUND_ERR:
-            this.sidebarElement.subtitle = WebInspector.UIString("'%s' not found.", reader.fileName());
-        break;
+            subtitle = WebInspector.UIString("'%s' not found.", reader.fileName());
+            break;
         case e.target.error.NOT_READABLE_ERR:
-            this.sidebarElement.subtitle = WebInspector.UIString("'%s' is not readable", reader.fileName());
-        break;
-        case e.target.error.ABORT_ERR:
+            subtitle = WebInspector.UIString("'%s' is not readable", reader.fileName());
             break;
+        case e.target.error.ABORT_ERR:
+            return;
         default:
-            this.sidebarElement.subtitle = WebInspector.UIString("'%s' error %d", reader.fileName(), e.target.error.code);
+            subtitle = WebInspector.UIString("'%s' error %d", reader.fileName(), e.target.error.code);
         }
+        this.updateStatus(subtitle);
     },
 
     /**
@@ -926,10 +919,9 @@ WebInspector.CPUProfileHeader.prototype = {
 
     /**
      * @override
-     * @param {!WebInspector.ProfilesPanel} profilesPanel
      * @return {!WebInspector.CPUProfileView}
      */
-    createView: function(profilesPanel)
+    createView: function()
     {
         return new WebInspector.CPUProfileView(this);
     },
@@ -940,7 +932,7 @@ WebInspector.CPUProfileHeader.prototype = {
      */
     canSaveToFile: function()
     {
-        return !!this._tempFile;
+        return !this.fromFile() && this._protocolProfile;
     },
 
     saveToFile: function()
@@ -962,7 +954,15 @@ WebInspector.CPUProfileHeader.prototype = {
                 else
                     fileOutputStream.close();
             }
-            this._tempFile.read(didRead.bind(this));
+            if (this._failedToCreateTempFile) {
+                WebInspector.log("Failed to open temp file with heap snapshot",
+                                 WebInspector.ConsoleMessage.MessageLevel.Error);
+                fileOutputStream.close();
+            } else if (this._tempFile) {
+                this._tempFile.read(didRead.bind(this));
+            } else {
+                this._onTempFileReady = onOpenForSave.bind(this, accepted);
+            }
         }
         this._fileName = this._fileName || "CPU-" + new Date().toISO8601Compact() + this._profileType.fileExtension();
         fileOutputStream.open(this._fileName, onOpenForSave.bind(this));
@@ -973,9 +973,7 @@ WebInspector.CPUProfileHeader.prototype = {
      */
     loadFromFile: function(file)
     {
-        this.sidebarElement.subtitle = WebInspector.UIString("Loading\u2026");
-        this.sidebarElement.wait = true;
-
+        this.updateStatus(WebInspector.UIString("Loading\u2026"), true);
         var fileReader = new WebInspector.ChunkedFileReader(file, 10000000, this);
         fileReader.start(this);
     },
@@ -996,6 +994,8 @@ WebInspector.CPUProfileHeader.prototype = {
     {
         this._protocolProfile = cpuProfile;
         this._saveProfileDataToTempFile(cpuProfile);
+        if (this.canSaveToFile())
+            this.dispatchEventToListeners(WebInspector.ProfileHeader.Events.ProfileReceived);
     },
 
     /**
@@ -1022,14 +1022,52 @@ WebInspector.CPUProfileHeader.prototype = {
     _writeToTempFile: function(tempFile, serializedData)
     {
         this._tempFile = tempFile;
-        if (tempFile)
-            tempFile.write(serializedData, tempFile.finishWriting.bind(tempFile));
+        if (!tempFile) {
+            this._failedToCreateTempFile = true;
+            this._notifyTempFileReady();
+            return;
+        }
+        /**
+         * @param {boolean} success
+         * @this {WebInspector.CPUProfileHeader}
+         */
+        function didWriteToTempFile(success)
+        {
+            if (!success)
+                this._failedToCreateTempFile = true;
+            tempFile.finishWriting();
+            this._notifyTempFileReady();
+        }
+        tempFile.write(serializedData, didWriteToTempFile.bind(this));
+    },
+
+    _notifyTempFileReady: function()
+    {
+        if (this._onTempFileReady) {
+            this._onTempFileReady();
+            this._onTempFileReady = null;
+        }
     },
 
     __proto__: WebInspector.ProfileHeader.prototype
 }
 
 /**
+ * @return {!WebInspector.FlameChart.ColorGenerator}
+ */
+WebInspector.CPUProfileView.colorGenerator = function()
+{
+    if (!WebInspector.CPUProfileView._colorGenerator) {
+        var colorGenerator = new WebInspector.FlameChart.ColorGenerator();
+        colorGenerator.colorPairForID("(idle)::0", 50);
+        colorGenerator.colorPairForID("(program)::0", 50);
+        colorGenerator.colorPairForID("(garbage collector)::0", 50);
+        WebInspector.CPUProfileView._colorGenerator = colorGenerator;
+    }
+    return WebInspector.CPUProfileView._colorGenerator;
+}
+
+/**
  * @constructor
  * @implements {WebInspector.FlameChartDataProvider}
  */
@@ -1037,23 +1075,30 @@ WebInspector.CPUFlameChartDataProvider = function(cpuProfileView)
 {
     WebInspector.FlameChartDataProvider.call(this);
     this._cpuProfileView = cpuProfileView;
+    this._colorGenerator = WebInspector.CPUProfileView.colorGenerator();
 }
 
 WebInspector.CPUFlameChartDataProvider.prototype = {
     /**
-     * @param {!WebInspector.FlameChart.ColorGenerator} colorGenerator
-     * @return {!Object}
+     * @return {?WebInspector.FlameChart.TimelineData}
+     */
+    timelineData: function()
+    {
+        return this._timelineData || this._calculateTimelineData();
+    },
+
+    /**
+     * @return {!WebInspector.FlameChart.ColorGenerator}
      */
-    timelineData: function(colorGenerator)
+    colorGenerator: function()
     {
-        return this._timelineData || this._calculateTimelineData(colorGenerator);
+        return this._colorGenerator;
     },
 
     /**
-     * @param {!WebInspector.FlameChart.ColorGenerator} colorGenerator
-     * @return {?Object}
+     * @return {?WebInspector.FlameChart.TimelineData}
      */
-    _calculateTimelineData: function(colorGenerator)
+    _calculateTimelineData: function()
     {
         if (!this._cpuProfileView.profileHead)
             return null;
@@ -1132,8 +1177,9 @@ WebInspector.CPUFlameChartDataProvider.prototype = {
                 continue;
             }
 
+            var colorGenerator = this._colorGenerator;
             while (node) {
-                var colorPair = colorGenerator._colorPairForID(node.functionName + ":" + node.url + ":" + node.lineNumber);
+                var colorPair = colorGenerator.colorPairForID(node.functionName + ":" + node.url + ":" + node.lineNumber);
                 var indexesForColor = colorEntryIndexes[colorPair.index];
                 if (!indexesForColor)
                     indexesForColor = colorEntryIndexes[colorPair.index] = [];
@@ -1186,7 +1232,7 @@ WebInspector.CPUFlameChartDataProvider.prototype = {
             entryDeoptFlags: entryDeoptFlags
         };
 
-        return this._timelineData;
+        return /** @type {!WebInspector.FlameChart.TimelineData} */ (this._timelineData);
     },
 
     /**