| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/renderer/console.h" | 5 #include "extensions/renderer/console.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/debug/alias.h" | 8 #include "base/debug/alias.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "content/public/child/worker_thread.h" | 14 #include "content/public/child/worker_thread.h" |
| 15 #include "content/public/renderer/render_frame.h" | 15 #include "content/public/renderer/render_frame.h" |
| 16 #include "extensions/renderer/extension_frame_helper.h" | 16 #include "extensions/renderer/extension_frame_helper.h" |
| 17 #include "extensions/renderer/script_context.h" | 17 #include "extensions/renderer/script_context.h" |
| 18 #include "extensions/renderer/script_context_set.h" | 18 #include "extensions/renderer/script_context_set.h" |
| 19 #include "extensions/renderer/v8_helpers.h" | 19 #include "extensions/renderer/v8_helpers.h" |
| 20 #include "gin/arguments.h" |
| 21 #include "gin/handle.h" |
| 22 #include "gin/object_template_builder.h" |
| 23 #include "gin/wrappable.h" |
| 20 | 24 |
| 21 namespace extensions { | 25 namespace extensions { |
| 22 namespace console { | 26 namespace console { |
| 23 | 27 |
| 24 using namespace v8_helpers; | 28 using namespace v8_helpers; |
| 25 | 29 |
| 26 namespace { | 30 namespace { |
| 27 | 31 |
| 28 // Writes |message| to stack to show up in minidump, then crashes. | 32 // Writes |message| to stack to show up in minidump, then crashes. |
| 29 void CheckWithMinidump(const std::string& message) { | 33 void CheckWithMinidump(const std::string& message) { |
| 30 char minidump[1024]; | 34 char minidump[1024]; |
| 31 base::debug::Alias(&minidump); | 35 base::debug::Alias(&minidump); |
| 32 base::snprintf( | 36 base::snprintf( |
| 33 minidump, arraysize(minidump), "e::console: %s", message.c_str()); | 37 minidump, arraysize(minidump), "e::console: %s", message.c_str()); |
| 34 CHECK(false) << message; | 38 CHECK(false) << message; |
| 35 } | 39 } |
| 36 | 40 |
| 37 typedef void (*LogMethod)(content::RenderFrame* render_frame, | 41 void BoundLogMethodCallback(content::ConsoleMessageLevel level, |
| 38 const std::string& message); | 42 gin::Arguments* arguments) { |
| 39 | |
| 40 void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
| 41 std::string message; | 43 std::string message; |
| 42 for (int i = 0; i < info.Length(); ++i) { | 44 for (int i = 0; i < arguments->Length(); ++i) { |
| 43 if (i > 0) | 45 if (i > 0) |
| 44 message += " "; | 46 message += " "; |
| 45 message += *v8::String::Utf8Value(info[i]); | 47 v8::Local<v8::Value> value; |
| 48 CHECK(arguments->GetNext(&value)); |
| 49 message += *v8::String::Utf8Value(value); |
| 46 } | 50 } |
| 47 | 51 |
| 48 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); | 52 v8::Local<v8::Context> context = arguments->isolate()->GetCurrentContext(); |
| 49 if (context.IsEmpty()) { | 53 if (context.IsEmpty()) { |
| 50 LOG(WARNING) << "Could not log \"" << message << "\": no context given"; | 54 LOG(WARNING) << "Could not log \"" << message << "\": no context given"; |
| 51 return; | 55 return; |
| 52 } | 56 } |
| 53 | 57 |
| 54 // A worker's ScriptContext neither lives in ScriptContextSet nor it has a | 58 // A worker's ScriptContext neither lives in ScriptContextSet nor it has a |
| 55 // RenderFrame associated with it, so early exit in this case. | 59 // RenderFrame associated with it, so early exit in this case. |
| 56 // TODO(lazyboy): Fix. | 60 // TODO(lazyboy): Fix. |
| 57 if (content::WorkerThread::GetCurrentId() > 0) | 61 if (content::WorkerThread::GetCurrentId() > 0) |
| 58 return; | 62 return; |
| 59 | 63 |
| 60 ScriptContext* script_context = | 64 ScriptContext* script_context = |
| 61 ScriptContextSet::GetContextByV8Context(context); | 65 ScriptContextSet::GetContextByV8Context(context); |
| 62 LogMethod log_method = | 66 content::RenderFrame* render_frame = |
| 63 reinterpret_cast<LogMethod>(info.Data().As<v8::External>()->Value()); | 67 script_context ? script_context->GetRenderFrame() : nullptr; |
| 64 (*log_method)(script_context ? script_context->GetRenderFrame() : nullptr, | 68 AddMessage(render_frame, level, message); |
| 65 message); | |
| 66 } | 69 } |
| 67 | 70 |
| 68 void BindLogMethod(v8::Isolate* isolate, | 71 class Console final : public gin::Wrappable<Console> { |
| 69 v8::Local<v8::Object> target, | 72 public: |
| 70 const std::string& name, | 73 static gin::WrapperInfo kWrapperInfo; |
| 71 LogMethod log_method) { | 74 |
| 72 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New( | 75 // gin::Wrappable: |
| 73 isolate, | 76 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 74 &BoundLogMethodCallback, | 77 v8::Isolate* isolate) final { |
| 75 v8::External::New(isolate, reinterpret_cast<void*>(log_method))); | 78 return Wrappable<Console>::GetObjectTemplateBuilder(isolate) |
| 76 tmpl->RemovePrototype(); | 79 .SetMethod("debug", base::Bind(BoundLogMethodCallback, |
| 77 v8::Local<v8::Function> function; | 80 content::CONSOLE_MESSAGE_LEVEL_VERBOSE)) |
| 78 if (!tmpl->GetFunction(isolate->GetCurrentContext()).ToLocal(&function)) { | 81 .SetMethod("info", base::Bind(BoundLogMethodCallback, |
| 79 LOG(FATAL) << "Could not create log function \"" << name << "\""; | 82 content::CONSOLE_MESSAGE_LEVEL_INFO)) |
| 80 return; | 83 .SetMethod("warn", base::Bind(BoundLogMethodCallback, |
| 84 content::CONSOLE_MESSAGE_LEVEL_WARNING)) |
| 85 .SetMethod("error", base::Bind(BoundLogMethodCallback, |
| 86 content::CONSOLE_MESSAGE_LEVEL_ERROR)); |
| 81 } | 87 } |
| 82 v8::Local<v8::String> v8_name = ToV8StringUnsafe(isolate, name); | 88 }; |
| 83 if (!SetProperty(isolate->GetCurrentContext(), target, v8_name, function)) { | 89 |
| 84 LOG(WARNING) << "Could not bind log method \"" << name << "\""; | 90 gin::WrapperInfo Console::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 85 } | |
| 86 SetProperty(isolate->GetCurrentContext(), target, v8_name, | |
| 87 tmpl->GetFunction()); | |
| 88 } | |
| 89 | 91 |
| 90 } // namespace | 92 } // namespace |
| 91 | 93 |
| 92 void Debug(content::RenderFrame* render_frame, const std::string& message) { | |
| 93 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_VERBOSE, message); | |
| 94 } | |
| 95 | |
| 96 void Log(content::RenderFrame* render_frame, const std::string& message) { | |
| 97 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_INFO, message); | |
| 98 } | |
| 99 | |
| 100 void Warn(content::RenderFrame* render_frame, const std::string& message) { | |
| 101 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); | |
| 102 } | |
| 103 | |
| 104 void Error(content::RenderFrame* render_frame, const std::string& message) { | |
| 105 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); | |
| 106 } | |
| 107 | |
| 108 void Fatal(content::RenderFrame* render_frame, const std::string& message) { | 94 void Fatal(content::RenderFrame* render_frame, const std::string& message) { |
| 109 Error(render_frame, message); | 95 Error(render_frame, message); |
| 110 CheckWithMinidump(message); | 96 CheckWithMinidump(message); |
| 111 } | 97 } |
| 112 | 98 |
| 113 void AddMessage(content::RenderFrame* render_frame, | 99 void AddMessage(content::RenderFrame* render_frame, |
| 114 content::ConsoleMessageLevel level, | 100 content::ConsoleMessageLevel level, |
| 115 const std::string& message) { | 101 const std::string& message) { |
| 116 if (!render_frame) { | 102 if (!render_frame) { |
| 117 LOG(WARNING) << "Could not log \"" << message | 103 LOG(WARNING) << "Could not log \"" << message |
| 118 << "\": no render frame found"; | 104 << "\": no render frame found"; |
| 119 } else { | 105 } else { |
| 120 render_frame->AddMessageToConsole(level, message); | 106 render_frame->AddMessageToConsole(level, message); |
| 121 } | 107 } |
| 122 } | 108 } |
| 123 | 109 |
| 124 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { | 110 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { |
| 125 v8::EscapableHandleScope handle_scope(isolate); | 111 return gin::CreateHandle(isolate, new Console).ToV8().As<v8::Object>(); |
| 126 v8::Local<v8::Object> console_object = v8::Object::New(isolate); | |
| 127 BindLogMethod(isolate, console_object, "debug", &Debug); | |
| 128 BindLogMethod(isolate, console_object, "log", &Log); | |
| 129 BindLogMethod(isolate, console_object, "warn", &Warn); | |
| 130 BindLogMethod(isolate, console_object, "error", &Error); | |
| 131 return handle_scope.Escape(console_object); | |
| 132 } | 112 } |
| 133 | 113 |
| 134 } // namespace console | 114 } // namespace console |
| 135 } // namespace extensions | 115 } // namespace extensions |
| OLD | NEW |