| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/renderer/extensions/v8_schema_registry.h" |
| 6 |
| 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" |
| 11 |
| 12 using content::V8ValueConverter; |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 V8SchemaRegistry::V8SchemaRegistry() : context_(v8::Context::New()) {} |
| 17 |
| 18 V8SchemaRegistry::~V8SchemaRegistry() { |
| 19 for (SchemaCache::iterator i = schema_cache_.begin(); |
| 20 i != schema_cache_.end(); ++i) { |
| 21 i->second.Dispose(); |
| 22 } |
| 23 context_.Dispose(); |
| 24 } |
| 25 |
| 26 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( |
| 27 const std::set<std::string>& apis) { |
| 28 v8::Context::Scope context_scope(context_); |
| 29 v8::Handle<v8::Array> v8_apis(v8::Array::New(apis.size())); |
| 30 size_t api_index = 0; |
| 31 for (std::set<std::string>::const_iterator i = apis.begin(); i != apis.end(); |
| 32 ++i) { |
| 33 v8_apis->Set(api_index++, GetSchema(*i)); |
| 34 } |
| 35 return v8_apis; |
| 36 } |
| 37 |
| 38 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { |
| 39 SchemaCache::iterator maybe_schema = schema_cache_.find(api); |
| 40 if (maybe_schema != schema_cache_.end()) |
| 41 return maybe_schema->second; |
| 42 |
| 43 const base::DictionaryValue* schema = |
| 44 ExtensionAPI::GetInstance()->GetSchema(api); |
| 45 CHECK(schema) << api; |
| 46 |
| 47 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); |
| 48 v8::Persistent<v8::Object> v8_schema = |
| 49 v8::Persistent<v8::Object>::New(v8::Handle<v8::Object>::Cast( |
| 50 v8_value_converter->ToV8Value(schema, context_))); |
| 51 CHECK(!v8_schema.IsEmpty()); |
| 52 schema_cache_[api] = v8_schema; |
| 53 return v8_schema; |
| 54 } |
| 55 |
| 56 } // namespace extensions |
| OLD | NEW |