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