blob: b763e8e3a27c3c1a845144a0ad8aa1d0038ed5a6 [file] [log] [blame]
[email protected]f55c90ee62014-04-12 00:50:031// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]d2663612013-03-17 09:25:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f55c90ee62014-04-12 00:50:035#include "extensions/renderer/console.h"
[email protected]d2663612013-03-17 09:25:566
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"
rdevlin.croninb2cec912015-06-24 20:36:0113#include "content/public/renderer/render_frame.h"
14#include "extensions/renderer/extension_frame_helper.h"
15#include "extensions/renderer/script_context.h"
16#include "extensions/renderer/script_context_set.h"
bashifacd48b82015-06-30 05:44:2017#include "extensions/renderer/v8_helpers.h"
[email protected]d2663612013-03-17 09:25:5618
19namespace extensions {
20namespace console {
21
bashifacd48b82015-06-30 05:44:2022using namespace v8_helpers;
23
[email protected]d2663612013-03-17 09:25:5624namespace {
25
[email protected]95ee77da2013-03-19 21:11:1126// Writes |message| to stack to show up in minidump, then crashes.
27void CheckWithMinidump(const std::string& message) {
[email protected]662c48b2013-07-12 03:50:5228 char minidump[1024];
[email protected]95ee77da2013-03-19 21:11:1129 base::debug::Alias(&minidump);
[email protected]f55c90ee62014-04-12 00:50:0330 base::snprintf(
31 minidump, arraysize(minidump), "e::console: %s", message.c_str());
[email protected]95ee77da2013-03-19 21:11:1132 CHECK(false) << message;
33}
34
rdevlin.croninb2cec912015-06-24 20:36:0135typedef void (*LogMethod)(content::RenderFrame* render_frame,
[email protected]7205a9c2013-08-03 03:15:0036 const std::string& message);
37
38void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
[email protected]7205a9c2013-08-03 03:15:0039 std::string message;
40 for (int i = 0; i < info.Length(); ++i) {
41 if (i > 0)
42 message += " ";
[email protected]95c6b3012013-12-02 14:30:3143 message += *v8::String::Utf8Value(info[i]);
[email protected]7205a9c2013-08-03 03:15:0044 }
rdevlin.croninb2cec912015-06-24 20:36:0145
46 v8::Local<v8::Context> context = info.GetIsolate()->GetCallingContext();
47 if (context.IsEmpty()) {
48 LOG(WARNING) << "Could not log \"" << message << "\": no context given";
49 return;
50 }
51
52 ScriptContext* script_context =
53 ScriptContextSet::GetContextByV8Context(context);
54 LogMethod log_method =
55 reinterpret_cast<LogMethod>(info.Data().As<v8::External>()->Value());
56 (*log_method)(script_context ? script_context->GetRenderFrame() : nullptr,
57 message);
[email protected]7205a9c2013-08-03 03:15:0058}
59
[email protected]69c87922013-11-18 16:47:5160void BindLogMethod(v8::Isolate* isolate,
61 v8::Local<v8::Object> target,
[email protected]7205a9c2013-08-03 03:15:0062 const std::string& name,
63 LogMethod log_method) {
64 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
[email protected]ef4e7bfb02013-12-09 09:56:0665 isolate,
[email protected]7205a9c2013-08-03 03:15:0066 &BoundLogMethodCallback,
[email protected]69c87922013-11-18 16:47:5167 v8::External::New(isolate, reinterpret_cast<void*>(log_method)));
bashifacd48b82015-06-30 05:44:2068 v8::Local<v8::Function> function;
69 if (!tmpl->GetFunction(isolate->GetCurrentContext()).ToLocal(&function)) {
70 LOG(FATAL) << "Could not create log function \"" << name << "\"";
71 return;
72 }
73 v8::Local<v8::String> v8_name = ToV8StringUnsafe(isolate, name);
74 if (!SetProperty(isolate->GetCurrentContext(), target, v8_name, function)) {
75 LOG(WARNING) << "Could not bind log method \"" << name << "\"";
76 }
77 SetProperty(isolate->GetCurrentContext(), target, v8_name,
[email protected]9c47471e2013-11-28 14:41:2178 tmpl->GetFunction());
[email protected]7205a9c2013-08-03 03:15:0079}
80
[email protected]d2663612013-03-17 09:25:5681} // namespace
82
rdevlin.croninb2cec912015-06-24 20:36:0183void Debug(content::RenderFrame* render_frame, const std::string& message) {
84 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message);
[email protected]d2663612013-03-17 09:25:5685}
86
rdevlin.croninb2cec912015-06-24 20:36:0187void Log(content::RenderFrame* render_frame, const std::string& message) {
88 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_LOG, message);
[email protected]d2663612013-03-17 09:25:5689}
90
rdevlin.croninb2cec912015-06-24 20:36:0191void Warn(content::RenderFrame* render_frame, const std::string& message) {
92 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
[email protected]d2663612013-03-17 09:25:5693}
94
rdevlin.croninb2cec912015-06-24 20:36:0195void Error(content::RenderFrame* render_frame, const std::string& message) {
96 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
[email protected]d2663612013-03-17 09:25:5697}
98
rdevlin.croninb2cec912015-06-24 20:36:0199void Fatal(content::RenderFrame* render_frame, const std::string& message) {
100 Error(render_frame, message);
[email protected]95ee77da2013-03-19 21:11:11101 CheckWithMinidump(message);
102}
103
rdevlin.croninb2cec912015-06-24 20:36:01104void AddMessage(content::RenderFrame* render_frame,
[email protected]d2663612013-03-17 09:25:56105 content::ConsoleMessageLevel level,
106 const std::string& message) {
rdevlin.croninb2cec912015-06-24 20:36:01107 if (!render_frame) {
108 LOG(WARNING) << "Could not log \"" << message
109 << "\": no render frame found";
110 } else {
111 render_frame->AddMessageToConsole(level, message);
[email protected]d2663612013-03-17 09:25:56112 }
[email protected]d2663612013-03-17 09:25:56113}
114
kalmanfb6f10ac2014-11-06 23:55:35115v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) {
[email protected]95c6b3012013-12-02 14:30:31116 v8::EscapableHandleScope handle_scope(isolate);
[email protected]505ffde2013-12-19 15:47:13117 v8::Local<v8::Object> console_object = v8::Object::New(isolate);
[email protected]69c87922013-11-18 16:47:51118 BindLogMethod(isolate, console_object, "debug", &Debug);
119 BindLogMethod(isolate, console_object, "log", &Log);
120 BindLogMethod(isolate, console_object, "warn", &Warn);
121 BindLogMethod(isolate, console_object, "error", &Error);
[email protected]95c6b3012013-12-02 14:30:31122 return handle_scope.Escape(console_object);
[email protected]7205a9c2013-08-03 03:15:00123}
124
[email protected]d2663612013-03-17 09:25:56125} // namespace console
126} // namespace extensions