[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame^] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame^] | 5 | #include "extensions/renderer/console.h" |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 6 | |
| 7 | #include "base/compiler_specific.h" |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 8 | #include "base/debug/alias.h" |
| 9 | #include "base/lazy_instance.h" |
[email protected] | a19a16d8 | 2013-06-11 17:45:12 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
| 11 | #include "base/strings/stringprintf.h" |
[email protected] | abfd149 | 2013-06-07 21:23:26 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 13 | #include "chrome/renderer/extensions/dispatcher.h" |
| 14 | #include "chrome/renderer/extensions/extension_helper.h" |
| 15 | #include "content/public/renderer/render_view.h" |
| 16 | #include "content/public/renderer/render_view_visitor.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 17 | #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
| 18 | #include "third_party/WebKit/public/web/WebFrame.h" |
| 19 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 20 | |
| 21 | namespace extensions { |
| 22 | namespace console { |
| 23 | |
| 24 | namespace { |
| 25 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 26 | // Finds the RenderView associated with a context. Note: there will be multiple |
| 27 | // contexts in each RenderView. |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 28 | class ByContextFinder : public content::RenderViewVisitor { |
| 29 | public: |
| 30 | static content::RenderView* Find(v8::Handle<v8::Context> context) { |
| 31 | ByContextFinder finder(context); |
| 32 | content::RenderView::ForEach(&finder); |
| 33 | return finder.found_; |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | explicit ByContextFinder(v8::Handle<v8::Context> context) |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame^] | 38 | : context_(context), found_(NULL) {} |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 39 | |
| 40 | virtual bool Visit(content::RenderView* render_view) OVERRIDE { |
| 41 | ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
| 42 | if (helper && |
| 43 | helper->dispatcher()->v8_context_set().GetByV8Context(context_)) { |
| 44 | found_ = render_view; |
| 45 | } |
| 46 | return !found_; |
| 47 | } |
| 48 | |
| 49 | v8::Handle<v8::Context> context_; |
| 50 | content::RenderView* found_; |
| 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(ByContextFinder); |
| 53 | }; |
| 54 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 55 | // Writes |message| to stack to show up in minidump, then crashes. |
| 56 | void CheckWithMinidump(const std::string& message) { |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 57 | char minidump[1024]; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 58 | base::debug::Alias(&minidump); |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame^] | 59 | base::snprintf( |
| 60 | minidump, arraysize(minidump), "e::console: %s", message.c_str()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 61 | CHECK(false) << message; |
| 62 | } |
| 63 | |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 64 | typedef void (*LogMethod)(v8::Handle<v8::Context> context, |
| 65 | const std::string& message); |
| 66 | |
| 67 | void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame^] | 68 | LogMethod log_method = |
| 69 | reinterpret_cast<LogMethod>(info.Data().As<v8::External>()->Value()); |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 70 | std::string message; |
| 71 | for (int i = 0; i < info.Length(); ++i) { |
| 72 | if (i > 0) |
| 73 | message += " "; |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 74 | message += *v8::String::Utf8Value(info[i]); |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 75 | } |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 76 | (*log_method)(info.GetIsolate()->GetCallingContext(), message); |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 69c8792 | 2013-11-18 16:47:51 | [diff] [blame] | 79 | void BindLogMethod(v8::Isolate* isolate, |
| 80 | v8::Local<v8::Object> target, |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 81 | const std::string& name, |
| 82 | LogMethod log_method) { |
| 83 | v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New( |
[email protected] | ef4e7bfb0 | 2013-12-09 09:56:06 | [diff] [blame] | 84 | isolate, |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 85 | &BoundLogMethodCallback, |
[email protected] | 69c8792 | 2013-11-18 16:47:51 | [diff] [blame] | 86 | v8::External::New(isolate, reinterpret_cast<void*>(log_method))); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 87 | target->Set(v8::String::NewFromUtf8(isolate, name.c_str()), |
| 88 | tmpl->GetFunction()); |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 91 | } // namespace |
| 92 | |
| 93 | void Debug(content::RenderView* render_view, const std::string& message) { |
| 94 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); |
| 95 | } |
| 96 | |
| 97 | void Log(content::RenderView* render_view, const std::string& message) { |
| 98 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message); |
| 99 | } |
| 100 | |
| 101 | void Warn(content::RenderView* render_view, const std::string& message) { |
| 102 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); |
| 103 | } |
| 104 | |
| 105 | void Error(content::RenderView* render_view, const std::string& message) { |
| 106 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 107 | } |
| 108 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 109 | void Fatal(content::RenderView* render_view, const std::string& message) { |
| 110 | Error(render_view, message); |
| 111 | CheckWithMinidump(message); |
| 112 | } |
| 113 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 114 | void AddMessage(content::RenderView* render_view, |
| 115 | content::ConsoleMessageLevel level, |
| 116 | const std::string& message) { |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 117 | blink::WebView* web_view = render_view->GetWebView(); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 118 | if (!web_view || !web_view->mainFrame()) |
| 119 | return; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 120 | blink::WebConsoleMessage::Level target_level = |
| 121 | blink::WebConsoleMessage::LevelLog; |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 122 | switch (level) { |
| 123 | case content::CONSOLE_MESSAGE_LEVEL_DEBUG: |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 124 | target_level = blink::WebConsoleMessage::LevelDebug; |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 125 | break; |
| 126 | case content::CONSOLE_MESSAGE_LEVEL_LOG: |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 127 | target_level = blink::WebConsoleMessage::LevelLog; |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 128 | break; |
| 129 | case content::CONSOLE_MESSAGE_LEVEL_WARNING: |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 130 | target_level = blink::WebConsoleMessage::LevelWarning; |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 131 | break; |
| 132 | case content::CONSOLE_MESSAGE_LEVEL_ERROR: |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 133 | target_level = blink::WebConsoleMessage::LevelError; |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | web_view->mainFrame()->addMessageToConsole( |
[email protected] | c91b9d4 | 2013-12-25 00:53:42 | [diff] [blame] | 137 | blink::WebConsoleMessage(target_level, base::UTF8ToUTF16(message))); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void Debug(v8::Handle<v8::Context> context, const std::string& message) { |
| 141 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); |
| 142 | } |
| 143 | |
| 144 | void Log(v8::Handle<v8::Context> context, const std::string& message) { |
| 145 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); |
| 146 | } |
| 147 | |
| 148 | void Warn(v8::Handle<v8::Context> context, const std::string& message) { |
| 149 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); |
| 150 | } |
| 151 | |
| 152 | void Error(v8::Handle<v8::Context> context, const std::string& message) { |
| 153 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 154 | } |
| 155 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 156 | void Fatal(v8::Handle<v8::Context> context, const std::string& message) { |
| 157 | Error(context, message); |
| 158 | CheckWithMinidump(message); |
| 159 | } |
| 160 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 161 | void AddMessage(v8::Handle<v8::Context> context, |
| 162 | content::ConsoleMessageLevel level, |
| 163 | const std::string& message) { |
| 164 | if (context.IsEmpty()) { |
| 165 | LOG(WARNING) << "Could not log \"" << message << "\": no context given"; |
| 166 | return; |
| 167 | } |
| 168 | content::RenderView* render_view = ByContextFinder::Find(context); |
| 169 | if (!render_view) { |
| 170 | LOG(WARNING) << "Could not log \"" << message << "\": no render view found"; |
| 171 | return; |
| 172 | } |
| 173 | AddMessage(render_view, level, message); |
| 174 | } |
| 175 | |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 176 | v8::Local<v8::Object> AsV8Object() { |
[email protected] | 69c8792 | 2013-11-18 16:47:51 | [diff] [blame] | 177 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 178 | v8::EscapableHandleScope handle_scope(isolate); |
[email protected] | 505ffde | 2013-12-19 15:47:13 | [diff] [blame] | 179 | v8::Local<v8::Object> console_object = v8::Object::New(isolate); |
[email protected] | 69c8792 | 2013-11-18 16:47:51 | [diff] [blame] | 180 | BindLogMethod(isolate, console_object, "debug", &Debug); |
| 181 | BindLogMethod(isolate, console_object, "log", &Log); |
| 182 | BindLogMethod(isolate, console_object, "warn", &Warn); |
| 183 | BindLogMethod(isolate, console_object, "error", &Error); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 184 | return handle_scope.Escape(console_object); |
[email protected] | 7205a9c | 2013-08-03 03:15:00 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 187 | } // namespace console |
| 188 | } // namespace extensions |