blob: 496c70773fa8b8f8629d9ec134e4b299fd2a909c [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"
17#include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h"
18#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
19#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
20
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) {
58 char minidump[256];
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]d2663612013-03-17 09:25:5665} // namespace
66
67void Debug(content::RenderView* render_view, const std::string& message) {
68 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message);
69}
70
71void Log(content::RenderView* render_view, const std::string& message) {
72 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message);
73}
74
75void Warn(content::RenderView* render_view, const std::string& message) {
76 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
77}
78
79void Error(content::RenderView* render_view, const std::string& message) {
80 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
81}
82
[email protected]95ee77da2013-03-19 21:11:1183void Fatal(content::RenderView* render_view, const std::string& message) {
84 Error(render_view, message);
85 CheckWithMinidump(message);
86}
87
[email protected]d2663612013-03-17 09:25:5688void 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
114void Debug(v8::Handle<v8::Context> context, const std::string& message) {
115 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message);
116}
117
118void Log(v8::Handle<v8::Context> context, const std::string& message) {
119 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message);
120}
121
122void Warn(v8::Handle<v8::Context> context, const std::string& message) {
123 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
124}
125
126void Error(v8::Handle<v8::Context> context, const std::string& message) {
127 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
128}
129
[email protected]95ee77da2013-03-19 21:11:11130void Fatal(v8::Handle<v8::Context> context, const std::string& message) {
131 Error(context, message);
132 CheckWithMinidump(message);
133}
134
[email protected]d2663612013-03-17 09:25:56135void 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