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

Side by Side Diff: extensions/renderer/console.cc

Issue 2718133003: Rewrite extensions console to not leak function templates. (Closed)
Patch Set: devlin Created 3 years, 9 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 unified diff | Download patch
« no previous file with comments | « extensions/renderer/console.h ('k') | extensions/renderer/module_system.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/converter.h"
21 #include "gin/per_isolate_data.h"
20 22
21 namespace extensions { 23 namespace extensions {
22 namespace console { 24 namespace console {
23 25
24 using namespace v8_helpers; 26 using namespace v8_helpers;
25 27
26 namespace { 28 namespace {
27 29
28 // Writes |message| to stack to show up in minidump, then crashes. 30 // Writes |message| to stack to show up in minidump, then crashes.
29 void CheckWithMinidump(const std::string& message) { 31 void CheckWithMinidump(const std::string& message) {
30 char minidump[1024]; 32 char minidump[1024];
31 base::debug::Alias(&minidump); 33 base::debug::Alias(&minidump);
32 base::snprintf( 34 base::snprintf(
33 minidump, arraysize(minidump), "e::console: %s", message.c_str()); 35 minidump, arraysize(minidump), "e::console: %s", message.c_str());
34 CHECK(false) << message; 36 CHECK(false) << message;
35 } 37 }
36 38
37 typedef void (*LogMethod)(content::RenderFrame* render_frame,
38 const std::string& message);
39
40 void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { 39 void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
41 std::string message; 40 std::string message;
42 for (int i = 0; i < info.Length(); ++i) { 41 for (int i = 0; i < info.Length(); ++i) {
43 if (i > 0) 42 if (i > 0)
44 message += " "; 43 message += " ";
45 message += *v8::String::Utf8Value(info[i]); 44 message += *v8::String::Utf8Value(info[i]);
46 } 45 }
47 46
48 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
49 if (context.IsEmpty()) {
50 LOG(WARNING) << "Could not log \"" << message << "\": no context given";
51 return;
52 }
53
54 // A worker's ScriptContext neither lives in ScriptContextSet nor it has a 47 // A worker's ScriptContext neither lives in ScriptContextSet nor it has a
55 // RenderFrame associated with it, so early exit in this case. 48 // RenderFrame associated with it, so early exit in this case.
56 // TODO(lazyboy): Fix. 49 // TODO(lazyboy): Fix.
57 if (content::WorkerThread::GetCurrentId() > 0) 50 if (content::WorkerThread::GetCurrentId() > 0)
58 return; 51 return;
59 52
53 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
60 ScriptContext* script_context = 54 ScriptContext* script_context =
61 ScriptContextSet::GetContextByV8Context(context); 55 ScriptContextSet::GetContextByV8Context(context);
62 LogMethod log_method = 56 // TODO(devlin): Consider (D)CHECK(script_context)
63 reinterpret_cast<LogMethod>(info.Data().As<v8::External>()->Value()); 57 content::RenderFrame* render_frame =
64 (*log_method)(script_context ? script_context->GetRenderFrame() : nullptr, 58 script_context ? script_context->GetRenderFrame() : nullptr;
65 message); 59 const auto level = static_cast<content::ConsoleMessageLevel>(
60 info.Data().As<v8::Int32>()->Value());
61 AddMessage(render_frame, level, message);
66 } 62 }
67 63
68 void BindLogMethod(v8::Isolate* isolate, 64 gin::WrapperInfo kWrapperInfo = {gin::kEmbedderNativeGin};
69 v8::Local<v8::Object> target,
70 const std::string& name,
71 LogMethod log_method) {
72 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
73 isolate,
74 &BoundLogMethodCallback,
75 v8::External::New(isolate, reinterpret_cast<void*>(log_method)));
76 tmpl->RemovePrototype();
77 v8::Local<v8::Function> function;
78 if (!tmpl->GetFunction(isolate->GetCurrentContext()).ToLocal(&function)) {
79 LOG(FATAL) << "Could not create log function \"" << name << "\"";
80 return;
81 }
82 v8::Local<v8::String> v8_name = ToV8StringUnsafe(isolate, name);
83 if (!SetProperty(isolate->GetCurrentContext(), target, v8_name, function)) {
84 LOG(WARNING) << "Could not bind log method \"" << name << "\"";
85 }
86 SetProperty(isolate->GetCurrentContext(), target, v8_name,
87 tmpl->GetFunction());
88 }
89 65
90 } // namespace 66 } // namespace
91 67
92 void Debug(content::RenderFrame* render_frame, const std::string& message) { 68 void Fatal(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); 69 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
106 }
107
108 void Fatal(content::RenderFrame* render_frame, const std::string& message) {
109 Error(render_frame, message);
110 CheckWithMinidump(message); 70 CheckWithMinidump(message);
111 } 71 }
112 72
113 void AddMessage(content::RenderFrame* render_frame, 73 void AddMessage(content::RenderFrame* render_frame,
114 content::ConsoleMessageLevel level, 74 content::ConsoleMessageLevel level,
115 const std::string& message) { 75 const std::string& message) {
116 if (!render_frame) { 76 if (!render_frame) {
117 LOG(WARNING) << "Could not log \"" << message 77 LOG(WARNING) << "Could not log \"" << message
118 << "\": no render frame found"; 78 << "\": no render frame found";
119 } else { 79 } else {
120 render_frame->AddMessageToConsole(level, message); 80 render_frame->AddMessageToConsole(level, message);
121 } 81 }
122 } 82 }
123 83
124 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { 84 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) {
125 v8::EscapableHandleScope handle_scope(isolate); 85 v8::EscapableHandleScope handle_scope(isolate);
126 v8::Local<v8::Object> console_object = v8::Object::New(isolate); 86 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
127 BindLogMethod(isolate, console_object, "debug", &Debug); 87 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(&kWrapperInfo);
128 BindLogMethod(isolate, console_object, "log", &Log); 88 if (templ.IsEmpty()) {
129 BindLogMethod(isolate, console_object, "warn", &Warn); 89 templ = v8::ObjectTemplate::New(isolate);
130 BindLogMethod(isolate, console_object, "error", &Error); 90 static const struct {
131 return handle_scope.Escape(console_object); 91 const char* name;
92 content::ConsoleMessageLevel level;
93 } methods[] = {
94 {"debug", content::CONSOLE_MESSAGE_LEVEL_VERBOSE},
95 {"log", content::CONSOLE_MESSAGE_LEVEL_INFO},
96 {"warn", content::CONSOLE_MESSAGE_LEVEL_WARNING},
97 {"error", content::CONSOLE_MESSAGE_LEVEL_ERROR},
98 };
99 for (const auto& method : methods) {
100 v8::Local<v8::FunctionTemplate> function =
101 v8::FunctionTemplate::New(isolate, BoundLogMethodCallback,
102 v8::Integer::New(isolate, method.level));
103 function->RemovePrototype();
104 templ->Set(gin::StringToSymbol(isolate, method.name), function);
105 }
106 data->SetObjectTemplate(&kWrapperInfo, templ);
107 }
108 return handle_scope.Escape(
109 templ->NewInstance(isolate->GetCurrentContext()).ToLocalChecked());
132 } 110 }
133 111
134 } // namespace console 112 } // namespace console
135 } // namespace extensions 113 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/console.h ('k') | extensions/renderer/module_system.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698