| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/extensions/v8_schema_registry.h" | 5 #include "chrome/renderer/extensions/v8_schema_registry.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/common/extensions/api/extension_api.h" | 9 #include "chrome/common/extensions/api/extension_api.h" |
| 10 #include "content/public/renderer/v8_value_converter.h" | 10 #include "content/public/renderer/v8_value_converter.h" |
| 11 | 11 |
| 12 using content::V8ValueConverter; | 12 using content::V8ValueConverter; |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 | 15 |
| 16 V8SchemaRegistry::V8SchemaRegistry() {} | 16 V8SchemaRegistry::V8SchemaRegistry() {} |
| 17 | 17 |
| 18 V8SchemaRegistry::~V8SchemaRegistry() { | 18 V8SchemaRegistry::~V8SchemaRegistry() { |
| 19 v8::HandleScope handle_scope; | 19 v8::HandleScope handle_scope; |
| 20 |
| 20 for (SchemaCache::iterator i = schema_cache_.begin(); | 21 for (SchemaCache::iterator i = schema_cache_.begin(); |
| 21 i != schema_cache_.end(); ++i) { | 22 i != schema_cache_.end(); ++i) { |
| 22 i->second.Dispose(i->second->CreationContext()->GetIsolate()); | 23 i->second.Dispose(i->second->CreationContext()->GetIsolate()); |
| 23 } | 24 } |
| 24 } | 25 } |
| 25 | 26 |
| 26 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( | 27 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( |
| 27 const std::set<std::string>& apis) { | 28 const std::set<std::string>& apis) { |
| 28 v8::HandleScope handle_scope; | 29 v8::HandleScope handle_scope; |
| 29 v8::Context::Scope context_scope(GetOrCreateContext()); | 30 v8::Context::Scope context_scope(GetOrCreateContext()); |
| 31 |
| 30 v8::Handle<v8::Array> v8_apis(v8::Array::New(apis.size())); | 32 v8::Handle<v8::Array> v8_apis(v8::Array::New(apis.size())); |
| 31 size_t api_index = 0; | 33 size_t api_index = 0; |
| 32 for (std::set<std::string>::const_iterator i = apis.begin(); i != apis.end(); | 34 for (std::set<std::string>::const_iterator i = apis.begin(); i != apis.end(); |
| 33 ++i) { | 35 ++i) { |
| 34 v8_apis->Set(api_index++, GetSchema(*i)); | 36 v8_apis->Set(api_index++, GetSchema(*i)); |
| 35 } | 37 } |
| 36 return v8_apis; | 38 return handle_scope.Close(v8_apis); |
| 37 } | 39 } |
| 38 | 40 |
| 39 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { | 41 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { |
| 40 v8::HandleScope handle_scope; | 42 v8::HandleScope handle_scope; |
| 43 |
| 41 SchemaCache::iterator maybe_schema = schema_cache_.find(api); | 44 SchemaCache::iterator maybe_schema = schema_cache_.find(api); |
| 42 if (maybe_schema != schema_cache_.end()) | 45 if (maybe_schema != schema_cache_.end()) |
| 43 return maybe_schema->second; | 46 return handle_scope.Close(maybe_schema->second); |
| 47 |
| 48 v8::Persistent<v8::Context> context = GetOrCreateContext(); |
| 49 v8::Context::Scope context_scope(context); |
| 44 | 50 |
| 45 const base::DictionaryValue* schema = | 51 const base::DictionaryValue* schema = |
| 46 ExtensionAPI::GetSharedInstance()->GetSchema(api); | 52 ExtensionAPI::GetSharedInstance()->GetSchema(api); |
| 47 CHECK(schema) << api; | 53 CHECK(schema) << api; |
| 48 | |
| 49 v8::Persistent<v8::Context> context = GetOrCreateContext(); | |
| 50 v8::Context::Scope context_scope(context); | |
| 51 | |
| 52 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); | 54 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); |
| 53 v8::Handle<v8::Value> value = v8_value_converter->ToV8Value(schema, context); | 55 v8::Handle<v8::Value> value = v8_value_converter->ToV8Value(schema, context); |
| 54 CHECK(!value.IsEmpty()); | 56 CHECK(!value.IsEmpty()); |
| 55 | 57 |
| 56 v8::Persistent<v8::Object> v8_schema = v8::Persistent<v8::Object>::New( | 58 v8::Persistent<v8::Object> v8_schema = v8::Persistent<v8::Object>::New( |
| 57 context->GetIsolate(), v8::Handle<v8::Object>::Cast(value)); | 59 context->GetIsolate(), v8::Handle<v8::Object>::Cast(value)); |
| 58 schema_cache_[api] = v8_schema; | 60 schema_cache_[api] = v8_schema; |
| 59 return v8_schema; | 61 return handle_scope.Close(v8_schema); |
| 60 } | 62 } |
| 61 | 63 |
| 62 v8::Persistent<v8::Context> V8SchemaRegistry::GetOrCreateContext() { | 64 v8::Persistent<v8::Context> V8SchemaRegistry::GetOrCreateContext() { |
| 63 if (context_.get().IsEmpty()) | 65 if (context_.get().IsEmpty()) |
| 64 context_.reset(v8::Context::New()); | 66 context_.reset(v8::Context::New()); |
| 65 return context_.get(); | 67 return context_.get(); |
| 66 } | 68 } |
| 67 | 69 |
| 68 } // namespace extensions | 70 } // namespace extensions |
| OLD | NEW |