Extract UI dependencies from ExtensionHost, part 3

1. Move WebContentsModalDialog support to ExtensionViewHost, as it is only
used by extension popups.
2. Break reliance on ChromeWebModalDialogManagerDelegate by providing our
own one-line IsWebContentsVisible() implementation.
3. Remove a bunch of OS_ANDROID ifdefs, since Android can use the components
module where web_modal stuff is implemented.

BUG=321341
TEST=browser_tests, esp. DownloadExtensionTest.DownloadExtensionTest_AcceptDanger

Review URL: https://codereview.chromium.org/92743002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238436 0039d316-1c4b-4281-b951-d872f2087c98
parent 9b9b8175
......@@ -25,13 +25,13 @@
#include "chrome/browser/media/media_capture_devices_dispatcher.h"
#include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/prefs/prefs_tab_helper.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/notification_service.h"
......@@ -40,7 +40,6 @@
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_error.h"
#include "extensions/browser/process_manager.h"
......@@ -50,10 +49,7 @@
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "ui/base/l10n/l10n_util.h"
#if !defined(OS_ANDROID)
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#endif
#include "ui/base/window_open_disposition.h"
using blink::WebDragOperation;
using blink::WebDragOperationsMask;
......@@ -212,25 +208,6 @@ const GURL& ExtensionHost::GetURL() const {
}
void ExtensionHost::LoadInitialURL() {
if (!IsBackgroundPage() &&
!ExtensionSystem::GetForBrowserContext(browser_context_)->
extension_service()->IsBackgroundPageReady(extension_)) {
// Make sure the background page loads before any others.
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
content::Source<Extension>(extension_));
return;
}
#if !defined(OS_ANDROID)
// TODO(jamescook): Move this to ExtensionViewHost, which handles popups.
if (extension_host_type_ == VIEW_TYPE_EXTENSION_POPUP) {
web_modal::WebContentsModalDialogManager::CreateForWebContents(
host_contents_.get());
web_modal::WebContentsModalDialogManager::FromWebContents(
host_contents_.get())->SetDelegate(this);
}
#endif
host_contents_->GetController().LoadURL(
initial_url_, content::Referrer(), content::PAGE_TRANSITION_LINK,
std::string());
......@@ -248,49 +225,10 @@ void ExtensionHost::Close() {
content::Details<ExtensionHost>(this));
}
#if !defined(OS_ANDROID)
web_modal::WebContentsModalDialogHost*
ExtensionHost::GetWebContentsModalDialogHost() {
return this;
}
gfx::NativeView ExtensionHost::GetHostView() const {
return NULL;
}
gfx::Point ExtensionHost::GetDialogPosition(const gfx::Size& size) {
if (!GetVisibleWebContents())
return gfx::Point();
gfx::Rect bounds = GetVisibleWebContents()->GetView()->GetViewBounds();
return gfx::Point(
std::max(0, (bounds.width() - size.width()) / 2),
std::max(0, (bounds.height() - size.height()) / 2));
}
gfx::Size ExtensionHost::GetMaximumDialogSize() {
if (!GetVisibleWebContents())
return gfx::Size();
return GetVisibleWebContents()->GetView()->GetViewBounds().size();
}
void ExtensionHost::AddObserver(
web_modal::ModalDialogHostObserver* observer) {
}
void ExtensionHost::RemoveObserver(
web_modal::ModalDialogHostObserver* observer) {
}
#endif
void ExtensionHost::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY:
DCHECK(ExtensionSystem::GetForBrowserContext(browser_context_)->
extension_service()->IsBackgroundPageReady(extension_));
LoadInitialURL();
break;
case chrome::NOTIFICATION_EXTENSION_UNLOADED:
// The extension object will be deleted after this notification has been
// sent. NULL it out so that dirty pointer issues don't arise in cases
......
......@@ -20,11 +20,6 @@
#include "extensions/common/stack_frame.h"
#include "extensions/common/view_type.h"
#if !defined(OS_ANDROID)
#include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"
#endif
class PrefsTabHelper;
namespace content {
......@@ -42,13 +37,7 @@ class WindowController;
// It handles setting up the renderer process, if needed, with special
// privileges available to extensions. It may have a view to be shown in the
// browser UI, or it may be hidden.
// TODO(jamescook): Move the ChromeWebModalDialogManagerDelegate interface to
// ExtensionViewHost.
class ExtensionHost : public content::WebContentsDelegate,
#if !defined(OS_ANDROID)
public ChromeWebModalDialogManagerDelegate,
public web_modal::WebContentsModalDialogHost,
#endif
public content::WebContentsObserver,
public ExtensionFunctionDispatcher::Delegate,
public content::NotificationObserver {
......@@ -128,6 +117,8 @@ class ExtensionHost : public content::WebContentsDelegate,
const content::NotificationDetails& details) OVERRIDE;
protected:
content::NotificationRegistrar* registrar() { return &registrar_; }
// Called after the extension page finishes loading but before the
// EXTENSION_HOST_DID_STOP_LOADING notification is sent.
virtual void OnDidStopLoading();
......@@ -135,6 +126,9 @@ class ExtensionHost : public content::WebContentsDelegate,
// Called once when the document first becomes available.
virtual void OnDocumentAvailable();
// Navigates to the initial page.
virtual void LoadInitialURL();
// Returns true if we're hosting a background page.
virtual bool IsBackgroundPage() const;
......@@ -147,25 +141,6 @@ class ExtensionHost : public content::WebContentsDelegate,
// Actually create the RenderView for this host. See CreateRenderViewSoon.
void CreateRenderViewNow();
// Navigates to the initial page.
void LoadInitialURL();
#if !defined(OS_ANDROID)
// TODO(jamescook): Move this to ExtensionViewHost.
// ChromeWebModalDialogManagerDelegate
virtual web_modal::WebContentsModalDialogHost*
GetWebContentsModalDialogHost() OVERRIDE;
// web_modal::WebContentsModalDialogHost
virtual gfx::NativeView GetHostView() const OVERRIDE;
virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
virtual void AddObserver(
web_modal::ModalDialogHostObserver* observer) OVERRIDE;
virtual void RemoveObserver(
web_modal::ModalDialogHostObserver* observer) OVERRIDE;
#endif
// Message handlers.
void OnRequest(const ExtensionHostMsg_Request_Params& params);
void OnEventAck();
......
......@@ -5,10 +5,18 @@
#include "chrome/browser/extensions/extension_view_host.h"
#include "base/strings/string_piece.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/window_controller.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/extension_messages.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/events/keycodes/keyboard_codes.h"
......@@ -18,6 +26,7 @@ using content::OpenURLParams;
using content::RenderViewHost;
using content::WebContents;
using content::WebContentsObserver;
using web_modal::WebContentsModalDialogManager;
namespace extensions {
......@@ -55,7 +64,14 @@ ExtensionViewHost::ExtensionViewHost(
host_type == VIEW_TYPE_EXTENSION_POPUP);
}
ExtensionViewHost::~ExtensionViewHost() {}
ExtensionViewHost::~ExtensionViewHost() {
// The hosting WebContents will be deleted in the base class, so unregister
// this object before it deletes the attached WebContentsModalDialogManager.
WebContentsModalDialogManager* manager =
WebContentsModalDialogManager::FromWebContents(host_contents());
if (manager)
manager->SetDelegate(NULL);
}
void ExtensionViewHost::CreateView(Browser* browser) {
#if defined(TOOLKIT_VIEWS)
......@@ -86,8 +102,6 @@ void ExtensionViewHost::SetAssociatedWebContents(WebContents* web_contents) {
}
}
// ExtensionHost overrides:
void ExtensionViewHost::UnhandledKeyboardEvent(
WebContents* source,
const content::NativeWebKeyboardEvent& event) {
......@@ -106,6 +120,8 @@ void ExtensionViewHost::UnhandledKeyboardEvent(
}
}
// ExtensionHost overrides:
void ExtensionViewHost::OnDidStopLoading() {
DCHECK(did_stop_loading());
#if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX)
......@@ -120,6 +136,26 @@ void ExtensionViewHost::OnDocumentAvailable() {
}
}
void ExtensionViewHost::LoadInitialURL() {
if (!ExtensionSystem::GetForBrowserContext(browser_context())->
extension_service()->IsBackgroundPageReady(extension())) {
// Make sure the background page loads before any others.
registrar()->Add(this,
chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
content::Source<Extension>(extension()));
return;
}
// Popups may spawn modal dialogs, which need positioning information.
if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP) {
WebContentsModalDialogManager::CreateForWebContents(host_contents());
WebContentsModalDialogManager::FromWebContents(
host_contents())->SetDelegate(this);
}
ExtensionHost::LoadInitialURL();
}
bool ExtensionViewHost::IsBackgroundPage() const {
DCHECK(view_);
return false;
......@@ -202,11 +238,41 @@ void ExtensionViewHost::RenderViewCreated(RenderViewHost* render_view_host) {
}
}
#if !defined(OS_ANDROID)
web_modal::WebContentsModalDialogHost*
ExtensionViewHost::GetWebContentsModalDialogHost() {
return this;
}
bool ExtensionViewHost::IsWebContentsVisible(WebContents* web_contents) {
return platform_util::IsVisible(web_contents->GetView()->GetNativeView());
}
gfx::NativeView ExtensionViewHost::GetHostView() const {
return view_->native_view();
}
#endif // !defined(OS_ANDROID)
gfx::Point ExtensionViewHost::GetDialogPosition(const gfx::Size& size) {
if (!GetVisibleWebContents())
return gfx::Point();
gfx::Rect bounds = GetVisibleWebContents()->GetView()->GetViewBounds();
return gfx::Point(
std::max(0, (bounds.width() - size.width()) / 2),
std::max(0, (bounds.height() - size.height()) / 2));
}
gfx::Size ExtensionViewHost::GetMaximumDialogSize() {
if (!GetVisibleWebContents())
return gfx::Size();
return GetVisibleWebContents()->GetView()->GetViewBounds().size();
}
void ExtensionViewHost::AddObserver(
web_modal::ModalDialogHostObserver* observer) {
}
void ExtensionViewHost::RemoveObserver(
web_modal::ModalDialogHostObserver* observer) {
}
WindowController* ExtensionViewHost::GetExtensionWindowController() const {
return view_->browser() ? view_->browser()->extension_window_controller()
......@@ -225,6 +291,18 @@ WebContents* ExtensionViewHost::GetVisibleWebContents() const {
return NULL;
}
void ExtensionViewHost::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY) {
DCHECK(ExtensionSystem::GetForBrowserContext(browser_context())->
extension_service()->IsBackgroundPageReady(extension()));
LoadInitialURL();
return;
}
ExtensionHost::Observe(type, source, details);
}
void ExtensionViewHost::InsertInfobarCSS() {
static const base::StringPiece css(
ResourceBundle::GetSharedInstance().GetRawDataResource(
......
......@@ -7,6 +7,8 @@
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/extension_host.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"
#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/ui/views/extensions/extension_view_views.h"
......@@ -30,7 +32,10 @@ namespace extensions {
// The ExtensionHost for an extension that backs a view in the browser UI. For
// example, this could be an extension popup, infobar or dialog, but not a
// background page.
class ExtensionViewHost : public ExtensionHost {
class ExtensionViewHost
: public ExtensionHost,
public web_modal::WebContentsModalDialogManagerDelegate,
public web_modal::WebContentsModalDialogHost {
public:
ExtensionViewHost(const Extension* extension,
content::SiteInstance* site_instance,
......@@ -70,6 +75,7 @@ class ExtensionViewHost : public ExtensionHost {
// ExtensionHost
virtual void OnDidStopLoading() OVERRIDE;
virtual void OnDocumentAvailable() OVERRIDE;
virtual void LoadInitialURL() OVERRIDE;
virtual bool IsBackgroundPage() const OVERRIDE;
// content::WebContentsDelegate
......@@ -90,16 +96,31 @@ class ExtensionViewHost : public ExtensionHost {
virtual void RenderViewCreated(
content::RenderViewHost* render_view_host) OVERRIDE;
#if !defined(OS_ANDROID)
// web_modal::WebContentsModalDialogManagerDelegate
virtual web_modal::WebContentsModalDialogHost*
GetWebContentsModalDialogHost() OVERRIDE;
virtual bool IsWebContentsVisible(
content::WebContents* web_contents) OVERRIDE;
// web_modal::WebContentsModalDialogHost
virtual gfx::NativeView GetHostView() const OVERRIDE;
#endif
virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
virtual void AddObserver(
web_modal::ModalDialogHostObserver* observer) OVERRIDE;
virtual void RemoveObserver(
web_modal::ModalDialogHostObserver* observer) OVERRIDE;
// ExtensionFunctionDispatcher::Delegate
virtual WindowController* GetExtensionWindowController() const OVERRIDE;
virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
virtual content::WebContents* GetVisibleWebContents() const OVERRIDE;
// content::NotificationObserver
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
private:
// Insert a default style sheet for Extension Infobars.
void InsertInfobarCSS();
......
......@@ -6,13 +6,16 @@
#define CHROME_BROWSER_UI_ANDROID_EXTENSIONS_EXTENSION_VIEW_ANDROID_H_
#include "base/basictypes.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/size.h"
class Browser;
// Android does not support extensions, so this is just a stub.
class ExtensionViewAndroid {
public:
Browser* browser() const { return NULL; }
gfx::NativeView native_view() const { return NULL; }
void ResizeDueToAutoResize(const gfx::Size& new_size);
void RenderViewCreated();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment