| 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 #include "chrome/renderer/extensions/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 "chrome/renderer/extensions/chrome_v8_context.h" | |
| 10 #include "content/public/renderer/v8_value_converter.h" | 9 #include "content/public/renderer/v8_value_converter.h" |
| 11 #include "extensions/common/extension_api.h" | 10 #include "extensions/common/extension_api.h" |
| 12 #include "extensions/renderer/object_backed_native_handler.h" | 11 #include "extensions/renderer/object_backed_native_handler.h" |
| 12 #include "extensions/renderer/script_context.h" |
| 13 | 13 |
| 14 using content::V8ValueConverter; | 14 using content::V8ValueConverter; |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { | 20 class SchemaRegistryNativeHandler : public ObjectBackedNativeHandler { |
| 21 public: | 21 public: |
| 22 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, | 22 SchemaRegistryNativeHandler(V8SchemaRegistry* registry, |
| 23 scoped_ptr<ChromeV8Context> context) | 23 scoped_ptr<ScriptContext> context) |
| 24 : ObjectBackedNativeHandler(context.get()), | 24 : ObjectBackedNativeHandler(context.get()), |
| 25 context_(context.Pass()), | 25 context_(context.Pass()), |
| 26 registry_(registry) { | 26 registry_(registry) { |
| 27 RouteFunction("GetSchema", | 27 RouteFunction("GetSchema", |
| 28 base::Bind(&SchemaRegistryNativeHandler::GetSchema, | 28 base::Bind(&SchemaRegistryNativeHandler::GetSchema, |
| 29 base::Unretained(this))); | 29 base::Unretained(this))); |
| 30 } | 30 } |
| 31 | 31 |
| 32 private: | 32 private: |
| 33 void GetSchema(const v8::FunctionCallbackInfo<v8::Value>& args) { | 33 void GetSchema(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 34 args.GetReturnValue().Set( | 34 args.GetReturnValue().Set( |
| 35 registry_->GetSchema(*v8::String::Utf8Value(args[0]))); | 35 registry_->GetSchema(*v8::String::Utf8Value(args[0]))); |
| 36 } | 36 } |
| 37 | 37 |
| 38 scoped_ptr<ChromeV8Context> context_; | 38 scoped_ptr<ScriptContext> context_; |
| 39 V8SchemaRegistry* registry_; | 39 V8SchemaRegistry* registry_; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 V8SchemaRegistry::V8SchemaRegistry() {} | 44 V8SchemaRegistry::V8SchemaRegistry() { |
| 45 } |
| 45 | 46 |
| 46 V8SchemaRegistry::~V8SchemaRegistry() {} | 47 V8SchemaRegistry::~V8SchemaRegistry() { |
| 48 } |
| 47 | 49 |
| 48 scoped_ptr<NativeHandler> V8SchemaRegistry::AsNativeHandler() { | 50 scoped_ptr<NativeHandler> V8SchemaRegistry::AsNativeHandler() { |
| 49 scoped_ptr<ChromeV8Context> context(new ChromeV8Context( | 51 scoped_ptr<ScriptContext> context( |
| 50 GetOrCreateContext(v8::Isolate::GetCurrent()), | 52 new ScriptContext(GetOrCreateContext(v8::Isolate::GetCurrent()), |
| 51 NULL, // no frame | 53 NULL, // no frame |
| 52 NULL, // no extension | 54 NULL, // no extension |
| 53 Feature::UNSPECIFIED_CONTEXT)); | 55 Feature::UNSPECIFIED_CONTEXT)); |
| 54 return scoped_ptr<NativeHandler>( | 56 return scoped_ptr<NativeHandler>( |
| 55 new SchemaRegistryNativeHandler(this, context.Pass())); | 57 new SchemaRegistryNativeHandler(this, context.Pass())); |
| 56 } | 58 } |
| 57 | 59 |
| 58 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( | 60 v8::Handle<v8::Array> V8SchemaRegistry::GetSchemas( |
| 59 const std::vector<std::string>& apis) { | 61 const std::vector<std::string>& apis) { |
| 60 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 62 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 61 v8::EscapableHandleScope handle_scope(isolate); | 63 v8::EscapableHandleScope handle_scope(isolate); |
| 62 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); | 64 v8::Context::Scope context_scope(GetOrCreateContext(isolate)); |
| 63 | 65 |
| 64 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); | 66 v8::Local<v8::Array> v8_apis(v8::Array::New(isolate, apis.size())); |
| 65 size_t api_index = 0; | 67 size_t api_index = 0; |
| 66 for (std::vector<std::string>::const_iterator i = apis.begin(); | 68 for (std::vector<std::string>::const_iterator i = apis.begin(); |
| 67 i != apis.end(); ++i) { | 69 i != apis.end(); |
| 70 ++i) { |
| 68 v8_apis->Set(api_index++, GetSchema(*i)); | 71 v8_apis->Set(api_index++, GetSchema(*i)); |
| 69 } | 72 } |
| 70 return handle_scope.Escape(v8_apis); | 73 return handle_scope.Escape(v8_apis); |
| 71 } | 74 } |
| 72 | 75 |
| 73 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { | 76 v8::Handle<v8::Object> V8SchemaRegistry::GetSchema(const std::string& api) { |
| 74 if (schema_cache_ != NULL) { | 77 if (schema_cache_ != NULL) { |
| 75 v8::Local<v8::Object> cached_schema = schema_cache_->Get(api); | 78 v8::Local<v8::Object> cached_schema = schema_cache_->Get(api); |
| 76 if (!cached_schema.IsEmpty()) { | 79 if (!cached_schema.IsEmpty()) { |
| 77 return cached_schema; | 80 return cached_schema; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 105 if (context_.IsEmpty()) { | 108 if (context_.IsEmpty()) { |
| 106 v8::Handle<v8::Context> context = v8::Context::New(isolate); | 109 v8::Handle<v8::Context> context = v8::Context::New(isolate); |
| 107 context_.reset(context); | 110 context_.reset(context); |
| 108 schema_cache_.reset(new SchemaCache(isolate)); | 111 schema_cache_.reset(new SchemaCache(isolate)); |
| 109 return context; | 112 return context; |
| 110 } | 113 } |
| 111 return context_.NewHandle(isolate); | 114 return context_.NewHandle(isolate); |
| 112 } | 115 } |
| 113 | 116 |
| 114 } // namespace extensions | 117 } // namespace extensions |
| OLD | NEW |