This source file includes following definitions.
- GetDragDestDelegate
- Focus
- TakeFocus
- StoreFocus
- RestoreFocus
- ShowContextMenu
- SizeChanged
- GetActiveNativeView
- GetTopLevelWidget
- GetFocusManager
- SetInitialFocus
- CreateWebContentsViewDelegate
#include "chrome/browser/ui/views/tab_contents/chrome_web_contents_view_delegate_views.h"
#include "chrome/browser/browser_shutdown.h"
#include "chrome/browser/ui/aura/tab_contents/web_drag_bookmark_handler_aura.h"
#include "chrome/browser/ui/sad_tab_helper.h"
#include "chrome/browser/ui/tab_contents/chrome_web_contents_view_delegate.h"
#include "chrome/browser/ui/views/renderer_context_menu/render_view_context_menu_views.h"
#include "chrome/browser/ui/views/sad_tab_view.h"
#include "chrome/common/chrome_switches.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_view.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/window.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/focus/view_storage.h"
#include "ui/views/widget/widget.h"
using web_modal::WebContentsModalDialogManager;
ChromeWebContentsViewDelegateViews::ChromeWebContentsViewDelegateViews(
content::WebContents* web_contents)
: web_contents_(web_contents) {
last_focused_view_storage_id_ =
views::ViewStorage::GetInstance()->CreateStorageID();
}
ChromeWebContentsViewDelegateViews::~ChromeWebContentsViewDelegateViews() {
views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
view_storage->RemoveView(last_focused_view_storage_id_);
}
content::WebDragDestDelegate*
ChromeWebContentsViewDelegateViews::GetDragDestDelegate() {
bookmark_handler_.reset(new WebDragBookmarkHandlerAura);
return bookmark_handler_.get();
}
bool ChromeWebContentsViewDelegateViews::Focus() {
SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
if (sad_tab_helper) {
SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
if (sad_tab) {
sad_tab->RequestFocus();
return true;
}
}
WebContentsModalDialogManager* web_contents_modal_dialog_manager =
WebContentsModalDialogManager::FromWebContents(web_contents_);
if (web_contents_modal_dialog_manager) {
if (web_contents_modal_dialog_manager->IsDialogActive()) {
web_contents_modal_dialog_manager->FocusTopmostDialog();
return true;
}
}
return false;
}
void ChromeWebContentsViewDelegateViews::TakeFocus(bool reverse) {
views::FocusManager* focus_manager = GetFocusManager();
if (focus_manager)
focus_manager->AdvanceFocus(reverse);
}
void ChromeWebContentsViewDelegateViews::StoreFocus() {
views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
view_storage->RemoveView(last_focused_view_storage_id_);
if (!GetFocusManager())
return;
views::View* focused_view = GetFocusManager()->GetFocusedView();
if (focused_view)
view_storage->StoreView(last_focused_view_storage_id_, focused_view);
}
void ChromeWebContentsViewDelegateViews::RestoreFocus() {
views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
views::View* last_focused_view =
view_storage->RetrieveView(last_focused_view_storage_id_);
if (!last_focused_view) {
SetInitialFocus();
} else {
if (last_focused_view->IsFocusable() &&
GetFocusManager()->ContainsView(last_focused_view)) {
last_focused_view->RequestFocus();
} else {
SetInitialFocus();
}
view_storage->RemoveView(last_focused_view_storage_id_);
}
}
void ChromeWebContentsViewDelegateViews::ShowContextMenu(
content::RenderFrameHost* render_frame_host,
const content::ContextMenuParams& params) {
views::Widget* top_level_widget = GetTopLevelWidget();
if (!top_level_widget)
return;
context_menu_.reset(
RenderViewContextMenuViews::Create(render_frame_host, params));
context_menu_->Init();
if (context_menu_->menu_model().GetItemCount() == 0)
return;
gfx::Point screen_point(params.x, params.y);
aura::Window* target_window = GetActiveNativeView();
aura::Window* root_window = target_window->GetRootWindow();
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(root_window);
if (screen_position_client) {
screen_position_client->ConvertPointToScreen(target_window,
&screen_point);
}
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
context_menu_->RunMenuAt(top_level_widget, screen_point, params.source_type);
}
void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) {
SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
if (!sad_tab_helper)
return;
SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
if (sad_tab)
sad_tab->GetWidget()->SetBounds(gfx::Rect(size));
}
aura::Window* ChromeWebContentsViewDelegateViews::GetActiveNativeView() {
return web_contents_->GetFullscreenRenderWidgetHostView() ?
web_contents_->GetFullscreenRenderWidgetHostView()->GetNativeView() :
web_contents_->GetView()->GetNativeView();
}
views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() {
return views::Widget::GetTopLevelWidgetForNativeView(GetActiveNativeView());
}
views::FocusManager*
ChromeWebContentsViewDelegateViews::GetFocusManager() {
views::Widget* toplevel_widget = GetTopLevelWidget();
return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
}
void ChromeWebContentsViewDelegateViews::SetInitialFocus() {
if (web_contents_->FocusLocationBarByDefault()) {
if (web_contents_->GetDelegate())
web_contents_->GetDelegate()->SetFocusToLocationBar(false);
} else {
web_contents_->GetView()->Focus();
}
}
namespace chrome {
content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
content::WebContents* web_contents) {
return new ChromeWebContentsViewDelegateViews(web_contents);
}
}