logging_native_handler.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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
  3. // found in the LICENSE file.
  4. #include "extensions/renderer/logging_native_handler.h"
  5. #include "base/bind.h"
  6. #include "base/logging.h"
  7. #include "extensions/renderer/script_context.h"
  8. #include "v8/include/v8-function-callback.h"
  9. #include "v8/include/v8-primitive.h"
  10. namespace extensions {
  11. LoggingNativeHandler::LoggingNativeHandler(ScriptContext* context)
  12. : ObjectBackedNativeHandler(context) {}
  13. LoggingNativeHandler::~LoggingNativeHandler() {}
  14. void LoggingNativeHandler::AddRoutes() {
  15. RouteHandlerFunction("DCHECK",
  16. base::BindRepeating(&LoggingNativeHandler::Dcheck,
  17. base::Unretained(this)));
  18. RouteHandlerFunction("CHECK",
  19. base::BindRepeating(&LoggingNativeHandler::Check,
  20. base::Unretained(this)));
  21. // A blatant ugly hack to get around our "dcheck is on" validity presubmit
  22. // checks (which assert that it's always written as `#if DCHECK_IS_ON()`).
  23. constexpr char kDCheckIsOnFunctionKey[] =
  24. "DCHECK_IS_"
  25. "ON";
  26. RouteHandlerFunction(kDCheckIsOnFunctionKey,
  27. base::BindRepeating(&LoggingNativeHandler::DcheckIsOn,
  28. base::Unretained(this)));
  29. RouteHandlerFunction("LOG", base::BindRepeating(&LoggingNativeHandler::Log,
  30. base::Unretained(this)));
  31. RouteHandlerFunction("WARNING",
  32. base::BindRepeating(&LoggingNativeHandler::Warning,
  33. base::Unretained(this)));
  34. }
  35. void LoggingNativeHandler::Check(
  36. const v8::FunctionCallbackInfo<v8::Value>& args) {
  37. bool check_value;
  38. std::string error_message;
  39. ParseArgs(args, &check_value, &error_message);
  40. CHECK(check_value) << error_message;
  41. }
  42. void LoggingNativeHandler::Dcheck(
  43. const v8::FunctionCallbackInfo<v8::Value>& args) {
  44. bool check_value;
  45. std::string error_message;
  46. ParseArgs(args, &check_value, &error_message);
  47. DCHECK(check_value) << error_message;
  48. }
  49. void LoggingNativeHandler::DcheckIsOn(
  50. const v8::FunctionCallbackInfo<v8::Value>& args) {
  51. args.GetReturnValue().Set(DCHECK_IS_ON());
  52. }
  53. void LoggingNativeHandler::Log(
  54. const v8::FunctionCallbackInfo<v8::Value>& args) {
  55. CHECK_EQ(1, args.Length());
  56. LOG(INFO) << *v8::String::Utf8Value(args.GetIsolate(), args[0]);
  57. }
  58. void LoggingNativeHandler::Warning(
  59. const v8::FunctionCallbackInfo<v8::Value>& args) {
  60. CHECK_EQ(1, args.Length());
  61. LOG(WARNING) << *v8::String::Utf8Value(args.GetIsolate(), args[0]);
  62. }
  63. void LoggingNativeHandler::ParseArgs(
  64. const v8::FunctionCallbackInfo<v8::Value>& args,
  65. bool* check_value,
  66. std::string* error_message) {
  67. CHECK_LE(args.Length(), 2);
  68. *check_value = args[0]->BooleanValue(context()->isolate());
  69. if (args.Length() == 2) {
  70. *error_message = "Error: " + std::string(*v8::String::Utf8Value(
  71. args.GetIsolate(), args[1]));
  72. }
  73. if (!check_value)
  74. *error_message += "\n" + context()->GetStackTraceAsString();
  75. }
  76. } // namespace extensions