chrome_views_delegate.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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 "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      GetDisplayNearestWindow(window->GetNativeView()).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
208  // We can use toplevel windows for most things. On Vista+ with DWM, they get
209  // blended via WS_EX_COMPOSITED. Otherwise, they're rendered by the software
210  // compositor and the hwnd is converted to a WS_EX_LAYERED.
211  if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH &&
212      params->parent &&
213      params->type != views::Widget::InitParams::TYPE_CONTROL &&
214      params->type != views::Widget::InitParams::TYPE_WINDOW) {
215    // When we set this to false, we get a DesktopNativeWidgetAura from the
216    // default case (not handled in this function).
217    use_non_toplevel_window = false;
218  }
219#endif  // OS_WIN
220#endif  // USE_AURA
221
222#if defined(OS_CHROMEOS)
223  // When we are doing straight chromeos builds, we still need to handle the
224  // toplevel window case.
225  // There may be a few remaining widgets in Chrome OS that are not top level,
226  // but have neither a context nor a parent. Provide a fallback context so
227  // users don't crash. Developers will hit the DCHECK and should provide a
228  // context.
229  if (params->context)
230    params->context = params->context->GetRootWindow();
231  DCHECK(params->parent || params->context || params->top_level)
232      << "Please provide a parent or context for this widget.";
233  if (!params->parent && !params->context)
234    params->context = ash::Shell::GetPrimaryRootWindow();
235#elif defined(USE_AURA)
236  // While the majority of the time, context wasn't plumbed through due to the
237  // existence of a global WindowTreeClient, if this window is a toplevel, it's
238  // possible that there is no contextual state that we can use.
239  if (params->parent == NULL && params->context == NULL && params->top_level) {
240    // We need to make a decision about where to place this window based on the
241    // desktop type.
242    switch (chrome::GetActiveDesktop()) {
243      case chrome::HOST_DESKTOP_TYPE_NATIVE:
244        // If we're native, we should give this window its own toplevel desktop
245        // widget.
246        params->native_widget = new views::DesktopNativeWidgetAura(delegate);
247        break;
248#if defined(USE_ASH)
249      case chrome::HOST_DESKTOP_TYPE_ASH:
250        // If we're in ash, give this window the context of the main monitor.
251        params->context = ash::Shell::GetPrimaryRootWindow();
252        break;
253#endif
254      default:
255        NOTREACHED();
256    }
257  } else if (use_non_toplevel_window) {
258    params->native_widget = new views::NativeWidgetAura(delegate);
259  } else if (params->type != views::Widget::InitParams::TYPE_TOOLTIP) {
260    // TODO(erg): Once we've threaded context to everywhere that needs it, we
261    // should remove this check here.
262    gfx::NativeView to_check =
263        params->context ? params->context : params->parent;
264    if (chrome::GetHostDesktopTypeForNativeView(to_check) ==
265        chrome::HOST_DESKTOP_TYPE_NATIVE) {
266      params->native_widget = new views::DesktopNativeWidgetAura(delegate);
267    }
268  }
269#endif
270}
271
272#if !defined(OS_CHROMEOS)
273base::TimeDelta
274ChromeViewsDelegate::GetDefaultTextfieldObscuredRevealDuration() {
275  return base::TimeDelta();
276}
277#endif
278