Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / 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 #include "bindings/core/v8/V8InspectorFrontendHost.h"
33
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"
45
46 namespace blink {
47
48 void V8InspectorFrontendHost::platformMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
49 {
50 #if OS(MACOSX)
51     v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "mac"));
52 #elif OS(WIN)
53     v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "windows"));
54 #else // Unix-like systems
55     v8SetReturnValue(info, v8AtomicString(info.GetIsolate(), "linux"));
56 #endif
57 }
58
59 void V8InspectorFrontendHost::portMethodCustom(const v8::FunctionCallbackInfo<v8::Value>&)
60 {
61 }
62
63 static bool populateContextMenuItems(const v8::Local<v8::Array>& itemArray, ContextMenu& menu, v8::Isolate* isolate)
64 {
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())
74             continue;
75         String typeString = toCoreStringWithNullCheck(type.As<v8::String>());
76         if (typeString == "separator") {
77             ContextMenuItem item(ContextMenuItem(SeparatorType,
78                 ContextMenuItemCustomTagNoAction,
79                 String()));
80             menu.appendItem(item);
81         } else if (typeString == "subMenu" && subItems->IsArray()) {
82             ContextMenu subMenu;
83             v8::Local<v8::Array> subItemsArray = v8::Local<v8::Array>::Cast(subItems);
84             if (!populateContextMenuItems(subItemsArray, subMenu, isolate))
85                 return false;
86             TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, labelString, label, false);
87             ContextMenuItem item(SubmenuType,
88                 ContextMenuItemCustomTagNoAction,
89                 labelString,
90                 &subMenu);
91             menu.appendItem(item);
92         } else {
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);
101         }
102     }
103     return true;
104 }
105
106 void V8InspectorFrontendHost::showContextMenuMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
107 {
108     if (info.Length() < 2)
109         return;
110
111     v8::Local<v8::Object> eventWrapper = v8::Local<v8::Object>::Cast(info[0]);
112     if (!V8MouseEvent::wrapperTypeInfo.equals(toWrapperTypeInfo(eventWrapper)))
113         return;
114
115     Event* event = V8Event::toImpl(eventWrapper);
116     if (!info[1]->IsArray())
117         return;
118
119     v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(info[1]);
120     ContextMenu menu;
121     if (!populateContextMenuItems(array, menu, info.GetIsolate()))
122         return;
123
124     InspectorFrontendHost* frontendHost = V8InspectorFrontendHost::toImpl(info.Holder());
125     Vector<ContextMenuItem> items = menu.items();
126     frontendHost->showContextMenu(event, items);
127 }
128
129 void V8InspectorFrontendHost::showContextMenuAtPointMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
130 {
131     if (info.Length() < 3)
132         return;
133
134     v8::Local<v8::Value> x = v8::Local<v8::Value>::Cast(info[0]);
135     if (!x->IsNumber())
136         return;
137
138     v8::Local<v8::Value> y = v8::Local<v8::Value>::Cast(info[1]);
139     if (!y->IsNumber())
140         return;
141
142     v8::Local<v8::Value> array = v8::Local<v8::Value>::Cast(info[2]);
143     if (!array->IsArray())
144         return;
145     ContextMenu menu;
146     if (!populateContextMenuItems(v8::Local<v8::Array>::Cast(array), menu, info.GetIsolate()))
147         return;
148
149     v8::Isolate* isolate = info.GetIsolate();
150     v8::Handle<v8::Object> windowWrapper = V8Window::findInstanceInPrototypeChain(isolate->GetEnteredContext()->Global(), isolate);
151     if (windowWrapper.IsEmpty())
152         return;
153     LocalDOMWindow* window = V8Window::toImpl(windowWrapper);
154     if (!window->document() || !window->document()->page())
155         return;
156
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);
160 }
161
162 static void histogramEnumeration(const char* name, const v8::FunctionCallbackInfo<v8::Value>& info, int boundaryValue)
163 {
164     if (info.Length() < 1 || !info[0]->IsInt32())
165         return;
166
167     int sample = info[0]->ToInt32()->Value();
168     if (sample < boundaryValue)
169         blink::Platform::current()->histogramEnumeration(name, sample, boundaryValue);
170 }
171
172 void V8InspectorFrontendHost::recordActionTakenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
173 {
174     histogramEnumeration("DevTools.ActionTaken", info, 100);
175 }
176
177 void V8InspectorFrontendHost::recordPanelShownMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
178 {
179     histogramEnumeration("DevTools.PanelShown", info, 20);
180 }
181
182 } // namespace blink
183