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.
5 #include "extensions/renderer/v8_schema_registry.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "content/public/child/v8_value_converter.h"
10 #include "extensions/common/extension_api.h"
11 #include "extensions/renderer/object_backed_native_handler.h"
12 #include "extensions/renderer/script_context.h"
14 using content::V8ValueConverter
;
16 namespace extensions
{
20 class SchemaRegistryNativeHandler
: public ObjectBackedNativeHandler
{
22 SchemaRegistryNativeHandler(V8SchemaRegistry
* registry
,
23 scoped_ptr
<ScriptContext
> context
)
24 : ObjectBackedNativeHandler(context
.get()),
25 context_(context
.Pass()),
27 RouteFunction("GetSchema",
28 base::Bind(&SchemaRegistryNativeHandler::GetSchema
,
29 base::Unretained(this)));
32 ~SchemaRegistryNativeHandler() override
{ context_
->Invalidate(); }
35 void GetSchema(const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
36 args
.GetReturnValue().Set(
37 registry_
->GetSchema(*v8::String::Utf8Value(args
[0])));
40 scoped_ptr
<ScriptContext
> context_
;
41 V8SchemaRegistry
* registry_
;
46 V8SchemaRegistry::V8SchemaRegistry() {
49 V8SchemaRegistry::~V8SchemaRegistry() {
52 scoped_ptr
<NativeHandler
> V8SchemaRegistry::AsNativeHandler() {
53 scoped_ptr
<ScriptContext
> context(
54 new ScriptContext(GetOrCreateContext(v8::Isolate::GetCurrent()),
57 Feature::UNSPECIFIED_CONTEXT
,
58 NULL
, // no effective extension
59 Feature::UNSPECIFIED_CONTEXT
));
60 return scoped_ptr
<NativeHandler
>(
61 new SchemaRegistryNativeHandler(this, context
.Pass()));
64 v8::Local
<v8::Array
> V8SchemaRegistry::GetSchemas(
65 const std::vector
<std::string
>& apis
) {
66 v8::Isolate
* isolate
= v8::Isolate::GetCurrent();
67 v8::EscapableHandleScope
handle_scope(isolate
);
68 v8::Context::Scope
context_scope(GetOrCreateContext(isolate
));
70 v8::Local
<v8::Array
> v8_apis(v8::Array::New(isolate
, apis
.size()));
72 for (std::vector
<std::string
>::const_iterator i
= apis
.begin();
75 v8_apis
->Set(api_index
++, GetSchema(*i
));
77 return handle_scope
.Escape(v8_apis
);
80 v8::Local
<v8::Object
> V8SchemaRegistry::GetSchema(const std::string
& api
) {
81 if (schema_cache_
!= NULL
) {
82 v8::Local
<v8::Object
> cached_schema
= schema_cache_
->Get(api
);
83 if (!cached_schema
.IsEmpty()) {
88 // Slow path: Need to build schema first.
90 v8::Isolate
* isolate
= v8::Isolate::GetCurrent();
91 v8::EscapableHandleScope
handle_scope(isolate
);
92 v8::Local
<v8::Context
> context
= GetOrCreateContext(isolate
);
93 v8::Context::Scope
context_scope(context
);
95 const base::DictionaryValue
* schema
=
96 ExtensionAPI::GetSharedInstance()->GetSchema(api
);
98 scoped_ptr
<V8ValueConverter
> v8_value_converter(V8ValueConverter::create());
99 v8::Local
<v8::Value
> value
= v8_value_converter
->ToV8Value(schema
, context
);
100 CHECK(!value
.IsEmpty());
102 v8::Local
<v8::Object
> v8_schema(v8::Local
<v8::Object
>::Cast(value
));
103 schema_cache_
->Set(api
, v8_schema
);
105 return handle_scope
.Escape(v8_schema
);
108 v8::Local
<v8::Context
> V8SchemaRegistry::GetOrCreateContext(
109 v8::Isolate
* isolate
) {
110 // It's ok to create local handles in this function, since this is only called
111 // when we have a HandleScope.
112 if (!context_holder_
) {
113 context_holder_
.reset(new gin::ContextHolder(isolate
));
114 context_holder_
->SetContext(v8::Context::New(isolate
));
115 schema_cache_
.reset(new SchemaCache(isolate
));
116 return context_holder_
->context();
118 return context_holder_
->context();
121 } // namespace extensions