diff options
author | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-27 16:37:17 +0000 |
---|---|---|
committer | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-27 16:37:17 +0000 |
commit | d9823b8e3db9bd45e3e079252bc6c1042c184317 (patch) | |
tree | 9d1d5f221ecc0cbdb29fc87dae2558c9cc818af1 | |
parent | e4fc7c04c27851b28aa03852bd47b6dcbd7c5d6a (diff) | |
download | chromium_src-d9823b8e3db9bd45e3e079252bc6c1042c184317.zip chromium_src-d9823b8e3db9bd45e3e079252bc6c1042c184317.tar.gz chromium_src-d9823b8e3db9bd45e3e079252bc6c1042c184317.tar.bz2 |
Extract UI dependencies from ExtensionHost, part 2
Reduce ExtensionHost dependence on Browser by moving to ExtensionViewHost:
* Infobar CSS insertion
* "Associated web contents", which is only used for dialogs and infobars
This is part of the AppShell extensions refactor project.
BUG=321341
TEST=browser_tests, manual testing of extension with infobar
R=yoz@chromium.org
Review URL: https://codereview.chromium.org/89833002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@237582 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/extensions/extension_host.cc | 73 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_host.h | 17 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_view_host.cc | 67 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_view_host.h | 16 |
4 files changed, 98 insertions, 75 deletions
diff --git a/chrome/browser/extensions/extension_host.cc b/chrome/browser/extensions/extension_host.cc index 41a0440..ae306d4 100644 --- a/chrome/browser/extensions/extension_host.cc +++ b/chrome/browser/extensions/extension_host.cc @@ -50,12 +50,7 @@ #include "extensions/common/extension_urls.h" #include "extensions/common/feature_switch.h" #include "extensions/common/manifest_handlers/background_info.h" -#include "grit/browser_resources.h" -#include "grit/chromium_strings.h" -#include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/resource/resource_bundle.h" -#include "ui/events/keycodes/keyboard_codes.h" #if !defined(OS_ANDROID) #include "components/web_modal/web_contents_modal_dialog_manager.h" @@ -144,8 +139,7 @@ ExtensionHost::ExtensionHost(const Extension* extension, document_element_available_(false), initial_url_(url), extension_function_dispatcher_(profile_, this), - extension_host_type_(host_type), - associated_web_contents_(NULL) { + extension_host_type_(host_type) { // Not used for panels, see PanelHost. DCHECK(host_type == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE || host_type == VIEW_TYPE_EXTENSION_DIALOG || @@ -181,29 +175,6 @@ ExtensionHost::~ExtensionHost() { ProcessCreationQueue::GetInstance()->Remove(this); } -WebContents* ExtensionHost::GetAssociatedWebContents() const { - return associated_web_contents_; -} - -WebContents* ExtensionHost::GetVisibleWebContents() const { - if (associated_web_contents_) - return associated_web_contents_; - if (extension_host_type_ == VIEW_TYPE_EXTENSION_POPUP) - return host_contents_.get(); - return NULL; -} - -// TODO(jamescook): Move this to ExtensionViewHost, as it is only used by -// dialogs and infobars. -void ExtensionHost::SetAssociatedWebContents( - content::WebContents* web_contents) { - associated_web_contents_ = web_contents; - if (web_contents) { - registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, - content::Source<WebContents>(associated_web_contents_)); - } -} - content::RenderProcessHost* ExtensionHost::render_process_host() const { return render_view_host()->GetProcess(); } @@ -335,12 +306,6 @@ void ExtensionHost::Observe(int type, extension_ = NULL; } break; - case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: - if (content::Source<WebContents>(source).ptr() == - associated_web_contents_) { - associated_web_contents_ = NULL; - } - break; default: NOTREACHED() << "Unexpected notification sent."; break; @@ -372,17 +337,6 @@ void ExtensionHost::RenderProcessGone(base::TerminationStatus status) { content::Details<ExtensionHost>(this)); } -// TODO(jamescook): Move to ExtensionViewHost, which handles infobars. -void ExtensionHost::InsertInfobarCSS() { - DCHECK(!IsBackgroundPage()); - - static const base::StringPiece css( - ResourceBundle::GetSharedInstance().GetRawDataResource( - IDR_EXTENSIONS_INFOBAR_CSS)); - - render_view_host()->InsertCSS(string16(), css.as_string()); -} - void ExtensionHost::DidStopLoading(content::RenderViewHost* render_view_host) { bool notify = !did_stop_loading_; did_stop_loading_ = true; @@ -426,20 +380,14 @@ void ExtensionHost::DocumentAvailableInMainFrame() { // bail. No need for the redundant setup. http://crbug.com/31170 if (document_element_available_) return; - document_element_available_ = true; - if (IsBackgroundPage()) { - ExtensionSystem::Get(profile_)->extension_service()-> - SetBackgroundPageReady(extension_); - } else { - switch (extension_host_type_) { - case VIEW_TYPE_EXTENSION_INFOBAR: - InsertInfobarCSS(); - break; - default: - break; // No style sheet for other types, at the moment. - } - } + OnDocumentAvailable(); +} + +void ExtensionHost::OnDocumentAvailable() { + DCHECK(extension_host_type_ == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); + ExtensionSystem::Get(profile_)->extension_service()-> + SetBackgroundPageReady(extension_); } void ExtensionHost::CloseContents(WebContents* contents) { @@ -503,8 +451,9 @@ void ExtensionHost::OnDetailedConsoleMessageAdded( int32 severity_level) { if (IsSourceFromAnExtension(source)) { GURL context_url; - if (associated_web_contents_) - context_url = associated_web_contents_->GetLastCommittedURL(); + WebContents* associated_contents = GetAssociatedWebContents(); + if (associated_contents) + context_url = associated_contents->GetLastCommittedURL(); else if (host_contents_.get()) context_url = host_contents_->GetLastCommittedURL(); diff --git a/chrome/browser/extensions/extension_host.h b/chrome/browser/extensions/extension_host.h index c2abeef..50a4b97 100644 --- a/chrome/browser/extensions/extension_host.h +++ b/chrome/browser/extensions/extension_host.h @@ -79,11 +79,6 @@ class ExtensionHost : public content::WebContentsDelegate, ViewType extension_host_type() const { return extension_host_type_; } const GURL& GetURL() const; - // ExtensionFunctionDispatcher::Delegate - virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE; - virtual content::WebContents* GetVisibleWebContents() const OVERRIDE; - void SetAssociatedWebContents(content::WebContents* web_contents); - // Returns true if the render view is initialized and didn't crash. bool IsRenderViewLive() const; @@ -92,9 +87,6 @@ class ExtensionHost : public content::WebContentsDelegate, // (can be NULL). This happens delayed to avoid locking the UI. void CreateRenderViewSoon(); - // Insert a default style sheet for Extension Infobars. - void InsertInfobarCSS(); - // Notifications from the JavaScriptDialogManager when a dialog is being // opened/closed. void WillRunJavaScriptDialog(); @@ -138,9 +130,13 @@ class ExtensionHost : public content::WebContentsDelegate, const content::NotificationDetails& details) OVERRIDE; protected: - // Called before the EXTENSION_HOST_DID_STOP_LOADING notification is sent. + // Called after the extension page finishes loading but before the + // EXTENSION_HOST_DID_STOP_LOADING notification is sent. virtual void OnDidStopLoading(); + // Called once when the document first becomes available. + virtual void OnDocumentAvailable(); + // Returns true if we're hosting a background page. virtual bool IsBackgroundPage() const; @@ -221,9 +217,6 @@ class ExtensionHost : public content::WebContentsDelegate, // The type of view being hosted. ViewType extension_host_type_; - // The relevant WebContents associated with this ExtensionHost, if any. - content::WebContents* associated_web_contents_; - // Used to measure how long it's been since the host was created. base::ElapsedTimer since_created_; diff --git a/chrome/browser/extensions/extension_view_host.cc b/chrome/browser/extensions/extension_view_host.cc index 2e2cd70..0623177 100644 --- a/chrome/browser/extensions/extension_view_host.cc +++ b/chrome/browser/extensions/extension_view_host.cc @@ -4,24 +4,51 @@ #include "chrome/browser/extensions/extension_view_host.h" +#include "base/strings/string_piece.h" #include "chrome/browser/extensions/window_controller.h" #include "chrome/browser/ui/browser.h" #include "chrome/common/extensions/extension_messages.h" #include "content/public/browser/render_view_host.h" +#include "grit/browser_resources.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/events/keycodes/keyboard_codes.h" using content::NativeWebKeyboardEvent; using content::OpenURLParams; using content::RenderViewHost; using content::WebContents; +using content::WebContentsObserver; namespace extensions { +// Notifies an ExtensionViewHost when a WebContents is destroyed. +class ExtensionViewHost::AssociatedWebContentsObserver + : public WebContentsObserver { + public: + AssociatedWebContentsObserver(ExtensionViewHost* host, + WebContents* web_contents) + : WebContentsObserver(web_contents), host_(host) {} + virtual ~AssociatedWebContentsObserver() {} + + // content::WebContentsObserver: + virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE { + // Deleting |this| from here is safe. + host_->SetAssociatedWebContents(NULL); + } + + private: + ExtensionViewHost* host_; + + DISALLOW_COPY_AND_ASSIGN(AssociatedWebContentsObserver); +}; + ExtensionViewHost::ExtensionViewHost( const Extension* extension, content::SiteInstance* site_instance, const GURL& url, ViewType host_type) - : ExtensionHost(extension, site_instance, url, host_type) { + : ExtensionHost(extension, site_instance, url, host_type), + associated_web_contents_(NULL) { // Not used for panels, see PanelHost. DCHECK(host_type == VIEW_TYPE_EXTENSION_DIALOG || host_type == VIEW_TYPE_EXTENSION_INFOBAR || @@ -48,6 +75,17 @@ void ExtensionViewHost::CreateView(Browser* browser) { #endif } +void ExtensionViewHost::SetAssociatedWebContents(WebContents* web_contents) { + associated_web_contents_ = web_contents; + if (associated_web_contents_) { + // Observe the new WebContents for deletion. + associated_web_contents_observer_.reset( + new AssociatedWebContentsObserver(this, associated_web_contents_)); + } else { + associated_web_contents_observer_.reset(); + } +} + // ExtensionHost overrides: void ExtensionViewHost::UnhandledKeyboardEvent( @@ -75,6 +113,13 @@ void ExtensionViewHost::OnDidStopLoading() { #endif } +void ExtensionViewHost::OnDocumentAvailable() { + if (extension_host_type() == VIEW_TYPE_EXTENSION_INFOBAR) { + // No style sheet for other types, at the moment. + InsertInfobarCSS(); + } +} + bool ExtensionViewHost::IsBackgroundPage() const { DCHECK(view_); return false; @@ -168,4 +213,24 @@ WindowController* ExtensionViewHost::GetExtensionWindowController() const { : NULL; } +WebContents* ExtensionViewHost::GetAssociatedWebContents() const { + return associated_web_contents_; +} + +WebContents* ExtensionViewHost::GetVisibleWebContents() const { + if (associated_web_contents_) + return associated_web_contents_; + if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP) + return host_contents(); + return NULL; +} + +void ExtensionViewHost::InsertInfobarCSS() { + static const base::StringPiece css( + ResourceBundle::GetSharedInstance().GetRawDataResource( + IDR_EXTENSIONS_INFOBAR_CSS)); + + render_view_host()->InsertCSS(string16(), css.as_string()); +} + } // namespace extensions diff --git a/chrome/browser/extensions/extension_view_host.h b/chrome/browser/extensions/extension_view_host.h index afb548d..aa92531 100644 --- a/chrome/browser/extensions/extension_view_host.h +++ b/chrome/browser/extensions/extension_view_host.h @@ -5,6 +5,7 @@ #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_ #define CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_ +#include "base/memory/scoped_ptr.h" #include "chrome/browser/extensions/extension_host.h" #if defined(TOOLKIT_VIEWS) @@ -57,6 +58,8 @@ class ExtensionViewHost : public ExtensionHost { // instantiate Browser objects. void CreateView(Browser* browser); + void SetAssociatedWebContents(content::WebContents* web_contents); + // Handles keyboard events that were not handled by HandleKeyboardEvent(). // Platform specific implementation may override this method to handle the // event in platform specific way. @@ -66,6 +69,7 @@ class ExtensionViewHost : public ExtensionHost { // ExtensionHost virtual void OnDidStopLoading() OVERRIDE; + virtual void OnDocumentAvailable() OVERRIDE; virtual bool IsBackgroundPage() const OVERRIDE; // content::WebContentsDelegate @@ -93,11 +97,23 @@ class ExtensionViewHost : public ExtensionHost { // ExtensionFunctionDispatcher::Delegate virtual WindowController* GetExtensionWindowController() const OVERRIDE; + virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE; + virtual content::WebContents* GetVisibleWebContents() const OVERRIDE; private: + // Insert a default style sheet for Extension Infobars. + void InsertInfobarCSS(); + // Optional view that shows the rendered content in the UI. scoped_ptr<PlatformExtensionView> view_; + // The relevant WebContents associated with this ExtensionViewHost, if any. + content::WebContents* associated_web_contents_; + + // Observer to detect when the associated web contents is destroyed. + class AssociatedWebContentsObserver; + scoped_ptr<AssociatedWebContentsObserver> associated_web_contents_observer_; + DISALLOW_COPY_AND_ASSIGN(ExtensionViewHost); }; |