Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Matt Lilek <webkit (at) mattlilek.com>
      4  * Copyright (C) 2009, 2010 Google Inc. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1.  Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  * 2.  Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     16  *     its contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "ConsoleMessage.h"
     33 
     34 #if ENABLE(INSPECTOR)
     35 
     36 #include "Console.h"
     37 #include "InjectedScript.h"
     38 #include "InjectedScriptManager.h"
     39 #include "InspectorFrontend.h"
     40 #include "InspectorValues.h"
     41 #include "ScriptArguments.h"
     42 #include "ScriptCallStack.h"
     43 #include "ScriptValue.h"
     44 
     45 namespace WebCore {
     46 
     47 ConsoleMessage::ConsoleMessage(MessageSource s, MessageType t, MessageLevel l, const String& m, unsigned li, const String& u)
     48     : m_source(s)
     49     , m_type(t)
     50     , m_level(l)
     51     , m_message(m)
     52     , m_line(li)
     53     , m_url(u)
     54     , m_repeatCount(1)
     55     , m_requestId(0)
     56 {
     57 }
     58 
     59 ConsoleMessage::ConsoleMessage(MessageSource s, MessageType t, MessageLevel l, const String& m, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
     60     : m_source(s)
     61     , m_type(t)
     62     , m_level(l)
     63     , m_message(m)
     64     , m_arguments(arguments)
     65     , m_callStack(callStack)
     66     , m_line(0)
     67     , m_url()
     68     , m_repeatCount(1)
     69     , m_requestId(0)
     70 {
     71 }
     72 
     73 ConsoleMessage::ConsoleMessage(MessageSource s, MessageType t, MessageLevel l, const String& m, const String& responseUrl, unsigned long identifier)
     74     : m_source(s)
     75     , m_type(t)
     76     , m_level(l)
     77     , m_message(m)
     78     , m_line(0)
     79     , m_url(responseUrl)
     80     , m_repeatCount(1)
     81     , m_requestId(identifier)
     82 {
     83 }
     84 
     85 ConsoleMessage::~ConsoleMessage()
     86 {
     87 }
     88 
     89 // Keep in sync with inspector/front-end/ConsoleView.js
     90 static String messageSourceValue(MessageSource source)
     91 {
     92     switch (source) {
     93     case HTMLMessageSource: return "html";
     94     case WMLMessageSource: return "wml";
     95     case XMLMessageSource: return "xml";
     96     case JSMessageSource: return "javascript";
     97     case CSSMessageSource: return "css";
     98     case OtherMessageSource: return "other";
     99     }
    100     return "other";
    101 }
    102 
    103 static String messageTypeValue(MessageType type)
    104 {
    105     switch (type) {
    106     case LogMessageType: return "log";
    107     case ObjectMessageType: return "other";
    108     case TraceMessageType: return "trace";
    109     case StartGroupMessageType: return "startGroup";
    110     case StartGroupCollapsedMessageType: return "startGroupCollapsed";
    111     case EndGroupMessageType: return "endGroup";
    112     case AssertMessageType: return "assert";
    113     case UncaughtExceptionMessageType: return "uncaughtException";
    114     case NetworkErrorMessageType: return "networkError";
    115     }
    116     return "other";
    117 }
    118 
    119 static String messageLevelValue(MessageLevel level)
    120 {
    121     switch (level) {
    122     case TipMessageLevel: return "tip";
    123     case LogMessageLevel: return "log";
    124     case WarningMessageLevel: return "warning";
    125     case ErrorMessageLevel: return "error";
    126     case DebugMessageLevel: return "debug";
    127     }
    128     return "log";
    129 }
    130 
    131 void ConsoleMessage::addToFrontend(InspectorFrontend::Console* frontend, InjectedScriptManager* injectedScriptManager)
    132 {
    133     RefPtr<InspectorObject> jsonObj = InspectorObject::create();
    134     jsonObj->setString("source", messageSourceValue(m_source));
    135     jsonObj->setString("type", messageTypeValue(m_type));
    136     jsonObj->setString("level", messageLevelValue(m_level));
    137     jsonObj->setNumber("line", static_cast<int>(m_line));
    138     jsonObj->setString("url", m_url);
    139     jsonObj->setNumber("repeatCount", static_cast<int>(m_repeatCount));
    140     jsonObj->setString("text", m_message);
    141     if (m_type == NetworkErrorMessageType)
    142         jsonObj->setNumber("networkIdentifier", m_requestId);
    143     if (m_arguments && m_arguments->argumentCount()) {
    144         InjectedScript injectedScript = injectedScriptManager->injectedScriptFor(m_arguments->globalState());
    145         if (!injectedScript.hasNoValue()) {
    146             RefPtr<InspectorArray> jsonArgs = InspectorArray::create();
    147             for (unsigned i = 0; i < m_arguments->argumentCount(); ++i) {
    148                 RefPtr<InspectorValue> inspectorValue = injectedScript.wrapObject(m_arguments->argumentAt(i), "console");
    149                 if (!inspectorValue) {
    150                     ASSERT_NOT_REACHED();
    151                     return;
    152                 }
    153                 jsonArgs->pushValue(inspectorValue);
    154             }
    155             jsonObj->setArray("parameters", jsonArgs);
    156         }
    157     }
    158     if (m_callStack)
    159         jsonObj->setArray("stackTrace", m_callStack->buildInspectorArray());
    160     frontend->messageAdded(jsonObj);
    161 }
    162 
    163 void ConsoleMessage::updateRepeatCountInConsole(InspectorFrontend::Console* frontend)
    164 {
    165     frontend->messageRepeatCountUpdated(m_repeatCount);
    166 }
    167 
    168 bool ConsoleMessage::isEqual(ConsoleMessage* msg) const
    169 {
    170     if (m_arguments) {
    171         if (!m_arguments->isEqual(msg->m_arguments.get()))
    172             return false;
    173     } else if (msg->m_arguments)
    174         return false;
    175 
    176     if (m_callStack) {
    177         if (!m_callStack->isEqual(msg->m_callStack.get()))
    178             return false;
    179     } else if (msg->m_callStack)
    180         return false;
    181 
    182     return msg->m_source == m_source
    183         && msg->m_type == m_type
    184         && msg->m_level == m_level
    185         && msg->m_message == m_message
    186         && msg->m_line == m_line
    187         && msg->m_url == m_url
    188         && msg->m_requestId == m_requestId;
    189 }
    190 
    191 } // namespace WebCore
    192 
    193 #endif // ENABLE(INSPECTOR)
    194