[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 5 | #ifndef EXTENSIONS_RENDERER_V8_SCHEMA_REGISTRY_H_ |
6 | #define EXTENSIONS_RENDERER_V8_SCHEMA_REGISTRY_H_ | ||||
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 7 | |
8 | #include <map> | ||||
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 10 | #include <string> |
[email protected] | 6876bb9 | 2013-06-14 06:03:51 | [diff] [blame] | 11 | #include <vector> |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 12 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 14 | #include "gin/public/context_holder.h" |
[email protected] | b90d08c | 2014-04-04 16:37:51 | [diff] [blame] | 15 | #include "v8/include/v8-util.h" |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 16 | #include "v8/include/v8.h" |
17 | |||||
18 | namespace extensions { | ||||
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 19 | class NativeHandler; |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 20 | |
21 | // A registry for the v8::Value representations of extension API schemas. | ||||
22 | // In a way, the v8 counterpart to ExtensionAPI. | ||||
23 | class V8SchemaRegistry { | ||||
24 | public: | ||||
25 | V8SchemaRegistry(); | ||||
26 | ~V8SchemaRegistry(); | ||||
27 | |||||
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 28 | // Creates a NativeHandler wrapper |this|. Supports GetSchema. |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 29 | std::unique_ptr<NativeHandler> AsNativeHandler(); |
[email protected] | f8d87d3 | 2013-06-06 02:51:29 | [diff] [blame] | 30 | |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 31 | // Returns a v8::Array with all the schemas for the APIs in |apis|. |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 32 | v8::Local<v8::Array> GetSchemas(const std::vector<std::string>& apis); |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 33 | |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 34 | // Returns a v8::Object for the schema for |api|, possibly from the cache. |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 35 | v8::Local<v8::Object> GetSchema(const std::string& api); |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 36 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 37 | private: |
[email protected] | b28deed9 | 2013-03-12 23:01:11 | [diff] [blame] | 38 | // Gets the separate context that backs the registry, creating a new one if |
[email protected] | b90d08c | 2014-04-04 16:37:51 | [diff] [blame] | 39 | // if necessary. Will also initialize schema_cache_. |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 40 | v8::Local<v8::Context> GetOrCreateContext(v8::Isolate* isolate); |
[email protected] | b28deed9 | 2013-03-12 23:01:11 | [diff] [blame] | 41 | |
[email protected] | b90d08c | 2014-04-04 16:37:51 | [diff] [blame] | 42 | // Cache of schemas. Created lazily by GetOrCreateContext. |
dcarney | 36f78b64 | 2015-04-24 10:22:49 | [diff] [blame] | 43 | typedef v8::StdGlobalValueMap<std::string, v8::Object> SchemaCache; |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 44 | std::unique_ptr<SchemaCache> schema_cache_; |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 45 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 46 | // Single per-instance gin::ContextHolder to create v8::Values. |
[email protected] | b28deed9 | 2013-03-12 23:01:11 | [diff] [blame] | 47 | // Created lazily via GetOrCreateContext. |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 48 | std::unique_ptr<gin::ContextHolder> context_holder_; |
[email protected] | 7b1780c8 | 2012-03-21 11:48:09 | [diff] [blame] | 49 | |
50 | DISALLOW_COPY_AND_ASSIGN(V8SchemaRegistry); | ||||
51 | }; | ||||
52 | |||||
53 | } // namespace extensions | ||||
54 | |||||
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 55 | #endif // EXTENSIONS_RENDERER_V8_SCHEMA_REGISTRY_H_ |