Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 24 #include "chrome/common/extensions/api/extension_api.h" | 24 #include "chrome/common/extensions/api/extension_api.h" |
| 25 #include "chrome/common/render_messages.h" | 25 #include "chrome/common/render_messages.h" |
| 26 #include "chrome/renderer/chrome_render_process_observer.h" | 26 #include "chrome/renderer/chrome_render_process_observer.h" |
| 27 #include "chrome/renderer/extensions/chrome_v8_context.h" | 27 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 28 #include "chrome/renderer/extensions/chrome_v8_context_set.h" | 28 #include "chrome/renderer/extensions/chrome_v8_context_set.h" |
| 29 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 29 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 30 #include "chrome/renderer/extensions/event_bindings.h" | 30 #include "chrome/renderer/extensions/event_bindings.h" |
| 31 #include "chrome/renderer/extensions/extension_dispatcher.h" | 31 #include "chrome/renderer/extensions/extension_dispatcher.h" |
| 32 #include "chrome/renderer/extensions/miscellaneous_bindings.h" | 32 #include "chrome/renderer/extensions/miscellaneous_bindings.h" |
| 33 #include "chrome/renderer/extensions/user_script_slave.h" | 33 #include "chrome/renderer/extensions/user_script_slave.h" |
| 34 #include "content/public/renderer/render_thread.h" | |
| 34 #include "content/public/renderer/render_view.h" | 35 #include "content/public/renderer/render_view.h" |
| 35 #include "content/public/renderer/v8_value_converter.h" | 36 #include "content/public/renderer/v8_value_converter.h" |
| 36 #include "grit/common_resources.h" | 37 #include "grit/common_resources.h" |
| 37 #include "grit/renderer_resources.h" | 38 #include "grit/renderer_resources.h" |
| 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 40 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 40 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 41 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 41 #include "third_party/skia/include/core/SkBitmap.h" | 42 #include "third_party/skia/include/core/SkBitmap.h" |
| 42 #include "third_party/skia/include/core/SkColor.h" | 43 #include "third_party/skia/include/core/SkColor.h" |
| 43 #include "ui/base/resource/resource_bundle.h" | 44 #include "ui/base/resource/resource_bundle.h" |
| 44 #include "v8/include/v8.h" | 45 #include "v8/include/v8.h" |
| 45 #include "webkit/glue/webkit_glue.h" | 46 #include "webkit/glue/webkit_glue.h" |
| 46 | 47 |
| 48 using content::RenderThread; | |
| 47 using content::V8ValueConverter; | 49 using content::V8ValueConverter; |
| 48 using extensions::ExtensionAPI; | 50 using extensions::ExtensionAPI; |
| 51 using extensions::Feature; | |
| 49 using WebKit::WebFrame; | 52 using WebKit::WebFrame; |
| 50 using WebKit::WebSecurityOrigin; | 53 using WebKit::WebSecurityOrigin; |
| 51 | 54 |
| 52 namespace { | 55 namespace { |
| 53 | 56 |
| 54 // Contains info relevant to a pending API request. | 57 // Contains info relevant to a pending API request. |
| 55 struct PendingRequest { | 58 struct PendingRequest { |
| 56 public : | 59 public : |
| 57 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name, | 60 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name, |
| 58 const std::string& extension_id) | 61 const std::string& extension_id) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 } | 114 } |
| 112 | 115 |
| 113 static v8::Handle<v8::Value> GetExtensionAPIDefinition( | 116 static v8::Handle<v8::Value> GetExtensionAPIDefinition( |
| 114 const v8::Arguments& args) { | 117 const v8::Arguments& args) { |
| 115 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); | 118 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); |
| 116 ExtensionDispatcher* dispatcher = self->extension_dispatcher_; | 119 ExtensionDispatcher* dispatcher = self->extension_dispatcher_; |
| 117 | 120 |
| 118 ChromeV8Context* v8_context = dispatcher->v8_context_set().GetCurrent(); | 121 ChromeV8Context* v8_context = dispatcher->v8_context_set().GetCurrent(); |
| 119 CHECK(v8_context); | 122 CHECK(v8_context); |
| 120 | 123 |
| 121 std::string extension_id = v8_context->extension_id(); | 124 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; | |
| 126 | 125 |
| 126 const std::string& extension_id = v8_context->extension_id(); | |
| 127 if (dispatcher->IsTestExtensionId(extension_id)) { | 127 if (dispatcher->IsTestExtensionId(extension_id)) { |
| 128 ExtensionAPI::GetInstance()->GetDefaultSchemas(filter, &schemas); | 128 apis.reset(new std::set<std::string>()); |
| 129 // The minimal set of APIs that tests need. | |
| 130 apis->insert("extension"); | |
| 129 } else { | 131 } else { |
| 130 const ::Extension* extension = | 132 const Extension* extension = NULL; |
| 131 dispatcher->extensions()->GetByID(extension_id); | 133 GURL url; |
| 132 CHECK(extension) << extension_id << " not found"; | 134 |
| 133 ExtensionAPI::GetInstance()->GetSchemasForExtension( | 135 switch (v8_context->context_type()) { |
| 134 *extension, filter, &schemas); | 136 case Feature::UNSPECIFIED_CONTEXT: |
| 137 break; | |
| 138 | |
| 139 case Feature::PRIVILEGED_CONTEXT: | |
| 140 case Feature::UNPRIVILEGED_CONTEXT: | |
| 141 case Feature::CONTENT_SCRIPT_CONTEXT: | |
| 142 extension = dispatcher->extensions()->GetByID(extension_id); | |
| 143 if (!extension && extension_id != "invalid") { | |
| 144 // There are conditions where despite a context being associated | |
| 145 // with an extension, no extension actually gets found. Ignore | |
| 146 // "invalid" because CSP blocks extension page loading by switching | |
| 147 // the extension ID to "invalid". This isn't interesting. | |
| 148 LOG(ERROR) << "Extension \"" << extension_id << "\" not found"; | |
| 149 RenderThread::Get()->RecordUserMetrics("ExtensionNotFound_ED"); | |
| 150 } | |
| 151 break; | |
| 152 | |
| 153 case Feature::WEB_PAGE_CONTEXT: | |
| 154 url = UserScriptSlave::GetDataSourceURLForFrame( | |
| 155 v8_context->web_frame()); | |
| 156 break; | |
| 157 } | |
| 158 | |
|
not at google - send to devlin
2012/03/20 03:32:13
This is basically http://codereview.chromium.org/9
| |
| 159 apis = ExtensionAPI::GetInstance()->GetAPIsForContext( | |
| 160 v8_context->context_type(), extension, url); | |
| 135 } | 161 } |
| 136 | 162 |
| 137 v8::Persistent<v8::Context> context(v8::Context::New()); | 163 v8::Persistent<v8::Context> context(v8::Context::New()); |
| 138 v8::Context::Scope context_scope(context); | 164 v8::Context::Scope context_scope(context); |
| 139 v8::Handle<v8::Array> api(v8::Array::New(schemas.size())); | 165 v8::Handle<v8::Array> api(v8::Array::New(apis->size())); |
| 140 size_t api_index = 0; | 166 size_t api_index = 0; |
| 141 for (ExtensionAPI::SchemaMap::iterator it = schemas.begin(); | 167 for (std::set<std::string>::iterator i = apis->begin(); i != apis->end(); |
| 142 it != schemas.end(); ++it) { | 168 ++i) { |
| 143 std::string api_name = it->first; | 169 // TODO(kalman): this caching is actually useless now, because |
| 144 api->Set(api_index, GetV8SchemaForAPI(self, context, api_name)); | 170 // SchemaGeneratedBindings is per-context not per-process. We should |
| 171 // (e.g.) hang a SchemaRegistry off ExtensionDispatcher (which maintains | |
| 172 // a *single* v8::Context rather than multiple ones as here). | |
| 173 api->Set(api_index, GetV8SchemaForAPI(self, context, *i)); | |
| 145 ++api_index; | 174 ++api_index; |
| 146 } | 175 } |
| 147 | 176 |
| 148 // The persistent extension_api_ will keep the context alive. | 177 // The persistent extension_api_ will keep the context alive. |
| 149 context.Dispose(); | 178 context.Dispose(); |
| 150 | 179 |
| 151 return api; | 180 return api; |
| 152 } | 181 } |
| 153 | 182 |
| 154 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { | 183 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 // We store this so that we don't have to parse it over and over again for | 345 // We store this so that we don't have to parse it over and over again for |
| 317 // every context that uses it. | 346 // every context that uses it. |
| 318 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap; | 347 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap; |
| 319 CachedSchemaMap schemas_; | 348 CachedSchemaMap schemas_; |
| 320 }; | 349 }; |
| 321 | 350 |
| 322 } // namespace | 351 } // namespace |
| 323 | 352 |
| 324 namespace extensions { | 353 namespace extensions { |
| 325 | 354 |
| 355 // static | |
| 326 ChromeV8Extension* SchemaGeneratedBindings::Get( | 356 ChromeV8Extension* SchemaGeneratedBindings::Get( |
| 327 ExtensionDispatcher* extension_dispatcher) { | 357 ExtensionDispatcher* extension_dispatcher) { |
| 328 return new ExtensionImpl(extension_dispatcher); | 358 return new ExtensionImpl(extension_dispatcher); |
| 329 } | 359 } |
| 330 | 360 |
| 331 // static | 361 // static |
| 332 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts, | 362 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts, |
| 333 int request_id, | 363 int request_id, |
| 334 bool success, | 364 bool success, |
| 335 const std::string& response, | 365 const std::string& response, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 const std::string& extension_id) { | 414 const std::string& extension_id) { |
| 385 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin(); | 415 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin(); |
| 386 it != g_pending_requests.Get().end(); ++it) { | 416 it != g_pending_requests.Get().end(); ++it) { |
| 387 if (it->second->extension_id == extension_id) | 417 if (it->second->extension_id == extension_id) |
| 388 return true; | 418 return true; |
| 389 } | 419 } |
| 390 return false; | 420 return false; |
| 391 } | 421 } |
| 392 | 422 |
| 393 } // namespace | 423 } // namespace |
| OLD | NEW |