1/*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2010 Google Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
16 *     its contributors may be used to endorse or promote products derived
17 *     from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "CommandLineAPIHost.h"
33
34#if ENABLE(INSPECTOR)
35
36#include "Element.h"
37#include "Frame.h"
38#include "FrameLoader.h"
39#include "HTMLFrameOwnerElement.h"
40#include "InspectorClient.h"
41#include "InspectorDOMAgent.h"
42#include "InspectorDOMStorageAgent.h"
43#include "InspectorDatabaseAgent.h"
44#include "InspectorWebFrontendDispatchers.h"
45#include "Pasteboard.h"
46#include "Storage.h"
47#include "markup.h"
48#include <bindings/ScriptValue.h>
49#include <inspector/InspectorValues.h>
50#include <inspector/agents/InspectorAgent.h>
51#include <inspector/agents/InspectorConsoleAgent.h>
52#include <wtf/RefPtr.h>
53#include <wtf/StdLibExtras.h>
54
55#if ENABLE(SQL_DATABASE)
56#include "Database.h"
57#endif
58
59using namespace Inspector;
60
61namespace WebCore {
62
63PassRefPtr<CommandLineAPIHost> CommandLineAPIHost::create()
64{
65    return adoptRef(new CommandLineAPIHost);
66}
67
68CommandLineAPIHost::CommandLineAPIHost()
69    : m_inspectorAgent(nullptr)
70    , m_consoleAgent(nullptr)
71    , m_domAgent(nullptr)
72    , m_domStorageAgent(nullptr)
73#if ENABLE(SQL_DATABASE)
74    , m_databaseAgent(nullptr)
75#endif
76{
77    m_defaultInspectableObject = std::make_unique<InspectableObject>();
78}
79
80CommandLineAPIHost::~CommandLineAPIHost()
81{
82}
83
84void CommandLineAPIHost::disconnect()
85{
86    m_inspectorAgent = nullptr;
87    m_consoleAgent = nullptr;
88    m_domAgent = nullptr;
89    m_domStorageAgent = nullptr;
90#if ENABLE(SQL_DATABASE)
91    m_databaseAgent = nullptr;
92#endif
93}
94
95void CommandLineAPIHost::inspectImpl(PassRefPtr<InspectorValue> object, PassRefPtr<InspectorValue> hints)
96{
97    if (m_inspectorAgent) {
98        RefPtr<Inspector::TypeBuilder::Runtime::RemoteObject> remoteObject = Inspector::TypeBuilder::Runtime::RemoteObject::runtimeCast(object);
99        m_inspectorAgent->inspect(remoteObject, hints->asObject());
100    }
101}
102
103void CommandLineAPIHost::getEventListenersImpl(Node* node, Vector<EventListenerInfo>& listenersArray)
104{
105    if (m_domAgent)
106        m_domAgent->getEventListeners(node, listenersArray, false);
107}
108
109void CommandLineAPIHost::clearConsoleMessages()
110{
111    if (m_consoleAgent) {
112        ErrorString error;
113        m_consoleAgent->clearMessages(&error);
114    }
115}
116
117void CommandLineAPIHost::copyText(const String& text)
118{
119    Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
120}
121
122Deprecated::ScriptValue CommandLineAPIHost::InspectableObject::get(JSC::ExecState*)
123{
124    return Deprecated::ScriptValue();
125};
126
127void CommandLineAPIHost::addInspectedObject(std::unique_ptr<CommandLineAPIHost::InspectableObject> object)
128{
129    m_inspectedObjects.insert(0, WTF::move(object));
130    while (m_inspectedObjects.size() > 5)
131        m_inspectedObjects.removeLast();
132}
133
134void CommandLineAPIHost::clearInspectedObjects()
135{
136    m_inspectedObjects.clear();
137}
138
139CommandLineAPIHost::InspectableObject* CommandLineAPIHost::inspectedObject(unsigned index)
140{
141    if (index >= m_inspectedObjects.size())
142        return m_defaultInspectableObject.get();
143
144    return m_inspectedObjects[index].get();
145}
146
147#if ENABLE(SQL_DATABASE)
148String CommandLineAPIHost::databaseIdImpl(Database* database)
149{
150    if (m_databaseAgent)
151        return m_databaseAgent->databaseId(database);
152    return String();
153}
154#endif
155
156String CommandLineAPIHost::storageIdImpl(Storage* storage)
157{
158    if (m_domStorageAgent)
159        return m_domStorageAgent->storageId(storage);
160    return String();
161}
162
163} // namespace WebCore
164
165#endif // ENABLE(INSPECTOR)
166