Web Inspector: Line error widget showed in the wrong resource
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Controllers / LogManager.js
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  * Copyright (C) 2015 Tobias Reiss <tobi+webkit@basecode.de>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 WebInspector.LogManager = class LogManager extends WebInspector.Object
28 {
29     constructor()
30     {
31         super();
32
33         this._clearMessagesRequested = false;
34         this._isNewPageOrReload = false;
35
36         this.clearLogOnNavigateSetting = new WebInspector.Setting("clear-log-on-navigate", true);
37
38         WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
39     }
40
41     // Public
42
43     messageWasAdded(source, level, text, type, url, line, column, repeatCount, parameters, stackTrace, requestId)
44     {
45         // Called from WebInspector.ConsoleObserver.
46
47         // FIXME: Get a request from request ID.
48
49         if (parameters)
50             parameters = parameters.map(WebInspector.RemoteObject.fromPayload);
51
52         let message = new WebInspector.ConsoleMessage(source, level, text, type, url, line, column, repeatCount, parameters, stackTrace, null);
53
54         this.dispatchEventToListeners(WebInspector.LogManager.Event.MessageAdded, {message});
55
56         if (message.level === "warning" || message.level === "error")
57             WebInspector.issueManager.issueWasAdded(message);
58     }
59
60     messagesCleared()
61     {
62         // Called from WebInspector.ConsoleObserver.
63
64         WebInspector.ConsoleCommandResultMessage.clearMaximumSavedResultIndex();
65
66         if (this._clearMessagesRequested) {
67             // Frontend requested "clear console" and Backend successfully completed the request.
68             this._clearMessagesRequested = false;
69             this.dispatchEventToListeners(WebInspector.LogManager.Event.Cleared);
70         } else {
71             // Received an unrequested clear console event.
72             // This could be for a navigation or other reasons (like console.clear()).
73             // If this was a reload, we may not want to dispatch WebInspector.LogManager.Event.Cleared.
74             // To detect if this is a reload we wait a turn and check if there was a main resource change reload.
75             setTimeout(this._delayedMessagesCleared.bind(this), 0);
76         }
77     }
78
79     _delayedMessagesCleared()
80     {
81         if (this._isNewPageOrReload) {
82             this._isNewPageOrReload = false;
83
84             if (!this.clearLogOnNavigateSetting.value)
85                 return;
86         }
87
88         // A console.clear() or command line clear() happened.
89         this.dispatchEventToListeners(WebInspector.LogManager.Event.Cleared);
90     }
91
92     messageRepeatCountUpdated(count)
93     {
94         // Called from WebInspector.ConsoleObserver.
95
96         this.dispatchEventToListeners(WebInspector.LogManager.Event.PreviousMessageRepeatCountUpdated, {count});
97     }
98
99     requestClearMessages()
100     {
101         this._clearMessagesRequested = true;
102
103         ConsoleAgent.clearMessages();
104     }
105
106     // Private
107
108     _mainResourceDidChange(event)
109     {
110         console.assert(event.target instanceof WebInspector.Frame);
111
112         if (!event.target.isMainFrame())
113             return;
114
115         this._isNewPageOrReload = true;
116
117         if (event.data.oldMainResource.url === event.target.mainResource.url)
118             this.dispatchEventToListeners(WebInspector.LogManager.Event.SessionStarted);
119
120         WebInspector.ConsoleCommandResultMessage.clearMaximumSavedResultIndex();
121     }
122 };
123
124 WebInspector.LogManager.Event = {
125     SessionStarted: "log-manager-session-was-started",
126     Cleared: "log-manager-cleared",
127     MessageAdded: "log-manager-message-added",
128     PreviousMessageRepeatCountUpdated: "log-manager-previous-message-repeat-count-updated"
129 };