Chromium Code Reviews| 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 // TODO(kalman): NOTE TO REVIEWER: this had an "!i->second.IsEmpty()" check | |
| 22 // around it before, but I don't see how this could be false, so yeah... | |
|
Aaron Boodman
2012/03/07 21:07:10
Makes sense. Note: for these kind of review commen
not at google - send to devlin
2012/03/08 00:00:33
Ok.
| |
| 23 i->second.Dispose(); | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( | |
| 28 const std::set<std::string>& apis) { | |
| 29 v8::Context::Scope context_scope(context_); | |
| 30 v8::Handle<v8::Array> v8_apis(v8::Array::New(apis.size())); | |
| 31 size_t api_index = 0; | |
| 32 for (std::set<std::string>::const_iterator i = apis.begin(); i != apis.end(); | |
| 33 ++i) { | |
| 34 v8_apis->Set(api_index++, GetSchema(*i)); | |
| 35 } | |
| 36 return v8_apis; | |
| 37 } | |
| 38 | |
| 39 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { | |
| 40 SchemaCache::iterator maybe_schema = schema_cache_.find(api); | |
| 41 if (maybe_schema != schema_cache_.end()) | |
| 42 return maybe_schema->second; | |
| 43 | |
| 44 const base::DictionaryValue* schema = | |
| 45 ExtensionAPI::GetInstance()->GetSchema(api); | |
| 46 CHECK(schema) << api; | |
| 47 | |
| 48 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); | |
| 49 v8::Persistent<v8::Object> v8_schema = | |
| 50 v8::Persistent<v8::Object>::New(v8::Handle<v8::Object>::Cast( | |
| 51 v8_value_converter->ToV8Value(schema, context_))); | |
| 52 CHECK(!v8_schema.IsEmpty()); | |
| 53 schema_cache_[api] = v8_schema; | |
| 54 return v8_schema; | |
| 55 } | |
| 56 | |
| 57 } // namespace extensions | |
| OLD | NEW |