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