Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / renderer / extensions / v8_schema_registry.cc
blob5aa785f4d12d092c7c13e9c132fed60c6e9951a2
1 // Copyright (c) 2012 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 "chrome/renderer/extensions/v8_schema_registry.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chrome/common/extensions/api/extension_api.h"
10 #include "content/public/renderer/v8_value_converter.h"
12 using content::V8ValueConverter;
14 namespace extensions {
16 V8SchemaRegistry::V8SchemaRegistry() {}
18 V8SchemaRegistry::~V8SchemaRegistry() {
19 v8::HandleScope handle_scope;
21 for (SchemaCache::iterator i = schema_cache_.begin();
22 i != schema_cache_.end(); ++i) {
23 i->second.Dispose(i->second->CreationContext()->GetIsolate());
27 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas(
28 const std::set<std::string>& apis) {
29 v8::HandleScope handle_scope;
30 v8::Context::Scope context_scope(GetOrCreateContext());
32 v8::Handle<v8::Array> v8_apis(v8::Array::New(apis.size()));
33 size_t api_index = 0;
34 for (std::set<std::string>::const_iterator i = apis.begin(); i != apis.end();
35 ++i) {
36 v8_apis->Set(api_index++, GetSchema(*i));
38 return handle_scope.Close(v8_apis);
41 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) {
42 v8::HandleScope handle_scope;
44 SchemaCache::iterator maybe_schema = schema_cache_.find(api);
45 if (maybe_schema != schema_cache_.end())
46 return handle_scope.Close(maybe_schema->second);
48 v8::Handle<v8::Context> context = GetOrCreateContext();
49 v8::Context::Scope context_scope(context);
51 const base::DictionaryValue* schema =
52 ExtensionAPI::GetSharedInstance()->GetSchema(api);
53 CHECK(schema) << api;
54 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create());
55 v8::Handle<v8::Value> value = v8_value_converter->ToV8Value(schema, context);
56 CHECK(!value.IsEmpty());
58 v8::Persistent<v8::Object> v8_schema(context->GetIsolate(),
59 v8::Handle<v8::Object>::Cast(value));
60 schema_cache_[api] = v8_schema;
61 return handle_scope.Close(v8_schema);
64 v8::Handle<v8::Context> V8SchemaRegistry::GetOrCreateContext() {
65 // It's ok to create local handles in this function, since this is only called
66 // when we have a HandleScope.
67 if (context_.get().IsEmpty()) {
68 v8::Handle<v8::Context> context =
69 v8::Context::New(v8::Isolate::GetCurrent());
70 context_.reset(context);
71 return context;
73 return v8::Local<v8::Context>::New(context_.get());
76 } // namespace extensions