Remove legacy accessibilityPrivate support for views.
[chromium-blink-merge.git] / chrome / browser / ui / views / chrome_views_delegate.cc
blob964431ebb696f9f997b36f64b7d92af07ec1cabc
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 "content/public/browser/context_factory.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/geometry/rect.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/views/widget/native_widget.h"
23 #include "ui/views/widget/widget.h"
25 #if defined(OS_WIN)
26 #include <dwmapi.h>
27 #include <shellapi.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"
33 #endif
35 #if defined(USE_AURA)
36 #include "ui/aura/window.h"
37 #include "ui/aura/window_event_dispatcher.h"
38 #endif
40 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
41 #include "chrome/browser/ui/host_desktop.h"
42 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
43 #include "ui/views/widget/native_widget_aura.h"
44 #endif
46 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
47 #include "ui/views/linux_ui/linux_ui.h"
48 #endif
50 #if defined(USE_ASH)
51 #include "ash/shell.h"
52 #include "ash/wm/window_state.h"
53 #include "chrome/browser/ui/ash/accessibility/automation_manager_ash.h"
54 #include "chrome/browser/ui/ash/ash_init.h"
55 #include "chrome/browser/ui/ash/ash_util.h"
56 #endif
59 // Helpers --------------------------------------------------------------------
61 namespace {
63 Profile* GetProfileForWindow(const views::Widget* window) {
64 if (!window)
65 return NULL;
66 return reinterpret_cast<Profile*>(
67 window->GetNativeWindowProperty(Profile::kProfileKey));
70 // If the given window has a profile associated with it, use that profile's
71 // preference service. Otherwise, store and retrieve the data from Local State.
72 // This function may return NULL if the necessary pref service has not yet
73 // been initialized.
74 // TODO(mirandac): This function will also separate windows by profile in a
75 // multi-profile environment.
76 PrefService* GetPrefsForWindow(const views::Widget* window) {
77 Profile* profile = GetProfileForWindow(window);
78 if (!profile) {
79 // Use local state for windows that have no explicit profile.
80 return g_browser_process->local_state();
82 return profile->GetPrefs();
85 #if defined(OS_WIN)
86 bool MonitorHasTopmostAutohideTaskbarForEdge(UINT edge, HMONITOR monitor) {
87 APPBARDATA taskbar_data = { sizeof(APPBARDATA), NULL, 0, edge };
88 // MSDN documents an ABM_GETAUTOHIDEBAREX, which supposedly takes a monitor
89 // rect and returns autohide bars on that monitor. This sounds like a good
90 // idea for multi-monitor systems. Unfortunately, it appears to not work at
91 // least some of the time (erroneously returning NULL) and there's almost no
92 // online documentation or other sample code using it that suggests ways to
93 // address this problem. So we just use ABM_GETAUTOHIDEBAR and hope the user
94 // only cares about autohide bars on the monitor with the primary taskbar.
96 // NOTE: This call spins a nested message loop.
97 HWND taskbar = reinterpret_cast<HWND>(SHAppBarMessage(ABM_GETAUTOHIDEBAR,
98 &taskbar_data));
99 return ::IsWindow(taskbar) &&
100 (MonitorFromWindow(taskbar, MONITOR_DEFAULTTONULL) == monitor) &&
101 (GetWindowLong(taskbar, GWL_EXSTYLE) & WS_EX_TOPMOST);
104 int GetAppbarAutohideEdgesOnWorkerThread(HMONITOR monitor) {
105 DCHECK(monitor);
107 int edges = 0;
108 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_LEFT, monitor))
109 edges |= views::ViewsDelegate::EDGE_LEFT;
110 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_TOP, monitor))
111 edges |= views::ViewsDelegate::EDGE_TOP;
112 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_RIGHT, monitor))
113 edges |= views::ViewsDelegate::EDGE_RIGHT;
114 if (MonitorHasTopmostAutohideTaskbarForEdge(ABE_BOTTOM, monitor))
115 edges |= views::ViewsDelegate::EDGE_BOTTOM;
116 return edges;
118 #endif
120 } // namespace
123 // ChromeViewsDelegate --------------------------------------------------------
125 #if defined(OS_WIN)
126 ChromeViewsDelegate::ChromeViewsDelegate()
127 : in_autohide_edges_callback_(false),
128 weak_factory_(this) {
129 #else
130 ChromeViewsDelegate::ChromeViewsDelegate() {
131 #endif
134 ChromeViewsDelegate::~ChromeViewsDelegate() {
137 void ChromeViewsDelegate::SaveWindowPlacement(const views::Widget* window,
138 const std::string& window_name,
139 const gfx::Rect& bounds,
140 ui::WindowShowState show_state) {
141 PrefService* prefs = GetPrefsForWindow(window);
142 if (!prefs)
143 return;
145 scoped_ptr<DictionaryPrefUpdate> pref_update =
146 chrome::GetWindowPlacementDictionaryReadWrite(window_name, prefs);
147 base::DictionaryValue* window_preferences = pref_update->Get();
148 window_preferences->SetInteger("left", bounds.x());
149 window_preferences->SetInteger("top", bounds.y());
150 window_preferences->SetInteger("right", bounds.right());
151 window_preferences->SetInteger("bottom", bounds.bottom());
152 window_preferences->SetBoolean("maximized",
153 show_state == ui::SHOW_STATE_MAXIMIZED);
154 gfx::Rect work_area(gfx::Screen::GetScreenFor(window->GetNativeView())->
155 GetDisplayNearestWindow(window->GetNativeView()).work_area());
156 window_preferences->SetInteger("work_area_left", work_area.x());
157 window_preferences->SetInteger("work_area_top", work_area.y());
158 window_preferences->SetInteger("work_area_right", work_area.right());
159 window_preferences->SetInteger("work_area_bottom", work_area.bottom());
162 bool ChromeViewsDelegate::GetSavedWindowPlacement(
163 const views::Widget* widget,
164 const std::string& window_name,
165 gfx::Rect* bounds,
166 ui::WindowShowState* show_state) const {
167 PrefService* prefs = g_browser_process->local_state();
168 if (!prefs)
169 return false;
171 DCHECK(prefs->FindPreference(window_name.c_str()));
172 const base::DictionaryValue* dictionary =
173 prefs->GetDictionary(window_name.c_str());
174 int left = 0;
175 int top = 0;
176 int right = 0;
177 int bottom = 0;
178 if (!dictionary || !dictionary->GetInteger("left", &left) ||
179 !dictionary->GetInteger("top", &top) ||
180 !dictionary->GetInteger("right", &right) ||
181 !dictionary->GetInteger("bottom", &bottom))
182 return false;
184 bounds->SetRect(left, top, right - left, bottom - top);
186 bool maximized = false;
187 if (dictionary)
188 dictionary->GetBoolean("maximized", &maximized);
189 *show_state = maximized ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL;
191 #if defined(USE_ASH)
192 // On Ash environment, a window won't span across displays. Adjust
193 // the bounds to fit the work area.
194 gfx::NativeView window = widget->GetNativeView();
195 if (chrome::GetHostDesktopTypeForNativeView(window) ==
196 chrome::HOST_DESKTOP_TYPE_ASH) {
197 gfx::Display display = gfx::Screen::GetScreenFor(window)->
198 GetDisplayMatching(*bounds);
199 bounds->AdjustToFit(display.work_area());
200 ash::wm::GetWindowState(window)->set_minimum_visibility(true);
202 #endif
203 return true;
206 void ChromeViewsDelegate::NotifyAccessibilityEvent(
207 views::View* view, ui::AXEvent event_type) {
208 #if defined(USE_ASH)
209 AutomationManagerAsh::GetInstance()->HandleEvent(
210 GetProfileForWindow(view->GetWidget()), view, event_type);
211 #endif
214 #if defined(OS_WIN)
215 HICON ChromeViewsDelegate::GetDefaultWindowIcon() const {
216 return GetAppIcon();
219 HICON ChromeViewsDelegate::GetSmallWindowIcon() const {
220 return GetSmallAppIcon();
223 bool ChromeViewsDelegate::IsWindowInMetro(gfx::NativeWindow window) const {
224 return chrome::IsNativeViewInAsh(window);
227 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
228 gfx::ImageSkia* ChromeViewsDelegate::GetDefaultWindowIcon() const {
229 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
230 return rb.GetImageSkiaNamed(IDR_PRODUCT_LOGO_64);
232 #endif
234 #if defined(USE_ASH)
235 views::NonClientFrameView* ChromeViewsDelegate::CreateDefaultNonClientFrameView(
236 views::Widget* widget) {
237 return chrome::IsNativeViewInAsh(widget->GetNativeView()) ?
238 ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget) : NULL;
240 #endif
242 void ChromeViewsDelegate::AddRef() {
243 g_browser_process->AddRefModule();
246 void ChromeViewsDelegate::ReleaseRef() {
247 g_browser_process->ReleaseModule();
250 void ChromeViewsDelegate::OnBeforeWidgetInit(
251 views::Widget::InitParams* params,
252 views::internal::NativeWidgetDelegate* delegate) {
253 // We need to determine opacity if it's not already specified.
254 if (params->opacity == views::Widget::InitParams::INFER_OPACITY)
255 params->opacity = GetOpacityForInitParams(*params);
257 // If we already have a native_widget, we don't have to try to come
258 // up with one.
259 if (params->native_widget)
260 return;
262 #if defined(USE_AURA) && !defined(OS_CHROMEOS)
263 bool use_non_toplevel_window =
264 params->parent &&
265 params->type != views::Widget::InitParams::TYPE_MENU &&
266 params->type != views::Widget::InitParams::TYPE_TOOLTIP;
268 #if defined(OS_WIN)
269 // On desktop Linux Chrome must run in an environment that supports a variety
270 // of window managers, some of which do not play nicely with parts of our UI
271 // that have specific expectations about window sizing and placement. For this
272 // reason windows opened as top level (!params.child) are always constrained
273 // by the browser frame, so we can position them correctly. This has some
274 // negative side effects, like dialogs being clipped by the browser frame, but
275 // the side effects are not as bad as the poor window manager interactions. On
276 // Windows however these WM interactions are not an issue, so we open windows
277 // requested as top_level as actual top level windows on the desktop.
278 use_non_toplevel_window = use_non_toplevel_window &&
279 (params->child ||
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;
291 } else {
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
295 // window).
296 if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH &&
297 params->parent &&
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;
305 #endif // OS_WIN
306 #endif // USE_AURA
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
314 // context.
315 if (params->context)
316 params->context = params->context->GetRootWindow();
317 DCHECK(params->parent || params->context || !params->child)
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 toplevel, it's
324 // possible that there is no contextual state that we can use.
325 if (params->parent == NULL && params->context == NULL && !params->child) {
326 // We need to make a decision about where to place this window based on the
327 // desktop type.
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
331 // widget.
332 params->native_widget = new views::DesktopNativeWidgetAura(delegate);
333 break;
334 #if defined(USE_ASH)
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();
338 break;
339 #endif
340 default:
341 NOTREACHED();
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,
350 parent_profile);
352 params->native_widget = native_widget;
353 } else {
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);
363 #endif
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
369 // windows.
370 views::LinuxUI* ui = views::LinuxUI::instance();
371 return maximized && ui && ui->UnityIsRunning();
373 #endif
375 ui::ContextFactory* ChromeViewsDelegate::GetContextFactory() {
376 return content::GetContextFactory();
379 #if defined(OS_WIN)
380 int ChromeViewsDelegate::GetAppbarAutohideEdges(HMONITOR monitor,
381 const base::Closure& callback) {
382 // Initialize the map with EDGE_BOTTOM. This is important, as if we return an
383 // initial value of 0 (no auto-hide edges) then we'll go fullscreen and
384 // windows will automatically remove WS_EX_TOPMOST from the appbar resulting
385 // in us thinking there is no auto-hide edges. By returning at least one edge
386 // we don't initially go fullscreen until we figure out the real auto-hide
387 // edges.
388 if (!appbar_autohide_edge_map_.count(monitor))
389 appbar_autohide_edge_map_[monitor] = EDGE_BOTTOM;
390 if (monitor && !in_autohide_edges_callback_) {
391 base::PostTaskAndReplyWithResult(
392 content::BrowserThread::GetBlockingPool(),
393 FROM_HERE,
394 base::Bind(&GetAppbarAutohideEdgesOnWorkerThread,
395 monitor),
396 base::Bind(&ChromeViewsDelegate::OnGotAppbarAutohideEdges,
397 weak_factory_.GetWeakPtr(),
398 callback,
399 monitor,
400 appbar_autohide_edge_map_[monitor]));
402 return appbar_autohide_edge_map_[monitor];
405 void ChromeViewsDelegate::OnGotAppbarAutohideEdges(
406 const base::Closure& callback,
407 HMONITOR monitor,
408 int returned_edges,
409 int edges) {
410 appbar_autohide_edge_map_[monitor] = edges;
411 if (returned_edges == edges)
412 return;
414 base::AutoReset<bool> in_callback_setter(&in_autohide_edges_callback_, true);
415 callback.Run();
417 #endif
419 #if !defined(USE_AURA) && !defined(USE_CHROMEOS)
420 views::Widget::InitParams::WindowOpacity
421 ChromeViewsDelegate::GetOpacityForInitParams(
422 const views::Widget::InitParams& params) {
423 return views::Widget::InitParams::OPAQUE_WINDOW;
425 #endif