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 #include "extensions/renderer/static_v8_external_one_byte_string_resource.h" |
17 #include "extensions/renderer/v8_helpers.h" | 18 #include "extensions/renderer/v8_helpers.h" |
18 | 19 |
19 using content::V8ValueConverter; | 20 using content::V8ValueConverter; |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Recursively freezes every v8 object on |object|. | 26 // Recursively freezes every v8 object on |object|. |
26 void DeepFreeze(const v8::Local<v8::Object>& object, | 27 void DeepFreeze(const v8::Local<v8::Object>& object, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 } | 123 } |
123 } | 124 } |
124 | 125 |
125 // Slow path: Need to build schema first. | 126 // Slow path: Need to build schema first. |
126 | 127 |
127 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 128 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
128 v8::EscapableHandleScope handle_scope(isolate); | 129 v8::EscapableHandleScope handle_scope(isolate); |
129 v8::Local<v8::Context> context = GetOrCreateContext(isolate); | 130 v8::Local<v8::Context> context = GetOrCreateContext(isolate); |
130 v8::Context::Scope context_scope(context); | 131 v8::Context::Scope context_scope(context); |
131 | 132 |
132 const base::DictionaryValue* schema = | 133 base::StringPiece schema_string = |
133 ExtensionAPI::GetSharedInstance()->GetSchema(api); | 134 ExtensionAPI::GetSharedInstance()->GetSchemaStringPiece(api); |
134 CHECK(schema) << api; | 135 CHECK(!schema_string.empty()); |
135 std::unique_ptr<V8ValueConverter> v8_value_converter( | 136 v8::MaybeLocal<v8::String> v8_maybe_string = |
136 V8ValueConverter::create()); | 137 v8::String::NewExternal( |
137 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context); | 138 isolate, new StaticV8ExternalOneByteStringResource(schema_string)); |
138 CHECK(!value.IsEmpty()); | 139 v8::Local<v8::String> v8_schema_string; |
| 140 CHECK(v8_maybe_string.ToLocal(&v8_schema_string)); |
139 | 141 |
140 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value)); | 142 v8::MaybeLocal<v8::Value> v8_maybe_schema_value = |
141 DeepFreeze(v8_schema, context); | 143 v8::JSON::Parse(context, v8_schema_string); |
142 schema_cache_->Set(api, v8_schema); | 144 v8::Local<v8::Value> v8_schema_value; |
| 145 CHECK(v8_maybe_schema_value.ToLocal(&v8_schema_value)); |
| 146 CHECK(v8_schema_value->IsObject()); |
143 | 147 |
144 return handle_scope.Escape(v8_schema); | 148 v8::Local<v8::Object> v8_schema_object( |
| 149 v8::Local<v8::Object>::Cast(v8_schema_value)); |
| 150 DeepFreeze(v8_schema_object, context); |
| 151 |
| 152 schema_cache_->Set(api, v8_schema_object); |
| 153 |
| 154 return handle_scope.Escape(v8_schema_object); |
145 } | 155 } |
146 | 156 |
147 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( | 157 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( |
148 v8::Isolate* isolate) { | 158 v8::Isolate* isolate) { |
149 // It's ok to create local handles in this function, since this is only called | 159 // It's ok to create local handles in this function, since this is only called |
150 // when we have a HandleScope. | 160 // when we have a HandleScope. |
151 if (!context_holder_) { | 161 if (!context_holder_) { |
152 context_holder_.reset(new gin::ContextHolder(isolate)); | 162 context_holder_.reset(new gin::ContextHolder(isolate)); |
153 context_holder_->SetContext(v8::Context::New(isolate)); | 163 context_holder_->SetContext(v8::Context::New(isolate)); |
154 schema_cache_.reset(new SchemaCache(isolate)); | 164 schema_cache_.reset(new SchemaCache(isolate)); |
155 return context_holder_->context(); | 165 return context_holder_->context(); |
156 } | 166 } |
157 return context_holder_->context(); | 167 return context_holder_->context(); |
158 } | 168 } |
159 | 169 |
160 } // namespace extensions | 170 } // namespace extensions |
OLD | NEW |