89e772a29bf534646b8ba15ab7a1d0279990162d
[WebKit-https.git] / Source / WebCore / bindings / js / JSInspectorFrontendHostCustom.cpp
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4  * Copyright (C) 2009 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 "JSInspectorFrontendHost.h"
38
39 #include "ContextMenuItem.h"
40 #include "InspectorController.h"
41 #include "InspectorFrontendHost.h"
42 #include "JSEvent.h"
43 #include "MouseEvent.h"
44 #include "PlatformString.h"
45 #include <runtime/JSArray.h>
46 #include <runtime/JSLock.h>
47 #include <runtime/JSObject.h>
48 #include <wtf/Vector.h>
49
50 using namespace JSC;
51
52 namespace WebCore {
53
54 JSValue JSInspectorFrontendHost::platform(ExecState* execState)
55 {
56 #if PLATFORM(MAC)
57     DEFINE_STATIC_LOCAL(const String, platform, (ASCIILiteral("mac")));
58 #elif OS(WINDOWS)
59     DEFINE_STATIC_LOCAL(const String, platform, (ASCIILiteral("windows")));
60 #elif OS(LINUX)
61     DEFINE_STATIC_LOCAL(const String, platform, (ASCIILiteral("linux")));
62 #elif OS(FREEBSD)
63     DEFINE_STATIC_LOCAL(const String, platform, (ASCIILiteral("freebsd")));
64 #elif OS(OPENBSD)
65     DEFINE_STATIC_LOCAL(const String, platform, (ASCIILiteral("openbsd")));
66 #elif OS(SOLARIS)
67     DEFINE_STATIC_LOCAL(const String, platform, (ASCIILiteral("solaris")));
68 #else
69     DEFINE_STATIC_LOCAL(const String, platform, (ASCIILiteral("unknown")));
70 #endif
71     return jsStringWithCache(execState, platform);
72 }
73
74 JSValue JSInspectorFrontendHost::port(ExecState* execState)
75 {
76 #if PLATFORM(QT)
77     DEFINE_STATIC_LOCAL(const String, port, (ASCIILiteral("qt")));
78 #elif PLATFORM(GTK)
79     DEFINE_STATIC_LOCAL(const String, port, (ASCIILiteral("gtk")));
80 #elif PLATFORM(WX)
81     DEFINE_STATIC_LOCAL(const String, port, (ASCIILiteral("wx")));
82 #else
83     DEFINE_STATIC_LOCAL(const String, port, (ASCIILiteral("unknown")));
84 #endif
85     return jsStringWithCache(execState, port);
86 }
87
88 #if ENABLE(CONTEXT_MENUS)
89 static void populateContextMenuItems(ExecState* exec, JSArray* array, ContextMenu& menu)
90 {
91     for (size_t i = 0; i < array->length(); ++i) {
92         JSObject* item = asObject(array->getIndex(i));
93         JSValue label = item->get(exec, Identifier(exec, "label"));
94         JSValue type = item->get(exec, Identifier(exec, "type"));
95         JSValue id = item->get(exec, Identifier(exec, "id"));
96         JSValue enabled = item->get(exec, Identifier(exec, "enabled"));
97         JSValue checked = item->get(exec, Identifier(exec, "checked"));
98         JSValue subItems = item->get(exec, Identifier(exec, "subItems"));
99         if (!type.isString())
100             continue;
101
102         String typeString = type.toString(exec)->value(exec);
103         if (typeString == "separator") {
104             ContextMenuItem item(SeparatorType,
105                                  ContextMenuItemCustomTagNoAction,
106                                  String());
107             menu.appendItem(item);
108         } else if (typeString == "subMenu" && subItems.inherits(&JSArray::s_info)) {
109             ContextMenu subMenu;
110             JSArray* subItemsArray = asArray(subItems);
111             populateContextMenuItems(exec, subItemsArray, subMenu);
112             ContextMenuItem item(SubmenuType,
113                                  ContextMenuItemCustomTagNoAction,
114                                  label.toString(exec)->value(exec),
115                                  &subMenu);
116             menu.appendItem(item);
117         } else {
118             ContextMenuAction typedId = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + id.toInt32(exec));
119             ContextMenuItem menuItem((typeString == "checkbox" ? CheckableActionType : ActionType), typedId, label.toString(exec)->value(exec));
120             if (!enabled.isUndefined())
121                 menuItem.setEnabled(enabled.toBoolean(exec));
122             if (!checked.isUndefined())
123                 menuItem.setChecked(checked.toBoolean(exec));
124             menu.appendItem(menuItem);
125         }
126     }
127 }
128 #endif
129
130 JSValue JSInspectorFrontendHost::showContextMenu(ExecState* exec)
131 {
132 #if ENABLE(CONTEXT_MENUS)
133     if (exec->argumentCount() < 2)
134         return jsUndefined();
135     Event* event = toEvent(exec->argument(0));
136
137     JSArray* array = asArray(exec->argument(1));
138     ContextMenu menu;
139     populateContextMenuItems(exec, array, menu);
140
141 #if !USE(CROSS_PLATFORM_CONTEXT_MENUS)
142     Vector<ContextMenuItem> items = contextMenuItemVector(menu.platformDescription());
143 #else
144     Vector<ContextMenuItem> items = menu.items();
145 #endif
146     impl()->showContextMenu(event, items);
147 #else
148     UNUSED_PARAM(exec);
149 #endif
150     return jsUndefined();
151 }
152
153 JSValue JSInspectorFrontendHost::recordActionTaken(ExecState*)
154 {
155     return jsUndefined();
156 }
157
158 JSValue JSInspectorFrontendHost::recordPanelShown(ExecState*)
159 {
160     return jsUndefined();
161 }
162
163 JSValue JSInspectorFrontendHost::recordSettingChanged(ExecState*)
164 {
165     return jsUndefined();
166 }
167
168 } // namespace WebCore
169
170 #endif // ENABLE(INSPECTOR)