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