OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / bindings / v8 / V8ConsoleMessage.cpp
1 /*
2  * Copyright (C) 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 "V8ConsoleMessage.h"
33
34 #include "Console.h"
35 #include "DOMWindow.h"
36 #include "Frame.h"
37 #include "OwnPtr.h"
38 #include "Page.h"
39 #include "ScriptCallStack.h"
40 #include "ScriptCallStackFactory.h"
41 #include "V8Binding.h"
42 #include "V8Proxy.h"
43
44 namespace WebCore {
45
46 Vector<V8ConsoleMessage>* V8ConsoleMessage::m_delayedMessages = 0;
47
48 V8ConsoleMessage::V8ConsoleMessage(const String& string, const String& sourceID, unsigned lineNumber)
49      : m_string(string)
50      , m_sourceID(sourceID)
51      , m_lineNumber(lineNumber)
52 {
53 }
54
55 void V8ConsoleMessage::dispatchNow(Page* page)
56 {
57     dispatchNow(page, 0);
58 }
59
60 void V8ConsoleMessage::dispatchLater()
61 {
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>();
67     }
68
69     m_delayedMessages->append(*this);
70 }
71
72 void V8ConsoleMessage::processDelayed()
73 {
74     if (!m_delayedMessages)
75         return;
76
77     // Take ownership of the delayed vector to avoid re-entrancy issues.
78     OwnPtr<Vector<V8ConsoleMessage> > delayedMessages(m_delayedMessages);
79     m_delayedMessages = 0;
80
81     // If we have a delayed vector it cannot be empty.
82     ASSERT(!delayedMessages->isEmpty());
83
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();
89     if (!frame)
90         return;
91     Page* page = frame->page();
92     if (!page)
93         return;
94
95     // Iterate through all the delayed messages and add them
96     // to the console.
97     const int size = delayedMessages->size();
98     for (int i = 0; i < size; ++i)
99         delayedMessages->at(i).dispatchNow(page);
100 }
101
102 void V8ConsoleMessage::handler(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data)
103 {
104     // Use the frame where JavaScript is called from.
105     Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
106     if (!frame)
107         return;
108     Page* page = frame->page();
109     if (!page)
110         return;
111
112     v8::Handle<v8::String> errorMessageString = message->Get();
113     ASSERT(!errorMessageString.IsEmpty());
114     String errorMessage = toWebCoreString(errorMessageString);
115
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);
122     }
123
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());
129 }
130
131 void V8ConsoleMessage::dispatchNow(Page* page, PassOwnPtr<ScriptCallStack> callStack)
132 {
133     ASSERT(page);
134
135     // Process any delayed messages to make sure that messages
136     // appear in the right order in the console.
137     processDelayed();
138
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);
142 }
143
144 } // namespace WebCore