v8_schema_registry.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #ifndef EXTENSIONS_RENDERER_V8_SCHEMA_REGISTRY_H_
  5. #define EXTENSIONS_RENDERER_V8_SCHEMA_REGISTRY_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "gin/public/context_holder.h"
  11. #include "v8/include/v8-forward.h"
  12. #include "v8/include/v8-util.h"
  13. namespace extensions {
  14. class NativeHandler;
  15. // A registry for the v8::Value representations of extension API schemas.
  16. // In a way, the v8 counterpart to ExtensionAPI.
  17. class V8SchemaRegistry {
  18. public:
  19. V8SchemaRegistry();
  20. V8SchemaRegistry(const V8SchemaRegistry&) = delete;
  21. V8SchemaRegistry& operator=(const V8SchemaRegistry&) = delete;
  22. ~V8SchemaRegistry();
  23. // Creates a NativeHandler wrapper |this|. Supports GetSchema.
  24. std::unique_ptr<NativeHandler> AsNativeHandler();
  25. // Returns a v8::Array with all the schemas for the APIs in |apis|.
  26. v8::Local<v8::Array> GetSchemas(const std::vector<std::string>& apis);
  27. // Returns a v8::Object for the schema for |api|, possibly from the cache.
  28. v8::Local<v8::Object> GetSchema(const std::string& api);
  29. private:
  30. // Gets the separate context that backs the registry, creating a new one if
  31. // if necessary. Will also initialize schema_cache_.
  32. v8::Local<v8::Context> GetOrCreateContext(v8::Isolate* isolate);
  33. // Cache of schemas. Created lazily by GetOrCreateContext.
  34. typedef v8::StdGlobalValueMap<std::string, v8::Object> SchemaCache;
  35. std::unique_ptr<SchemaCache> schema_cache_;
  36. // Single per-instance gin::ContextHolder to create v8::Values.
  37. // Created lazily via GetOrCreateContext.
  38. std::unique_ptr<gin::ContextHolder> context_holder_;
  39. };
  40. } // namespace extensions
  41. #endif // EXTENSIONS_RENDERER_V8_SCHEMA_REGISTRY_H_