| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // TODO(dcarney): Remove this when UnsafePersistent is removed. | 5 // TODO(dcarney): Remove this when UnsafePersistent is removed. |
| 6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR | 6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR |
| 7 | 7 |
| 8 #include "chrome/renderer/extensions/v8_schema_registry.h" | 8 #include "chrome/renderer/extensions/v8_schema_registry.h" |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 context_(context.Pass()), | 28 context_(context.Pass()), |
| 29 registry_(registry) { | 29 registry_(registry) { |
| 30 RouteFunction("GetSchema", | 30 RouteFunction("GetSchema", |
| 31 base::Bind(&SchemaRegistryNativeHandler::GetSchema, | 31 base::Bind(&SchemaRegistryNativeHandler::GetSchema, |
| 32 base::Unretained(this))); | 32 base::Unretained(this))); |
| 33 } | 33 } |
| 34 | 34 |
| 35 private: | 35 private: |
| 36 void GetSchema(const v8::FunctionCallbackInfo<v8::Value>& args) { | 36 void GetSchema(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 37 args.GetReturnValue().Set( | 37 args.GetReturnValue().Set( |
| 38 registry_->GetSchema(*v8::String::AsciiValue(args[0]))); | 38 registry_->GetSchema(*v8::String::Utf8Value(args[0]))); |
| 39 } | 39 } |
| 40 | 40 |
| 41 scoped_ptr<ChromeV8Context> context_; | 41 scoped_ptr<ChromeV8Context> context_; |
| 42 V8SchemaRegistry* registry_; | 42 V8SchemaRegistry* registry_; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 } // namespace | 45 } // namespace |
| 46 | 46 |
| 47 V8SchemaRegistry::V8SchemaRegistry() {} | 47 V8SchemaRegistry::V8SchemaRegistry() {} |
| 48 | 48 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 59 NULL, // no frame | 59 NULL, // no frame |
| 60 NULL, // no extension | 60 NULL, // no extension |
| 61 Feature::UNSPECIFIED_CONTEXT)); | 61 Feature::UNSPECIFIED_CONTEXT)); |
| 62 return scoped_ptr<NativeHandler>( | 62 return scoped_ptr<NativeHandler>( |
| 63 new SchemaRegistryNativeHandler(this, context.Pass())); | 63 new SchemaRegistryNativeHandler(this, context.Pass())); |
| 64 } | 64 } |
| 65 | 65 |
| 66 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( | 66 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( |
| 67 const std::vector<std::string>& apis) { | 67 const std::vector<std::string>& apis) { |
| 68 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 68 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 69 v8::HandleScope handle_scope(isolate); | 69 v8::EscapableHandleScope handle_scope(isolate); |
| 70 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); | 70 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); |
| 71 | 71 |
| 72 v8::Handle<v8::Array> v8_apis(v8::Array::New(apis.size())); | 72 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); |
| 73 size_t api_index = 0; | 73 size_t api_index = 0; |
| 74 for (std::vector<std::string>::const_iterator i = apis.begin(); | 74 for (std::vector<std::string>::const_iterator i = apis.begin(); |
| 75 i != apis.end(); ++i) { | 75 i != apis.end(); ++i) { |
| 76 v8_apis->Set(api_index++, GetSchema(*i)); | 76 v8_apis->Set(api_index++, GetSchema(*i)); |
| 77 } | 77 } |
| 78 return handle_scope.Close(v8_apis); | 78 return handle_scope.Escape(v8_apis); |
| 79 } | 79 } |
| 80 | 80 |
| 81 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { | 81 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { |
| 82 | 82 |
| 83 SchemaCache::iterator maybe_schema = schema_cache_.find(api); | 83 SchemaCache::iterator maybe_schema = schema_cache_.find(api); |
| 84 if (maybe_schema != schema_cache_.end()) | 84 if (maybe_schema != schema_cache_.end()) |
| 85 return maybe_schema->second.newLocal(v8::Isolate::GetCurrent()); | 85 return maybe_schema->second.newLocal(v8::Isolate::GetCurrent()); |
| 86 | 86 |
| 87 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 87 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 88 v8::HandleScope handle_scope(isolate); | 88 v8::EscapableHandleScope handle_scope(isolate); |
| 89 v8::Handle<v8::Context> context = GetOrCreateContext(isolate); | 89 v8::Handle<v8::Context> context = GetOrCreateContext(isolate); |
| 90 v8::Context::Scope context_scope(context); | 90 v8::Context::Scope context_scope(context); |
| 91 | 91 |
| 92 const base::DictionaryValue* schema = | 92 const base::DictionaryValue* schema = |
| 93 ExtensionAPI::GetSharedInstance()->GetSchema(api); | 93 ExtensionAPI::GetSharedInstance()->GetSchema(api); |
| 94 CHECK(schema) << api; | 94 CHECK(schema) << api; |
| 95 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); | 95 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); |
| 96 v8::Handle<v8::Value> value = v8_value_converter->ToV8Value(schema, context); | 96 v8::Handle<v8::Value> value = v8_value_converter->ToV8Value(schema, context); |
| 97 CHECK(!value.IsEmpty()); | 97 CHECK(!value.IsEmpty()); |
| 98 | 98 |
| 99 v8::Persistent<v8::Object> v8_schema(context->GetIsolate(), | 99 v8::Persistent<v8::Object> v8_schema(context->GetIsolate(), |
| 100 v8::Handle<v8::Object>::Cast(value)); | 100 v8::Handle<v8::Object>::Cast(value)); |
| 101 v8::Local<v8::Object> to_return = | 101 v8::Local<v8::Object> to_return = |
| 102 v8::Local<v8::Object>::New(isolate, v8_schema); | 102 v8::Local<v8::Object>::New(isolate, v8_schema); |
| 103 schema_cache_[api] = UnsafePersistent<v8::Object>(&v8_schema); | 103 schema_cache_[api] = UnsafePersistent<v8::Object>(&v8_schema); |
| 104 return handle_scope.Close(to_return); | 104 return handle_scope.Escape(to_return); |
| 105 } | 105 } |
| 106 | 106 |
| 107 v8::Handle<v8::Context> V8SchemaRegistry::GetOrCreateContext( | 107 v8::Handle<v8::Context> V8SchemaRegistry::GetOrCreateContext( |
| 108 v8::Isolate* isolate) { | 108 v8::Isolate* isolate) { |
| 109 // It's ok to create local handles in this function, since this is only called | 109 // It's ok to create local handles in this function, since this is only called |
| 110 // when we have a HandleScope. | 110 // when we have a HandleScope. |
| 111 if (context_.IsEmpty()) { | 111 if (context_.IsEmpty()) { |
| 112 v8::Handle<v8::Context> context = v8::Context::New(isolate); | 112 v8::Handle<v8::Context> context = v8::Context::New(isolate); |
| 113 context_.reset(context); | 113 context_.reset(context); |
| 114 return context; | 114 return context; |
| 115 } | 115 } |
| 116 return context_.NewHandle(isolate); | 116 return context_.NewHandle(isolate); |
| 117 } | 117 } |
| 118 | 118 |
| 119 } // namespace extensions | 119 } // namespace extensions |
| OLD | NEW |