chrome_views_delegate.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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/chrome_views_delegate.h"
6
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 "ui/base/ui_base_switches.h"
18#include "ui/gfx/rect.h"
19#include "ui/gfx/screen.h"
20#include "ui/views/widget/native_widget.h"
21#include "ui/views/widget/widget.h"
22
23#if defined(OS_WIN)
24#include <dwmapi.h>
25#include "base/win/windows_version.h"
26#include "chrome/browser/app_icon_win.h"
27#include "ui/base/win/shell.h"
28#endif
29
30#if defined(USE_AURA)
31#include "ui/aura/root_window.h"
32#endif
33
34#if defined(USE_AURA) && !defined(OS_CHROMEOS)
35#include "chrome/browser/ui/host_desktop.h"
36#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
37#include "ui/views/widget/native_widget_aura.h"
38#endif
39
40#if defined(USE_ASH)
41#include "ash/shell.h"
42#include "ash/wm/window_state.h"
43#include "chrome/browser/ui/ash/ash_init.h"
44#include "chrome/browser/ui/ash/ash_util.h"
45#endif
46
47namespace {
48
49// If the given window has a profile associated with it, use that profile's
50// preference service. Otherwise, store and retrieve the data from Local State.
51// This function may return NULL if the necessary pref service has not yet
52// been initialized.
53// TODO(mirandac): This function will also separate windows by profile in a
54// multi-profile environment.
55PrefService* GetPrefsForWindow(const views::Widget* window) {
56  Profile* profile = reinterpret_cast<Profile*>(
57      window->GetNativeWindowProperty(Profile::kProfileKey));
58  if (!profile) {
59    // Use local state for windows that have no explicit profile.
60    return g_browser_process->local_state();
61  }
62  return profile->GetPrefs();
63}
64
65}  // namespace
66
67///////////////////////////////////////////////////////////////////////////////
68// ChromeViewsDelegate, views::ViewsDelegate implementation:
69
70void ChromeViewsDelegate::SaveWindowPlacement(const views::Widget* window,
71                                              const std::string& window_name,
72                                              const gfx::Rect& bounds,
73                                              ui::WindowShowState show_state) {
74  PrefService* prefs = GetPrefsForWindow(window);
75  if (!prefs)
76    return;
77
78  DCHECK(prefs->FindPreference(window_name.c_str()));
79  DictionaryPrefUpdate update(prefs, window_name.c_str());
80  DictionaryValue* window_preferences = update.Get();
81  window_preferences->SetInteger("left", bounds.x());
82  window_preferences->SetInteger("top", bounds.y());
83  window_preferences->SetInteger("right", bounds.right());
84  window_preferences->SetInteger("bottom", bounds.bottom());
85  window_preferences->SetBoolean("maximized",
86                                 show_state == ui::SHOW_STATE_MAXIMIZED);
87  gfx::Rect work_area(gfx::Screen::GetScreenFor(window->GetNativeView())->
88      GetDisplayNearestWindow(window->GetNativeView()).work_area());
89  window_preferences->SetInteger("work_area_left", work_area.x());
90  window_preferences->SetInteger("work_area_top", work_area.y());
91  window_preferences->SetInteger("work_area_right", work_area.right());
92  window_preferences->SetInteger("work_area_bottom", work_area.bottom());
93}
94
95bool ChromeViewsDelegate::GetSavedWindowPlacement(
96    const views::Widget* widget,
97    const std::string& window_name,
98    gfx::Rect* bounds,
99    ui::WindowShowState* show_state) const {
100  PrefService* prefs = g_browser_process->local_state();
101  if (!prefs)
102    return false;
103
104  DCHECK(prefs->FindPreference(window_name.c_str()));
105  const DictionaryValue* dictionary = prefs->GetDictionary(window_name.c_str());
106  int left, top, right, bottom;
107  if (!dictionary || !dictionary->GetInteger("left", &left) ||
108      !dictionary->GetInteger("top", &top) ||
109      !dictionary->GetInteger("right", &right) ||
110      !dictionary->GetInteger("bottom", &bottom))
111    return false;
112
113  bounds->SetRect(left, top, right - left, bottom - top);
114
115  bool maximized = false;
116  if (dictionary)
117    dictionary->GetBoolean("maximized", &maximized);
118  *show_state = maximized ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL;
119
120#if defined(USE_ASH)
121  // On Ash environment, a window won't span across displays.  Adjust
122  // the bounds to fit the work area.
123  gfx::NativeView window = widget->GetNativeView();
124  if (chrome::GetHostDesktopTypeForNativeView(window) ==
125      chrome::HOST_DESKTOP_TYPE_ASH) {
126    gfx::Display display = gfx::Screen::GetScreenFor(window)->
127        GetDisplayMatching(*bounds);
128    bounds->AdjustToFit(display.work_area());
129    ash::wm::GetWindowState(window)->set_minimum_visibility(true);
130  }
131#endif
132  return true;
133}
134
135void ChromeViewsDelegate::NotifyAccessibilityEvent(
136    views::View* view, ui::AccessibilityTypes::Event event_type) {
137  AccessibilityEventRouterViews::GetInstance()->HandleAccessibilityEvent(
138      view, event_type);
139}
140
141void ChromeViewsDelegate::NotifyMenuItemFocused(const string16& menu_name,
142                                                const string16& menu_item_name,
143                                                int item_index,
144                                                int item_count,
145                                                bool has_submenu) {
146  AccessibilityEventRouterViews::GetInstance()->HandleMenuItemFocused(
147      menu_name, menu_item_name, item_index, item_count, has_submenu);
148}
149
150#if defined(OS_WIN)
151HICON ChromeViewsDelegate::GetDefaultWindowIcon() const {
152  return GetAppIcon();
153}
154#endif
155
156views::NonClientFrameView* ChromeViewsDelegate::CreateDefaultNonClientFrameView(
157    views::Widget* widget) {
158#if defined(USE_ASH)
159  if (chrome::IsNativeViewInAsh(widget->GetNativeView()))
160    return ash::Shell::GetInstance()->CreateDefaultNonClientFrameView(widget);
161#endif
162  return NULL;
163}
164
165bool ChromeViewsDelegate::UseTransparentWindows() const {
166#if defined(USE_ASH)
167  // TODO(scottmg): http://crbug.com/133312. This needs context to determine
168  // if it's desktop or ash.
169#if defined(OS_CHROMEOS)
170  return true;
171#else
172  NOTIMPLEMENTED();
173  return false;
174#endif
175#else
176  return false;
177#endif
178}
179
180void ChromeViewsDelegate::AddRef() {
181  g_browser_process->AddRefModule();
182}
183
184void ChromeViewsDelegate::ReleaseRef() {
185  g_browser_process->ReleaseModule();
186}
187
188content::WebContents* ChromeViewsDelegate::CreateWebContents(
189    content::BrowserContext* browser_context,
190    content::SiteInstance* site_instance) {
191  return NULL;
192}
193
194void ChromeViewsDelegate::OnBeforeWidgetInit(
195    views::Widget::InitParams* params,
196    views::internal::NativeWidgetDelegate* delegate) {
197  // If we already have a native_widget, we don't have to try to come
198  // up with one.
199  if (params->native_widget)
200    return;
201
202#if defined(USE_AURA) && !defined(OS_CHROMEOS)
203  bool use_non_toplevel_window =
204      params->parent && params->type != views::Widget::InitParams::TYPE_MENU;
205
206#if defined(OS_WIN)
207  // On desktop Linux Chrome must run in an environment that supports a variety
208  // of window managers, some of which do not play nicely with parts of our UI
209  // that have specific expectations about window sizing and placement. For this
210  // reason windows opened as top level (params.top_level) are always
211  // constrained by the browser frame, so we can position them correctly. This
212  // has some negative side effects, like dialogs being clipped by the browser
213  // frame, but the side effects are not as bad as the poor window manager
214  // interactions. On Windows however these WM interactions are not an issue, so
215  // we open windows requested as top_level as actual top level windows on the
216  // desktop.
217  use_non_toplevel_window = use_non_toplevel_window &&
218      (!params->top_level ||
219       chrome::GetHostDesktopTypeForNativeView(params->parent) !=
220          chrome::HOST_DESKTOP_TYPE_NATIVE);
221
222  if (!ui::win::IsAeroGlassEnabled()) {
223    // If we don't have composition (either because Glass is not enabled or
224    // because it was disabled at the command line), anything that requires
225    // transparency will be broken with a toplevel window, so force the use of
226    // a non toplevel window.
227    if (params->opacity == views::Widget::InitParams::TRANSLUCENT_WINDOW &&
228        params->type != views::Widget::InitParams::TYPE_MENU)
229      use_non_toplevel_window = true;
230  } else {
231    // If we're on Vista+ with composition enabled, then we can use toplevel
232    // windows for most things (they get blended via WS_EX_COMPOSITED, which
233    // allows for animation effects, but also exceeding the bounds of the parent
234    // window).
235    if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH &&
236        params->parent &&
237        params->type != views::Widget::InitParams::TYPE_CONTROL &&
238        params->type != views::Widget::InitParams::TYPE_WINDOW) {
239      // When we set this to false, we get a DesktopNativeWidgetAura from the
240      // default case (not handled in this function).
241      use_non_toplevel_window = false;
242    }
243  }
244#endif  // OS_WIN
245#endif  // USE_AURA
246
247#if defined(OS_CHROMEOS)
248  // When we are doing straight chromeos builds, we still need to handle the
249  // toplevel window case.
250  // There may be a few remaining widgets in Chrome OS that are not top level,
251  // but have neither a context nor a parent. Provide a fallback context so
252  // users don't crash. Developers will hit the DCHECK and should provide a
253  // context.
254  if (params->context)
255    params->context = params->context->GetRootWindow();
256  DCHECK(params->parent || params->context || params->top_level)
257      << "Please provide a parent or context for this widget.";
258  if (!params->parent && !params->context)
259    params->context = ash::Shell::GetPrimaryRootWindow();
260#elif defined(USE_AURA)
261  // While the majority of the time, context wasn't plumbed through due to the
262  // existence of a global WindowTreeClient, if this window is a toplevel, it's
263  // possible that there is no contextual state that we can use.
264  if (params->parent == NULL && params->context == NULL && params->top_level) {
265    // We need to make a decision about where to place this window based on the
266    // desktop type.
267    switch (chrome::GetActiveDesktop()) {
268      case chrome::HOST_DESKTOP_TYPE_NATIVE:
269        // If we're native, we should give this window its own toplevel desktop
270        // widget.
271        params->native_widget = new views::DesktopNativeWidgetAura(delegate);
272        break;
273#if defined(USE_ASH)
274      case chrome::HOST_DESKTOP_TYPE_ASH:
275        // If we're in ash, give this window the context of the main monitor.
276        params->context = ash::Shell::GetPrimaryRootWindow();
277        break;
278#endif
279      default:
280        NOTREACHED();
281    }
282  } else if (use_non_toplevel_window) {
283    params->native_widget = new views::NativeWidgetAura(delegate);
284  } else if (params->type != views::Widget::InitParams::TYPE_TOOLTIP) {
285    // TODO(erg): Once we've threaded context to everywhere that needs it, we
286    // should remove this check here.
287    gfx::NativeView to_check =
288        params->context ? params->context : params->parent;
289    if (chrome::GetHostDesktopTypeForNativeView(to_check) ==
290        chrome::HOST_DESKTOP_TYPE_NATIVE) {
291      params->native_widget = new views::DesktopNativeWidgetAura(delegate);
292    }
293  }
294#endif
295}
296
297base::TimeDelta
298ChromeViewsDelegate::GetDefaultTextfieldObscuredRevealDuration() {
299  return base::TimeDelta();
300}
301