OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/console.h" | 5 #include "extensions/renderer/console.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/debug/alias.h" | 8 #include "base/debug/alias.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "content/public/renderer/render_view.h" | 13 #include "content/public/renderer/render_view.h" |
14 #include "content/public/renderer/render_view_visitor.h" | 14 #include "content/public/renderer/render_view_visitor.h" |
15 #include "extensions/renderer/dispatcher.h" | 15 #include "extensions/renderer/dispatcher.h" |
16 #include "extensions/renderer/extension_helper.h" | 16 #include "extensions/renderer/extension_helper.h" |
17 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 17 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
18 #include "third_party/WebKit/public/web/WebFrame.h" | 18 #include "third_party/WebKit/public/web/WebFrame.h" |
19 #include "third_party/WebKit/public/web/WebView.h" | 19 #include "third_party/WebKit/public/web/WebView.h" |
20 | 20 |
21 namespace extensions { | 21 namespace extensions { |
22 namespace console { | 22 namespace console { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // Finds the RenderView associated with a context. Note: there will be multiple | 26 // Finds the RenderView associated with a context. Note: there will be multiple |
27 // contexts in each RenderView. | 27 // contexts in each RenderView. |
28 class ByContextFinder : public content::RenderViewVisitor { | 28 class ByContextFinder : public content::RenderViewVisitor { |
29 public: | 29 public: |
30 static content::RenderView* Find(v8::Handle<v8::Context> context) { | 30 static content::RenderView* Find(v8::Local<v8::Context> context) { |
31 ByContextFinder finder(context); | 31 ByContextFinder finder(context); |
32 content::RenderView::ForEach(&finder); | 32 content::RenderView::ForEach(&finder); |
33 return finder.found_; | 33 return finder.found_; |
34 } | 34 } |
35 | 35 |
36 private: | 36 private: |
37 explicit ByContextFinder(v8::Handle<v8::Context> context) | 37 explicit ByContextFinder(v8::Local<v8::Context> context) |
38 : context_(context), found_(NULL) {} | 38 : context_(context), found_(NULL) {} |
39 | 39 |
40 bool Visit(content::RenderView* render_view) override { | 40 bool Visit(content::RenderView* render_view) override { |
41 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 41 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
42 if (helper) { | 42 if (helper) { |
43 ScriptContext* script_context = | 43 ScriptContext* script_context = |
44 helper->dispatcher()->script_context_set().GetByV8Context(context_); | 44 helper->dispatcher()->script_context_set().GetByV8Context(context_); |
45 if (script_context && script_context->GetRenderView() == render_view) | 45 if (script_context && script_context->GetRenderView() == render_view) |
46 found_ = render_view; | 46 found_ = render_view; |
47 } | 47 } |
48 return !found_; | 48 return !found_; |
49 } | 49 } |
50 | 50 |
51 v8::Handle<v8::Context> context_; | 51 v8::Local<v8::Context> context_; |
52 content::RenderView* found_; | 52 content::RenderView* found_; |
53 | 53 |
54 DISALLOW_COPY_AND_ASSIGN(ByContextFinder); | 54 DISALLOW_COPY_AND_ASSIGN(ByContextFinder); |
55 }; | 55 }; |
56 | 56 |
57 // Writes |message| to stack to show up in minidump, then crashes. | 57 // Writes |message| to stack to show up in minidump, then crashes. |
58 void CheckWithMinidump(const std::string& message) { | 58 void CheckWithMinidump(const std::string& message) { |
59 char minidump[1024]; | 59 char minidump[1024]; |
60 base::debug::Alias(&minidump); | 60 base::debug::Alias(&minidump); |
61 base::snprintf( | 61 base::snprintf( |
62 minidump, arraysize(minidump), "e::console: %s", message.c_str()); | 62 minidump, arraysize(minidump), "e::console: %s", message.c_str()); |
63 CHECK(false) << message; | 63 CHECK(false) << message; |
64 } | 64 } |
65 | 65 |
66 typedef void (*LogMethod)(v8::Handle<v8::Context> context, | 66 typedef void (*LogMethod)(v8::Local<v8::Context> context, |
67 const std::string& message); | 67 const std::string& message); |
68 | 68 |
69 void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { | 69 void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
70 LogMethod log_method = | 70 LogMethod log_method = |
71 reinterpret_cast<LogMethod>(info.Data().As<v8::External>()->Value()); | 71 reinterpret_cast<LogMethod>(info.Data().As<v8::External>()->Value()); |
72 std::string message; | 72 std::string message; |
73 for (int i = 0; i < info.Length(); ++i) { | 73 for (int i = 0; i < info.Length(); ++i) { |
74 if (i > 0) | 74 if (i > 0) |
75 message += " "; | 75 message += " "; |
76 message += *v8::String::Utf8Value(info[i]); | 76 message += *v8::String::Utf8Value(info[i]); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 target_level = blink::WebConsoleMessage::LevelWarning; | 132 target_level = blink::WebConsoleMessage::LevelWarning; |
133 break; | 133 break; |
134 case content::CONSOLE_MESSAGE_LEVEL_ERROR: | 134 case content::CONSOLE_MESSAGE_LEVEL_ERROR: |
135 target_level = blink::WebConsoleMessage::LevelError; | 135 target_level = blink::WebConsoleMessage::LevelError; |
136 break; | 136 break; |
137 } | 137 } |
138 web_view->mainFrame()->addMessageToConsole( | 138 web_view->mainFrame()->addMessageToConsole( |
139 blink::WebConsoleMessage(target_level, base::UTF8ToUTF16(message))); | 139 blink::WebConsoleMessage(target_level, base::UTF8ToUTF16(message))); |
140 } | 140 } |
141 | 141 |
142 void Debug(v8::Handle<v8::Context> context, const std::string& message) { | 142 void Debug(v8::Local<v8::Context> context, const std::string& message) { |
143 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); | 143 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); |
144 } | 144 } |
145 | 145 |
146 void Log(v8::Handle<v8::Context> context, const std::string& message) { | 146 void Log(v8::Local<v8::Context> context, const std::string& message) { |
147 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); | 147 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); |
148 } | 148 } |
149 | 149 |
150 void Warn(v8::Handle<v8::Context> context, const std::string& message) { | 150 void Warn(v8::Local<v8::Context> context, const std::string& message) { |
151 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); | 151 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); |
152 } | 152 } |
153 | 153 |
154 void Error(v8::Handle<v8::Context> context, const std::string& message) { | 154 void Error(v8::Local<v8::Context> context, const std::string& message) { |
155 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); | 155 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
156 } | 156 } |
157 | 157 |
158 void Fatal(v8::Handle<v8::Context> context, const std::string& message) { | 158 void Fatal(v8::Local<v8::Context> context, const std::string& message) { |
159 Error(context, message); | 159 Error(context, message); |
160 CheckWithMinidump(message); | 160 CheckWithMinidump(message); |
161 } | 161 } |
162 | 162 |
163 void AddMessage(v8::Handle<v8::Context> context, | 163 void AddMessage(v8::Local<v8::Context> context, |
164 content::ConsoleMessageLevel level, | 164 content::ConsoleMessageLevel level, |
165 const std::string& message) { | 165 const std::string& message) { |
166 if (context.IsEmpty()) { | 166 if (context.IsEmpty()) { |
167 LOG(WARNING) << "Could not log \"" << message << "\": no context given"; | 167 LOG(WARNING) << "Could not log \"" << message << "\": no context given"; |
168 return; | 168 return; |
169 } | 169 } |
170 content::RenderView* render_view = ByContextFinder::Find(context); | 170 content::RenderView* render_view = ByContextFinder::Find(context); |
171 if (!render_view) { | 171 if (!render_view) { |
172 LOG(WARNING) << "Could not log \"" << message << "\": no render view found"; | 172 LOG(WARNING) << "Could not log \"" << message << "\": no render view found"; |
173 return; | 173 return; |
174 } | 174 } |
175 AddMessage(render_view, level, message); | 175 AddMessage(render_view, level, message); |
176 } | 176 } |
177 | 177 |
178 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { | 178 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { |
179 v8::EscapableHandleScope handle_scope(isolate); | 179 v8::EscapableHandleScope handle_scope(isolate); |
180 v8::Local<v8::Object> console_object = v8::Object::New(isolate); | 180 v8::Local<v8::Object> console_object = v8::Object::New(isolate); |
181 BindLogMethod(isolate, console_object, "debug", &Debug); | 181 BindLogMethod(isolate, console_object, "debug", &Debug); |
182 BindLogMethod(isolate, console_object, "log", &Log); | 182 BindLogMethod(isolate, console_object, "log", &Log); |
183 BindLogMethod(isolate, console_object, "warn", &Warn); | 183 BindLogMethod(isolate, console_object, "warn", &Warn); |
184 BindLogMethod(isolate, console_object, "error", &Error); | 184 BindLogMethod(isolate, console_object, "error", &Error); |
185 return handle_scope.Escape(console_object); | 185 return handle_scope.Escape(console_object); |
186 } | 186 } |
187 | 187 |
188 } // namespace console | 188 } // namespace console |
189 } // namespace extensions | 189 } // namespace extensions |
OLD | NEW |