- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / tab_contents / chrome_web_contents_view_delegate_views.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/tab_contents/chrome_web_contents_view_delegate_views.h"
6
7 #include "chrome/browser/browser_shutdown.h"
8 #include "chrome/browser/ui/sad_tab_helper.h"
9 #include "chrome/browser/ui/tab_contents/chrome_web_contents_view_delegate.h"
10 #include "chrome/browser/ui/views/sad_tab_view.h"
11 #include "chrome/browser/ui/views/tab_contents/render_view_context_menu_views.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/render_widget_host_view.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/browser/web_contents_view.h"
20 #include "ui/views/focus/focus_manager.h"
21 #include "ui/views/focus/view_storage.h"
22 #include "ui/views/widget/widget.h"
23
24 #if defined(USE_AURA)
25 #include "chrome/browser/ui/aura/tab_contents/web_drag_bookmark_handler_aura.h"
26 #include "ui/aura/client/screen_position_client.h"
27 #include "ui/aura/window.h"
28 #else
29 #include "chrome/browser/ui/views/tab_contents/web_drag_bookmark_handler_win.h"
30 #endif
31
32 using web_modal::WebContentsModalDialogManager;
33
34 ChromeWebContentsViewDelegateViews::ChromeWebContentsViewDelegateViews(
35     content::WebContents* web_contents)
36     : web_contents_(web_contents) {
37   last_focused_view_storage_id_ =
38       views::ViewStorage::GetInstance()->CreateStorageID();
39 }
40
41 ChromeWebContentsViewDelegateViews::~ChromeWebContentsViewDelegateViews() {
42   // Makes sure to remove any stored view we may still have in the ViewStorage.
43   //
44   // It is possible the view went away before us, so we only do this if the
45   // view is registered.
46   views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
47   if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
48     view_storage->RemoveView(last_focused_view_storage_id_);
49 }
50
51 content::WebDragDestDelegate*
52     ChromeWebContentsViewDelegateViews::GetDragDestDelegate() {
53   // We install a chrome specific handler to intercept bookmark drags for the
54   // bookmark manager/extension API.
55 #if defined(USE_AURA)
56   bookmark_handler_.reset(new WebDragBookmarkHandlerAura);
57 #else
58   bookmark_handler_.reset(new WebDragBookmarkHandlerWin);
59 #endif
60   return bookmark_handler_.get();
61 }
62
63 bool ChromeWebContentsViewDelegateViews::Focus() {
64   SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
65   if (sad_tab_helper) {
66     SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
67     if (sad_tab) {
68       sad_tab->RequestFocus();
69       return true;
70     }
71   }
72
73   WebContentsModalDialogManager* web_contents_modal_dialog_manager =
74       WebContentsModalDialogManager::FromWebContents(web_contents_);
75   if (web_contents_modal_dialog_manager) {
76     // TODO(erg): WebContents used to own web contents modal dialogs, which is
77     // why this is here. Eventually this should be ported to a containing view
78     // specializing in web contents modal dialog management.
79     if (web_contents_modal_dialog_manager->IsDialogActive()) {
80       web_contents_modal_dialog_manager->FocusTopmostDialog();
81       return true;
82     }
83   }
84
85   return false;
86 }
87
88 void ChromeWebContentsViewDelegateViews::TakeFocus(bool reverse) {
89   views::FocusManager* focus_manager = GetFocusManager();
90   if (focus_manager)
91     focus_manager->AdvanceFocus(reverse);
92 }
93
94 void ChromeWebContentsViewDelegateViews::StoreFocus() {
95   views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
96
97   if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
98     view_storage->RemoveView(last_focused_view_storage_id_);
99
100   if (!GetFocusManager())
101     return;
102   views::View* focused_view = GetFocusManager()->GetFocusedView();
103   if (focused_view)
104     view_storage->StoreView(last_focused_view_storage_id_, focused_view);
105 }
106
107 void ChromeWebContentsViewDelegateViews::RestoreFocus() {
108   views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
109   views::View* last_focused_view =
110       view_storage->RetrieveView(last_focused_view_storage_id_);
111
112   if (!last_focused_view) {
113     SetInitialFocus();
114   } else {
115     if (last_focused_view->IsFocusable() &&
116         GetFocusManager()->ContainsView(last_focused_view)) {
117       last_focused_view->RequestFocus();
118     } else {
119       // The focused view may not belong to the same window hierarchy (e.g.
120       // if the location bar was focused and the tab is dragged out), or it may
121       // no longer be focusable (e.g. if the location bar was focused and then
122       // we switched to fullscreen mode).  In that case we default to the
123       // default focus.
124       SetInitialFocus();
125     }
126     view_storage->RemoveView(last_focused_view_storage_id_);
127   }
128 }
129
130 void ChromeWebContentsViewDelegateViews::ShowContextMenu(
131     const content::ContextMenuParams& params) {
132   // Menus need a Widget to work. If we're not the active tab we won't
133   // necessarily be in a widget.
134   views::Widget* top_level_widget = GetTopLevelWidget();
135   if (!top_level_widget)
136     return;
137
138   context_menu_.reset(
139       RenderViewContextMenuViews::Create(web_contents_, params));
140   context_menu_->Init();
141
142   // Don't show empty menus.
143   if (context_menu_->menu_model().GetItemCount() == 0)
144     return;
145
146   gfx::Point screen_point(params.x, params.y);
147
148 #if defined(USE_AURA)
149   // Convert from content coordinates to window coordinates.
150   aura::Window* web_contents_window =
151       web_contents_->GetView()->GetNativeView();
152   aura::Window* root_window = web_contents_window->GetRootWindow();
153   aura::client::ScreenPositionClient* screen_position_client =
154       aura::client::GetScreenPositionClient(root_window);
155   if (screen_position_client) {
156     screen_position_client->ConvertPointToScreen(web_contents_window,
157                                                  &screen_point);
158   }
159 #else
160   POINT temp = screen_point.ToPOINT();
161   ClientToScreen(web_contents_->GetView()->GetNativeView(), &temp);
162   screen_point = temp;
163 #endif
164
165   // Enable recursive tasks on the message loop so we can get updates while
166   // the context menu is being displayed.
167   base::MessageLoop::ScopedNestableTaskAllower allow(
168       base::MessageLoop::current());
169   context_menu_->RunMenuAt(top_level_widget, screen_point, params.source_type);
170 }
171
172 void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) {
173   SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
174   if (!sad_tab_helper)
175     return;
176   SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
177   if (sad_tab)
178     sad_tab->GetWidget()->SetBounds(gfx::Rect(size));
179 }
180
181 views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() {
182   return views::Widget::GetTopLevelWidgetForNativeView(
183       web_contents_->GetView()->GetNativeView());
184 }
185
186 views::FocusManager*
187     ChromeWebContentsViewDelegateViews::GetFocusManager() {
188   views::Widget* toplevel_widget = GetTopLevelWidget();
189   return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
190 }
191
192 void ChromeWebContentsViewDelegateViews::SetInitialFocus() {
193   if (web_contents_->FocusLocationBarByDefault()) {
194     if (web_contents_->GetDelegate())
195       web_contents_->GetDelegate()->SetFocusToLocationBar(false);
196   } else {
197     web_contents_->GetView()->Focus();
198   }
199 }
200
201 namespace chrome {
202
203 content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
204     content::WebContents* web_contents) {
205   return new ChromeWebContentsViewDelegateViews(web_contents);
206 }
207
208 }  // namespace chrome