• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "JSInspectorFrontendHost.h"
35 
36 #if ENABLE(INSPECTOR)
37 
38 #include "ContextMenuItem.h"
39 #include "InspectorController.h"
40 #include "InspectorFrontendHost.h"
41 #include "JSEvent.h"
42 #include "MouseEvent.h"
43 #include "PlatformString.h"
44 #include <runtime/JSArray.h>
45 #include <runtime/JSLock.h>
46 #include <runtime/JSObject.h>
47 #include <wtf/Vector.h>
48 
49 using namespace JSC;
50 
51 namespace WebCore {
52 
platform(ExecState * execState)53 JSValue JSInspectorFrontendHost::platform(ExecState* execState)
54 {
55 #if PLATFORM(MAC)
56     DEFINE_STATIC_LOCAL(const String, platform, ("mac"));
57 #elif OS(WINDOWS)
58     DEFINE_STATIC_LOCAL(const String, platform, ("windows"));
59 #elif OS(LINUX)
60     DEFINE_STATIC_LOCAL(const String, platform, ("linux"));
61 #elif OS(FREEBSD)
62     DEFINE_STATIC_LOCAL(const String, platform, ("freebsd"));
63 #else
64     DEFINE_STATIC_LOCAL(const String, platform, ("unknown"));
65 #endif
66     return jsString(execState, platform);
67 }
68 
port(ExecState * execState)69 JSValue JSInspectorFrontendHost::port(ExecState* execState)
70 {
71 #if PLATFORM(QT)
72     DEFINE_STATIC_LOCAL(const String, port, ("qt"));
73 #elif PLATFORM(GTK)
74     DEFINE_STATIC_LOCAL(const String, port, ("gtk"));
75 #elif PLATFORM(WX)
76     DEFINE_STATIC_LOCAL(const String, port, ("wx"));
77 #else
78     DEFINE_STATIC_LOCAL(const String, port, ("unknown"));
79 #endif
80     return jsString(execState, port);
81 }
82 
showContextMenu(ExecState * exec)83 JSValue JSInspectorFrontendHost::showContextMenu(ExecState* exec)
84 {
85     if (exec->argumentCount() < 2)
86         return jsUndefined();
87 #if ENABLE(CONTEXT_MENUS)
88     Event* event = toEvent(exec->argument(0));
89 
90     JSArray* array = asArray(exec->argument(1));
91     Vector<ContextMenuItem*> items;
92 
93     for (size_t i = 0; i < array->length(); ++i) {
94         JSObject* item = asObject(array->getIndex(i));
95         JSValue label = item->get(exec, Identifier(exec, "label"));
96         JSValue type = item->get(exec, Identifier(exec, "type"));
97         JSValue id = item->get(exec, Identifier(exec, "id"));
98         JSValue enabled = item->get(exec, Identifier(exec, "enabled"));
99         JSValue checked = item->get(exec, Identifier(exec, "checked"));
100         if (!type.isString())
101             continue;
102 
103         String typeString = ustringToString(type.toString(exec));
104         if (typeString == "separator") {
105             items.append(new ContextMenuItem(SeparatorType,
106                                              ContextMenuItemCustomTagNoAction,
107                                              String()));
108         } else {
109             ContextMenuAction typedId = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + id.toInt32(exec));
110             ContextMenuItem* menuItem = new ContextMenuItem((typeString == "checkbox" ? CheckableActionType : ActionType), typedId, ustringToString(label.toString(exec)));
111             if (!enabled.isUndefined())
112                 menuItem->setEnabled(enabled.toBoolean(exec));
113             if (!checked.isUndefined())
114                 menuItem->setChecked(checked.toBoolean(exec));
115             items.append(menuItem);
116         }
117     }
118 
119     impl()->showContextMenu(event, items);
120 #endif
121     return jsUndefined();
122 }
123 
124 } // namespace WebCore
125 
126 #endif // ENABLE(INSPECTOR)
127