1/* 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com> 4 * Copyright (C) 2010-2011 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 are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following disclaimer 14 * in the documentation and/or other materials provided with the 15 * distribution. 16 * * Neither the name of Google Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include "config.h" 34 35#if ENABLE(INSPECTOR) 36 37#include "JSCommandLineAPIHost.h" 38 39#include "CommandLineAPIHost.h" 40#include "InspectorDOMAgent.h" 41#include "JSEventListener.h" 42#include "JSNode.h" 43#include "JSStorage.h" 44#include "Storage.h" 45#include <bindings/ScriptValue.h> 46#include <inspector/InspectorValues.h> 47#include <parser/SourceCode.h> 48#include <runtime/IdentifierInlines.h> 49#include <runtime/JSArray.h> 50#include <runtime/JSFunction.h> 51#include <runtime/JSLock.h> 52#include <runtime/ObjectConstructor.h> 53 54#if ENABLE(SQL_DATABASE) 55#include "Database.h" 56#include "JSDatabase.h" 57#endif 58 59using namespace JSC; 60 61namespace WebCore { 62 63JSValue JSCommandLineAPIHost::inspectedObject(ExecState* exec) 64{ 65 if (exec->argumentCount() < 1) 66 return jsUndefined(); 67 68 CommandLineAPIHost::InspectableObject* object = impl().inspectedObject(exec->uncheckedArgument(0).toInt32(exec)); 69 if (!object) 70 return jsUndefined(); 71 72 JSLockHolder lock(exec); 73 Deprecated::ScriptValue scriptValue = object->get(exec); 74 if (scriptValue.hasNoValue()) 75 return jsUndefined(); 76 77 return scriptValue.jsValue(); 78} 79 80static JSArray* getJSListenerFunctions(ExecState* exec, Document* document, const EventListenerInfo& listenerInfo) 81{ 82 JSArray* result = constructEmptyArray(exec, nullptr); 83 size_t handlersCount = listenerInfo.eventListenerVector.size(); 84 for (size_t i = 0, outputIndex = 0; i < handlersCount; ++i) { 85 const JSEventListener* jsListener = JSEventListener::cast(listenerInfo.eventListenerVector[i].listener.get()); 86 if (!jsListener) { 87 ASSERT_NOT_REACHED(); 88 continue; 89 } 90 91 // Hide listeners from other contexts. 92 if (&jsListener->isolatedWorld() != ¤tWorld(exec)) 93 continue; 94 95 JSObject* function = jsListener->jsFunction(document); 96 if (!function) 97 continue; 98 99 JSObject* listenerEntry = constructEmptyObject(exec); 100 listenerEntry->putDirect(exec->vm(), Identifier(exec, "listener"), function); 101 listenerEntry->putDirect(exec->vm(), Identifier(exec, "useCapture"), jsBoolean(listenerInfo.eventListenerVector[i].useCapture)); 102 result->putDirectIndex(exec, outputIndex++, JSValue(listenerEntry)); 103 } 104 return result; 105} 106 107JSValue JSCommandLineAPIHost::getEventListeners(ExecState* exec) 108{ 109 if (exec->argumentCount() < 1) 110 return jsUndefined(); 111 112 JSValue value = exec->uncheckedArgument(0); 113 if (!value.isObject() || value.isNull()) 114 return jsUndefined(); 115 116 Node* node = toNode(value); 117 if (!node) 118 return jsUndefined(); 119 120 Vector<EventListenerInfo> listenersArray; 121 impl().getEventListenersImpl(node, listenersArray); 122 123 JSObject* result = constructEmptyObject(exec); 124 for (size_t i = 0; i < listenersArray.size(); ++i) { 125 JSArray* listeners = getJSListenerFunctions(exec, &node->document(), listenersArray[i]); 126 if (!listeners->length()) 127 continue; 128 AtomicString eventType = listenersArray[i].eventType; 129 result->putDirect(exec->vm(), Identifier(exec, eventType.impl()), JSValue(listeners)); 130 } 131 132 return result; 133} 134 135JSValue JSCommandLineAPIHost::inspect(ExecState* exec) 136{ 137 if (exec->argumentCount() >= 2) { 138 Deprecated::ScriptValue object(exec->vm(), exec->uncheckedArgument(0)); 139 Deprecated::ScriptValue hints(exec->vm(), exec->uncheckedArgument(1)); 140 impl().inspectImpl(object.toInspectorValue(exec), hints.toInspectorValue(exec)); 141 } 142 143 return jsUndefined(); 144} 145 146JSValue JSCommandLineAPIHost::databaseId(ExecState* exec) 147{ 148 if (exec->argumentCount() < 1) 149 return jsUndefined(); 150 151#if ENABLE(SQL_DATABASE) 152 Database* database = toDatabase(exec->uncheckedArgument(0)); 153 if (database) 154 return jsStringWithCache(exec, impl().databaseIdImpl(database)); 155#endif 156 157 return jsUndefined(); 158} 159 160JSValue JSCommandLineAPIHost::storageId(ExecState* exec) 161{ 162 if (exec->argumentCount() < 1) 163 return jsUndefined(); 164 165 Storage* storage = toStorage(exec->uncheckedArgument(0)); 166 if (storage) 167 return jsStringWithCache(exec, impl().storageIdImpl(storage)); 168 169 return jsUndefined(); 170} 171 172} // namespace WebCore 173 174#endif // ENABLE(INSPECTOR) 175