Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(784)

Side by Side Diff: extensions/renderer/v8_schema_registry.cc

Issue 1906593002: [Extensions] Finish freezing schema (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/renderer/safe_builtins.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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) {
robwu 2016/04/21 14:07:50 As explained at https://crbug.com/604901#c8, add s
Devlin 2016/04/21 22:07:36 Done. This also has the happy side-effect of maki
27 v8::Local<v8::Array> property_names = object->GetOwnPropertyNames();
28 for (uint32_t i = 0; i < property_names->Length(); ++i) {
29 v8::Local<v8::Value> child = object->Get(property_names->Get(i));
30 if (child->IsObject())
31 DeepFreeze(v8::Local<v8::Object>::Cast(child), context);
32 }
33 object->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen);
34 }
35
24 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { 36 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler {
25 public: 37 public:
26 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, 38 SchemaRegistryNativeHandler(V8SchemaRegistry* registry,
27 scoped_ptr<ScriptContext> context) 39 scoped_ptr<ScriptContext> context)
28 : ObjectBackedNativeHandler(context.get()), 40 : ObjectBackedNativeHandler(context.get()),
29 context_(std::move(context)), 41 context_(std::move(context)),
30 registry_(registry) { 42 registry_(registry) {
31 RouteFunction("GetSchema", 43 RouteFunction("GetSchema",
32 base::Bind(&SchemaRegistryNativeHandler::GetSchema, 44 base::Bind(&SchemaRegistryNativeHandler::GetSchema,
33 base::Unretained(this))); 45 base::Unretained(this)));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 v8::Context::Scope context_scope(context); 109 v8::Context::Scope context_scope(context);
98 110
99 const base::DictionaryValue* schema = 111 const base::DictionaryValue* schema =
100 ExtensionAPI::GetSharedInstance()->GetSchema(api); 112 ExtensionAPI::GetSharedInstance()->GetSchema(api);
101 CHECK(schema) << api; 113 CHECK(schema) << api;
102 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); 114 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create());
103 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context); 115 v8::Local<v8::Value> value = v8_value_converter->ToV8Value(schema, context);
104 CHECK(!value.IsEmpty()); 116 CHECK(!value.IsEmpty());
105 117
106 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value)); 118 v8::Local<v8::Object> v8_schema(v8::Local<v8::Object>::Cast(value));
107 v8_schema->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen); 119 DeepFreeze(v8_schema, context);
108 schema_cache_->Set(api, v8_schema); 120 schema_cache_->Set(api, v8_schema);
109 121
110 return handle_scope.Escape(v8_schema); 122 return handle_scope.Escape(v8_schema);
111 } 123 }
112 124
113 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext( 125 v8::Local<v8::Context> V8SchemaRegistry::GetOrCreateContext(
114 v8::Isolate* isolate) { 126 v8::Isolate* isolate) {
115 // It's ok to create local handles in this function, since this is only called 127 // It's ok to create local handles in this function, since this is only called
116 // when we have a HandleScope. 128 // when we have a HandleScope.
117 if (!context_holder_) { 129 if (!context_holder_) {
118 context_holder_.reset(new gin::ContextHolder(isolate)); 130 context_holder_.reset(new gin::ContextHolder(isolate));
119 context_holder_->SetContext(v8::Context::New(isolate)); 131 context_holder_->SetContext(v8::Context::New(isolate));
120 schema_cache_.reset(new SchemaCache(isolate)); 132 schema_cache_.reset(new SchemaCache(isolate));
121 return context_holder_->context(); 133 return context_holder_->context();
122 } 134 }
123 return context_holder_->context(); 135 return context_holder_->context();
124 } 136 }
125 137
126 } // namespace extensions 138 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/safe_builtins.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698