blob: 009d665b22bef9c81aab89628441271c1c92f203 [file] [log] [blame]
[email protected]d2663612013-03-17 09:25:561// 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]95ee77da2013-03-19 21:11:118#include "base/debug/alias.h"
9#include "base/lazy_instance.h"
[email protected]a19a16d82013-06-11 17:45:1210#include "base/strings/string_util.h"
11#include "base/strings/stringprintf.h"
[email protected]abfd1492013-06-07 21:23:2612#include "base/strings/utf_string_conversions.h"
[email protected]d2663612013-03-17 09:25:5613#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]2255a9332013-06-17 05:12:3117#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]d2663612013-03-17 09:25:5620
21namespace extensions {
22namespace console {
23
24namespace {
25
[email protected]95ee77da2013-03-19 21:11:1126// Finds the RenderView associated with a context. Note: there will be multiple
27// contexts in each RenderView.
[email protected]d2663612013-03-17 09:25:5628class 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]95ee77da2013-03-19 21:11:1156// Writes |message| to stack to show up in minidump, then crashes.
57void CheckWithMinidump(const std::string& message) {
[email protected]662c48b2013-07-12 03:50:5258 char minidump[1024];
[email protected]95ee77da2013-03-19 21:11:1159 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]7205a9c2013-08-03 03:15:0065typedef void (*LogMethod)(v8::Handle<v8::Context> context,
66 const std::string& message);
67
68void 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
[email protected]69c87922013-11-18 16:47:5180void BindLogMethod(v8::Isolate* isolate,
81 v8::Local<v8::Object> target,
[email protected]7205a9c2013-08-03 03:15:0082 const std::string& name,
83 LogMethod log_method) {
84 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
85 &BoundLogMethodCallback,
[email protected]69c87922013-11-18 16:47:5186 v8::External::New(isolate, reinterpret_cast<void*>(log_method)));
[email protected]9c47471e2013-11-28 14:41:2187 target->Set(v8::String::NewFromUtf8(isolate, name.c_str()),
88 tmpl->GetFunction());
[email protected]7205a9c2013-08-03 03:15:0089}
90
[email protected]d2663612013-03-17 09:25:5691} // namespace
92
93void Debug(content::RenderView* render_view, const std::string& message) {
94 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message);
95}
96
97void Log(content::RenderView* render_view, const std::string& message) {
98 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message);
99}
100
101void Warn(content::RenderView* render_view, const std::string& message) {
102 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
103}
104
105void Error(content::RenderView* render_view, const std::string& message) {
106 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
107}
108
[email protected]95ee77da2013-03-19 21:11:11109void Fatal(content::RenderView* render_view, const std::string& message) {
110 Error(render_view, message);
111 CheckWithMinidump(message);
112}
113
[email protected]d2663612013-03-17 09:25:56114void AddMessage(content::RenderView* render_view,
115 content::ConsoleMessageLevel level,
116 const std::string& message) {
[email protected]a1221aea2013-11-07 01:31:30117 blink::WebView* web_view = render_view->GetWebView();
[email protected]d2663612013-03-17 09:25:56118 if (!web_view || !web_view->mainFrame())
119 return;
[email protected]a1221aea2013-11-07 01:31:30120 blink::WebConsoleMessage::Level target_level =
121 blink::WebConsoleMessage::LevelLog;
[email protected]d2663612013-03-17 09:25:56122 switch (level) {
123 case content::CONSOLE_MESSAGE_LEVEL_DEBUG:
[email protected]a1221aea2013-11-07 01:31:30124 target_level = blink::WebConsoleMessage::LevelDebug;
[email protected]d2663612013-03-17 09:25:56125 break;
126 case content::CONSOLE_MESSAGE_LEVEL_LOG:
[email protected]a1221aea2013-11-07 01:31:30127 target_level = blink::WebConsoleMessage::LevelLog;
[email protected]d2663612013-03-17 09:25:56128 break;
129 case content::CONSOLE_MESSAGE_LEVEL_WARNING:
[email protected]a1221aea2013-11-07 01:31:30130 target_level = blink::WebConsoleMessage::LevelWarning;
[email protected]d2663612013-03-17 09:25:56131 break;
132 case content::CONSOLE_MESSAGE_LEVEL_ERROR:
[email protected]a1221aea2013-11-07 01:31:30133 target_level = blink::WebConsoleMessage::LevelError;
[email protected]d2663612013-03-17 09:25:56134 break;
135 }
136 web_view->mainFrame()->addMessageToConsole(
[email protected]a1221aea2013-11-07 01:31:30137 blink::WebConsoleMessage(target_level, ASCIIToUTF16(message)));
[email protected]d2663612013-03-17 09:25:56138}
139
140void Debug(v8::Handle<v8::Context> context, const std::string& message) {
141 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message);
142}
143
144void Log(v8::Handle<v8::Context> context, const std::string& message) {
145 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message);
146}
147
148void Warn(v8::Handle<v8::Context> context, const std::string& message) {
149 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
150}
151
152void Error(v8::Handle<v8::Context> context, const std::string& message) {
153 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
154}
155
[email protected]95ee77da2013-03-19 21:11:11156void Fatal(v8::Handle<v8::Context> context, const std::string& message) {
157 Error(context, message);
158 CheckWithMinidump(message);
159}
160
[email protected]d2663612013-03-17 09:25:56161void 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]7205a9c2013-08-03 03:15:00176v8::Local<v8::Object> AsV8Object() {
[email protected]69c87922013-11-18 16:47:51177 v8::Isolate* isolate = v8::Isolate::GetCurrent();
178 v8::HandleScope handle_scope(isolate);
[email protected]7205a9c2013-08-03 03:15:00179 v8::Local<v8::Object> console_object = v8::Object::New();
[email protected]69c87922013-11-18 16:47:51180 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]7205a9c2013-08-03 03:15:00184 return handle_scope.Close(console_object);
185}
186
[email protected]d2663612013-03-17 09:25:56187} // namespace console
188} // namespace extensions