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 "V8Binding.h"
40 #include "V8Proxy.h"
41
42 namespace WebCore {
43
44 Vector<V8ConsoleMessage>* V8ConsoleMessage::m_delayedMessages = 0;
45
V8ConsoleMessage(const String & string,const String & sourceID,unsigned lineNumber)46 V8ConsoleMessage::V8ConsoleMessage(const String& string, const String& sourceID, unsigned lineNumber)
47 : m_string(string)
48 , m_sourceID(sourceID)
49 , m_lineNumber(lineNumber)
50 {
51 }
52
dispatchNow(Page * page)53 void V8ConsoleMessage::dispatchNow(Page* page)
54 {
55 ASSERT(page);
56
57 // Process any delayed messages to make sure that messages
58 // appear in the right order in the console.
59 processDelayed();
60
61 Console* console = page->mainFrame()->domWindow()->console();
62 console->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, m_string, m_lineNumber, m_sourceID);
63 }
64
dispatchLater()65 void V8ConsoleMessage::dispatchLater()
66 {
67 if (!m_delayedMessages) {
68 // Allocate a vector for the delayed messages. Will be
69 // deallocated when the delayed messages are processed
70 // in processDelayed().
71 m_delayedMessages = new Vector<V8ConsoleMessage>();
72 }
73
74 m_delayedMessages->append(*this);
75 }
76
processDelayed()77 void V8ConsoleMessage::processDelayed()
78 {
79 if (!m_delayedMessages)
80 return;
81
82 // Take ownership of the delayed vector to avoid re-entrancy issues.
83 OwnPtr<Vector<V8ConsoleMessage> > delayedMessages(m_delayedMessages);
84 m_delayedMessages = 0;
85
86 // If we have a delayed vector it cannot be empty.
87 ASSERT(!delayedMessages->isEmpty());
88
89 // Add the delayed messages to the page of the active
90 // context. If that for some bizarre reason does not
91 // exist, we clear the list of delayed messages to avoid
92 // posting messages. We still deallocate the vector.
93 Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
94 if (!frame)
95 return;
96 Page* page = frame->page();
97 if (!page)
98 return;
99
100 // Iterate through all the delayed messages and add them
101 // to the console.
102 const int size = delayedMessages->size();
103 for (int i = 0; i < size; ++i)
104 delayedMessages->at(i).dispatchNow(page);
105 }
106
handler(v8::Handle<v8::Message> message,v8::Handle<v8::Value> data)107 void V8ConsoleMessage::handler(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data)
108 {
109 // Use the frame where JavaScript is called from.
110 Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
111 if (!frame)
112 return;
113 Page* page = frame->page();
114 if (!page)
115 return;
116
117 v8::Handle<v8::String> errorMessageString = message->Get();
118 ASSERT(!errorMessageString.IsEmpty());
119 String errorMessage = toWebCoreString(errorMessageString);
120
121 v8::Handle<v8::Value> resourceName = message->GetScriptResourceName();
122 bool useURL = resourceName.IsEmpty() || !resourceName->IsString();
123 String resourceNameString = useURL ? frame->document()->url() : toWebCoreString(resourceName);
124 V8ConsoleMessage consoleMessage(errorMessage, resourceNameString, message->GetLineNumber());
125 consoleMessage.dispatchNow(page);
126 }
127
128 } // namespace WebCore
129