diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-11 07:58:29 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-11 07:58:29 +0000 |
commit | a76226ddf7e9f4273f11b849d8aa89e3acdf8f3b (patch) | |
tree | 7f8918b0c531c2506027d4c46ef7a18be4904fa1 /chrome/renderer | |
parent | 9deb0413477034b261252253d86ae5f705418727 (diff) | |
download | chromium_src-a76226ddf7e9f4273f11b849d8aa89e3acdf8f3b.zip chromium_src-a76226ddf7e9f4273f11b849d8aa89e3acdf8f3b.tar.gz chromium_src-a76226ddf7e9f4273f11b849d8aa89e3acdf8f3b.tar.bz2 |
Remove unneeded extension_messages_browsertest.cc + cleanup.
Review URL: http://codereview.chromium.org/10024055
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131730 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
11 files changed, 88 insertions, 114 deletions
diff --git a/chrome/renderer/chrome_content_renderer_client.cc b/chrome/renderer/chrome_content_renderer_client.cc index c7270cab..5f902a2 100644 --- a/chrome/renderer/chrome_content_renderer_client.cc +++ b/chrome/renderer/chrome_content_renderer_client.cc @@ -641,7 +641,7 @@ bool ChromeContentRendererClient::RunIdleHandlerWhenWidgetsHidden() { bool ChromeContentRendererClient::AllowPopup(const GURL& creator) { ChromeV8Context* current_context = extension_dispatcher_->v8_context_set().GetCurrent(); - return current_context && !current_context->extension_id().empty(); + return current_context && current_context->extension(); } bool ChromeContentRendererClient::ShouldFork(WebFrame* frame, diff --git a/chrome/renderer/extensions/api_definitions_natives.cc b/chrome/renderer/extensions/api_definitions_natives.cc index 37621ac..bc8b4b0 100644 --- a/chrome/renderer/extensions/api_definitions_natives.cc +++ b/chrome/renderer/extensions/api_definitions_natives.cc @@ -4,9 +4,6 @@ #include "chrome/renderer/extensions/api_definitions_natives.h" -#include "chrome/common/extensions/api/extension_api.h" -#include "chrome/renderer/extensions/user_script_slave.h" - namespace extensions { ApiDefinitionsNatives::ApiDefinitionsNatives( @@ -22,28 +19,8 @@ v8::Handle<v8::Value> ApiDefinitionsNatives::GetExtensionAPIDefinition( ChromeV8Context* v8_context = extension_dispatcher()->v8_context_set().GetCurrent(); CHECK(v8_context); - - // TODO(kalman): This is being calculated twice, first in - // ExtensionDispatcher then again here. It might as well be a property of - // ChromeV8Context, however, this would require making ChromeV8Context take - // an Extension rather than an extension ID. In itself this is fine, - // however it does not play correctly with the "IsTestExtensionId" checks. - // We need to remove that first. - scoped_ptr<std::set<std::string> > apis; - - const std::string& extension_id = v8_context->extension_id(); - if (extension_dispatcher()->IsTestExtensionId(extension_id)) { - apis.reset(new std::set<std::string>()); - // The minimal set of APIs that tests need. - apis->insert("extension"); - } else { - apis = ExtensionAPI::GetSharedInstance()->GetAPIsForContext( - v8_context->context_type(), - extension_dispatcher()->extensions()->GetByID(extension_id), - UserScriptSlave::GetDataSourceURLForFrame(v8_context->web_frame())); - } - - return extension_dispatcher()->v8_schema_registry()->GetSchemas(*apis); + return extension_dispatcher()->v8_schema_registry()->GetSchemas( + v8_context->GetAvailableExtensionAPIs()); } } // namespace extensions diff --git a/chrome/renderer/extensions/app_bindings.cc b/chrome/renderer/extensions/app_bindings.cc index a5a902a..40857d9 100644 --- a/chrome/renderer/extensions/app_bindings.cc +++ b/chrome/renderer/extensions/app_bindings.cc @@ -73,10 +73,7 @@ AppBindings::AppBindings(ExtensionDispatcher* dispatcher, v8::Handle<v8::Value> AppBindings::GetIsInstalled( const v8::Arguments& args) { - // TODO(aa): Hm, maybe ExtensionBindingsContext should have GetExtension() - // afterall? - const ::Extension* extension = - extension_dispatcher_->extensions()->GetByID(context_->extension_id()); + const Extension* extension = context_->extension(); // TODO(aa): Why only hosted app? bool result = extension && extension->is_hosted_app() && diff --git a/chrome/renderer/extensions/chrome_v8_context.cc b/chrome/renderer/extensions/chrome_v8_context.cc index d5786e1..599b235 100644 --- a/chrome/renderer/extensions/chrome_v8_context.cc +++ b/chrome/renderer/extensions/chrome_v8_context.cc @@ -8,8 +8,10 @@ #include "base/logging.h" #include "base/string_split.h" #include "base/values.h" +#include "chrome/common/extensions/api/extension_api.h" #include "chrome/common/extensions/extension_set.h" #include "chrome/renderer/extensions/chrome_v8_extension.h" +#include "chrome/renderer/extensions/user_script_slave.h" #include "content/public/renderer/render_view.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" @@ -42,24 +44,28 @@ std::string GetContextTypeDescription(Feature::Context context_type) { ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, WebKit::WebFrame* web_frame, - const std::string& extension_id, + const Extension* extension, Feature::Context context_type) : v8_context_(v8::Persistent<v8::Context>::New(v8_context)), web_frame_(web_frame), - extension_id_(extension_id), + extension_(extension), context_type_(context_type) { - VLOG(1) << "Created context for extension\n" - << " id: " << extension_id << "\n" + VLOG(1) << "Created context:\n" + << " extension id: " << GetExtensionID() << "\n" << " frame: " << web_frame_ << "\n" << " context type: " << GetContextTypeDescription(context_type); } ChromeV8Context::~ChromeV8Context() { VLOG(1) << "Destroyed context for extension\n" - << " id: " << extension_id_; + << " extension id: " << GetExtensionID(); v8_context_.Dispose(); } +std::string ChromeV8Context::GetExtensionID() { + return extension_ ? extension_->id() : ""; +} + // static v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( v8::Handle<v8::Context> context) { @@ -146,19 +152,30 @@ bool ChromeV8Context::CallChromeHiddenMethod( return true; } +const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { + if (!available_extension_apis_.get()) { + available_extension_apis_ = + extensions::ExtensionAPI::GetSharedInstance()->GetAPIsForContext( + context_type_, + extension_, + UserScriptSlave::GetDataSourceURLForFrame(web_frame_)).Pass(); + } + return *(available_extension_apis_.get()); +} + void ChromeV8Context::DispatchOnLoadEvent(bool is_extension_process, bool is_incognito_process, - int manifest_version) const { + int manifest_version) { v8::HandleScope handle_scope; v8::Handle<v8::Value> argv[4]; - argv[0] = v8::String::New(extension_id_.c_str()); + argv[0] = v8::String::New(GetExtensionID().c_str()); argv[1] = v8::Boolean::New(is_extension_process); argv[2] = v8::Boolean::New(is_incognito_process); argv[3] = v8::Integer::New(manifest_version); CallChromeHiddenMethod("dispatchOnLoad", arraysize(argv), argv, NULL); } -void ChromeV8Context::DispatchOnUnloadEvent() const { +void ChromeV8Context::DispatchOnUnloadEvent() { v8::HandleScope handle_scope; CallChromeHiddenMethod("dispatchOnUnload", 0, NULL, NULL); } diff --git a/chrome/renderer/extensions/chrome_v8_context.h b/chrome/renderer/extensions/chrome_v8_context.h index e2c8911..e49b638 100644 --- a/chrome/renderer/extensions/chrome_v8_context.h +++ b/chrome/renderer/extensions/chrome_v8_context.h @@ -30,7 +30,7 @@ class ChromeV8Context { public: ChromeV8Context(v8::Handle<v8::Context> context, WebKit::WebFrame* frame, - const std::string& extension_id, + const Extension* extension, extensions::Feature::Context context_type); ~ChromeV8Context(); @@ -38,8 +38,8 @@ class ChromeV8Context { return v8_context_; } - const std::string& extension_id() const { - return extension_id_; + const Extension* extension() const { + return extension_; } WebKit::WebFrame* web_frame() const { @@ -57,6 +57,10 @@ class ChromeV8Context { module_system_ = module_system.Pass(); } + // Returns the ID of the extension associated with this context, or empty + // string if there is no such extension. + std::string GetExtensionID(); + // Returns a special Chrome-specific hidden object that is associated with a // context, but not reachable from the JavaScript in that context. This is // used by our v8::Extension implementations as a way to share code and as a @@ -77,8 +81,8 @@ class ChromeV8Context { // TODO(aa): Move this to EventBindings. void DispatchOnLoadEvent(bool is_extension_process, bool is_incognito_process, - int manifest_version) const; - void DispatchOnUnloadEvent() const; + int manifest_version); + void DispatchOnUnloadEvent(); // Call the named method of the chromeHidden object in this context. // The function can be a sub-property like "Port.dispatchOnMessage". Returns @@ -90,6 +94,10 @@ class ChromeV8Context { v8::Handle<v8::Value>* argv, v8::Handle<v8::Value>* result) const; + // Returns the set of extension APIs that are available to this context. If no + // APIs are available, returns an empty set. + const std::set<std::string>& GetAvailableExtensionAPIs(); + private: // The v8 context the bindings are accessible to. We keep a strong reference // to it for simplicity. In the case of content scripts, this is necessary @@ -104,8 +112,9 @@ class ChromeV8Context { // object can outlive is destroyed asynchronously. WebKit::WebFrame* web_frame_; - // The extension ID this context is associated with. - std::string extension_id_; + // The extension associated with this context, or NULL if there is none. This + // might be a hosted app in the case that this context is hosting a web URL. + const Extension* extension_; // The type of context. extensions::Feature::Context context_type_; @@ -113,6 +122,9 @@ class ChromeV8Context { // Owns and structures the JS that is injected to set up extension bindings. scoped_ptr<ModuleSystem> module_system_; + // The extension APIs available to this context. + scoped_ptr<std::set<std::string> > available_extension_apis_; + DISALLOW_COPY_AND_ASSIGN(ChromeV8Context); }; diff --git a/chrome/renderer/extensions/chrome_v8_context_set.cc b/chrome/renderer/extensions/chrome_v8_context_set.cc index e8ba257..bbb8a41 100644 --- a/chrome/renderer/extensions/chrome_v8_context_set.cc +++ b/chrome/renderer/extensions/chrome_v8_context_set.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -117,8 +117,11 @@ void ChromeV8ContextSet::DispatchChromeHiddenMethod( if ((*it)->v8_context().IsEmpty()) continue; - if (!extension_id.empty() && extension_id != (*it)->extension_id()) - continue; + if (!extension_id.empty()) { + const Extension* extension = (*it)->extension(); + if (!extension || (extension_id != extension->id())) + continue; + } content::RenderView* context_render_view = (*it)->GetRenderView(); if (!context_render_view) diff --git a/chrome/renderer/extensions/chrome_v8_context_set_unittest.cc b/chrome/renderer/extensions/chrome_v8_context_set_unittest.cc index 818e2cd..92443aa 100644 --- a/chrome/renderer/extensions/chrome_v8_context_set_unittest.cc +++ b/chrome/renderer/extensions/chrome_v8_context_set_unittest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/message_loop.h" +#include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/feature.h" #include "chrome/renderer/extensions/chrome_v8_context.h" #include "chrome/renderer/extensions/chrome_v8_context_set.h" @@ -21,11 +22,11 @@ TEST(ChromeV8ContextSet, Lifecycle) { // Dirty hack, but we don't actually need the frame, and this is easier than // creating a whole webview. WebKit::WebFrame* frame = reinterpret_cast<WebKit::WebFrame*>(1); - std::string extension_id = "00000000000000000000000000000000"; + const Extension* extension = NULL; ChromeV8Context* context = new ChromeV8Context( v8_context, frame, - extension_id, + extension, extensions::Feature::BLESSED_EXTENSION_CONTEXT); context_set.Add(context); diff --git a/chrome/renderer/extensions/event_bindings.cc b/chrome/renderer/extensions/event_bindings.cc index 6aec2c4..478c7f2 100644 --- a/chrome/renderer/extensions/event_bindings.cc +++ b/chrome/renderer/extensions/event_bindings.cc @@ -77,21 +77,20 @@ class ExtensionImpl : public ChromeV8Extension { event_name)) return v8::Undefined(); + std::string extension_id = context->GetExtensionID(); EventListenerCounts& listener_counts = - g_listener_counts.Get()[context->extension_id()]; + g_listener_counts.Get()[extension_id]; if (++listener_counts[event_name] == 1) { content::RenderThread::Get()->Send( - new ExtensionHostMsg_AddListener(context->extension_id(), - event_name)); + new ExtensionHostMsg_AddListener(extension_id, event_name)); } // This is called the first time the page has added a listener. Since // the background page is the only lazy page, we know this is the first // time this listener has been registered. - if (self->IsLazyBackgroundPage(context->extension_id())) { + if (self->IsLazyBackgroundPage(context->extension())) { content::RenderThread::Get()->Send( - new ExtensionHostMsg_AddLazyListener(context->extension_id(), - event_name)); + new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); } } @@ -111,25 +110,24 @@ class ExtensionImpl : public ChromeV8Extension { if (!context) return v8::Undefined(); + std::string extension_id = context->GetExtensionID(); EventListenerCounts& listener_counts = - g_listener_counts.Get()[context->extension_id()]; + g_listener_counts.Get()[extension_id]; std::string event_name(*v8::String::AsciiValue(args[0])); bool is_manual = args[1]->BooleanValue(); if (--listener_counts[event_name] == 0) { content::RenderThread::Get()->Send( - new ExtensionHostMsg_RemoveListener(context->extension_id(), - event_name)); + new ExtensionHostMsg_RemoveListener(extension_id, event_name)); } // DetachEvent is called when the last listener for the context is // removed. If the context is the background page, and it removes the // last listener manually, then we assume that it is no longer interested // in being awakened for this event. - if (is_manual && self->IsLazyBackgroundPage(context->extension_id())) { + if (is_manual && self->IsLazyBackgroundPage(context->extension())) { content::RenderThread::Get()->Send( - new ExtensionHostMsg_RemoveLazyListener(context->extension_id(), - event_name)); + new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); } } @@ -138,14 +136,12 @@ class ExtensionImpl : public ChromeV8Extension { private: - bool IsLazyBackgroundPage(const std::string& extension_id) { + bool IsLazyBackgroundPage(const Extension* extension) { content::RenderView* render_view = GetCurrentRenderView(); if (!render_view) return false; ExtensionHelper* helper = ExtensionHelper::Get(render_view); - const ::Extension* extension = - extension_dispatcher()->extensions()->GetByID(extension_id); return (extension && extension->has_lazy_background_page() && helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); } diff --git a/chrome/renderer/extensions/extension_dispatcher.cc b/chrome/renderer/extensions/extension_dispatcher.cc index eb320fd..ed3b2c1 100644 --- a/chrome/renderer/extensions/extension_dispatcher.cc +++ b/chrome/renderer/extensions/extension_dispatcher.cc @@ -141,10 +141,10 @@ class LazyBackgroundPageNativeHandler : public ChromeV8Extension { v8::Handle<v8::Value> IncrementKeepaliveCount(const v8::Arguments& args) { ChromeV8Context* context = extension_dispatcher()->v8_context_set().GetCurrent(); - if (IsCurrentContextLazyBackgroundPage(context->extension_id())) { + if (IsCurrentContextLazyBackgroundPage(context->extension())) { content::RenderThread::Get()->Send( new ExtensionHostMsg_IncrementLazyKeepaliveCount( - context->extension_id())); + context->extension()->id())); } return v8::Undefined(); } @@ -152,23 +152,21 @@ class LazyBackgroundPageNativeHandler : public ChromeV8Extension { v8::Handle<v8::Value> DecrementKeepaliveCount(const v8::Arguments& args) { ChromeV8Context* context = extension_dispatcher()->v8_context_set().GetCurrent(); - if (IsCurrentContextLazyBackgroundPage(context->extension_id())) { + if (IsCurrentContextLazyBackgroundPage(context->extension())) { content::RenderThread::Get()->Send( new ExtensionHostMsg_DecrementLazyKeepaliveCount( - context->extension_id())); + context->extension()->id())); } return v8::Undefined(); } private: - bool IsCurrentContextLazyBackgroundPage(const std::string& extension_id) { + bool IsCurrentContextLazyBackgroundPage(const Extension* extension) { content::RenderView* render_view = GetCurrentRenderView(); if (!render_view) return false; ExtensionHelper* helper = ExtensionHelper::Get(render_view); - const ::Extension* extension = - extension_dispatcher()->extensions()->GetByID(extension_id); return (extension && extension->has_lazy_background_page() && helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); } @@ -569,7 +567,7 @@ void ExtensionDispatcher::DidCreateScriptContext( std::string extension_id = GetExtensionID(frame, world_id); const Extension* extension = extensions_.GetByID(extension_id); - if (!extension && !extension_id.empty() && !IsTestExtensionId(extension_id)) { + if (!extension && !extension_id.empty()) { // There are conditions where despite a context being associated with an // extension, no extension actually gets found. Ignore "invalid" because // CSP blocks extension page loading by switching the extension ID to @@ -578,6 +576,8 @@ void ExtensionDispatcher::DidCreateScriptContext( LOG(ERROR) << "Extension \"" << extension_id << "\" not found"; RenderThread::Get()->RecordUserMetrics("ExtensionNotFound_ED"); } + + extension_id = ""; } ExtensionURLInfo url_info(frame->document().securityOrigin(), @@ -587,7 +587,7 @@ void ExtensionDispatcher::DidCreateScriptContext( ClassifyJavaScriptContext(extension_id, extension_group, url_info); ChromeV8Context* context = - new ChromeV8Context(v8_context, frame, extension_id, context_type); + new ChromeV8Context(v8_context, frame, extension, context_type); v8_context_set_.Add(context); scoped_ptr<ModuleSystem> module_system(new ModuleSystem(&source_map_)); @@ -637,11 +637,9 @@ void ExtensionDispatcher::DidCreateScriptContext( // regardless of |context_type|. ExtensionAPI knows how to return the // correct APIs, however, until it doesn't have a 2MB overhead we can't // load it in every process. - scoped_ptr<std::set<std::string> > apis = - ExtensionAPI::GetSharedInstance()->GetAPIsForContext( - context_type, extension, url_info.url()); - for (std::set<std::string>::iterator i = apis->begin(); i != apis->end(); - ++i) { + const std::set<std::string>& apis = context->GetAvailableExtensionAPIs(); + for (std::set<std::string>::const_iterator i = apis.begin(); + i != apis.end(); ++i) { InstallBindings(module_system.get(), v8_context, *i); } @@ -649,15 +647,6 @@ void ExtensionDispatcher::DidCreateScriptContext( } } - // TODO(kalman): this is probably the most unfortunate thing I've ever had to - // write. We really need to factor things differently to delete the concept - // of a test extension ID. - if (IsTestExtensionId(extension_id)) { - module_system->Require("miscellaneous_bindings"); - module_system->Require("schema_generated_bindings"); - InstallBindings(module_system.get(), v8_context, "extension"); - } - // Inject custom JS into the platform app context to block certain features // of the document and window. if (extension && extension->is_platform_app()) @@ -674,9 +663,7 @@ void ExtensionDispatcher::DidCreateScriptContext( } std::string ExtensionDispatcher::GetExtensionID(WebFrame* frame, int world_id) { - if (!test_extension_id_.empty()) { - return test_extension_id_; - } else if (world_id != 0) { + if (world_id != 0) { // Isolated worlds (content script). return user_script_slave_->GetExtensionIdForIsolatedWorld(world_id); } else { @@ -699,15 +686,6 @@ void ExtensionDispatcher::WillReleaseScriptContext( VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); } -void ExtensionDispatcher::SetTestExtensionId(const std::string& id) { - CHECK(!id.empty()); - test_extension_id_ = id; -} - -bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) { - return !test_extension_id_.empty() && id == test_extension_id_; -} - void ExtensionDispatcher::OnActivateExtension( const std::string& extension_id) { active_extension_ids_.insert(extension_id); @@ -885,12 +863,8 @@ bool ExtensionDispatcher::CheckCurrentContextAccessToExtensionAPI( return false; } - const ::Extension* extension = NULL; - if (!context->extension_id().empty()) { - extension = extensions()->GetByID(context->extension_id()); - } - - if (!extension || !extension->HasAPIPermission(function_name)) { + if (!context->extension() || + !context->extension()->HasAPIPermission(function_name)) { static const char kMessage[] = "You do not have permission to use '%s'. Be sure to declare" " in your manifest what permissions you need."; @@ -900,7 +874,7 @@ bool ExtensionDispatcher::CheckCurrentContextAccessToExtensionAPI( return false; } - if (!IsExtensionActive(extension->id()) && + if (!IsExtensionActive(context->extension()->id()) && ExtensionAPI::GetSharedInstance()->IsPrivileged(function_name)) { static const char kMessage[] = "%s can only be used in an extension process."; diff --git a/chrome/renderer/extensions/extension_dispatcher.h b/chrome/renderer/extensions/extension_dispatcher.h index 0b0eb25..4e87366 100644 --- a/chrome/renderer/extensions/extension_dispatcher.h +++ b/chrome/renderer/extensions/extension_dispatcher.h @@ -89,9 +89,6 @@ class ExtensionDispatcher : public content::RenderProcessObserver { v8::Handle<v8::Context> context, int world_id); - void SetTestExtensionId(const std::string& extension_id); - bool IsTestExtensionId(const std::string& id); - // TODO(mpcomplete): remove. http://crbug.com/100411 bool IsAdblockWithWebRequestInstalled() const { return webrequest_adblock_; @@ -222,8 +219,6 @@ class ExtensionDispatcher : public content::RenderProcessObserver { // True once WebKit has been initialized (and it is therefore safe to poke). bool is_webkit_initialized_; - std::string test_extension_id_; - // Status of webrequest usage for known extensions. // TODO(mpcomplete): remove. http://crbug.com/100411 bool webrequest_adblock_; diff --git a/chrome/renderer/extensions/extension_request_sender.cc b/chrome/renderer/extensions/extension_request_sender.cc index a7e5b98..9b6ce3f 100644 --- a/chrome/renderer/extensions/extension_request_sender.cc +++ b/chrome/renderer/extensions/extension_request_sender.cc @@ -96,13 +96,15 @@ void ExtensionRequestSender::StartRequest( v8::Persistent<v8::Context> v8_context = v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); DCHECK(!v8_context.IsEmpty()); + + std::string extension_id = current_context->GetExtensionID(); InsertRequest(request_id, new PendingRequest( - v8_context, name, current_context->extension_id())); + v8_context, name, extension_id)); ExtensionHostMsg_Request_Params params; params.name = name; params.arguments.Swap(value_args); - params.extension_id = current_context->extension_id(); + params.extension_id = extension_id; params.source_url = source_url; params.source_origin = source_origin.toString(); params.request_id = request_id; |