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/browser_window_state.h"
16 #include "chrome/common/chrome_version_info.h"
17 #include "content/public/browser/context_factory.h"
18 #include "grit/chrome_unscaled_resources.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/base/ui_base_switches.h"
21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/gfx/screen.h"
23 #include "ui/views/widget/native_widget.h"
24 #include "ui/views/widget/widget.h"
29 #include "base/profiler/scoped_tracker.h"
30 #include "base/task_runner_util.h"
31 #include "base/win/windows_version.h"
32 #include "chrome/browser/app_icon_win.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "ui/base/win/shell.h"
38 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
39 #include "ui/aura/window.h"
40 #include "ui/aura/window_event_dispatcher.h"
43 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
44 #include "chrome/browser/ui/host_desktop.h"
45 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
46 #include "ui/views/widget/native_widget_aura.h"
49 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
50 #include "ui/views/linux_ui/linux_ui.h"
54 #include "ash/shell.h"
55 #include "ash/wm/window_state.h"
56 #include "chrome/browser/ui/ash/ash_init.h"
57 #include "chrome/browser/ui/ash/ash_util.h"
61 // Helpers --------------------------------------------------------------------
65 Profile
* GetProfileForWindow(const views::Widget
* window
) {
68 return reinterpret_cast<Profile
*>(
69 window
->GetNativeWindowProperty(Profile::kProfileKey
));
72 // If the given window has a profile associated with it, use that profile's
73 // preference service. Otherwise, store and retrieve the data from Local State.
74 // This function may return NULL if the necessary pref service has not yet
76 // TODO(mirandac): This function will also separate windows by profile in a
77 // multi-profile environment.
78 PrefService
* GetPrefsForWindow(const views::Widget
* window
) {
79 Profile
* profile
= GetProfileForWindow(window
);
81 // Use local state for windows that have no explicit profile.
82 return g_browser_process
->local_state();
84 return profile
->GetPrefs();
88 bool MonitorHasTopmostAutohideTaskbarForEdge(UINT edge
, HMONITOR monitor
) {
89 APPBARDATA taskbar_data
= { sizeof(APPBARDATA
), NULL
, 0, edge
};
91 // TODO(robliao): Remove ScopedTracker below once crbug.com/462368 is fixed.
92 tracked_objects::ScopedTracker
tracking_profile(
93 FROM_HERE_WITH_EXPLICIT_FUNCTION(
94 "462368 MonitorHasTopmostAutohideTaskbarForEdge"));
96 // MSDN documents an ABM_GETAUTOHIDEBAREX, which supposedly takes a monitor
97 // rect and returns autohide bars on that monitor. This sounds like a good
98 // idea for multi-monitor systems. Unfortunately, it appears to not work at
99 // least some of the time (erroneously returning NULL) and there's almost no
100 // online documentation or other sample code using it that suggests ways to
101 // address this problem. So we just use ABM_GETAUTOHIDEBAR and hope the user
102 // only cares about autohide bars on the monitor with the primary taskbar.
104 // NOTE: This call spins a nested message loop.
105 HWND taskbar
= reinterpret_cast<HWND
>(SHAppBarMessage(ABM_GETAUTOHIDEBAR
,
107 return ::IsWindow(taskbar
) &&
108 (MonitorFromWindow(taskbar
, MONITOR_DEFAULTTONULL
) == monitor
) &&
109 (GetWindowLong(taskbar
, GWL_EXSTYLE
) & WS_EX_TOPMOST
);
112 int GetAppbarAutohideEdgesOnWorkerThread(HMONITOR monitor
) {
116 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_LEFT
, monitor
))
117 edges
|= views::ViewsDelegate::EDGE_LEFT
;
118 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_TOP
, monitor
))
119 edges
|= views::ViewsDelegate::EDGE_TOP
;
120 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_RIGHT
, monitor
))
121 edges
|= views::ViewsDelegate::EDGE_RIGHT
;
122 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_BOTTOM
, monitor
))
123 edges
|= views::ViewsDelegate::EDGE_BOTTOM
;
131 // ChromeViewsDelegate --------------------------------------------------------
134 ChromeViewsDelegate::ChromeViewsDelegate()
135 : in_autohide_edges_callback_(false),
136 weak_factory_(this) {
138 ChromeViewsDelegate::ChromeViewsDelegate() {
142 ChromeViewsDelegate::~ChromeViewsDelegate() {
145 void ChromeViewsDelegate::SaveWindowPlacement(const views::Widget
* window
,
146 const std::string
& window_name
,
147 const gfx::Rect
& bounds
,
148 ui::WindowShowState show_state
) {
149 PrefService
* prefs
= GetPrefsForWindow(window
);
153 scoped_ptr
<DictionaryPrefUpdate
> pref_update
=
154 chrome::GetWindowPlacementDictionaryReadWrite(window_name
, prefs
);
155 base::DictionaryValue
* window_preferences
= pref_update
->Get();
156 window_preferences
->SetInteger("left", bounds
.x());
157 window_preferences
->SetInteger("top", bounds
.y());
158 window_preferences
->SetInteger("right", bounds
.right());
159 window_preferences
->SetInteger("bottom", bounds
.bottom());
160 window_preferences
->SetBoolean("maximized",
161 show_state
== ui::SHOW_STATE_MAXIMIZED
);
162 gfx::Rect
work_area(gfx::Screen::GetScreenFor(window
->GetNativeView())->
163 GetDisplayNearestWindow(window
->GetNativeView()).work_area());
164 window_preferences
->SetInteger("work_area_left", work_area
.x());
165 window_preferences
->SetInteger("work_area_top", work_area
.y());
166 window_preferences
->SetInteger("work_area_right", work_area
.right());
167 window_preferences
->SetInteger("work_area_bottom", work_area
.bottom());
170 bool ChromeViewsDelegate::GetSavedWindowPlacement(
171 const views::Widget
* widget
,
172 const std::string
& window_name
,
174 ui::WindowShowState
* show_state
) const {
175 PrefService
* prefs
= g_browser_process
->local_state();
179 DCHECK(prefs
->FindPreference(window_name
.c_str()));
180 const base::DictionaryValue
* dictionary
=
181 prefs
->GetDictionary(window_name
.c_str());
186 if (!dictionary
|| !dictionary
->GetInteger("left", &left
) ||
187 !dictionary
->GetInteger("top", &top
) ||
188 !dictionary
->GetInteger("right", &right
) ||
189 !dictionary
->GetInteger("bottom", &bottom
))
192 bounds
->SetRect(left
, top
, right
- left
, bottom
- top
);
194 bool maximized
= false;
196 dictionary
->GetBoolean("maximized", &maximized
);
197 *show_state
= maximized
? ui::SHOW_STATE_MAXIMIZED
: ui::SHOW_STATE_NORMAL
;
200 // On Ash environment, a window won't span across displays. Adjust
201 // the bounds to fit the work area.
202 gfx::NativeView window
= widget
->GetNativeView();
203 if (chrome::GetHostDesktopTypeForNativeView(window
) ==
204 chrome::HOST_DESKTOP_TYPE_ASH
) {
205 gfx::Display display
= gfx::Screen::GetScreenFor(window
)->
206 GetDisplayMatching(*bounds
);
207 bounds
->AdjustToFit(display
.work_area());
208 ash::wm::GetWindowState(window
)->set_minimum_visibility(true);
214 void ChromeViewsDelegate::NotifyAccessibilityEvent(
215 views::View
* view
, ui::AXEvent event_type
) {
216 #if defined(USE_AURA)
217 AutomationManagerAura::GetInstance()->HandleEvent(
218 GetProfileForWindow(view
->GetWidget()), view
, event_type
);
223 HICON
ChromeViewsDelegate::GetDefaultWindowIcon() const {
227 HICON
ChromeViewsDelegate::GetSmallWindowIcon() const {
228 return GetSmallAppIcon();
231 bool ChromeViewsDelegate::IsWindowInMetro(gfx::NativeWindow window
) const {
232 return chrome::IsNativeViewInAsh(window
);
235 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
236 gfx::ImageSkia
* ChromeViewsDelegate::GetDefaultWindowIcon() const {
237 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
238 return rb
.GetImageSkiaNamed(IDR_PRODUCT_LOGO_64
);
243 views::NonClientFrameView
* ChromeViewsDelegate::CreateDefaultNonClientFrameView(
244 views::Widget
* widget
) {
245 return chrome::IsNativeViewInAsh(widget
->GetNativeView()) ?
246 ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget
) : NULL
;
250 void ChromeViewsDelegate::AddRef() {
251 g_browser_process
->AddRefModule();
254 void ChromeViewsDelegate::ReleaseRef() {
255 g_browser_process
->ReleaseModule();
258 void ChromeViewsDelegate::OnBeforeWidgetInit(
259 views::Widget::InitParams
* params
,
260 views::internal::NativeWidgetDelegate
* delegate
) {
261 // We need to determine opacity if it's not already specified.
262 if (params
->opacity
== views::Widget::InitParams::INFER_OPACITY
)
263 params
->opacity
= GetOpacityForInitParams(*params
);
265 // If we already have a native_widget, we don't have to try to come
267 if (params
->native_widget
)
270 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
271 bool use_non_toplevel_window
=
273 params
->type
!= views::Widget::InitParams::TYPE_MENU
&&
274 params
->type
!= views::Widget::InitParams::TYPE_TOOLTIP
;
277 // On desktop Linux Chrome must run in an environment that supports a variety
278 // of window managers, some of which do not play nicely with parts of our UI
279 // that have specific expectations about window sizing and placement. For this
280 // reason windows opened as top level (!params.child) are always constrained
281 // by the browser frame, so we can position them correctly. This has some
282 // negative side effects, like dialogs being clipped by the browser frame, but
283 // the side effects are not as bad as the poor window manager interactions. On
284 // Windows however these WM interactions are not an issue, so we open windows
285 // requested as top_level as actual top level windows on the desktop.
286 use_non_toplevel_window
= use_non_toplevel_window
&&
288 chrome::GetHostDesktopTypeForNativeView(params
->parent
) !=
289 chrome::HOST_DESKTOP_TYPE_NATIVE
);
291 if (!ui::win::IsAeroGlassEnabled()) {
292 // If we don't have composition (either because Glass is not enabled or
293 // because it was disabled at the command line), anything that requires
294 // transparency will be broken with a toplevel window, so force the use of
295 // a non toplevel window.
296 if (params
->opacity
== views::Widget::InitParams::TRANSLUCENT_WINDOW
&&
297 params
->type
!= views::Widget::InitParams::TYPE_MENU
)
298 use_non_toplevel_window
= true;
300 // If we're on Vista+ with composition enabled, then we can use toplevel
301 // windows for most things (they get blended via WS_EX_COMPOSITED, which
302 // allows for animation effects, but also exceeding the bounds of the parent
304 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH
&&
306 params
->type
!= views::Widget::InitParams::TYPE_CONTROL
&&
307 params
->type
!= views::Widget::InitParams::TYPE_WINDOW
) {
308 // When we set this to false, we get a DesktopNativeWidgetAura from the
309 // default case (not handled in this function).
310 use_non_toplevel_window
= false;
316 #if defined(OS_CHROMEOS)
317 // When we are doing straight chromeos builds, we still need to handle the
318 // toplevel window case.
319 // There may be a few remaining widgets in Chrome OS that are not top level,
320 // but have neither a context nor a parent. Provide a fallback context so
321 // users don't crash. Developers will hit the DCHECK and should provide a
324 params
->context
= params
->context
->GetRootWindow();
325 DCHECK(params
->parent
|| params
->context
|| !params
->child
)
326 << "Please provide a parent or context for this widget.";
327 if (!params
->parent
&& !params
->context
)
328 params
->context
= ash::Shell::GetPrimaryRootWindow();
329 #elif defined(USE_AURA)
330 // While the majority of the time, context wasn't plumbed through due to the
331 // existence of a global WindowTreeClient, if this window is toplevel, it's
332 // possible that there is no contextual state that we can use.
333 if (params
->parent
== NULL
&& params
->context
== NULL
&& !params
->child
) {
334 // We need to make a decision about where to place this window based on the
336 switch (chrome::GetActiveDesktop()) {
337 case chrome::HOST_DESKTOP_TYPE_NATIVE
:
338 // If we're native, we should give this window its own toplevel desktop
340 params
->native_widget
= new views::DesktopNativeWidgetAura(delegate
);
343 case chrome::HOST_DESKTOP_TYPE_ASH
:
344 // If we're in ash, give this window the context of the main monitor.
345 params
->context
= ash::Shell::GetPrimaryRootWindow();
351 } else if (use_non_toplevel_window
) {
352 views::NativeWidgetAura
* native_widget
=
353 new views::NativeWidgetAura(delegate
);
354 if (params
->parent
) {
355 Profile
* parent_profile
= reinterpret_cast<Profile
*>(
356 params
->parent
->GetNativeWindowProperty(Profile::kProfileKey
));
357 native_widget
->SetNativeWindowProperty(Profile::kProfileKey
,
360 params
->native_widget
= native_widget
;
362 // TODO(erg): Once we've threaded context to everywhere that needs it, we
363 // should remove this check here.
364 gfx::NativeView to_check
=
365 params
->context
? params
->context
: params
->parent
;
366 if (chrome::GetHostDesktopTypeForNativeView(to_check
) ==
367 chrome::HOST_DESKTOP_TYPE_NATIVE
) {
368 params
->native_widget
= new views::DesktopNativeWidgetAura(delegate
);
374 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
375 bool ChromeViewsDelegate::WindowManagerProvidesTitleBar(bool maximized
) {
376 // On Ubuntu Unity, the system always provides a title bar for maximized
378 views::LinuxUI
* ui
= views::LinuxUI::instance();
379 return maximized
&& ui
&& ui
->UnityIsRunning();
383 ui::ContextFactory
* ChromeViewsDelegate::GetContextFactory() {
384 return content::GetContextFactory();
387 std::string
ChromeViewsDelegate::GetApplicationName() {
388 return chrome::VersionInfo().Name();
392 int ChromeViewsDelegate::GetAppbarAutohideEdges(HMONITOR monitor
,
393 const base::Closure
& callback
) {
394 // Initialize the map with EDGE_BOTTOM. This is important, as if we return an
395 // initial value of 0 (no auto-hide edges) then we'll go fullscreen and
396 // windows will automatically remove WS_EX_TOPMOST from the appbar resulting
397 // in us thinking there is no auto-hide edges. By returning at least one edge
398 // we don't initially go fullscreen until we figure out the real auto-hide
400 if (!appbar_autohide_edge_map_
.count(monitor
))
401 appbar_autohide_edge_map_
[monitor
] = EDGE_BOTTOM
;
402 if (monitor
&& !in_autohide_edges_callback_
) {
403 base::PostTaskAndReplyWithResult(
404 content::BrowserThread::GetBlockingPool(),
406 base::Bind(&GetAppbarAutohideEdgesOnWorkerThread
,
408 base::Bind(&ChromeViewsDelegate::OnGotAppbarAutohideEdges
,
409 weak_factory_
.GetWeakPtr(),
412 appbar_autohide_edge_map_
[monitor
]));
414 return appbar_autohide_edge_map_
[monitor
];
417 void ChromeViewsDelegate::OnGotAppbarAutohideEdges(
418 const base::Closure
& callback
,
422 appbar_autohide_edge_map_
[monitor
] = edges
;
423 if (returned_edges
== edges
)
426 base::AutoReset
<bool> in_callback_setter(&in_autohide_edges_callback_
, true);
431 #if !defined(USE_AURA) && !defined(USE_CHROMEOS)
432 views::Widget::InitParams::WindowOpacity
433 ChromeViewsDelegate::GetOpacityForInitParams(
434 const views::Widget::InitParams
& params
) {
435 return views::Widget::InitParams::OPAQUE_WINDOW
;