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 #include "bindings/core/v8/V8InspectorFrontendHost.h" 33 34 #include "bindings/core/v8/V8MouseEvent.h" 35 #include "bindings/v8/V8Binding.h" 36 #include "core/inspector/InspectorController.h" 37 #include "core/inspector/InspectorFrontendClient.h" 38 #include "core/inspector/InspectorFrontendHost.h" 39 #include "platform/ContextMenu.h" 40 #include "public/platform/Platform.h" 41 #include "wtf/text/WTFString.h" 42 43 namespace WebCore { 44 45 void V8InspectorFrontendHost::platformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info) 46 { 47 #if OS(MACOSX) 48 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "mac")); 49 #elif OS(WIN) 50 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "windows")); 51 #else // Unix-like systems 52 v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "linux")); 53 #endif 54 } 55 56 void V8InspectorFrontendHost::portMethodCustom(const v8::FunctionCallbackInfo<v8::Value>&) 57 { 58 } 59 60 static bool populateContextMenuItems(v8::Local<v8::Array>& itemArray, ContextMenu& menu, v8::Isolate* isolate) 61 { 62 for (size_t i = 0; i < itemArray->Length(); ++i) { 63 v8::Local<v8::Object> item = v8::Local<v8::Object>::Cast(itemArray->Get(i)); 64 v8::Local<v8::Value> type = item->Get(v8AtomicString(isolate, "type")); 65 v8::Local<v8::Value> id = item->Get(v8AtomicString(isolate, "id")); 66 v8::Local<v8::Value> label = item->Get(v8AtomicString(isolate, "label")); 67 v8::Local<v8::Value> enabled = item->Get(v8AtomicString(isolate, "enabled")); 68 v8::Local<v8::Value> checked = item->Get(v8AtomicString(isolate, "checked")); 69 v8::Local<v8::Value> subItems = item->Get(v8AtomicString(isolate, "subItems")); 70 if (!type->IsString()) 71 continue; 72 String typeString = toCoreStringWithNullCheck(type.As<v8::String>()); 73 if (typeString == "separator") { 74 ContextMenuItem item(ContextMenuItem(SeparatorType, 75 ContextMenuItemCustomTagNoAction, 76 String())); 77 menu.appendItem(item); 78 } else if (typeString == "subMenu" && subItems->IsArray()) { 79 ContextMenu subMenu; 80 v8::Local<v8::Array> subItemsArray = v8::Local<v8::Array>::Cast(subItems); 81 if (!populateContextMenuItems(subItemsArray, subMenu, isolate)) 82 return false; 83 TOSTRING_DEFAULT(V8StringResource<WithNullCheck>, labelString, label, false); 84 ContextMenuItem item(SubmenuType, 85 ContextMenuItemCustomTagNoAction, 86 labelString, 87 &subMenu); 88 menu.appendItem(item); 89 } else { 90 ContextMenuAction typedId = static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + id->ToInt32()->Value()); 91 TOSTRING_DEFAULT(V8StringResource<WithNullCheck>, labelString, label, false); 92 ContextMenuItem menuItem((typeString == "checkbox" ? CheckableActionType : ActionType), typedId, labelString); 93 if (checked->IsBoolean()) 94 menuItem.setChecked(checked->ToBoolean()->Value()); 95 if (enabled->IsBoolean()) 96 menuItem.setEnabled(enabled->ToBoolean()->Value()); 97 menu.appendItem(menuItem); 98 } 99 } 100 return true; 101 } 102 103 void V8InspectorFrontendHost::showContextMenuMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info) 104 { 105 if (info.Length() < 2) 106 return; 107 108 v8::Local<v8::Object> eventWrapper = v8::Local<v8::Object>::Cast(info[0]); 109 if (!V8MouseEvent::wrapperTypeInfo.equals(toWrapperTypeInfo(eventWrapper))) 110 return; 111 112 Event* event = V8Event::toNative(eventWrapper); 113 if (!info[1]->IsArray()) 114 return; 115 116 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(info[1]); 117 ContextMenu menu; 118 if (!populateContextMenuItems(array, menu, info.GetIsolate())) 119 return; 120 121 InspectorFrontendHost* frontendHost = V8InspectorFrontendHost::toNative(info.Holder()); 122 Vector<ContextMenuItem> items = menu.items(); 123 frontendHost->showContextMenu(event, items); 124 } 125 126 static void histogramEnumeration(const char* name, const v8::FunctionCallbackInfo<v8::Value>& info, int boundaryValue) 127 { 128 if (info.Length() < 1 || !info[0]->IsInt32()) 129 return; 130 131 int sample = info[0]->ToInt32()->Value(); 132 if (sample < boundaryValue) 133 blink::Platform::current()->histogramEnumeration(name, sample, boundaryValue); 134 } 135 136 void V8InspectorFrontendHost::recordActionTakenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info) 137 { 138 histogramEnumeration("DevTools.ActionTaken", info, 100); 139 } 140 141 void V8InspectorFrontendHost::recordPanelShownMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info) 142 { 143 histogramEnumeration("DevTools.PanelShown", info, 20); 144 } 145 146 } // namespace WebCore 147 148