[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/renderer/extensions/console.h" |
| 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) |
| 38 | : context_(context), found_(NULL) { |
| 39 | } |
| 40 | |
| 41 | virtual bool Visit(content::RenderView* render_view) OVERRIDE { |
| 42 | ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
| 43 | if (helper && |
| 44 | helper->dispatcher()->v8_context_set().GetByV8Context(context_)) { |
| 45 | found_ = render_view; |
| 46 | } |
| 47 | return !found_; |
| 48 | } |
| 49 | |
| 50 | v8::Handle<v8::Context> context_; |
| 51 | content::RenderView* found_; |
| 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(ByContextFinder); |
| 54 | }; |
| 55 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 56 | // Writes |message| to stack to show up in minidump, then crashes. |
| 57 | void CheckWithMinidump(const std::string& message) { |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame^] | 58 | char minidump[1024]; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 59 | base::debug::Alias(&minidump); |
| 60 | base::snprintf(minidump, arraysize(minidump), |
| 61 | "e::console: %s", message.c_str()); |
| 62 | CHECK(false) << message; |
| 63 | } |
| 64 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 65 | } // namespace |
| 66 | |
| 67 | void Debug(content::RenderView* render_view, const std::string& message) { |
| 68 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); |
| 69 | } |
| 70 | |
| 71 | void Log(content::RenderView* render_view, const std::string& message) { |
| 72 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message); |
| 73 | } |
| 74 | |
| 75 | void Warn(content::RenderView* render_view, const std::string& message) { |
| 76 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); |
| 77 | } |
| 78 | |
| 79 | void Error(content::RenderView* render_view, const std::string& message) { |
| 80 | AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 81 | } |
| 82 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 83 | void Fatal(content::RenderView* render_view, const std::string& message) { |
| 84 | Error(render_view, message); |
| 85 | CheckWithMinidump(message); |
| 86 | } |
| 87 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 88 | void AddMessage(content::RenderView* render_view, |
| 89 | content::ConsoleMessageLevel level, |
| 90 | const std::string& message) { |
| 91 | WebKit::WebView* web_view = render_view->GetWebView(); |
| 92 | if (!web_view || !web_view->mainFrame()) |
| 93 | return; |
| 94 | WebKit::WebConsoleMessage::Level target_level = |
| 95 | WebKit::WebConsoleMessage::LevelLog; |
| 96 | switch (level) { |
| 97 | case content::CONSOLE_MESSAGE_LEVEL_DEBUG: |
| 98 | target_level = WebKit::WebConsoleMessage::LevelDebug; |
| 99 | break; |
| 100 | case content::CONSOLE_MESSAGE_LEVEL_LOG: |
| 101 | target_level = WebKit::WebConsoleMessage::LevelLog; |
| 102 | break; |
| 103 | case content::CONSOLE_MESSAGE_LEVEL_WARNING: |
| 104 | target_level = WebKit::WebConsoleMessage::LevelWarning; |
| 105 | break; |
| 106 | case content::CONSOLE_MESSAGE_LEVEL_ERROR: |
| 107 | target_level = WebKit::WebConsoleMessage::LevelError; |
| 108 | break; |
| 109 | } |
| 110 | web_view->mainFrame()->addMessageToConsole( |
| 111 | WebKit::WebConsoleMessage(target_level, ASCIIToUTF16(message))); |
| 112 | } |
| 113 | |
| 114 | void Debug(v8::Handle<v8::Context> context, const std::string& message) { |
| 115 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); |
| 116 | } |
| 117 | |
| 118 | void Log(v8::Handle<v8::Context> context, const std::string& message) { |
| 119 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); |
| 120 | } |
| 121 | |
| 122 | void Warn(v8::Handle<v8::Context> context, const std::string& message) { |
| 123 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); |
| 124 | } |
| 125 | |
| 126 | void Error(v8::Handle<v8::Context> context, const std::string& message) { |
| 127 | AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 128 | } |
| 129 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 130 | void Fatal(v8::Handle<v8::Context> context, const std::string& message) { |
| 131 | Error(context, message); |
| 132 | CheckWithMinidump(message); |
| 133 | } |
| 134 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 135 | void AddMessage(v8::Handle<v8::Context> context, |
| 136 | content::ConsoleMessageLevel level, |
| 137 | const std::string& message) { |
| 138 | if (context.IsEmpty()) { |
| 139 | LOG(WARNING) << "Could not log \"" << message << "\": no context given"; |
| 140 | return; |
| 141 | } |
| 142 | content::RenderView* render_view = ByContextFinder::Find(context); |
| 143 | if (!render_view) { |
| 144 | LOG(WARNING) << "Could not log \"" << message << "\": no render view found"; |
| 145 | return; |
| 146 | } |
| 147 | AddMessage(render_view, level, message); |
| 148 | } |
| 149 | |
| 150 | } // namespace console |
| 151 | } // namespace extensions |