1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2015 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 "PageConsoleAgent.h"
34
35#include "CommandLineAPIHost.h"
36#include "InspectorDOMAgent.h"
37#include "InstrumentingAgents.h"
38#include "Logging.h"
39#include "Node.h"
40#include "Page.h"
41#include "WebInjectedScriptManager.h"
42#include <JavaScriptCore/ConsoleMessage.h>
43
44namespace WebCore {
45
46using namespace Inspector;
47
48PageConsoleAgent::PageConsoleAgent(PageAgentContext& context)
49 : WebConsoleAgent(context)
50 , m_instrumentingAgents(context.instrumentingAgents)
51 , m_inspectedPage(context.inspectedPage)
52{
53}
54
55void PageConsoleAgent::clearMessages(ErrorString& errorString)
56{
57 if (auto* domAgent = m_instrumentingAgents.inspectorDOMAgent())
58 domAgent->releaseDanglingNodes();
59
60 WebConsoleAgent::clearMessages(errorString);
61}
62
63void PageConsoleAgent::getLoggingChannels(ErrorString&, RefPtr<JSON::ArrayOf<Inspector::Protocol::Console::Channel>>& channels)
64{
65 static const struct ChannelTable {
66 NeverDestroyed<String> name;
67 Inspector::Protocol::Console::ChannelSource source;
68 } channelTable[] = {
69 { MAKE_STATIC_STRING_IMPL("WebRTC"), Inspector::Protocol::Console::ChannelSource::WebRTC },
70 { MAKE_STATIC_STRING_IMPL("Media"), Inspector::Protocol::Console::ChannelSource::Media },
71 { MAKE_STATIC_STRING_IMPL("MediaSource"), Inspector::Protocol::Console::ChannelSource::MediaSource },
72 };
73
74 channels = JSON::ArrayOf<Inspector::Protocol::Console::Channel>::create();
75
76 size_t length = WTF_ARRAY_LENGTH(channelTable);
77 for (size_t i = 0; i < length; ++i) {
78 auto* logChannel = getLogChannel(channelTable[i].name);
79 if (!logChannel)
80 return;
81
82 auto level = Inspector::Protocol::Console::ChannelLevel::Off;
83 if (logChannel->state != WTFLogChannelState::Off) {
84 switch (logChannel->level) {
85 case WTFLogLevel::Always:
86 case WTFLogLevel::Error:
87 case WTFLogLevel::Warning:
88 case WTFLogLevel::Info:
89 level = Inspector::Protocol::Console::ChannelLevel::Basic;
90 break;
91 case WTFLogLevel::Debug:
92 level = Inspector::Protocol::Console::ChannelLevel::Verbose;
93 break;
94 }
95 }
96
97 auto channel = Inspector::Protocol::Console::Channel::create()
98 .setSource(channelTable[i].source)
99 .setLevel(level)
100 .release();
101 channels->addItem(WTFMove(channel));
102 }
103}
104
105static Optional<std::pair<WTFLogChannelState, WTFLogLevel>> channelConfigurationForString(const String& levelString)
106{
107 if (equalIgnoringASCIICase(levelString, "off"))
108 return { { WTFLogChannelState::Off, WTFLogLevel::Error } };
109
110 if (equalIgnoringASCIICase(levelString, "basic"))
111 return { { WTFLogChannelState::On, WTFLogLevel::Info } };
112
113 if (equalIgnoringASCIICase(levelString, "verbose"))
114 return { { WTFLogChannelState::On, WTFLogLevel::Debug } };
115
116 return WTF::nullopt;
117}
118
119void PageConsoleAgent::setLoggingChannelLevel(ErrorString& errorString, const String& channelName, const String& channelLevel)
120{
121 auto configuration = channelConfigurationForString(channelLevel);
122 if (!configuration) {
123 errorString = "Invalid logging level"_s;
124 return;
125 }
126
127 m_inspectedPage.configureLoggingChannel(channelName, configuration.value().first, configuration.value().second);
128}
129
130} // namespace WebCore
131