callback.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (c) 2019 GitHub, Inc. All rights reserved.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/gin_helper/callback.h"
  5. #include "content/public/browser/browser_thread.h"
  6. #include "gin/dictionary.h"
  7. namespace gin_helper {
  8. namespace {
  9. struct TranslaterHolder {
  10. explicit TranslaterHolder(v8::Isolate* isolate)
  11. : handle(isolate, v8::External::New(isolate, this)) {
  12. handle.SetWeak(this, &GC, v8::WeakCallbackType::kFinalizer);
  13. }
  14. ~TranslaterHolder() {
  15. if (!handle.IsEmpty()) {
  16. handle.ClearWeak();
  17. handle.Reset();
  18. }
  19. }
  20. static void GC(const v8::WeakCallbackInfo<TranslaterHolder>& data) {
  21. delete data.GetParameter();
  22. }
  23. v8::Global<v8::External> handle;
  24. Translater translater;
  25. };
  26. // Cached JavaScript version of |CallTranslater|.
  27. v8::Persistent<v8::FunctionTemplate> g_call_translater;
  28. void CallTranslater(v8::Local<v8::External> external,
  29. v8::Local<v8::Object> state,
  30. gin::Arguments* args) {
  31. // Whether the callback should only be called once.
  32. v8::Isolate* isolate = args->isolate();
  33. auto context = isolate->GetCurrentContext();
  34. bool one_time =
  35. state->Has(context, gin::StringToSymbol(isolate, "oneTime")).ToChecked();
  36. // Check if the callback has already been called.
  37. if (one_time) {
  38. auto called_symbol = gin::StringToSymbol(isolate, "called");
  39. if (state->Has(context, called_symbol).ToChecked()) {
  40. args->ThrowTypeError("One-time callback was called more than once");
  41. return;
  42. } else {
  43. state->Set(context, called_symbol, v8::Boolean::New(isolate, true))
  44. .ToChecked();
  45. }
  46. }
  47. auto* holder = static_cast<TranslaterHolder*>(external->Value());
  48. holder->translater.Run(args);
  49. // Free immediately for one-time callback.
  50. if (one_time)
  51. delete holder;
  52. }
  53. } // namespace
  54. // Destroy the class on UI thread when possible.
  55. struct DeleteOnUIThread {
  56. template <typename T>
  57. static void Destruct(const T* x) {
  58. if (gin_helper::Locker::IsBrowserProcess() &&
  59. !content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
  60. content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
  61. x);
  62. } else {
  63. delete x;
  64. }
  65. }
  66. };
  67. // Like v8::Global, but ref-counted.
  68. template <typename T>
  69. class RefCountedGlobal
  70. : public base::RefCountedThreadSafe<RefCountedGlobal<T>, DeleteOnUIThread> {
  71. public:
  72. RefCountedGlobal(v8::Isolate* isolate, v8::Local<v8::Value> value)
  73. : handle_(isolate, v8::Local<T>::Cast(value)) {}
  74. bool IsAlive() const { return !handle_.IsEmpty(); }
  75. v8::Local<T> NewHandle(v8::Isolate* isolate) const {
  76. return v8::Local<T>::New(isolate, handle_);
  77. }
  78. private:
  79. v8::Global<T> handle_;
  80. DISALLOW_COPY_AND_ASSIGN(RefCountedGlobal);
  81. };
  82. SafeV8Function::SafeV8Function(v8::Isolate* isolate, v8::Local<v8::Value> value)
  83. : v8_function_(new RefCountedGlobal<v8::Function>(isolate, value)) {}
  84. SafeV8Function::SafeV8Function(const SafeV8Function& other) = default;
  85. SafeV8Function::~SafeV8Function() = default;
  86. bool SafeV8Function::IsAlive() const {
  87. return v8_function_.get() && v8_function_->IsAlive();
  88. }
  89. v8::Local<v8::Function> SafeV8Function::NewHandle(v8::Isolate* isolate) const {
  90. return v8_function_->NewHandle(isolate);
  91. }
  92. v8::Local<v8::Value> CreateFunctionFromTranslater(v8::Isolate* isolate,
  93. const Translater& translater,
  94. bool one_time) {
  95. // The FunctionTemplate is cached.
  96. if (g_call_translater.IsEmpty())
  97. g_call_translater.Reset(
  98. isolate,
  99. CreateFunctionTemplate(isolate, base::BindRepeating(&CallTranslater)));
  100. v8::Local<v8::FunctionTemplate> call_translater =
  101. v8::Local<v8::FunctionTemplate>::New(isolate, g_call_translater);
  102. auto* holder = new TranslaterHolder(isolate);
  103. holder->translater = translater;
  104. gin::Dictionary state = gin::Dictionary::CreateEmpty(isolate);
  105. if (one_time)
  106. state.Set("oneTime", true);
  107. auto context = isolate->GetCurrentContext();
  108. return BindFunctionWith(
  109. isolate, context, call_translater->GetFunction(context).ToLocalChecked(),
  110. holder->handle.Get(isolate), gin::ConvertToV8(isolate, state));
  111. }
  112. // func.bind(func, arg1).
  113. // NB(zcbenz): Using C++11 version crashes VS.
  114. v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
  115. v8::Local<v8::Context> context,
  116. v8::Local<v8::Function> func,
  117. v8::Local<v8::Value> arg1,
  118. v8::Local<v8::Value> arg2) {
  119. v8::MaybeLocal<v8::Value> bind =
  120. func->Get(context, gin::StringToV8(isolate, "bind"));
  121. CHECK(!bind.IsEmpty());
  122. v8::Local<v8::Function> bind_func =
  123. v8::Local<v8::Function>::Cast(bind.ToLocalChecked());
  124. v8::Local<v8::Value> converted[] = {func, arg1, arg2};
  125. return bind_func->Call(context, func, base::size(converted), converted)
  126. .ToLocalChecked();
  127. }
  128. } // namespace gin_helper