Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Unified Diff: extensions/renderer/console.cc

Issue 2720733004: Rewrite the extensions console to use gin::Wrappable. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/renderer/console.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/console.cc
diff --git a/extensions/renderer/console.cc b/extensions/renderer/console.cc
index 30e42eb34b56ac5ac7c34a1ae12d1c4710b2fcae..a94a20ae15578819a3b3331283b053eb93c6a454 100644
--- a/extensions/renderer/console.cc
+++ b/extensions/renderer/console.cc
@@ -17,6 +17,10 @@
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/v8_helpers.h"
+#include "gin/arguments.h"
+#include "gin/handle.h"
+#include "gin/object_template_builder.h"
+#include "gin/wrappable.h"
namespace extensions {
namespace console {
@@ -34,18 +38,18 @@ void CheckWithMinidump(const std::string& message) {
CHECK(false) << message;
}
-typedef void (*LogMethod)(content::RenderFrame* render_frame,
- const std::string& message);
-
-void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
+void BoundLogMethodCallback(content::ConsoleMessageLevel level,
+ gin::Arguments* arguments) {
std::string message;
- for (int i = 0; i < info.Length(); ++i) {
+ for (int i = 0; i < arguments->Length(); ++i) {
if (i > 0)
message += " ";
- message += *v8::String::Utf8Value(info[i]);
+ v8::Local<v8::Value> value;
+ CHECK(arguments->GetNext(&value));
+ message += *v8::String::Utf8Value(value);
}
- v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
+ v8::Local<v8::Context> context = arguments->isolate()->GetCurrentContext();
if (context.IsEmpty()) {
LOG(WARNING) << "Could not log \"" << message << "\": no context given";
return;
@@ -59,51 +63,33 @@ void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
ScriptContext* script_context =
ScriptContextSet::GetContextByV8Context(context);
- LogMethod log_method =
- reinterpret_cast<LogMethod>(info.Data().As<v8::External>()->Value());
- (*log_method)(script_context ? script_context->GetRenderFrame() : nullptr,
- message);
+ content::RenderFrame* render_frame =
+ script_context ? script_context->GetRenderFrame() : nullptr;
+ AddMessage(render_frame, level, message);
}
-void BindLogMethod(v8::Isolate* isolate,
- v8::Local<v8::Object> target,
- const std::string& name,
- LogMethod log_method) {
- v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
- isolate,
- &BoundLogMethodCallback,
- v8::External::New(isolate, reinterpret_cast<void*>(log_method)));
- tmpl->RemovePrototype();
- v8::Local<v8::Function> function;
- if (!tmpl->GetFunction(isolate->GetCurrentContext()).ToLocal(&function)) {
- LOG(FATAL) << "Could not create log function \"" << name << "\"";
- return;
+class Console final : public gin::Wrappable<Console> {
+ public:
+ static gin::WrapperInfo kWrapperInfo;
+
+ // gin::Wrappable:
+ gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
+ v8::Isolate* isolate) final {
+ return Wrappable<Console>::GetObjectTemplateBuilder(isolate)
+ .SetMethod("debug", base::Bind(BoundLogMethodCallback,
+ content::CONSOLE_MESSAGE_LEVEL_VERBOSE))
+ .SetMethod("info", base::Bind(BoundLogMethodCallback,
+ content::CONSOLE_MESSAGE_LEVEL_INFO))
+ .SetMethod("warn", base::Bind(BoundLogMethodCallback,
+ content::CONSOLE_MESSAGE_LEVEL_WARNING))
+ .SetMethod("error", base::Bind(BoundLogMethodCallback,
+ content::CONSOLE_MESSAGE_LEVEL_ERROR));
}
- v8::Local<v8::String> v8_name = ToV8StringUnsafe(isolate, name);
- if (!SetProperty(isolate->GetCurrentContext(), target, v8_name, function)) {
- LOG(WARNING) << "Could not bind log method \"" << name << "\"";
- }
- SetProperty(isolate->GetCurrentContext(), target, v8_name,
- tmpl->GetFunction());
-}
+};
-} // namespace
-
-void Debug(content::RenderFrame* render_frame, const std::string& message) {
- AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_VERBOSE, message);
-}
+gin::WrapperInfo Console::kWrapperInfo = {gin::kEmbedderNativeGin};
-void Log(content::RenderFrame* render_frame, const std::string& message) {
- AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_INFO, message);
-}
-
-void Warn(content::RenderFrame* render_frame, const std::string& message) {
- AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
-}
-
-void Error(content::RenderFrame* render_frame, const std::string& message) {
- AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
-}
+} // namespace
void Fatal(content::RenderFrame* render_frame, const std::string& message) {
Error(render_frame, message);
@@ -122,13 +108,7 @@ void AddMessage(content::RenderFrame* render_frame,
}
v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) {
- v8::EscapableHandleScope handle_scope(isolate);
- v8::Local<v8::Object> console_object = v8::Object::New(isolate);
- BindLogMethod(isolate, console_object, "debug", &Debug);
- BindLogMethod(isolate, console_object, "log", &Log);
- BindLogMethod(isolate, console_object, "warn", &Warn);
- BindLogMethod(isolate, console_object, "error", &Error);
- return handle_scope.Escape(console_object);
+ return gin::CreateHandle(isolate, new Console).ToV8().As<v8::Object>();
}
} // namespace console
« no previous file with comments | « extensions/renderer/console.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698