| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/public/child/v8_value_converter.h" | 13 #include "content/public/child/v8_value_converter.h" |
| 14 #include "extensions/common/extension_api.h" | 14 #include "extensions/common/extension_api.h" |
| 15 #include "extensions/renderer/object_backed_native_handler.h" | 15 #include "extensions/renderer/object_backed_native_handler.h" |
| 16 #include "extensions/renderer/script_context.h" | 16 #include "extensions/renderer/script_context.h" |
| 17 | 17 |
| 18 using content::V8ValueConverter; | 18 using content::V8ValueConverter; |
| 19 | 19 |
| 20 namespace extensions { | 20 namespace extensions { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { | 24 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { |
| 25 public: | 25 public: |
| 26 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, | 26 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, |
| 27 scoped_ptr<ScriptContext> context) | 27 std::unique_ptr<ScriptContext> context) |
| 28 : ObjectBackedNativeHandler(context.get()), | 28 : ObjectBackedNativeHandler(context.get()), |
| 29 context_(std::move(context)), | 29 context_(std::move(context)), |
| 30 registry_(registry) { | 30 registry_(registry) { |
| 31 RouteFunction("GetSchema", | 31 RouteFunction("GetSchema", |
| 32 base::Bind(&SchemaRegistryNativeHandler::GetSchema, | 32 base::Bind(&SchemaRegistryNativeHandler::GetSchema, |
| 33 base::Unretained(this))); | 33 base::Unretained(this))); |
| 34 } | 34 } |
| 35 | 35 |
| 36 ~SchemaRegistryNativeHandler() override { context_->Invalidate(); } | 36 ~SchemaRegistryNativeHandler() override { context_->Invalidate(); } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 void GetSchema(const v8::FunctionCallbackInfo<v8::Value>& args) { | 39 void GetSchema(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 40 args.GetReturnValue().Set( | 40 args.GetReturnValue().Set( |
| 41 registry_->GetSchema(*v8::String::Utf8Value(args[0]))); | 41 registry_->GetSchema(*v8::String::Utf8Value(args[0]))); |
| 42 } | 42 } |
| 43 | 43 |
| 44 scoped_ptr<ScriptContext> context_; | 44 std::unique_ptr<ScriptContext> context_; |
| 45 V8SchemaRegistry* registry_; | 45 V8SchemaRegistry* registry_; |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 V8SchemaRegistry::V8SchemaRegistry() { | 50 V8SchemaRegistry::V8SchemaRegistry() { |
| 51 } | 51 } |
| 52 | 52 |
| 53 V8SchemaRegistry::~V8SchemaRegistry() { | 53 V8SchemaRegistry::~V8SchemaRegistry() { |
| 54 } | 54 } |
| 55 | 55 |
| 56 scoped_ptr<NativeHandler> V8SchemaRegistry::AsNativeHandler() { | 56 std::unique_ptr<NativeHandler> V8SchemaRegistry::AsNativeHandler() { |
| 57 scoped_ptr<ScriptContext> context( | 57 std::unique_ptr<ScriptContext> context( |
| 58 new ScriptContext(GetOrCreateContext(v8::Isolate::GetCurrent()), | 58 new ScriptContext(GetOrCreateContext(v8::Isolate::GetCurrent()), |
| 59 NULL, // no frame | 59 NULL, // no frame |
| 60 NULL, // no extension | 60 NULL, // no extension |
| 61 Feature::UNSPECIFIED_CONTEXT, | 61 Feature::UNSPECIFIED_CONTEXT, |
| 62 NULL, // no effective extension | 62 NULL, // no effective extension |
| 63 Feature::UNSPECIFIED_CONTEXT)); | 63 Feature::UNSPECIFIED_CONTEXT)); |
| 64 return scoped_ptr<NativeHandler>( | 64 return std::unique_ptr<NativeHandler>( |
| 65 new SchemaRegistryNativeHandler(this, std::move(context))); | 65 new SchemaRegistryNativeHandler(this, std::move(context))); |
| 66 } | 66 } |
| 67 | 67 |
| 68 v8::Local<v8::Array> V8SchemaRegistry::GetSchemas( | 68 v8::Local<v8::Array> V8SchemaRegistry::GetSchemas( |
| 69 const std::vector<std::string>& apis) { | 69 const std::vector<std::string>& apis) { |
| 70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 71 v8::EscapableHandleScope handle_scope(isolate); | 71 v8::EscapableHandleScope handle_scope(isolate); |
| 72 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); | 72 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); |
| 73 | 73 |
| 74 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); | 74 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 92 // Slow path: Need to build schema first. | 92 // Slow path: Need to build schema first. |
| 93 | 93 |
| 94 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 94 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 95 v8::EscapableHandleScope handle_scope(isolate); | 95 v8::EscapableHandleScope handle_scope(isolate); |
| 96 v8::Local<v8::Context> context = GetOrCreateContext(isolate); | 96 v8::Local<v8::Context> context = GetOrCreateContext(isolate); |
| 97 v8::Context::Scope context_scope(context); | 97 v8::Context::Scope context_scope(context); |
| 98 | 98 |
| 99 const base::DictionaryValue* schema = | 99 const base::DictionaryValue* schema = |
| 100 ExtensionAPI::GetSharedInstance()->GetSchema(api); | 100 ExtensionAPI::GetSharedInstance()->GetSchema(api); |
| 101 CHECK(schema) << api; | 101 CHECK(schema) << api; |
| 102 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); | 102 std::unique_ptr<V8ValueConverter> v8_value_converter( |
| 103 V8ValueConverter::create()); |
| 103 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context); | 104 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context); |
| 104 CHECK(!value.IsEmpty()); | 105 CHECK(!value.IsEmpty()); |
| 105 | 106 |
| 106 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value)); | 107 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value)); |
| 107 v8_schema->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen); | 108 v8_schema->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen); |
| 108 schema_cache_->Set(api, v8_schema); | 109 schema_cache_->Set(api, v8_schema); |
| 109 | 110 |
| 110 return handle_scope.Escape(v8_schema); | 111 return handle_scope.Escape(v8_schema); |
| 111 } | 112 } |
| 112 | 113 |
| 113 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( | 114 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( |
| 114 v8::Isolate* isolate) { | 115 v8::Isolate* isolate) { |
| 115 // It's ok to create local handles in this function, since this is only called | 116 // It's ok to create local handles in this function, since this is only called |
| 116 // when we have a HandleScope. | 117 // when we have a HandleScope. |
| 117 if (!context_holder_) { | 118 if (!context_holder_) { |
| 118 context_holder_.reset(new gin::ContextHolder(isolate)); | 119 context_holder_.reset(new gin::ContextHolder(isolate)); |
| 119 context_holder_->SetContext(v8::Context::New(isolate)); | 120 context_holder_->SetContext(v8::Context::New(isolate)); |
| 120 schema_cache_.reset(new SchemaCache(isolate)); | 121 schema_cache_.reset(new SchemaCache(isolate)); |
| 121 return context_holder_->context(); | 122 return context_holder_->context(); |
| 122 } | 123 } |
| 123 return context_holder_->context(); | 124 return context_holder_->context(); |
| 124 } | 125 } |
| 125 | 126 |
| 126 } // namespace extensions | 127 } // namespace extensions |
| OLD | NEW |