diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-11 15:41:05 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-11 15:41:05 +0000 |
commit | 5b5013c60fe1cca1ff0715c67ecb2b4a72e074e3 (patch) | |
tree | 8842f76810c78f179e1d80b4d48c602be4c53bf6 /content | |
parent | bf242bc689021423303cb2dea7b6f456f0f2e651 (diff) | |
download | chromium_src-5b5013c60fe1cca1ff0715c67ecb2b4a72e074e3.zip chromium_src-5b5013c60fe1cca1ff0715c67ecb2b4a72e074e3.tar.gz chromium_src-5b5013c60fe1cca1ff0715c67ecb2b4a72e074e3.tar.bz2 |
Move RenderViewHostDelegateViewHelper to content.
Rename to be something less of a tongue-twister.
BUG=93804, 95573
TEST=no visible change
Review URL: http://codereview.chromium.org/9114047
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117220 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/browser/tab_contents/tab_contents_view_helper.cc | 179 | ||||
-rw-r--r-- | content/browser/tab_contents/tab_contents_view_helper.h | 99 | ||||
-rw-r--r-- | content/content_browser.gypi | 2 |
3 files changed, 280 insertions, 0 deletions
diff --git a/content/browser/tab_contents/tab_contents_view_helper.cc b/content/browser/tab_contents/tab_contents_view_helper.cc new file mode 100644 index 0000000..3d434d4 --- /dev/null +++ b/content/browser/tab_contents/tab_contents_view_helper.cc @@ -0,0 +1,179 @@ +// 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. + +#include "content/browser/tab_contents/tab_contents_view_helper.h" + +#include "content/browser/renderer_host/render_view_host.h" +#include "content/browser/renderer_host/render_widget_host.h" +#include "content/browser/renderer_host/render_widget_host_view.h" +#include "content/browser/tab_contents/tab_contents.h" +#include "content/browser/tab_contents/tab_contents_view.h" +#include "content/common/view_messages.h" +#include "content/public/browser/notification_service.h" +#include "content/public/browser/notification_source.h" +#include "content/public/browser/notification_types.h" +#include "content/public/browser/web_contents.h" +#include "content/public/browser/web_contents_delegate.h" + +using content::WebContents; + +TabContentsViewHelper::TabContentsViewHelper() { + registrar_.Add(this, + content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, + content::NotificationService::AllBrowserContextsAndSources()); +} + +TabContentsViewHelper::~TabContentsViewHelper() {} + +void TabContentsViewHelper::Observe( + int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + DCHECK_EQ(type, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); + RenderWidgetHost* host = content::Source<RenderWidgetHost>(source).ptr(); + for (PendingWidgetViews::iterator i = pending_widget_views_.begin(); + i != pending_widget_views_.end(); ++i) { + if (host->view() == i->second) { + pending_widget_views_.erase(i); + break; + } + } +} + +TabContents* TabContentsViewHelper::CreateNewWindow( + WebContents* web_contents, + int route_id, + const ViewHostMsg_CreateWindow_Params& params) { + bool should_create = true; + if (web_contents->GetDelegate()) { + should_create = web_contents->GetDelegate()->ShouldCreateWebContents( + web_contents, + route_id, + params.window_container_type, + params.frame_name); + } + + if (!should_create) + return NULL; + + // Create the new web contents. This will automatically create the new + // TabContentsView. In the future, we may want to create the view separately. + TabContents* new_contents = + new TabContents(web_contents->GetBrowserContext(), + web_contents->GetSiteInstance(), + route_id, + static_cast<TabContents*>(web_contents), + NULL); + new_contents->set_opener_web_ui_type( + web_contents->GetWebUITypeForCurrentState()); + TabContentsView* new_view = new_contents->GetView(); + + // TODO(brettw): It seems bogus that we have to call this function on the + // newly created object and give it one of its own member variables. + new_view->CreateViewForWidget(new_contents->GetRenderViewHost()); + + // Save the created window associated with the route so we can show it later. + pending_contents_[route_id] = new_contents; + + if (web_contents->GetDelegate()) + web_contents->GetDelegate()->WebContentsCreated(web_contents, + params.opener_frame_id, + params.target_url, + new_contents); + + return new_contents; +} + +RenderWidgetHostView* TabContentsViewHelper::CreateNewWidget( + WebContents* web_contents, + int route_id, + bool is_fullscreen, + WebKit::WebPopupType popup_type) { + content::RenderProcessHost* process = web_contents->GetRenderProcessHost(); + RenderWidgetHost* widget_host = new RenderWidgetHost(process, route_id); + RenderWidgetHostView* widget_view = + RenderWidgetHostView::CreateViewForWidget(widget_host); + if (!is_fullscreen) { + // Popups should not get activated. + widget_view->set_popup_type(popup_type); + } + // Save the created widget associated with the route so we can show it later. + pending_widget_views_[route_id] = widget_view; + return widget_view; +} + +TabContents* TabContentsViewHelper::GetCreatedWindow(int route_id) { + PendingContents::iterator iter = pending_contents_.find(route_id); + + // Certain systems can block the creation of new windows. If we didn't succeed + // in creating one, just return NULL. + if (iter == pending_contents_.end()) { + return NULL; + } + + TabContents* new_contents = iter->second; + pending_contents_.erase(route_id); + + if (!new_contents->GetRenderProcessHost()->HasConnection() || + !new_contents->GetRenderViewHost()->view()) + return NULL; + + // TODO(brettw): It seems bogus to reach into here and initialize the host. + new_contents->GetRenderViewHost()->Init(); + return new_contents; +} + +RenderWidgetHostView* TabContentsViewHelper::GetCreatedWidget(int route_id) { + PendingWidgetViews::iterator iter = pending_widget_views_.find(route_id); + if (iter == pending_widget_views_.end()) { + DCHECK(false); + return NULL; + } + + RenderWidgetHostView* widget_host_view = iter->second; + pending_widget_views_.erase(route_id); + + RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost(); + if (!widget_host->process()->HasConnection()) { + // The view has gone away or the renderer crashed. Nothing to do. + return NULL; + } + + return widget_host_view; +} + +TabContents* TabContentsViewHelper::ShowCreatedWindow( + WebContents* web_contents, + int route_id, + WindowOpenDisposition disposition, + const gfx::Rect& initial_pos, + bool user_gesture) { + TabContents* contents = GetCreatedWindow(route_id); + if (contents) { + web_contents->AddNewContents(contents, + disposition, + initial_pos, + user_gesture); + } + return contents; +} + +RenderWidgetHostView* TabContentsViewHelper::ShowCreatedWidget( + WebContents* web_contents, + int route_id, + bool is_fullscreen, + const gfx::Rect& initial_pos) { + if (web_contents->GetDelegate()) + web_contents->GetDelegate()->RenderWidgetShowing(); + + RenderWidgetHostView* widget_host_view = GetCreatedWidget(route_id); + if (is_fullscreen) { + widget_host_view->InitAsFullscreen(web_contents->GetRenderWidgetHostView()); + } else { + widget_host_view->InitAsPopup(web_contents->GetRenderWidgetHostView(), + initial_pos); + } + widget_host_view->GetRenderWidgetHost()->Init(); + return widget_host_view; +} diff --git a/content/browser/tab_contents/tab_contents_view_helper.h b/content/browser/tab_contents/tab_contents_view_helper.h new file mode 100644 index 0000000..fe13ba3 --- /dev/null +++ b/content/browser/tab_contents/tab_contents_view_helper.h @@ -0,0 +1,99 @@ +// 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. + +#ifndef CONTENT_BROWSER_TAB_CONTENTS_TAB_CONTENTS_VIEW_HELPER_H_ +#define CONTENT_BROWSER_TAB_CONTENTS_TAB_CONTENTS_VIEW_HELPER_H_ +#pragma once + +#include <map> + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupType.h" +#include "webkit/glue/window_open_disposition.h" + +class RenderWidgetHostView; +class TabContents; +struct ViewHostMsg_CreateWindow_Params; + +namespace content { +class WebContents; +} + +namespace gfx { +class Rect; +} + +// TODO(avi): Once all the TabContentsViews implementations are in content (I'm +// looking at you, TabContentsViewViews...) then change the parameters to take +// WebContentsImpl rather than WebContents. + +// Provides helper methods that provide common implementations of some +// TabContentsView methods. +class TabContentsViewHelper : public content::NotificationObserver { + public: + TabContentsViewHelper(); + virtual ~TabContentsViewHelper(); + + // Creates a new window; call |ShowCreatedWindow| below to show it. + TabContents* CreateNewWindow(content::WebContents* web_contents, + int route_id, + const ViewHostMsg_CreateWindow_Params& params); + + // Creates a new popup or fullscreen widget; call |ShowCreatedWidget| below to + // show it. If |is_fullscreen| is true it is a fullscreen widget, if not then + // a pop-up. |popup_type| is only meaningful for a pop-up. + RenderWidgetHostView* CreateNewWidget(content::WebContents* web_contents, + int route_id, + bool is_fullscreen, + WebKit::WebPopupType popup_type); + + // Shows a window created with |CreateNewWindow| above. + TabContents* ShowCreatedWindow(content::WebContents* web_contents, + int route_id, + WindowOpenDisposition disposition, + const gfx::Rect& initial_pos, + bool user_gesture); + + // Shows a widget created with |CreateNewWidget| above. |initial_pos| is only + // meaningful for non-fullscreen widgets. + RenderWidgetHostView* ShowCreatedWidget(content::WebContents* web_contents, + int route_id, + bool is_fullscreen, + const gfx::Rect& initial_pos); + + private: + // content::NotificationObserver implementation + virtual void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) OVERRIDE; + + // Finds the new RenderWidgetHost and returns it. Note that this can only be + // called once as this call also removes it from the internal map. + RenderWidgetHostView* GetCreatedWidget(int route_id); + + // Finds the new TabContents by route_id, initializes it for + // renderer-initiated creation, and returns it. Note that this can only be + // called once as this call also removes it from the internal map. + TabContents* GetCreatedWindow(int route_id); + + // Tracks created TabContents objects that have not been shown yet. They are + // identified by the route ID passed to CreateNewWindow. + typedef std::map<int, TabContents*> PendingContents; + PendingContents pending_contents_; + + // These maps hold on to the widgets that we created on behalf of the renderer + // that haven't shown yet. + typedef std::map<int, RenderWidgetHostView*> PendingWidgetViews; + PendingWidgetViews pending_widget_views_; + + // Registers and unregisters us for notifications. + content::NotificationRegistrar registrar_; + + DISALLOW_COPY_AND_ASSIGN(TabContentsViewHelper); +}; + +#endif // CONTENT_BROWSER_TAB_CONTENTS_TAB_CONTENTS_VIEW_HELPER_H_ diff --git a/content/content_browser.gypi b/content/content_browser.gypi index 2c1f174..f667729 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -614,6 +614,8 @@ 'browser/tab_contents/tab_contents.h', 'browser/tab_contents/tab_contents_view.cc', 'browser/tab_contents/tab_contents_view.h', + 'browser/tab_contents/tab_contents_view_helper.cc', + 'browser/tab_contents/tab_contents_view_helper.h', 'browser/tab_contents/title_updated_details.h', 'browser/tab_contents/web_drag_dest_delegate.h', 'browser/tab_contents/web_drag_dest_gtk.cc', |