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.
5 #include "chrome/browser/ui/views/chrome_views_delegate.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views.h"
16 #include "chrome/common/pref_names.h"
17 #include "grit/chrome_unscaled_resources.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/base/ui_base_switches.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/views/widget/native_widget.h"
23 #include "ui/views/widget/widget.h"
28 #include "base/task_runner_util.h"
29 #include "base/win/windows_version.h"
30 #include "chrome/browser/app_icon_win.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "ui/base/win/shell.h"
36 #include "content/public/browser/context_factory.h"
37 #include "ui/aura/window.h"
38 #include "ui/aura/window_event_dispatcher.h"
41 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
42 #include "chrome/browser/ui/host_desktop.h"
43 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
44 #include "ui/views/widget/native_widget_aura.h"
47 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
48 #include "ui/views/linux_ui/linux_ui.h"
52 #include "ash/shell.h"
53 #include "ash/wm/window_state.h"
54 #include "chrome/browser/ui/ash/accessibility/automation_manager_views.h"
55 #include "chrome/browser/ui/ash/ash_init.h"
56 #include "chrome/browser/ui/ash/ash_util.h"
60 // Helpers --------------------------------------------------------------------
64 Profile
* GetProfileForWindow(const views::Widget
* window
) {
67 return reinterpret_cast<Profile
*>(
68 window
->GetNativeWindowProperty(Profile::kProfileKey
));
71 // If the given window has a profile associated with it, use that profile's
72 // preference service. Otherwise, store and retrieve the data from Local State.
73 // This function may return NULL if the necessary pref service has not yet
75 // TODO(mirandac): This function will also separate windows by profile in a
76 // multi-profile environment.
77 PrefService
* GetPrefsForWindow(const views::Widget
* window
) {
78 Profile
* profile
= GetProfileForWindow(window
);
80 // Use local state for windows that have no explicit profile.
81 return g_browser_process
->local_state();
83 return profile
->GetPrefs();
87 bool MonitorHasTopmostAutohideTaskbarForEdge(UINT edge
, const RECT
& rect
) {
88 APPBARDATA taskbar_data
= { sizeof(APPBARDATA
), NULL
, 0, edge
, rect
};
89 // NOTE: This call spins a nested message loop.
90 HWND taskbar
= reinterpret_cast<HWND
>(SHAppBarMessage(ABM_GETAUTOHIDEBAREX
,
92 return ::IsWindow(taskbar
) &&
93 (GetWindowLong(taskbar
, GWL_EXSTYLE
) & WS_EX_TOPMOST
);
96 int GetAppbarAutohideEdgesOnWorkerThread(HMONITOR monitor
) {
99 MONITORINFO mi
= { sizeof(MONITORINFO
) };
100 GetMonitorInfo(monitor
, &mi
);
103 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_LEFT
, mi
.rcMonitor
))
104 edges
|= views::ViewsDelegate::EDGE_LEFT
;
105 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_TOP
, mi
.rcMonitor
))
106 edges
|= views::ViewsDelegate::EDGE_TOP
;
107 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_RIGHT
, mi
.rcMonitor
))
108 edges
|= views::ViewsDelegate::EDGE_RIGHT
;
109 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_BOTTOM
, mi
.rcMonitor
))
110 edges
|= views::ViewsDelegate::EDGE_BOTTOM
;
118 // ChromeViewsDelegate --------------------------------------------------------
121 ChromeViewsDelegate::ChromeViewsDelegate()
122 : weak_factory_(this),
123 in_autohide_edges_callback_(false) {
125 ChromeViewsDelegate::ChromeViewsDelegate() {
129 ChromeViewsDelegate::~ChromeViewsDelegate() {
132 void ChromeViewsDelegate::SaveWindowPlacement(const views::Widget
* window
,
133 const std::string
& window_name
,
134 const gfx::Rect
& bounds
,
135 ui::WindowShowState show_state
) {
136 PrefService
* prefs
= GetPrefsForWindow(window
);
140 DCHECK(prefs
->FindPreference(window_name
.c_str()));
141 DictionaryPrefUpdate
update(prefs
, window_name
.c_str());
142 base::DictionaryValue
* window_preferences
= update
.Get();
143 window_preferences
->SetInteger("left", bounds
.x());
144 window_preferences
->SetInteger("top", bounds
.y());
145 window_preferences
->SetInteger("right", bounds
.right());
146 window_preferences
->SetInteger("bottom", bounds
.bottom());
147 window_preferences
->SetBoolean("maximized",
148 show_state
== ui::SHOW_STATE_MAXIMIZED
);
149 gfx::Rect
work_area(gfx::Screen::GetScreenFor(window
->GetNativeView())->
150 GetDisplayNearestWindow(window
->GetNativeView()).work_area());
151 window_preferences
->SetInteger("work_area_left", work_area
.x());
152 window_preferences
->SetInteger("work_area_top", work_area
.y());
153 window_preferences
->SetInteger("work_area_right", work_area
.right());
154 window_preferences
->SetInteger("work_area_bottom", work_area
.bottom());
157 bool ChromeViewsDelegate::GetSavedWindowPlacement(
158 const views::Widget
* widget
,
159 const std::string
& window_name
,
161 ui::WindowShowState
* show_state
) const {
162 PrefService
* prefs
= g_browser_process
->local_state();
166 DCHECK(prefs
->FindPreference(window_name
.c_str()));
167 const base::DictionaryValue
* dictionary
=
168 prefs
->GetDictionary(window_name
.c_str());
169 int left
, top
, right
, bottom
;
170 if (!dictionary
|| !dictionary
->GetInteger("left", &left
) ||
171 !dictionary
->GetInteger("top", &top
) ||
172 !dictionary
->GetInteger("right", &right
) ||
173 !dictionary
->GetInteger("bottom", &bottom
))
176 bounds
->SetRect(left
, top
, right
- left
, bottom
- top
);
178 bool maximized
= false;
180 dictionary
->GetBoolean("maximized", &maximized
);
181 *show_state
= maximized
? ui::SHOW_STATE_MAXIMIZED
: ui::SHOW_STATE_NORMAL
;
184 // On Ash environment, a window won't span across displays. Adjust
185 // the bounds to fit the work area.
186 gfx::NativeView window
= widget
->GetNativeView();
187 if (chrome::GetHostDesktopTypeForNativeView(window
) ==
188 chrome::HOST_DESKTOP_TYPE_ASH
) {
189 gfx::Display display
= gfx::Screen::GetScreenFor(window
)->
190 GetDisplayMatching(*bounds
);
191 bounds
->AdjustToFit(display
.work_area());
192 ash::wm::GetWindowState(window
)->set_minimum_visibility(true);
198 void ChromeViewsDelegate::NotifyAccessibilityEvent(
199 views::View
* view
, ui::AXEvent event_type
) {
200 AccessibilityEventRouterViews::GetInstance()->HandleAccessibilityEvent(
204 AutomationManagerViews::GetInstance()->HandleEvent(
205 GetProfileForWindow(view
->GetWidget()), view
, event_type
);
209 void ChromeViewsDelegate::NotifyMenuItemFocused(
210 const base::string16
& menu_name
,
211 const base::string16
& menu_item_name
,
215 AccessibilityEventRouterViews::GetInstance()->HandleMenuItemFocused(
216 menu_name
, menu_item_name
, item_index
, item_count
, has_submenu
);
220 HICON
ChromeViewsDelegate::GetDefaultWindowIcon() const {
224 bool ChromeViewsDelegate::IsWindowInMetro(gfx::NativeWindow window
) const {
225 return chrome::IsNativeViewInAsh(window
);
228 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
229 gfx::ImageSkia
* ChromeViewsDelegate::GetDefaultWindowIcon() const {
230 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
231 return rb
.GetImageSkiaNamed(IDR_PRODUCT_LOGO_64
);
236 views::NonClientFrameView
* ChromeViewsDelegate::CreateDefaultNonClientFrameView(
237 views::Widget
* widget
) {
238 return chrome::IsNativeViewInAsh(widget
->GetNativeView()) ?
239 ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget
) : NULL
;
243 void ChromeViewsDelegate::AddRef() {
244 g_browser_process
->AddRefModule();
247 void ChromeViewsDelegate::ReleaseRef() {
248 g_browser_process
->ReleaseModule();
251 void ChromeViewsDelegate::OnBeforeWidgetInit(
252 views::Widget::InitParams
* params
,
253 views::internal::NativeWidgetDelegate
* delegate
) {
254 // We need to determine opacity if it's not already specified.
255 if (params
->opacity
== views::Widget::InitParams::INFER_OPACITY
)
256 params
->opacity
= GetOpacityForInitParams(*params
);
258 // If we already have a native_widget, we don't have to try to come
260 if (params
->native_widget
)
263 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
264 bool use_non_toplevel_window
=
265 params
->parent
&& params
->type
!= views::Widget::InitParams::TYPE_MENU
;
268 // On desktop Linux Chrome must run in an environment that supports a variety
269 // of window managers, some of which do not play nicely with parts of our UI
270 // that have specific expectations about window sizing and placement. For this
271 // reason windows opened as top level (params.top_level) are always
272 // constrained by the browser frame, so we can position them correctly. This
273 // has some negative side effects, like dialogs being clipped by the browser
274 // frame, but the side effects are not as bad as the poor window manager
275 // interactions. On Windows however these WM interactions are not an issue, so
276 // we open windows requested as top_level as actual top level windows on the
278 use_non_toplevel_window
= use_non_toplevel_window
&&
279 (!params
->top_level
||
280 chrome::GetHostDesktopTypeForNativeView(params
->parent
) !=
281 chrome::HOST_DESKTOP_TYPE_NATIVE
);
283 if (!ui::win::IsAeroGlassEnabled()) {
284 // If we don't have composition (either because Glass is not enabled or
285 // because it was disabled at the command line), anything that requires
286 // transparency will be broken with a toplevel window, so force the use of
287 // a non toplevel window.
288 if (params
->opacity
== views::Widget::InitParams::TRANSLUCENT_WINDOW
&&
289 params
->type
!= views::Widget::InitParams::TYPE_MENU
)
290 use_non_toplevel_window
= true;
292 // If we're on Vista+ with composition enabled, then we can use toplevel
293 // windows for most things (they get blended via WS_EX_COMPOSITED, which
294 // allows for animation effects, but also exceeding the bounds of the parent
296 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH
&&
298 params
->type
!= views::Widget::InitParams::TYPE_CONTROL
&&
299 params
->type
!= views::Widget::InitParams::TYPE_WINDOW
) {
300 // When we set this to false, we get a DesktopNativeWidgetAura from the
301 // default case (not handled in this function).
302 use_non_toplevel_window
= false;
308 #if defined(OS_CHROMEOS)
309 // When we are doing straight chromeos builds, we still need to handle the
310 // toplevel window case.
311 // There may be a few remaining widgets in Chrome OS that are not top level,
312 // but have neither a context nor a parent. Provide a fallback context so
313 // users don't crash. Developers will hit the DCHECK and should provide a
316 params
->context
= params
->context
->GetRootWindow();
317 DCHECK(params
->parent
|| params
->context
|| params
->top_level
)
318 << "Please provide a parent or context for this widget.";
319 if (!params
->parent
&& !params
->context
)
320 params
->context
= ash::Shell::GetPrimaryRootWindow();
321 #elif defined(USE_AURA)
322 // While the majority of the time, context wasn't plumbed through due to the
323 // existence of a global WindowTreeClient, if this window is a toplevel, it's
324 // possible that there is no contextual state that we can use.
325 if (params
->parent
== NULL
&& params
->context
== NULL
&& params
->top_level
) {
326 // We need to make a decision about where to place this window based on the
328 switch (chrome::GetActiveDesktop()) {
329 case chrome::HOST_DESKTOP_TYPE_NATIVE
:
330 // If we're native, we should give this window its own toplevel desktop
332 params
->native_widget
= new views::DesktopNativeWidgetAura(delegate
);
335 case chrome::HOST_DESKTOP_TYPE_ASH
:
336 // If we're in ash, give this window the context of the main monitor.
337 params
->context
= ash::Shell::GetPrimaryRootWindow();
343 } else if (use_non_toplevel_window
) {
344 views::NativeWidgetAura
* native_widget
=
345 new views::NativeWidgetAura(delegate
);
346 if (params
->parent
) {
347 Profile
* parent_profile
= reinterpret_cast<Profile
*>(
348 params
->parent
->GetNativeWindowProperty(Profile::kProfileKey
));
349 native_widget
->SetNativeWindowProperty(Profile::kProfileKey
,
352 params
->native_widget
= native_widget
;
353 } else if (params
->type
!= views::Widget::InitParams::TYPE_TOOLTIP
) {
354 // TODO(erg): Once we've threaded context to everywhere that needs it, we
355 // should remove this check here.
356 gfx::NativeView to_check
=
357 params
->context
? params
->context
: params
->parent
;
358 if (chrome::GetHostDesktopTypeForNativeView(to_check
) ==
359 chrome::HOST_DESKTOP_TYPE_NATIVE
) {
360 params
->native_widget
= new views::DesktopNativeWidgetAura(delegate
);
366 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
367 bool ChromeViewsDelegate::WindowManagerProvidesTitleBar(bool maximized
) {
368 // On Ubuntu Unity, the system always provides a title bar for maximized
370 views::LinuxUI
* ui
= views::LinuxUI::instance();
371 return maximized
&& ui
&& ui
->UnityIsRunning();
375 #if defined(USE_AURA)
376 ui::ContextFactory
* ChromeViewsDelegate::GetContextFactory() {
377 return content::GetContextFactory();
382 int ChromeViewsDelegate::GetAppbarAutohideEdges(HMONITOR monitor
,
383 const base::Closure
& callback
) {
384 // Initialize the map with EDGE_BOTTOM. This is important, as if we return an
385 // initial value of 0 (no auto-hide edges) then we'll go fullscreen and
386 // windows will automatically remove WS_EX_TOPMOST from the appbar resulting
387 // in us thinking there is no auto-hide edges. By returning at least one edge
388 // we don't initially go fullscreen until we figure out the real auto-hide
390 if (!appbar_autohide_edge_map_
.count(monitor
))
391 appbar_autohide_edge_map_
[monitor
] = EDGE_BOTTOM
;
392 if (monitor
&& !in_autohide_edges_callback_
) {
393 base::PostTaskAndReplyWithResult(
394 content::BrowserThread::GetBlockingPool(),
396 base::Bind(&GetAppbarAutohideEdgesOnWorkerThread
,
398 base::Bind(&ChromeViewsDelegate::OnGotAppbarAutohideEdges
,
399 weak_factory_
.GetWeakPtr(),
402 appbar_autohide_edge_map_
[monitor
]));
404 return appbar_autohide_edge_map_
[monitor
];
407 void ChromeViewsDelegate::OnGotAppbarAutohideEdges(
408 const base::Closure
& callback
,
412 appbar_autohide_edge_map_
[monitor
] = edges
;
413 if (returned_edges
== edges
)
416 base::AutoReset
<bool> in_callback_setter(&in_autohide_edges_callback_
, true);
421 #if !defined(USE_AURA) && !defined(USE_CHROMEOS)
422 views::Widget::InitParams::WindowOpacity
423 ChromeViewsDelegate::GetOpacityForInitParams(
424 const views::Widget::InitParams
& params
) {
425 return views::Widget::InitParams::OPAQUE_WINDOW
;