per_isolate_data.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2013 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. #ifndef GIN_PER_ISOLATE_DATA_H_
  5. #define GIN_PER_ISOLATE_DATA_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "gin/gin_export.h"
  12. #include "gin/public/isolate_holder.h"
  13. #include "gin/public/wrapper_info.h"
  14. #include "gin/v8_foreground_task_runner_base.h"
  15. #include "v8/include/v8-array-buffer.h"
  16. #include "v8/include/v8-forward.h"
  17. namespace gin {
  18. class V8IdleTaskRunner;
  19. class IndexedPropertyInterceptor;
  20. class NamedPropertyInterceptor;
  21. class WrappableBase;
  22. // There is one instance of PerIsolateData per v8::Isolate managed by Gin. This
  23. // class stores all the Gin-related data that varies per isolate.
  24. class GIN_EXPORT PerIsolateData {
  25. public:
  26. PerIsolateData(v8::Isolate* isolate,
  27. v8::ArrayBuffer::Allocator* allocator,
  28. IsolateHolder::AccessMode access_mode,
  29. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  30. PerIsolateData(const PerIsolateData&) = delete;
  31. PerIsolateData& operator=(const PerIsolateData&) = delete;
  32. ~PerIsolateData();
  33. static PerIsolateData* From(v8::Isolate* isolate);
  34. // Each isolate is associated with a collection of v8::ObjectTemplates and
  35. // v8::FunctionTemplates. Typically these template objects are created
  36. // lazily.
  37. void SetObjectTemplate(WrapperInfo* info,
  38. v8::Local<v8::ObjectTemplate> object_template);
  39. void SetFunctionTemplate(WrapperInfo* info,
  40. v8::Local<v8::FunctionTemplate> function_template);
  41. // These are low-level functions for retrieving object or function templates
  42. // stored in this object. Because these templates are often created lazily,
  43. // most clients should call higher-level functions that know how to populate
  44. // these templates if they haven't already been created.
  45. v8::Local<v8::ObjectTemplate> GetObjectTemplate(WrapperInfo* info);
  46. v8::Local<v8::FunctionTemplate> GetFunctionTemplate(WrapperInfo* info);
  47. // We maintain a map from Wrappable objects that derive from one of the
  48. // interceptor interfaces to the interceptor interface pointers.
  49. void SetIndexedPropertyInterceptor(WrappableBase* base,
  50. IndexedPropertyInterceptor* interceptor);
  51. void SetNamedPropertyInterceptor(WrappableBase* base,
  52. NamedPropertyInterceptor* interceptor);
  53. void ClearIndexedPropertyInterceptor(WrappableBase* base,
  54. IndexedPropertyInterceptor* interceptor);
  55. void ClearNamedPropertyInterceptor(WrappableBase* base,
  56. NamedPropertyInterceptor* interceptor);
  57. IndexedPropertyInterceptor* GetIndexedPropertyInterceptor(
  58. WrappableBase* base);
  59. NamedPropertyInterceptor* GetNamedPropertyInterceptor(WrappableBase* base);
  60. void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
  61. v8::Isolate* isolate() { return isolate_; }
  62. v8::ArrayBuffer::Allocator* allocator() { return allocator_; }
  63. std::shared_ptr<v8::TaskRunner> task_runner() { return task_runner_; }
  64. private:
  65. typedef std::map<
  66. WrapperInfo*, v8::Eternal<v8::ObjectTemplate> > ObjectTemplateMap;
  67. typedef std::map<
  68. WrapperInfo*, v8::Eternal<v8::FunctionTemplate> > FunctionTemplateMap;
  69. typedef std::map<WrappableBase*, IndexedPropertyInterceptor*>
  70. IndexedPropertyInterceptorMap;
  71. typedef std::map<WrappableBase*, NamedPropertyInterceptor*>
  72. NamedPropertyInterceptorMap;
  73. // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
  74. // owned by the IsolateHolder, which also owns the PerIsolateData.
  75. raw_ptr<v8::Isolate> isolate_;
  76. raw_ptr<v8::ArrayBuffer::Allocator> allocator_;
  77. ObjectTemplateMap object_templates_;
  78. FunctionTemplateMap function_templates_;
  79. IndexedPropertyInterceptorMap indexed_interceptors_;
  80. NamedPropertyInterceptorMap named_interceptors_;
  81. std::shared_ptr<V8ForegroundTaskRunnerBase> task_runner_;
  82. };
  83. } // namespace gin
  84. #endif // GIN_PER_ISOLATE_DATA_H_