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