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

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

Issue 1200503002: [Extensions] Kill off ExtensionMsg_AddMessageToConsole (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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
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/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/renderer/render_view.h" 13 #include "content/public/renderer/render_frame.h"
14 #include "content/public/renderer/render_view_visitor.h"
15 #include "extensions/renderer/dispatcher.h" 14 #include "extensions/renderer/dispatcher.h"
16 #include "extensions/renderer/extension_helper.h" 15 #include "extensions/renderer/extension_frame_helper.h"
17 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
18 #include "third_party/WebKit/public/web/WebFrame.h"
19 #include "third_party/WebKit/public/web/WebView.h"
20 16
21 namespace extensions { 17 namespace extensions {
22 namespace console { 18 namespace console {
23 19
24 namespace { 20 namespace {
25 21
26 // Finds the RenderView associated with a context. Note: there will be multiple
27 // contexts in each RenderView.
28 class ByContextFinder : public content::RenderViewVisitor {
29 public:
30 static content::RenderView* Find(v8::Local<v8::Context> context) {
31 ByContextFinder finder(context);
32 content::RenderView::ForEach(&finder);
33 return finder.found_;
34 }
35
36 private:
37 explicit ByContextFinder(v8::Local<v8::Context> context)
38 : context_(context), found_(NULL) {}
39
40 bool Visit(content::RenderView* render_view) override {
41 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
42 if (helper) {
43 ScriptContext* script_context =
44 helper->dispatcher()->script_context_set().GetByV8Context(context_);
45 if (script_context && script_context->GetRenderView() == render_view)
46 found_ = render_view;
47 }
48 return !found_;
49 }
50
51 v8::Local<v8::Context> context_;
52 content::RenderView* found_;
53
54 DISALLOW_COPY_AND_ASSIGN(ByContextFinder);
55 };
56
57 // Writes |message| to stack to show up in minidump, then crashes. 22 // Writes |message| to stack to show up in minidump, then crashes.
58 void CheckWithMinidump(const std::string& message) { 23 void CheckWithMinidump(const std::string& message) {
59 char minidump[1024]; 24 char minidump[1024];
60 base::debug::Alias(&minidump); 25 base::debug::Alias(&minidump);
61 base::snprintf( 26 base::snprintf(
62 minidump, arraysize(minidump), "e::console: %s", message.c_str()); 27 minidump, arraysize(minidump), "e::console: %s", message.c_str());
63 CHECK(false) << message; 28 CHECK(false) << message;
64 } 29 }
65 30
66 typedef void (*LogMethod)(v8::Local<v8::Context> context, 31 typedef void (*LogMethod)(v8::Local<v8::Context> context,
(...skipping 18 matching lines...) Expand all
85 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New( 50 v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
86 isolate, 51 isolate,
87 &BoundLogMethodCallback, 52 &BoundLogMethodCallback,
88 v8::External::New(isolate, reinterpret_cast<void*>(log_method))); 53 v8::External::New(isolate, reinterpret_cast<void*>(log_method)));
89 target->Set(v8::String::NewFromUtf8(isolate, name.c_str()), 54 target->Set(v8::String::NewFromUtf8(isolate, name.c_str()),
90 tmpl->GetFunction()); 55 tmpl->GetFunction());
91 } 56 }
92 57
93 } // namespace 58 } // namespace
94 59
95 void Debug(content::RenderView* render_view, const std::string& message) {
96 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message);
97 }
98
99 void Log(content::RenderView* render_view, const std::string& message) {
100 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message);
101 }
102
103 void Warn(content::RenderView* render_view, const std::string& message) {
104 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
105 }
106
107 void Error(content::RenderView* render_view, const std::string& message) {
108 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
109 }
110
111 void Fatal(content::RenderView* render_view, const std::string& message) {
112 Error(render_view, message);
113 CheckWithMinidump(message);
114 }
115
116 void AddMessage(content::RenderView* render_view,
117 content::ConsoleMessageLevel level,
118 const std::string& message) {
119 blink::WebView* web_view = render_view->GetWebView();
120 if (!web_view || !web_view->mainFrame())
121 return;
122 blink::WebConsoleMessage::Level target_level =
123 blink::WebConsoleMessage::LevelLog;
124 switch (level) {
125 case content::CONSOLE_MESSAGE_LEVEL_DEBUG:
126 target_level = blink::WebConsoleMessage::LevelDebug;
127 break;
128 case content::CONSOLE_MESSAGE_LEVEL_LOG:
129 target_level = blink::WebConsoleMessage::LevelLog;
130 break;
131 case content::CONSOLE_MESSAGE_LEVEL_WARNING:
132 target_level = blink::WebConsoleMessage::LevelWarning;
133 break;
134 case content::CONSOLE_MESSAGE_LEVEL_ERROR:
135 target_level = blink::WebConsoleMessage::LevelError;
136 break;
137 }
138 web_view->mainFrame()->addMessageToConsole(
139 blink::WebConsoleMessage(target_level, base::UTF8ToUTF16(message)));
140 }
141
142 void Debug(v8::Local<v8::Context> context, const std::string& message) { 60 void Debug(v8::Local<v8::Context> context, const std::string& message) {
143 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); 61 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message);
144 } 62 }
145 63
146 void Log(v8::Local<v8::Context> context, const std::string& message) { 64 void Log(v8::Local<v8::Context> context, const std::string& message) {
147 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); 65 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message);
148 } 66 }
149 67
150 void Warn(v8::Local<v8::Context> context, const std::string& message) { 68 void Warn(v8::Local<v8::Context> context, const std::string& message) {
151 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); 69 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message);
152 } 70 }
153 71
154 void Error(v8::Local<v8::Context> context, const std::string& message) { 72 void Error(v8::Local<v8::Context> context, const std::string& message) {
155 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); 73 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message);
156 } 74 }
157 75
158 void Fatal(v8::Local<v8::Context> context, const std::string& message) { 76 void Fatal(v8::Local<v8::Context> context, const std::string& message) {
159 Error(context, message); 77 Error(context, message);
160 CheckWithMinidump(message); 78 CheckWithMinidump(message);
161 } 79 }
162 80
163 void AddMessage(v8::Local<v8::Context> context, 81 void AddMessage(v8::Local<v8::Context> context,
164 content::ConsoleMessageLevel level, 82 content::ConsoleMessageLevel level,
165 const std::string& message) { 83 const std::string& message) {
166 if (context.IsEmpty()) { 84 if (context.IsEmpty()) {
167 LOG(WARNING) << "Could not log \"" << message << "\": no context given"; 85 LOG(WARNING) << "Could not log \"" << message << "\": no context given";
168 return; 86 return;
169 } 87 }
170 content::RenderView* render_view = ByContextFinder::Find(context); 88 ScriptContext* script_context =
171 if (!render_view) { 89 Dispatcher::Get()->script_context_set().GetByV8Context(context);
172 LOG(WARNING) << "Could not log \"" << message << "\": no render view found"; 90 content::RenderFrame* render_frame =
91 ExtensionFrameHelper::RenderFrameForContext(script_context);
not at google - send to devlin 2015/06/19 23:14:58 I preferred the old code, conceptually. You have a
Devlin 2015/06/19 23:53:52 Two things: - Most importantly, please note that t
not at google - send to devlin 2015/06/22 17:54:11 Ok, I see where we're getting tangled up, in this
Devlin 2015/06/22 19:52:32 Per offline discussion, changed all these to take
92 if (!render_frame) {
93 LOG(WARNING) << "Could not log \"" << message
94 << "\": no render frame found";
173 return; 95 return;
174 } 96 }
175 AddMessage(render_view, level, message); 97 render_frame->AddMessageToConsole(level, message);
176 } 98 }
177 99
178 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { 100 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) {
179 v8::EscapableHandleScope handle_scope(isolate); 101 v8::EscapableHandleScope handle_scope(isolate);
180 v8::Local<v8::Object> console_object = v8::Object::New(isolate); 102 v8::Local<v8::Object> console_object = v8::Object::New(isolate);
181 BindLogMethod(isolate, console_object, "debug", &Debug); 103 BindLogMethod(isolate, console_object, "debug", &Debug);
182 BindLogMethod(isolate, console_object, "log", &Log); 104 BindLogMethod(isolate, console_object, "log", &Log);
183 BindLogMethod(isolate, console_object, "warn", &Warn); 105 BindLogMethod(isolate, console_object, "warn", &Warn);
184 BindLogMethod(isolate, console_object, "error", &Error); 106 BindLogMethod(isolate, console_object, "error", &Error);
185 return handle_scope.Escape(console_object); 107 return handle_scope.Escape(console_object);
186 } 108 }
187 109
188 } // namespace console 110 } // namespace console
189 } // namespace extensions 111 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698