2 * Copyright (C) 2007-2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "bindings/core/v8/V8InspectorFrontendHost.h"
34 #include "bindings/core/v8/V8Binding.h"
35 #include "bindings/core/v8/V8MouseEvent.h"
36 #include "bindings/core/v8/V8Window.h"
37 #include "core/dom/Document.h"
38 #include "core/frame/LocalDOMWindow.h"
39 #include "core/inspector/InspectorController.h"
40 #include "core/inspector/InspectorFrontendClient.h"
41 #include "core/inspector/InspectorFrontendHost.h"
42 #include "platform/ContextMenu.h"
43 #include "public/platform/Platform.h"
44 #include "wtf/text/WTFString.h"
48 void V8InspectorFrontendHost::platformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
51 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "mac"));
53 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "windows"));
54 #else // Unix-like systems
55 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "linux"));
59 void V8InspectorFrontendHost::portMethodCustom(const v8::FunctionCallbackInfo<v8::Value>&)
63 static bool populateContextMenuItems(const v8::Local<v8::Array>& itemArray, ContextMenu& menu, v8::Isolate* isolate)
65 for (size_t i = 0; i < itemArray->Length(); ++i) {
66 v8::Local<v8::Object> item = v8::Local<v8::Object>::Cast(itemArray->Get(i));
67 v8::Local<v8::Value> type = item->Get(v8AtomicString(isolate, "type"));
68 v8::Local<v8::Value> id = item->Get(v8AtomicString(isolate, "id"));
69 v8::Local<v8::Value> label = item->Get(v8AtomicString(isolate, "label"));
70 v8::Local<v8::Value> enabled = item->Get(v8AtomicString(isolate, "enabled"));
71 v8::Local<v8::Value> checked = item->Get(v8AtomicString(isolate, "checked"));
72 v8::Local<v8::Value> subItems = item->Get(v8AtomicString(isolate, "subItems"));
73 if (!type->IsString())
75 String typeString = toCoreStringWithNullCheck(type.As<v8::String>());
76 if (typeString == "separator") {
77 ContextMenuItem item(ContextMenuItem(SeparatorType,
78 ContextMenuItemCustomTagNoAction,
80 menu.appendItem(item);
81 } else if (typeString == "subMenu" && subItems->IsArray()) {
83 v8::Local<v8::Array> subItemsArray = v8::Local<v8::Array>::Cast(subItems);
84 if (!populateContextMenuItems(subItemsArray, subMenu, isolate))
86 TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, labelString, label, false);
87 ContextMenuItem item(SubmenuType,
88 ContextMenuItemCustomTagNoAction,
91 menu.appendItem(item);
93 ContextMenuAction typedId = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + id->ToInt32()->Value());
94 TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, labelString, label, false);
95 ContextMenuItem menuItem((typeString == "checkbox" ? CheckableActionType : ActionType), typedId, labelString);
96 if (checked->IsBoolean())
97 menuItem.setChecked(checked->ToBoolean()->Value());
98 if (enabled->IsBoolean())
99 menuItem.setEnabled(enabled->ToBoolean()->Value());
100 menu.appendItem(menuItem);
106 void V8InspectorFrontendHost::showContextMenuMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
108 if (info.Length() < 2)
111 v8::Local<v8::Object> eventWrapper = v8::Local<v8::Object>::Cast(info[0]);
112 if (!V8MouseEvent::wrapperTypeInfo.equals(toWrapperTypeInfo(eventWrapper)))
115 Event* event = V8Event::toImpl(eventWrapper);
116 if (!info[1]->IsArray())
119 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(info[1]);
121 if (!populateContextMenuItems(array, menu, info.GetIsolate()))
124 InspectorFrontendHost* frontendHost = V8InspectorFrontendHost::toImpl(info.Holder());
125 Vector<ContextMenuItem> items = menu.items();
126 frontendHost->showContextMenu(event, items);
129 void V8InspectorFrontendHost::showContextMenuAtPointMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
131 if (info.Length() < 3)
134 v8::Local<v8::Value> x = v8::Local<v8::Value>::Cast(info[0]);
138 v8::Local<v8::Value> y = v8::Local<v8::Value>::Cast(info[1]);
142 v8::Local<v8::Value> array = v8::Local<v8::Value>::Cast(info[2]);
143 if (!array->IsArray())
146 if (!populateContextMenuItems(v8::Local<v8::Array>::Cast(array), menu, info.GetIsolate()))
149 v8::Isolate* isolate = info.GetIsolate();
150 v8::Handle<v8::Object> windowWrapper = V8Window::findInstanceInPrototypeChain(isolate->GetEnteredContext()->Global(), isolate);
151 if (windowWrapper.IsEmpty())
153 LocalDOMWindow* window = V8Window::toImpl(windowWrapper);
154 if (!window->document() || !window->document()->page())
157 InspectorFrontendHost* frontendHost = V8InspectorFrontendHost::toImpl(info.Holder());
158 Vector<ContextMenuItem> items = menu.items();
159 frontendHost->showContextMenu(window->document()->page(), static_cast<float>(x->NumberValue()), static_cast<float>(y->NumberValue()), items);
162 static void histogramEnumeration(const char* name, const v8::FunctionCallbackInfo<v8::Value>& info, int boundaryValue)
164 if (info.Length() < 1 || !info[0]->IsInt32())
167 int sample = info[0]->ToInt32()->Value();
168 if (sample < boundaryValue)
169 blink::Platform::current()->histogramEnumeration(name, sample, boundaryValue);
172 void V8InspectorFrontendHost::recordActionTakenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
174 histogramEnumeration("DevTools.ActionTaken", info, 100);
177 void V8InspectorFrontendHost::recordPanelShownMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
179 histogramEnumeration("DevTools.PanelShown", info, 20);