95f82261daf1e7baaab9426fc0268f65aa2e28ba
[WebKit-https.git] / Source / WebCore / bindings / v8 / custom / V8InspectorFrontendHostCustom.cpp
1 /*
2  * Copyright (C) 2007-2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #if ENABLE(INSPECTOR)
33 #include "V8InspectorFrontendHost.h"
34
35 #include "HistogramSupport.h"
36 #include "InspectorController.h"
37 #include "InspectorFrontendClient.h"
38 #include "InspectorFrontendHost.h"
39 #include "PlatformString.h"
40
41 #include "V8Binding.h"
42 #include "V8MouseEvent.h"
43
44 namespace WebCore {
45
46 v8::Handle<v8::Value> V8InspectorFrontendHost::platformCallback(const v8::Arguments& args)
47 {
48 #if defined(OS_MACOSX)
49     return v8String("mac", args.GetIsolate());
50 #elif defined(OS_LINUX)
51     return v8String("linux", args.GetIsolate());
52 #elif defined(OS_FREEBSD)
53     return v8String("freebsd", args.GetIsolate());
54 #elif defined(OS_OPENBSD)
55     return v8String("openbsd", args.GetIsolate());
56 #elif defined(OS_SOLARIS)
57     return v8String("solaris", args.GetIsolate());
58 #elif defined(OS_WIN)
59     return v8String("windows", args.GetIsolate());
60 #else
61     return v8String("unknown", args.GetIsolate());
62 #endif
63 }
64
65 v8::Handle<v8::Value> V8InspectorFrontendHost::portCallback(const v8::Arguments&)
66 {
67     return v8::Undefined();
68 }
69
70 static void populateContextMenuItems(v8::Local<v8::Array>& itemArray, ContextMenu& menu)
71 {
72     for (size_t i = 0; i < itemArray->Length(); ++i) {
73         v8::Local<v8::Object> item = v8::Local<v8::Object>::Cast(itemArray->Get(i));
74         v8::Local<v8::Value> type = item->Get(v8::String::New("type"));
75         v8::Local<v8::Value> id = item->Get(v8::String::New("id"));
76         v8::Local<v8::Value> label = item->Get(v8::String::New("label"));
77         v8::Local<v8::Value> enabled = item->Get(v8::String::New("enabled"));
78         v8::Local<v8::Value> checked = item->Get(v8::String::New("checked"));
79         v8::Local<v8::Value> subItems = item->Get(v8::String::New("subItems"));
80         if (!type->IsString())
81             continue;
82         String typeString = toWebCoreStringWithNullCheck(type);
83         if (typeString == "separator") {
84             ContextMenuItem item(ContextMenuItem(SeparatorType,
85                                  ContextMenuItemCustomTagNoAction,
86                                  String()));
87             menu.appendItem(item);
88         } else if (typeString == "subMenu" && subItems->IsArray()) {
89             ContextMenu subMenu;
90             v8::Local<v8::Array> subItemsArray = v8::Local<v8::Array>::Cast(subItems);
91             populateContextMenuItems(subItemsArray, subMenu);
92             ContextMenuItem item(SubmenuType,
93                                  ContextMenuItemCustomTagNoAction,
94                                  toWebCoreStringWithNullCheck(label),
95                                  &subMenu);
96             menu.appendItem(item);
97         } else {
98             ContextMenuAction typedId = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + id->ToInt32()->Value());
99             ContextMenuItem menuItem((typeString == "checkbox" ? CheckableActionType : ActionType), typedId, toWebCoreStringWithNullCheck(label));
100             if (checked->IsBoolean())
101                 menuItem.setChecked(checked->ToBoolean()->Value());
102             if (enabled->IsBoolean())
103                 menuItem.setEnabled(enabled->ToBoolean()->Value());
104             menu.appendItem(menuItem);
105         }
106     }
107 }
108
109 v8::Handle<v8::Value> V8InspectorFrontendHost::showContextMenuCallback(const v8::Arguments& args)
110 {
111     if (args.Length() < 2)
112         return v8::Undefined();
113
114     v8::Local<v8::Object> eventWrapper = v8::Local<v8::Object>::Cast(args[0]);
115     if (!V8MouseEvent::info.equals(V8DOMWrapper::domWrapperType(eventWrapper)))
116         return v8::Undefined();
117
118     Event* event = V8Event::toNative(eventWrapper);
119     if (!args[1]->IsArray())
120         return v8::Undefined();
121
122     v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(args[1]);
123     ContextMenu menu;
124     populateContextMenuItems(array, menu);
125
126     InspectorFrontendHost* frontendHost = V8InspectorFrontendHost::toNative(args.Holder());
127 #if !USE(CROSS_PLATFORM_CONTEXT_MENUS)
128     Vector<ContextMenuItem> items = contextMenuItemVector(menu.platformDescription());
129 #else
130     Vector<ContextMenuItem> items = menu.items();
131 #endif
132     frontendHost->showContextMenu(event, items);
133
134     return v8::Undefined();
135 }
136
137 static v8::Handle<v8::Value> histogramEnumeration(const char* name, const v8::Arguments& args, int boundaryValue)
138 {
139     if (args.Length() < 1 || !args[0]->IsInt32())
140         return v8::Undefined();
141
142     int sample = args[0]->ToInt32()->Value();
143     if (sample < boundaryValue)
144         HistogramSupport::histogramEnumeration(name, sample, boundaryValue);
145
146     return v8::Undefined();
147 }
148
149 v8::Handle<v8::Value> V8InspectorFrontendHost::recordActionTakenCallback(const v8::Arguments& args)
150 {
151     return histogramEnumeration("DevTools.ActionTaken", args, 100);
152 }
153
154 v8::Handle<v8::Value> V8InspectorFrontendHost::recordPanelShownCallback(const v8::Arguments& args)
155 {
156     return histogramEnumeration("DevTools.PanelShown", args, 20);
157 }
158
159 v8::Handle<v8::Value> V8InspectorFrontendHost::recordSettingChangedCallback(const v8::Arguments& args)
160 {
161     return histogramEnumeration("DevTools.SettingChanged", args, 100);
162 }
163
164 } // namespace WebCore
165
166 #endif // ENABLE(INSPECTOR)