| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/renderer/v8_schema_registry.h" | 5 #include "extensions/renderer/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 "content/public/child/v8_value_converter.h" | 9 #include "content/public/child/v8_value_converter.h" |
| 10 #include "extensions/common/extension_api.h" | 10 #include "extensions/common/extension_api.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 new ScriptContext(GetOrCreateContext(v8::Isolate::GetCurrent()), | 54 new ScriptContext(GetOrCreateContext(v8::Isolate::GetCurrent()), |
| 55 NULL, // no frame | 55 NULL, // no frame |
| 56 NULL, // no extension | 56 NULL, // no extension |
| 57 Feature::UNSPECIFIED_CONTEXT, | 57 Feature::UNSPECIFIED_CONTEXT, |
| 58 NULL, // no effective extension | 58 NULL, // no effective extension |
| 59 Feature::UNSPECIFIED_CONTEXT)); | 59 Feature::UNSPECIFIED_CONTEXT)); |
| 60 return scoped_ptr<NativeHandler>( | 60 return scoped_ptr<NativeHandler>( |
| 61 new SchemaRegistryNativeHandler(this, context.Pass())); | 61 new SchemaRegistryNativeHandler(this, context.Pass())); |
| 62 } | 62 } |
| 63 | 63 |
| 64 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( | 64 v8::Local<v8::Array> V8SchemaRegistry::GetSchemas( |
| 65 const std::vector<std::string>& apis) { | 65 const std::vector<std::string>& apis) { |
| 66 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 66 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 67 v8::EscapableHandleScope handle_scope(isolate); | 67 v8::EscapableHandleScope handle_scope(isolate); |
| 68 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); | 68 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); |
| 69 | 69 |
| 70 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); | 70 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); |
| 71 size_t api_index = 0; | 71 size_t api_index = 0; |
| 72 for (std::vector<std::string>::const_iterator i = apis.begin(); | 72 for (std::vector<std::string>::const_iterator i = apis.begin(); |
| 73 i != apis.end(); | 73 i != apis.end(); |
| 74 ++i) { | 74 ++i) { |
| 75 v8_apis->Set(api_index++, GetSchema(*i)); | 75 v8_apis->Set(api_index++, GetSchema(*i)); |
| 76 } | 76 } |
| 77 return handle_scope.Escape(v8_apis); | 77 return handle_scope.Escape(v8_apis); |
| 78 } | 78 } |
| 79 | 79 |
| 80 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { | 80 v8::Local<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { |
| 81 if (schema_cache_ != NULL) { | 81 if (schema_cache_ != NULL) { |
| 82 v8::Local<v8::Object> cached_schema = schema_cache_->Get(api); | 82 v8::Local<v8::Object> cached_schema = schema_cache_->Get(api); |
| 83 if (!cached_schema.IsEmpty()) { | 83 if (!cached_schema.IsEmpty()) { |
| 84 return cached_schema; | 84 return cached_schema; |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Slow path: Need to build schema first. | 88 // Slow path: Need to build schema first. |
| 89 | 89 |
| 90 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 90 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 91 v8::EscapableHandleScope handle_scope(isolate); | 91 v8::EscapableHandleScope handle_scope(isolate); |
| 92 v8::Handle<v8::Context> context = GetOrCreateContext(isolate); | 92 v8::Local<v8::Context> context = GetOrCreateContext(isolate); |
| 93 v8::Context::Scope context_scope(context); | 93 v8::Context::Scope context_scope(context); |
| 94 | 94 |
| 95 const base::DictionaryValue* schema = | 95 const base::DictionaryValue* schema = |
| 96 ExtensionAPI::GetSharedInstance()->GetSchema(api); | 96 ExtensionAPI::GetSharedInstance()->GetSchema(api); |
| 97 CHECK(schema) << api; | 97 CHECK(schema) << api; |
| 98 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); | 98 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); |
| 99 v8::Handle<v8::Value> value = v8_value_converter->ToV8Value(schema, context); | 99 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context); |
| 100 CHECK(!value.IsEmpty()); | 100 CHECK(!value.IsEmpty()); |
| 101 | 101 |
| 102 v8::Local<v8::Object> v8_schema(v8::Handle<v8::Object>::Cast(value)); | 102 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value)); |
| 103 schema_cache_->Set(api, v8_schema); | 103 schema_cache_->Set(api, v8_schema); |
| 104 | 104 |
| 105 return handle_scope.Escape(v8_schema); | 105 return handle_scope.Escape(v8_schema); |
| 106 } | 106 } |
| 107 | 107 |
| 108 v8::Handle<v8::Context> V8SchemaRegistry::GetOrCreateContext( | 108 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( |
| 109 v8::Isolate* isolate) { | 109 v8::Isolate* isolate) { |
| 110 // It's ok to create local handles in this function, since this is only called | 110 // It's ok to create local handles in this function, since this is only called |
| 111 // when we have a HandleScope. | 111 // when we have a HandleScope. |
| 112 if (!context_holder_) { | 112 if (!context_holder_) { |
| 113 context_holder_.reset(new gin::ContextHolder(isolate)); | 113 context_holder_.reset(new gin::ContextHolder(isolate)); |
| 114 context_holder_->SetContext(v8::Context::New(isolate)); | 114 context_holder_->SetContext(v8::Context::New(isolate)); |
| 115 schema_cache_.reset(new SchemaCache(isolate)); | 115 schema_cache_.reset(new SchemaCache(isolate)); |
| 116 return context_holder_->context(); | 116 return context_holder_->context(); |
| 117 } | 117 } |
| 118 return context_holder_->context(); | 118 return context_holder_->context(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 } // namespace extensions | 121 } // namespace extensions |
| OLD | NEW |