File: console.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (135 lines) | stat: -rw-r--r-- 4,747 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/console.h"

#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/child/worker_thread.h"
#include "content/public/renderer/render_frame.h"
#include "extensions/renderer/extension_frame_helper.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/v8_helpers.h"

namespace extensions {
namespace console {

using namespace v8_helpers;

namespace {

// Writes |message| to stack to show up in minidump, then crashes.
void CheckWithMinidump(const std::string& message) {
  char minidump[1024];
  base::debug::Alias(&minidump);
  base::snprintf(
      minidump, arraysize(minidump), "e::console: %s", message.c_str());
  CHECK(false) << message;
}

typedef void (*LogMethod)(content::RenderFrame* render_frame,
                          const std::string& message);

void BoundLogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
  std::string message;
  for (int i = 0; i < info.Length(); ++i) {
    if (i > 0)
      message += " ";
    message += *v8::String::Utf8Value(info[i]);
  }

  v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
  if (context.IsEmpty()) {
    LOG(WARNING) << "Could not log \"" << message << "\": no context given";
    return;
  }

  // A worker's ScriptContext neither lives in ScriptContextSet nor it has a
  // RenderFrame associated with it, so early exit in this case.
  // TODO(lazyboy): Fix.
  if (content::WorkerThread::GetCurrentId() > 0)
    return;

  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);
}

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;
  }
  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_DEBUG, message);
}

void Log(content::RenderFrame* render_frame, const std::string& message) {
  AddMessage(render_frame, content::CONSOLE_MESSAGE_LEVEL_LOG, 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);
}

void Fatal(content::RenderFrame* render_frame, const std::string& message) {
  Error(render_frame, message);
  CheckWithMinidump(message);
}

void AddMessage(content::RenderFrame* render_frame,
                content::ConsoleMessageLevel level,
                const std::string& message) {
  if (!render_frame) {
    LOG(WARNING) << "Could not log \"" << message
                 << "\": no render frame found";
  } else {
    render_frame->AddMessageToConsole(level, message);
  }
}

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);
}

}  // namespace console
}  // namespace extensions