NeverDestroyed<String>(ASCIILiteral(...)) is not thread safe.
[WebKit-https.git] / Source / WebCore / inspector / PageDebuggerAgent.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "PageDebuggerAgent.h"
34
35 #include "CachedResource.h"
36 #include "InspectorOverlay.h"
37 #include "InspectorPageAgent.h"
38 #include "InstrumentingAgents.h"
39 #include "MainFrame.h"
40 #include "Page.h"
41 #include "PageConsoleClient.h"
42 #include "PageScriptDebugServer.h"
43 #include "ScriptState.h"
44 #include <inspector/InjectedScript.h>
45 #include <inspector/InjectedScriptManager.h>
46 #include <inspector/ScriptCallStack.h>
47 #include <inspector/ScriptCallStackFactory.h>
48 #include <wtf/NeverDestroyed.h>
49
50 using namespace Inspector;
51
52 namespace WebCore {
53
54 PageDebuggerAgent::PageDebuggerAgent(PageAgentContext& context, InspectorPageAgent* pageAgent, InspectorOverlay* overlay)
55     : WebDebuggerAgent(context)
56     , m_page(context.inspectedPage)
57     , m_pageAgent(pageAgent)
58     , m_overlay(overlay)
59 {
60 }
61
62 void PageDebuggerAgent::enable()
63 {
64     WebDebuggerAgent::enable();
65     m_instrumentingAgents.setPageDebuggerAgent(this);
66 }
67
68 void PageDebuggerAgent::disable(bool isBeingDestroyed)
69 {
70     WebDebuggerAgent::disable(isBeingDestroyed);
71     m_instrumentingAgents.setPageDebuggerAgent(nullptr);
72 }
73
74 String PageDebuggerAgent::sourceMapURLForScript(const Script& script)
75 {
76     static NeverDestroyed<String> sourceMapHTTPHeader(MAKE_STATIC_STRING_IMPL("SourceMap"));
77     static NeverDestroyed<String> sourceMapHTTPHeaderDeprecated(MAKE_STATIC_STRING_IMPL("X-SourceMap"));
78
79     if (!script.url.isEmpty()) {
80         CachedResource* resource = m_pageAgent->cachedResource(&m_page.mainFrame(), URL(ParsedURLString, script.url));
81         if (resource) {
82             String sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeader);
83             if (!sourceMapHeader.isEmpty())
84                 return sourceMapHeader;
85
86             sourceMapHeader = resource->response().httpHeaderField(sourceMapHTTPHeaderDeprecated);
87             if (!sourceMapHeader.isEmpty())
88                 return sourceMapHeader;
89         }
90     }
91
92     return InspectorDebuggerAgent::sourceMapURLForScript(script);
93 }
94
95 void PageDebuggerAgent::muteConsole()
96 {
97     PageConsoleClient::mute();
98 }
99
100 void PageDebuggerAgent::unmuteConsole()
101 {
102     PageConsoleClient::unmute();
103 }
104
105 void PageDebuggerAgent::breakpointActionLog(JSC::ExecState& state, const String& message)
106 {
107     m_pageAgent->page().console().addMessage(MessageSource::JS, MessageLevel::Log, message, createScriptCallStack(&state, ScriptCallStack::maxCallStackSizeToCapture));
108 }
109
110 InjectedScript PageDebuggerAgent::injectedScriptForEval(ErrorString& errorString, const int* executionContextId)
111 {
112     if (!executionContextId) {
113         JSC::ExecState* scriptState = mainWorldExecState(&m_pageAgent->mainFrame());
114         return injectedScriptManager().injectedScriptFor(scriptState);
115     }
116
117     InjectedScript injectedScript = injectedScriptManager().injectedScriptForId(*executionContextId);
118     if (injectedScript.hasNoValue())
119         errorString = ASCIILiteral("Execution context with given id not found.");
120
121     return injectedScript;
122 }
123
124 void PageDebuggerAgent::setOverlayMessage(ErrorString&, const String* message)
125 {
126     m_overlay->setPausedInDebuggerMessage(message);
127 }
128
129 void PageDebuggerAgent::didClearMainFrameWindowObject()
130 {
131     didClearGlobalObject();
132 }
133
134 void PageDebuggerAgent::mainFrameStartedLoading()
135 {
136     if (isPaused()) {
137         setSuppressAllPauses(true);
138         ErrorString unused;
139         resume(unused);
140     }
141 }
142
143 void PageDebuggerAgent::mainFrameStoppedLoading()
144 {
145     setSuppressAllPauses(false);
146 }
147
148 void PageDebuggerAgent::mainFrameNavigated()
149 {
150     setSuppressAllPauses(false);
151 }
152
153 } // namespace WebCore