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/V8HTMLDocument.h"
36 #include "bindings/core/v8/V8MouseEvent.h"
37 #include "bindings/core/v8/V8Window.h"
38 #include "core/dom/Document.h"
39 #include "core/frame/LocalDOMWindow.h"
40 #include "core/inspector/InspectorController.h"
41 #include "core/inspector/InspectorFrontendClient.h"
42 #include "core/inspector/InspectorFrontendHost.h"
43 #include "platform/ContextMenu.h"
44 #include "public/platform/Platform.h"
45 #include "wtf/text/WTFString.h"
49 void V8InspectorFrontendHost::platformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
52 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "mac"));
54 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "windows"));
55 #else // Unix-like systems
56 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "linux"));
60 void V8InspectorFrontendHost::portMethodCustom(const v8::FunctionCallbackInfo<v8::Value>&)
64 static bool populateContextMenuItems(const v8::Local<v8::Array>& itemArray, ContextMenu& menu, v8::Isolate* isolate)
66 for (size_t i = 0; i < itemArray->Length(); ++i) {
67 v8::Local<v8::Object> item = v8::Local<v8::Object>::Cast(itemArray->Get(i));
68 v8::Local<v8::Value> type = item->Get(v8AtomicString(isolate, "type"));
69 v8::Local<v8::Value> id = item->Get(v8AtomicString(isolate, "id"));
70 v8::Local<v8::Value> label = item->Get(v8AtomicString(isolate, "label"));
71 v8::Local<v8::Value> enabled = item->Get(v8AtomicString(isolate, "enabled"));
72 v8::Local<v8::Value> checked = item->Get(v8AtomicString(isolate, "checked"));
73 v8::Local<v8::Value> subItems = item->Get(v8AtomicString(isolate, "subItems"));
74 if (!type->IsString())
76 String typeString = toCoreStringWithNullCheck(type.As<v8::String>());
77 if (typeString == "separator") {
78 ContextMenuItem item(ContextMenuItem(SeparatorType,
79 ContextMenuItemCustomTagNoAction,
81 menu.appendItem(item);
82 } else if (typeString == "subMenu" && subItems->IsArray()) {
84 v8::Local<v8::Array> subItemsArray = v8::Local<v8::Array>::Cast(subItems);
85 if (!populateContextMenuItems(subItemsArray, subMenu, isolate))
87 TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, labelString, label, false);
88 ContextMenuItem item(SubmenuType,
89 ContextMenuItemCustomTagNoAction,
92 menu.appendItem(item);
94 ContextMenuAction typedId = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + id->ToInt32()->Value());
95 TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, labelString, label, false);
96 ContextMenuItem menuItem((typeString == "checkbox" ? CheckableActionType : ActionType), typedId, labelString);
97 if (checked->IsBoolean())
98 menuItem.setChecked(checked->ToBoolean()->Value());
99 if (enabled->IsBoolean())
100 menuItem.setEnabled(enabled->ToBoolean()->Value());
101 menu.appendItem(menuItem);
107 void V8InspectorFrontendHost::showContextMenuMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
109 if (info.Length() < 2)
112 v8::Local<v8::Object> eventWrapper = v8::Local<v8::Object>::Cast(info[0]);
113 if (!V8MouseEvent::wrapperTypeInfo.equals(toWrapperTypeInfo(eventWrapper)))
116 Event* event = V8Event::toImpl(eventWrapper);
117 if (!info[1]->IsArray())
120 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(info[1]);
122 if (!populateContextMenuItems(array, menu, info.GetIsolate()))
125 InspectorFrontendHost* frontendHost = V8InspectorFrontendHost::toImpl(info.Holder());
126 Vector<ContextMenuItem> items = menu.items();
127 frontendHost->showContextMenu(event, items);
130 void V8InspectorFrontendHost::showContextMenuAtPointMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
132 if (info.Length() < 3)
135 v8::Local<v8::Value> x = v8::Local<v8::Value>::Cast(info[0]);
139 v8::Local<v8::Value> y = v8::Local<v8::Value>::Cast(info[1]);
143 v8::Local<v8::Value> array = v8::Local<v8::Value>::Cast(info[2]);
144 if (!array->IsArray())
147 if (!populateContextMenuItems(v8::Local<v8::Array>::Cast(array), menu, info.GetIsolate()))
150 Document* document = nullptr;
151 if (info.Length() >= 4) {
152 v8::Local<v8::Object> documentWrapper = v8::Local<v8::Object>::Cast(info[3]);
153 if (!V8HTMLDocument::wrapperTypeInfo.equals(toWrapperTypeInfo(documentWrapper)))
155 document = V8HTMLDocument::toImpl(documentWrapper);
157 v8::Isolate* isolate = info.GetIsolate();
158 v8::Handle<v8::Object> windowWrapper = V8Window::findInstanceInPrototypeChain(isolate->GetEnteredContext()->Global(), isolate);
159 if (windowWrapper.IsEmpty())
161 LocalDOMWindow* window = V8Window::toImpl(windowWrapper);
162 document = window ? window->document() : nullptr;
164 if (!document || !document->page())
167 InspectorFrontendHost* frontendHost = V8InspectorFrontendHost::toImpl(info.Holder());
168 Vector<ContextMenuItem> items = menu.items();
169 frontendHost->showContextMenu(document->page(), static_cast<float>(x->NumberValue()), static_cast<float>(y->NumberValue()), items);
172 static void histogramEnumeration(const char* name, const v8::FunctionCallbackInfo<v8::Value>& info, int boundaryValue)
174 if (info.Length() < 1 || !info[0]->IsInt32())
177 int sample = info[0]->ToInt32()->Value();
178 if (sample < boundaryValue)
179 blink::Platform::current()->histogramEnumeration(name, sample, boundaryValue);
182 void V8InspectorFrontendHost::recordActionTakenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
184 histogramEnumeration("DevTools.ActionTaken", info, 100);
187 void V8InspectorFrontendHost::recordPanelShownMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
189 histogramEnumeration("DevTools.PanelShown", info, 20);