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