| 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" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // 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 |
| 48 // RenderFrame associated with it, so early exit in this case. | 48 // RenderFrame associated with it, so early exit in this case. |
| 49 // TODO(lazyboy): Fix. | 49 // TODO(lazyboy): Fix. |
| 50 if (content::WorkerThread::GetCurrentId() > 0) | 50 if (content::WorkerThread::GetCurrentId() > 0) |
| 51 return; | 51 return; |
| 52 | 52 |
| 53 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); | 53 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); |
| 54 ScriptContext* script_context = | 54 ScriptContext* script_context = |
| 55 ScriptContextSet::GetContextByV8Context(context); | 55 ScriptContextSet::GetContextByV8Context(context); |
| 56 // TODO(devlin): Consider (D)CHECK(script_context) | 56 // TODO(devlin): Consider (D)CHECK(script_context) |
| 57 content::RenderFrame* render_frame = | |
| 58 script_context ? script_context->GetRenderFrame() : nullptr; | |
| 59 const auto level = static_cast<content::ConsoleMessageLevel>( | 57 const auto level = static_cast<content::ConsoleMessageLevel>( |
| 60 info.Data().As<v8::Int32>()->Value()); | 58 info.Data().As<v8::Int32>()->Value()); |
| 61 AddMessage(render_frame, level, message); | 59 AddMessage(script_context, level, message); |
| 62 } | 60 } |
| 63 | 61 |
| 64 gin::WrapperInfo kWrapperInfo = {gin::kEmbedderNativeGin}; | 62 gin::WrapperInfo kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 65 | 63 |
| 66 } // namespace | 64 } // namespace |
| 67 | 65 |
| 68 void Fatal(content::RenderFrame* render_frame, const std::string& message) { | 66 void Fatal(ScriptContext* context, const std::string& message) { |
| 69 AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); | 67 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 70 CheckWithMinidump(message); | 68 CheckWithMinidump(message); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void AddMessage(content::RenderFrame* render_frame, | 71 void AddMessage(ScriptContext* script_context, |
| 74 content::ConsoleMessageLevel level, | 72 content::ConsoleMessageLevel level, |
| 75 const std::string& message) { | 73 const std::string& message) { |
| 74 if (!script_context) { |
| 75 LOG(WARNING) << "Could not log \"" << message |
| 76 << "\": no ScriptContext found"; |
| 77 return; |
| 78 } |
| 79 content::RenderFrame* render_frame = script_context->GetRenderFrame(); |
| 76 if (!render_frame) { | 80 if (!render_frame) { |
| 81 // TODO(lazyboy/devlin): This can happen when this is the context for a |
| 82 // service worker. blink::WebEmbeddedWorker has an AddMessageToConsole |
| 83 // method that we could theoretically hook into. |
| 77 LOG(WARNING) << "Could not log \"" << message | 84 LOG(WARNING) << "Could not log \"" << message |
| 78 << "\": no render frame found"; | 85 << "\": no render frame found"; |
| 79 } else { | 86 return; |
| 80 render_frame->AddMessageToConsole(level, message); | |
| 81 } | 87 } |
| 88 |
| 89 render_frame->AddMessageToConsole(level, message); |
| 82 } | 90 } |
| 83 | 91 |
| 84 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { | 92 v8::Local<v8::Object> AsV8Object(v8::Isolate* isolate) { |
| 85 v8::EscapableHandleScope handle_scope(isolate); | 93 v8::EscapableHandleScope handle_scope(isolate); |
| 86 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | 94 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
| 87 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(&kWrapperInfo); | 95 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(&kWrapperInfo); |
| 88 if (templ.IsEmpty()) { | 96 if (templ.IsEmpty()) { |
| 89 templ = v8::ObjectTemplate::New(isolate); | 97 templ = v8::ObjectTemplate::New(isolate); |
| 90 static const struct { | 98 static const struct { |
| 91 const char* name; | 99 const char* name; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 104 templ->Set(gin::StringToSymbol(isolate, method.name), function); | 112 templ->Set(gin::StringToSymbol(isolate, method.name), function); |
| 105 } | 113 } |
| 106 data->SetObjectTemplate(&kWrapperInfo, templ); | 114 data->SetObjectTemplate(&kWrapperInfo, templ); |
| 107 } | 115 } |
| 108 return handle_scope.Escape( | 116 return handle_scope.Escape( |
| 109 templ->NewInstance(isolate->GetCurrentContext()).ToLocalChecked()); | 117 templ->NewInstance(isolate->GetCurrentContext()).ToLocalChecked()); |
| 110 } | 118 } |
| 111 | 119 |
| 112 } // namespace console | 120 } // namespace console |
| 113 } // namespace extensions | 121 } // namespace extensions |
| OLD | NEW |