1/*
2 * Copyright (C) 2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "InspectorAuditAgent.h"
28
29#include "InjectedScript.h"
30#include "InjectedScriptManager.h"
31#include "JSLock.h"
32#include "ObjectConstructor.h"
33#include "ScriptDebugServer.h"
34#include <wtf/RefPtr.h>
35#include <wtf/Vector.h>
36#include <wtf/text/StringBuilder.h>
37#include <wtf/text/WTFString.h>
38
39namespace Inspector {
40
41using namespace JSC;
42
43InspectorAuditAgent::InspectorAuditAgent(AgentContext& context)
44 : InspectorAgentBase("Audit"_s)
45 , m_backendDispatcher(AuditBackendDispatcher::create(context.backendDispatcher, this))
46 , m_injectedScriptManager(context.injectedScriptManager)
47 , m_scriptDebugServer(context.environment.scriptDebugServer())
48{
49}
50
51void InspectorAuditAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
52{
53}
54
55void InspectorAuditAgent::willDestroyFrontendAndBackend(DisconnectReason)
56{
57}
58
59void InspectorAuditAgent::setup(ErrorString& errorString, const int* executionContextId)
60{
61 if (hasActiveAudit()) {
62 errorString = "Must call teardown before calling setup again."_s;
63 return;
64 }
65
66 InjectedScript injectedScript = injectedScriptForEval(errorString, executionContextId);
67 if (injectedScript.hasNoValue())
68 return;
69
70 JSC::ExecState* execState = injectedScript.scriptState();
71 if (!execState) {
72 errorString = "Missing execution state for injected script."_s;
73 return;
74 }
75
76 VM& vm = execState->vm();
77
78 JSC::JSLockHolder lock(execState);
79
80 m_injectedWebInspectorAuditValue.set(vm, constructEmptyObject(execState));
81 if (!m_injectedWebInspectorAuditValue) {
82 errorString = "Unable to construct injected WebInspectorAudit object."_s;
83 return;
84 }
85
86 populateAuditObject(execState, m_injectedWebInspectorAuditValue);
87}
88
89void InspectorAuditAgent::run(ErrorString& errorString, const String& test, const int* executionContextId, RefPtr<Protocol::Runtime::RemoteObject>& result, Optional<bool>& wasThrown)
90{
91 InjectedScript injectedScript = injectedScriptForEval(errorString, executionContextId);
92 if (injectedScript.hasNoValue())
93 return;
94
95 StringBuilder functionString;
96 functionString.appendLiteral("(function(WebInspectorAudit) { \"use strict\"; return eval(`(");
97 functionString.append(test.isolatedCopy().replace('`', "\\`"));
98 functionString.appendLiteral(")`)(WebInspectorAudit); })");
99
100 InjectedScript::ExecuteOptions options;
101 options.objectGroup = "audit"_s;
102 if (m_injectedWebInspectorAuditValue)
103 options.args = { m_injectedWebInspectorAuditValue.get() };
104
105 Optional<int> savedResultIndex;
106
107 ScriptDebugServer::PauseOnExceptionsState previousPauseOnExceptionsState = m_scriptDebugServer.pauseOnExceptionsState();
108
109 m_scriptDebugServer.setPauseOnExceptionsState(ScriptDebugServer::DontPauseOnExceptions);
110 muteConsole();
111
112 injectedScript.execute(errorString, functionString.toString(), WTFMove(options), result, wasThrown, savedResultIndex);
113
114 unmuteConsole();
115 m_scriptDebugServer.setPauseOnExceptionsState(previousPauseOnExceptionsState);
116}
117
118void InspectorAuditAgent::teardown(ErrorString& errorString)
119{
120 if (!hasActiveAudit()) {
121 errorString = "Must call setup before calling teardown."_s;
122 return;
123 }
124
125 m_injectedWebInspectorAuditValue.clear();
126}
127
128bool InspectorAuditAgent::hasActiveAudit() const
129{
130 return !!m_injectedWebInspectorAuditValue;
131}
132
133void InspectorAuditAgent::populateAuditObject(JSC::ExecState* execState, JSC::Strong<JSC::JSObject>& auditObject)
134{
135 ASSERT(execState);
136 if (!execState)
137 return;
138
139 JSC::JSLockHolder lock(execState);
140
141 auditObject->putDirect(execState->vm(), JSC::Identifier::fromString(execState, "Version"), JSC::JSValue(Inspector::Protocol::Audit::VERSION));
142}
143
144} // namespace Inspector
145