| 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/schema_generated_bindings.h" | 5 #include "chrome/renderer/extensions/schema_generated_bindings.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 111 } |
| 112 | 112 |
| 113 static v8::Handle<v8::Value> GetExtensionAPIDefinition( | 113 static v8::Handle<v8::Value> GetExtensionAPIDefinition( |
| 114 const v8::Arguments& args) { | 114 const v8::Arguments& args) { |
| 115 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); | 115 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); |
| 116 ExtensionDispatcher* dispatcher = self->extension_dispatcher_; | 116 ExtensionDispatcher* dispatcher = self->extension_dispatcher_; |
| 117 | 117 |
| 118 ChromeV8Context* v8_context = dispatcher->v8_context_set().GetCurrent(); | 118 ChromeV8Context* v8_context = dispatcher->v8_context_set().GetCurrent(); |
| 119 CHECK(v8_context); | 119 CHECK(v8_context); |
| 120 | 120 |
| 121 // TODO(kalman): can we just cache this in the ChromeV8Context instance? | 121 std::string extension_id = v8_context->extension_id(); |
| 122 scoped_ptr<std::set<std::string> > apis; | 122 ExtensionAPI::SchemaMap schemas; |
| 123 ExtensionAPI::GetSchemasFilter filter = |
| 124 dispatcher->is_extension_process() ? |
| 125 ExtensionAPI::ALL : ExtensionAPI::ONLY_UNPRIVILEGED; |
| 123 | 126 |
| 124 const std::string& extension_id = v8_context->extension_id(); | |
| 125 if (dispatcher->IsTestExtensionId(extension_id)) { | 127 if (dispatcher->IsTestExtensionId(extension_id)) { |
| 126 apis.reset(new std::set<std::string>()); | 128 ExtensionAPI::GetInstance()->GetDefaultSchemas(filter, &schemas); |
| 127 // The minimal set of APIs that tests need. | |
| 128 apis->insert("extension"); | |
| 129 } else { | 129 } else { |
| 130 apis = ExtensionAPI::GetInstance()->GetAPIsForContext( | 130 const ::Extension* extension = |
| 131 v8_context->context_type(), | 131 dispatcher->extensions()->GetByID(extension_id); |
| 132 dispatcher->extensions()->GetByID(extension_id), | 132 CHECK(extension) << extension_id << " not found"; |
| 133 UserScriptSlave::GetDataSourceURLForFrame(v8_context->web_frame())); | 133 ExtensionAPI::GetInstance()->GetSchemasForExtension( |
| 134 *extension, filter, &schemas); |
| 134 } | 135 } |
| 135 | 136 |
| 136 v8::Persistent<v8::Context> context(v8::Context::New()); | 137 v8::Persistent<v8::Context> context(v8::Context::New()); |
| 137 v8::Context::Scope context_scope(context); | 138 v8::Context::Scope context_scope(context); |
| 138 v8::Handle<v8::Array> api(v8::Array::New(apis->size())); | 139 v8::Handle<v8::Array> api(v8::Array::New(schemas.size())); |
| 139 size_t api_index = 0; | 140 size_t api_index = 0; |
| 140 for (std::set<std::string>::iterator i = apis->begin(); i != apis->end(); | 141 for (ExtensionAPI::SchemaMap::iterator it = schemas.begin(); |
| 141 ++i) { | 142 it != schemas.end(); ++it) { |
| 142 // TODO(kalman): this caching is actually useless now, because | 143 std::string api_name = it->first; |
| 143 // SchemaGeneratedBindings is per-context not per-process. We should | 144 api->Set(api_index, GetV8SchemaForAPI(self, context, api_name)); |
| 144 // (e.g.) hang a SchemaRegistry off ExtensionDispatcher (which maintains | |
| 145 // a *single* v8::Context rather than multiple ones as here). | |
| 146 api->Set(api_index, GetV8SchemaForAPI(self, context, *i)); | |
| 147 ++api_index; | 145 ++api_index; |
| 148 } | 146 } |
| 149 | 147 |
| 150 // The persistent extension_api_ will keep the context alive. | 148 // The persistent extension_api_ will keep the context alive. |
| 151 context.Dispose(); | 149 context.Dispose(); |
| 152 | 150 |
| 153 return api; | 151 return api; |
| 154 } | 152 } |
| 155 | 153 |
| 156 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { | 154 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 // We store this so that we don't have to parse it over and over again for | 316 // We store this so that we don't have to parse it over and over again for |
| 319 // every context that uses it. | 317 // every context that uses it. |
| 320 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap; | 318 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap; |
| 321 CachedSchemaMap schemas_; | 319 CachedSchemaMap schemas_; |
| 322 }; | 320 }; |
| 323 | 321 |
| 324 } // namespace | 322 } // namespace |
| 325 | 323 |
| 326 namespace extensions { | 324 namespace extensions { |
| 327 | 325 |
| 328 // static | |
| 329 ChromeV8Extension* SchemaGeneratedBindings::Get( | 326 ChromeV8Extension* SchemaGeneratedBindings::Get( |
| 330 ExtensionDispatcher* extension_dispatcher) { | 327 ExtensionDispatcher* extension_dispatcher) { |
| 331 return new ExtensionImpl(extension_dispatcher); | 328 return new ExtensionImpl(extension_dispatcher); |
| 332 } | 329 } |
| 333 | 330 |
| 334 // static | 331 // static |
| 335 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts, | 332 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts, |
| 336 int request_id, | 333 int request_id, |
| 337 bool success, | 334 bool success, |
| 338 const std::string& response, | 335 const std::string& response, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 const std::string& extension_id) { | 384 const std::string& extension_id) { |
| 388 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin(); | 385 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin(); |
| 389 it != g_pending_requests.Get().end(); ++it) { | 386 it != g_pending_requests.Get().end(); ++it) { |
| 390 if (it->second->extension_id == extension_id) | 387 if (it->second->extension_id == extension_id) |
| 391 return true; | 388 return true; |
| 392 } | 389 } |
| 393 return false; | 390 return false; |
| 394 } | 391 } |
| 395 | 392 |
| 396 } // namespace | 393 } // namespace |
| OLD | NEW |