2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8ConsoleMessage.h"
35 #include "DOMWindow.h"
39 #include "ScriptCallStack.h"
40 #include "ScriptCallStackFactory.h"
41 #include "V8Binding.h"
46 Vector<V8ConsoleMessage>* V8ConsoleMessage::m_delayedMessages = 0;
48 V8ConsoleMessage::V8ConsoleMessage(const String& string, const String& sourceID, unsigned lineNumber)
50 , m_sourceID(sourceID)
51 , m_lineNumber(lineNumber)
55 void V8ConsoleMessage::dispatchNow(Page* page)
60 void V8ConsoleMessage::dispatchLater()
62 if (!m_delayedMessages) {
63 // Allocate a vector for the delayed messages. Will be
64 // deallocated when the delayed messages are processed
65 // in processDelayed().
66 m_delayedMessages = new Vector<V8ConsoleMessage>();
69 m_delayedMessages->append(*this);
72 void V8ConsoleMessage::processDelayed()
74 if (!m_delayedMessages)
77 // Take ownership of the delayed vector to avoid re-entrancy issues.
78 OwnPtr<Vector<V8ConsoleMessage> > delayedMessages(m_delayedMessages);
79 m_delayedMessages = 0;
81 // If we have a delayed vector it cannot be empty.
82 ASSERT(!delayedMessages->isEmpty());
84 // Add the delayed messages to the page of the active
85 // context. If that for some bizarre reason does not
86 // exist, we clear the list of delayed messages to avoid
87 // posting messages. We still deallocate the vector.
88 Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
91 Page* page = frame->page();
95 // Iterate through all the delayed messages and add them
97 const int size = delayedMessages->size();
98 for (int i = 0; i < size; ++i)
99 delayedMessages->at(i).dispatchNow(page);
102 void V8ConsoleMessage::handler(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data)
104 // Use the frame where JavaScript is called from.
105 Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
108 Page* page = frame->page();
112 v8::Handle<v8::String> errorMessageString = message->Get();
113 ASSERT(!errorMessageString.IsEmpty());
114 String errorMessage = toWebCoreString(errorMessageString);
116 v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace();
117 OwnPtr<ScriptCallStack> callStack;
118 // Currently stack trace is only collected when inspector is open.
119 if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0) {
120 v8::Local<v8::Context> context = v8::Context::GetEntered();
121 callStack = createScriptCallStack(context, stackTrace, ScriptCallStack::maxCallStackSizeToCapture);
124 v8::Handle<v8::Value> resourceName = message->GetScriptResourceName();
125 bool useURL = resourceName.IsEmpty() || !resourceName->IsString();
126 String resourceNameString = useURL ? frame->document()->url() : toWebCoreString(resourceName);
127 V8ConsoleMessage consoleMessage(errorMessage, resourceNameString, message->GetLineNumber());
128 consoleMessage.dispatchNow(page, callStack.release());
131 void V8ConsoleMessage::dispatchNow(Page* page, PassOwnPtr<ScriptCallStack> callStack)
135 // Process any delayed messages to make sure that messages
136 // appear in the right order in the console.
139 Console* console = page->mainFrame()->domWindow()->console();
140 MessageType messageType = callStack ? UncaughtExceptionMessageType : LogMessageType;
141 console->addMessage(JSMessageSource, messageType, ErrorMessageLevel, m_string, m_lineNumber, m_sourceID, callStack);
144 } // namespace WebCore