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" 8 #include "base/debug/alias.h" 9 #include "base/lazy_instance.h" 10 #include "base/strings/string_util.h" 11 #include "base/strings/stringprintf.h" 12 #include "base/strings/utf_string_conversions.h" 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" 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" 20 21 namespace extensions { 22 namespace console { 23 24 namespace { 25 26 // Finds the RenderView associated with a context. Note: there will be multiple 27 // contexts in each RenderView. 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 56 // Writes |message| to stack to show up in minidump, then crashes. 57 void CheckWithMinidump(const std::string& message) { 58 char minidump[1024]; 59 base::debug::Alias(&minidump); 60 base::snprintf(minidump, arraysize(minidump), 61 "e::console: %s", message.c_str()); 62 CHECK(false) << message; 63 } 64 65 typedef void (*LogMethod)(v8::Handle<v8::Context> context, 66 const std::string& message); 67 68 void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { 69 LogMethod log_method = reinterpret_cast<LogMethod>( 70 info.Data().As<v8::External>()->Value()); 71 std::string message; 72 for (int i = 0; i < info.Length(); ++i) { 73 if (i > 0) 74 message += " "; 75 message += *v8::String::AsciiValue(info[i]); 76 } 77 (*log_method)(v8::Context::GetCalling(), message); 78 } 79 80 void BindLogMethod(v8::Local<v8::Object> target, 81 const std::string& name, 82 LogMethod log_method) { 83 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New( 84 &BoundLogMethodCallback, 85 v8::External::New(reinterpret_cast<void*>(log_method))); 86 target->Set(v8::String::New(name.c_str()), tmpl->GetFunction()); 87 } 88 89 } // namespace 90 91 void Debug(content::RenderView* render_view, const std::string& message) { 92 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); 93 } 94 95 void Log(content::RenderView* render_view, const std::string& message) { 96 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message); 97 } 98 99 void Warn(content::RenderView* render_view, const std::string& message) { 100 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); 101 } 102 103 void Error(content::RenderView* render_view, const std::string& message) { 104 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); 105 } 106 107 void Fatal(content::RenderView* render_view, const std::string& message) { 108 Error(render_view, message); 109 CheckWithMinidump(message); 110 } 111 112 void AddMessage(content::RenderView* render_view, 113 content::ConsoleMessageLevel level, 114 const std::string& message) { 115 WebKit::WebView* web_view = render_view->GetWebView(); 116 if (!web_view || !web_view->mainFrame()) 117 return; 118 WebKit::WebConsoleMessage::Level target_level = 119 WebKit::WebConsoleMessage::LevelLog; 120 switch (level) { 121 case content::CONSOLE_MESSAGE_LEVEL_DEBUG: 122 target_level = WebKit::WebConsoleMessage::LevelDebug; 123 break; 124 case content::CONSOLE_MESSAGE_LEVEL_LOG: 125 target_level = WebKit::WebConsoleMessage::LevelLog; 126 break; 127 case content::CONSOLE_MESSAGE_LEVEL_WARNING: 128 target_level = WebKit::WebConsoleMessage::LevelWarning; 129 break; 130 case content::CONSOLE_MESSAGE_LEVEL_ERROR: 131 target_level = WebKit::WebConsoleMessage::LevelError; 132 break; 133 } 134 web_view->mainFrame()->addMessageToConsole( 135 WebKit::WebConsoleMessage(target_level, ASCIIToUTF16(message))); 136 } 137 138 void Debug(v8::Handle<v8::Context> context, const std::string& message) { 139 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); 140 } 141 142 void Log(v8::Handle<v8::Context> context, const std::string& message) { 143 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); 144 } 145 146 void Warn(v8::Handle<v8::Context> context, const std::string& message) { 147 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); 148 } 149 150 void Error(v8::Handle<v8::Context> context, const std::string& message) { 151 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); 152 } 153 154 void Fatal(v8::Handle<v8::Context> context, const std::string& message) { 155 Error(context, message); 156 CheckWithMinidump(message); 157 } 158 159 void AddMessage(v8::Handle<v8::Context> context, 160 content::ConsoleMessageLevel level, 161 const std::string& message) { 162 if (context.IsEmpty()) { 163 LOG(WARNING) << "Could not log \"" << message << "\": no context given"; 164 return; 165 } 166 content::RenderView* render_view = ByContextFinder::Find(context); 167 if (!render_view) { 168 LOG(WARNING) << "Could not log \"" << message << "\": no render view found"; 169 return; 170 } 171 AddMessage(render_view, level, message); 172 } 173 174 v8::Local<v8::Object> AsV8Object() { 175 v8::HandleScope handle_scope; 176 v8::Local<v8::Object> console_object = v8::Object::New(); 177 BindLogMethod(console_object, "debug", &Debug); 178 BindLogMethod(console_object, "log", &Log); 179 BindLogMethod(console_object, "warn", &Warn); 180 BindLogMethod(console_object, "error", &Error); 181 return handle_scope.Close(console_object); 182 } 183 184 } // namespace console 185 } // namespace extensions 186