JSInspectorFrontendHostCustom.cpp [plain text]
#include "config.h"
#include "JSInspectorFrontendHost.h"
#include "ContextMenuItem.h"
#include "InspectorController.h"
#include "InspectorFrontendHost.h"
#include "JSDOMBinding.h"
#include "JSEvent.h"
#include "MouseEvent.h"
#include <runtime/JSArray.h>
#include <runtime/JSLock.h>
#include <runtime/JSObject.h>
#include <wtf/text/WTFString.h>
using namespace JSC;
namespace WebCore {
#if ENABLE(CONTEXT_MENUS)
static void populateContextMenuItems(ExecState* exec, JSArray* array, ContextMenu& menu)
{
VM& vm = exec->vm();
for (size_t i = 0; i < array->length(); ++i) {
JSObject* item = asObject(array->getIndex(exec, i));
JSValue label = item->get(exec, Identifier::fromString(exec, "label"));
JSValue type = item->get(exec, Identifier::fromString(exec, "type"));
JSValue id = item->get(exec, Identifier::fromString(exec, "id"));
JSValue enabled = item->get(exec, Identifier::fromString(exec, "enabled"));
JSValue checked = item->get(exec, Identifier::fromString(exec, "checked"));
JSValue subItems = item->get(exec, Identifier::fromString(exec, "subItems"));
if (!type.isString())
continue;
String typeString = asString(type)->value(exec);
if (typeString == "separator") {
ContextMenuItem item(SeparatorType, ContextMenuItemTagNoAction, String());
menu.appendItem(item);
} else if (typeString == "subMenu" && subItems.inherits(vm, JSArray::info())) {
ContextMenu subMenu;
JSArray* subItemsArray = asArray(subItems);
populateContextMenuItems(exec, subItemsArray, subMenu);
ContextMenuItem item(SubmenuType, ContextMenuItemTagNoAction, label.toWTFString(exec), &subMenu);
menu.appendItem(item);
} else {
ContextMenuAction typedId = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + id.toInt32(exec));
ContextMenuItem menuItem((typeString == "checkbox" ? CheckableActionType : ActionType), typedId, label.toWTFString(exec));
if (!enabled.isUndefined())
menuItem.setEnabled(enabled.toBoolean(exec));
if (!checked.isUndefined())
menuItem.setChecked(checked.toBoolean(exec));
menu.appendItem(menuItem);
}
}
}
#endif
JSValue JSInspectorFrontendHost::showContextMenu(ExecState& state)
{
#if ENABLE(CONTEXT_MENUS)
if (state.argumentCount() < 2)
return jsUndefined();
VM& vm = state.vm();
Event* event = JSEvent::toWrapped(vm, state.argument(0));
JSArray* array = asArray(state.argument(1));
ContextMenu menu;
populateContextMenuItems(&state, array, menu);
wrapped().showContextMenu(event, menu.items());
#else
UNUSED_PARAM(state);
#endif
return jsUndefined();
}
}