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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "content/public/child/v8_value_converter.h" | 9 #include "content/public/child/v8_value_converter.h" |
10 #include "extensions/common/extension_api.h" | 10 #include "extensions/common/extension_api.h" |
11 #include "extensions/renderer/object_backed_native_handler.h" | 11 #include "extensions/renderer/object_backed_native_handler.h" |
12 #include "extensions/renderer/script_context.h" | 12 #include "extensions/renderer/script_context.h" |
| 13 #include "extensions/renderer/v8_maybe_helpers.h" |
13 | 14 |
14 using content::V8ValueConverter; | 15 using content::V8ValueConverter; |
15 | 16 |
16 namespace extensions { | 17 namespace extensions { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { | 21 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { |
21 public: | 22 public: |
22 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, | 23 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 NULL, // no effective extension | 59 NULL, // no effective extension |
59 Feature::UNSPECIFIED_CONTEXT)); | 60 Feature::UNSPECIFIED_CONTEXT)); |
60 return scoped_ptr<NativeHandler>( | 61 return scoped_ptr<NativeHandler>( |
61 new SchemaRegistryNativeHandler(this, context.Pass())); | 62 new SchemaRegistryNativeHandler(this, context.Pass())); |
62 } | 63 } |
63 | 64 |
64 v8::Local<v8::Array> V8SchemaRegistry::GetSchemas( | 65 v8::Local<v8::Array> V8SchemaRegistry::GetSchemas( |
65 const std::vector<std::string>& apis) { | 66 const std::vector<std::string>& apis) { |
66 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 67 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
67 v8::EscapableHandleScope handle_scope(isolate); | 68 v8::EscapableHandleScope handle_scope(isolate); |
68 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); | 69 v8::Local<v8::Context> context = GetOrCreateContext(isolate); |
| 70 v8::Context::Scope context_scope(context); |
69 | 71 |
70 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); | 72 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); |
71 size_t api_index = 0; | 73 size_t api_index = 0; |
72 for (std::vector<std::string>::const_iterator i = apis.begin(); | 74 for (std::vector<std::string>::const_iterator i = apis.begin(); |
73 i != apis.end(); | 75 i != apis.end(); |
74 ++i) { | 76 ++i) { |
75 v8_apis->Set(api_index++, GetSchema(*i)); | 77 SetProperty(context, v8_apis, api_index++, GetSchema(*i)); |
76 } | 78 } |
77 return handle_scope.Escape(v8_apis); | 79 return handle_scope.Escape(v8_apis); |
78 } | 80 } |
79 | 81 |
80 v8::Local<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { | 82 v8::Local<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { |
81 if (schema_cache_ != NULL) { | 83 if (schema_cache_ != NULL) { |
82 v8::Local<v8::Object> cached_schema = schema_cache_->Get(api); | 84 v8::Local<v8::Object> cached_schema = schema_cache_->Get(api); |
83 if (!cached_schema.IsEmpty()) { | 85 if (!cached_schema.IsEmpty()) { |
84 return cached_schema; | 86 return cached_schema; |
85 } | 87 } |
(...skipping 26 matching lines...) Expand all Loading... |
112 if (!context_holder_) { | 114 if (!context_holder_) { |
113 context_holder_.reset(new gin::ContextHolder(isolate)); | 115 context_holder_.reset(new gin::ContextHolder(isolate)); |
114 context_holder_->SetContext(v8::Context::New(isolate)); | 116 context_holder_->SetContext(v8::Context::New(isolate)); |
115 schema_cache_.reset(new SchemaCache(isolate)); | 117 schema_cache_.reset(new SchemaCache(isolate)); |
116 return context_holder_->context(); | 118 return context_holder_->context(); |
117 } | 119 } |
118 return context_holder_->context(); | 120 return context_holder_->context(); |
119 } | 121 } |
120 | 122 |
121 } // namespace extensions | 123 } // namespace extensions |
OLD | NEW |