| 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 // Recursively freezes every v8 object on |object|. | |
| 25 void DeepFreeze(const v8::Local<v8::Object>& object, | |
| 26 const v8::Local<v8::Context>& context) { | |
| 27 // Don't let the object trace upwards via the prototype. | |
| 28 v8::Maybe<bool> maybe = | |
| 29 object->SetPrototype(context, v8::Null(context->GetIsolate())); | |
| 30 CHECK(maybe.IsJust() && maybe.FromJust()); | |
| 31 v8::Local<v8::Array> property_names = object->GetOwnPropertyNames(); | |
| 32 for (uint32_t i = 0; i < property_names->Length(); ++i) { | |
| 33 v8::Local<v8::Value> child = object->Get(property_names->Get(i)); | |
| 34 if (child->IsObject()) | |
| 35 DeepFreeze(v8::Local<v8::Object>::Cast(child), context); | |
| 36 } | |
| 37 object->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen); | |
| 38 } | |
| 39 | |
| 40 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { | 24 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { |
| 41 public: | 25 public: |
| 42 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, | 26 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, |
| 43 scoped_ptr<ScriptContext> context) | 27 scoped_ptr<ScriptContext> context) |
| 44 : ObjectBackedNativeHandler(context.get()), | 28 : ObjectBackedNativeHandler(context.get()), |
| 45 context_(std::move(context)), | 29 context_(std::move(context)), |
| 46 registry_(registry) { | 30 registry_(registry) { |
| 47 RouteFunction("GetSchema", | 31 RouteFunction("GetSchema", |
| 48 base::Bind(&SchemaRegistryNativeHandler::GetSchema, | 32 base::Bind(&SchemaRegistryNativeHandler::GetSchema, |
| 49 base::Unretained(this))); | 33 base::Unretained(this))); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 v8::Context::Scope context_scope(context); | 97 v8::Context::Scope context_scope(context); |
| 114 | 98 |
| 115 const base::DictionaryValue* schema = | 99 const base::DictionaryValue* schema = |
| 116 ExtensionAPI::GetSharedInstance()->GetSchema(api); | 100 ExtensionAPI::GetSharedInstance()->GetSchema(api); |
| 117 CHECK(schema) << api; | 101 CHECK(schema) << api; |
| 118 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); | 102 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); |
| 119 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context); | 103 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context); |
| 120 CHECK(!value.IsEmpty()); | 104 CHECK(!value.IsEmpty()); |
| 121 | 105 |
| 122 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value)); | 106 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value)); |
| 123 DeepFreeze(v8_schema, context); | 107 v8_schema->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen); |
| 124 schema_cache_->Set(api, v8_schema); | 108 schema_cache_->Set(api, v8_schema); |
| 125 | 109 |
| 126 return handle_scope.Escape(v8_schema); | 110 return handle_scope.Escape(v8_schema); |
| 127 } | 111 } |
| 128 | 112 |
| 129 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( | 113 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( |
| 130 v8::Isolate* isolate) { | 114 v8::Isolate* isolate) { |
| 131 // It's ok to create local handles in this function, since this is only called | 115 // It's ok to create local handles in this function, since this is only called |
| 132 // when we have a HandleScope. | 116 // when we have a HandleScope. |
| 133 if (!context_holder_) { | 117 if (!context_holder_) { |
| 134 context_holder_.reset(new gin::ContextHolder(isolate)); | 118 context_holder_.reset(new gin::ContextHolder(isolate)); |
| 135 context_holder_->SetContext(v8::Context::New(isolate)); | 119 context_holder_->SetContext(v8::Context::New(isolate)); |
| 136 schema_cache_.reset(new SchemaCache(isolate)); | 120 schema_cache_.reset(new SchemaCache(isolate)); |
| 137 return context_holder_->context(); | 121 return context_holder_->context(); |
| 138 } | 122 } |
| 139 return context_holder_->context(); | 123 return context_holder_->context(); |
| 140 } | 124 } |
| 141 | 125 |
| 142 } // namespace extensions | 126 } // namespace extensions |
| OLD | NEW |