chrome_views_delegate.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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#if defined(OS_WIN)
192  // If we're on Vista+ with composition enabled, then we can use toplevel
193  // windows for most things (they get blended via WS_EX_COMPOSITED, which
194  // allows for animation effects, but also exceeding the bounds of the parent
195  // window).
196  if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
197    BOOL composition_enabled = FALSE;
198    HRESULT hr = DwmIsCompositionEnabled(&composition_enabled);
199    if (CommandLine::ForCurrentProcess()->HasSwitch(
200            switches::kDisableDwmComposition))
201      composition_enabled = FALSE;
202    if (SUCCEEDED(hr) && composition_enabled) {
203      if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH &&
204          params->parent &&
205          params->type != views::Widget::InitParams::TYPE_CONTROL &&
206          params->type != views::Widget::InitParams::TYPE_WINDOW) {
207        // When we set this to false, we get a DesktopNativeWidgetAura from the
208        // default case (not handled in this function).
209        use_non_toplevel_window = false;
210      }
211    }
212  }
213#endif  // OS_WIN
214#endif  // USE_AURA
215
216#if defined(OS_CHROMEOS)
217  // When we are doing straight chromeos builds, we still need to handle the
218  // toplevel window case.
219  // There may be a few remaining widgets in Chrome OS that are not top level,
220  // but have neither a context nor a parent. Provide a fallback context so
221  // users don't crash. Developers will hit the DCHECK and should provide a
222  // context.
223  DCHECK(params->parent || params->context || params->top_level)
224      << "Please provide a parent or context for this widget.";
225  if (!params->parent && !params->context)
226    params->context = ash::Shell::GetPrimaryRootWindow();
227#elif defined(USE_AURA)
228  // While the majority of the time, context wasn't plumbed through due to the
229  // existence of a global StackingClient, if this window is a toplevel, it's
230  // possible that there is no contextual state that we can use.
231  if (params->parent == NULL && params->context == NULL && params->top_level) {
232    // We need to make a decision about where to place this window based on the
233    // desktop type.
234    switch (chrome::GetActiveDesktop()) {
235      case chrome::HOST_DESKTOP_TYPE_NATIVE:
236        // If we're native, we should give this window its own toplevel desktop
237        // widget.
238        params->native_widget = new views::DesktopNativeWidgetAura(delegate);
239        break;
240#if defined(USE_ASH)
241      case chrome::HOST_DESKTOP_TYPE_ASH:
242        // If we're in ash, give this window the context of the main monitor.
243        params->context = ash::Shell::GetPrimaryRootWindow();
244        break;
245#endif
246      default:
247        NOTREACHED();
248    }
249  } else if (use_non_toplevel_window) {
250    params->native_widget = new views::NativeWidgetAura(delegate);
251  } else if (params->type != views::Widget::InitParams::TYPE_TOOLTIP) {
252    // TODO(erg): Once we've threaded context to everywhere that needs it, we
253    // should remove this check here.
254    gfx::NativeView to_check =
255        params->context ? params->context : params->parent;
256    if (chrome::GetHostDesktopTypeForNativeView(to_check) ==
257        chrome::HOST_DESKTOP_TYPE_NATIVE) {
258      params->native_widget = new views::DesktopNativeWidgetAura(delegate);
259    }
260  }
261#endif
262}
263
264#if !defined(OS_CHROMEOS)
265base::TimeDelta
266ChromeViewsDelegate::GetDefaultTextfieldObscuredRevealDuration() {
267  return base::TimeDelta();
268}
269#endif
270