browser_frame_ash.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2013 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/frame/browser_frame_ash.h" 6 7#include "ash/wm/window_state.h" 8#include "ash/wm/window_state_delegate.h" 9#include "ash/wm/window_util.h" 10#include "chrome/browser/ui/browser_commands.h" 11#include "chrome/browser/ui/browser_finder.h" 12#include "chrome/browser/ui/views/frame/browser_shutdown.h" 13#include "chrome/browser/ui/views/frame/browser_view.h" 14#include "ui/aura/client/aura_constants.h" 15#include "ui/aura/window.h" 16#include "ui/aura/window_observer.h" 17#include "ui/views/view.h" 18 19using aura::Window; 20 21namespace { 22 23// BrowserWindowStateDelegate class handles a user's fullscreen 24// request (Shift+F4/F4). 25class BrowserWindowStateDelegate : public ash::wm::WindowStateDelegate { 26 public: 27 explicit BrowserWindowStateDelegate(Browser* browser) 28 : browser_(browser) { 29 DCHECK(browser_); 30 } 31 virtual ~BrowserWindowStateDelegate(){} 32 33 // Overridden from ash::wm::WindowStateDelegate. 34 virtual bool ToggleFullscreen(ash::wm::WindowState* window_state) OVERRIDE { 35 DCHECK(window_state->IsFullscreen() || window_state->CanMaximize()); 36 // Windows which cannot be maximized should not be fullscreened. 37 if (!window_state->IsFullscreen() && !window_state->CanMaximize()) 38 return true; 39 chrome::ToggleFullscreenMode(browser_); 40 return true; 41 } 42 private: 43 Browser* browser_; // not owned. 44 45 DISALLOW_COPY_AND_ASSIGN(BrowserWindowStateDelegate); 46}; 47 48} // namespace 49 50/////////////////////////////////////////////////////////////////////////////// 51// BrowserFrameAsh, public: 52 53// static 54const char BrowserFrameAsh::kWindowName[] = "BrowserFrameAsh"; 55 56BrowserFrameAsh::BrowserFrameAsh(BrowserFrame* browser_frame, 57 BrowserView* browser_view) 58 : views::NativeWidgetAura(browser_frame), 59 browser_view_(browser_view) { 60 GetNativeWindow()->SetName(kWindowName); 61 Browser* browser = browser_view->browser(); 62 ash::wm::WindowState* window_state = 63 ash::wm::GetWindowState(GetNativeWindow()); 64 window_state->SetDelegate( 65 scoped_ptr<ash::wm::WindowStateDelegate>( 66 new BrowserWindowStateDelegate(browser)).Pass()); 67 68 // Turn on auto window management if we don't need an explicit bounds. 69 // This way the requested bounds are honored. 70 if (!browser->bounds_overridden() && !browser->is_session_restore()) 71 SetWindowAutoManaged(); 72#if defined(OS_CHROMEOS) 73 if (browser->is_type_tabbed()) { 74 // Animating to immersive fullscreen does not look good. Immersive 75 // fullscreen is the default fullscreen type on ChromeOS for tabbed browser 76 // windows. The WindowState constructor disables animating to fullscreen 77 // completely when switches::UseImmersiveFullscreenForAllWindows() returns 78 // true. 79 window_state->set_animate_to_fullscreen(false); 80 } 81 82 // For legacy reasons v1 apps (like Secure Shell) are allowed to consume keys 83 // like brightness, volume, etc. Otherwise these keys are handled by the 84 // Ash window manager. 85 window_state->set_can_consume_system_keys(browser->is_app()); 86#endif // defined(OS_CHROMEOS) 87} 88 89/////////////////////////////////////////////////////////////////////////////// 90// BrowserFrameAsh, views::NativeWidgetAura overrides: 91 92void BrowserFrameAsh::OnWindowDestroying() { 93 // Destroy any remaining WebContents early on. Doing so may result in 94 // calling back to one of the Views/LayoutManagers or supporting classes of 95 // BrowserView. By destroying here we ensure all said classes are valid. 96 DestroyBrowserWebContents(browser_view_->browser()); 97 NativeWidgetAura::OnWindowDestroying(); 98} 99 100void BrowserFrameAsh::OnWindowTargetVisibilityChanged(bool visible) { 101 if (visible) { 102 // Once the window has been shown we know the requested bounds 103 // (if provided) have been honored and we can switch on window management. 104 SetWindowAutoManaged(); 105 } 106 views::NativeWidgetAura::OnWindowTargetVisibilityChanged(visible); 107} 108 109//////////////////////////////////////////////////////////////////////////////// 110// BrowserFrameAsh, NativeBrowserFrame implementation: 111 112views::NativeWidget* BrowserFrameAsh::AsNativeWidget() { 113 return this; 114} 115 116const views::NativeWidget* BrowserFrameAsh::AsNativeWidget() const { 117 return this; 118} 119 120bool BrowserFrameAsh::UsesNativeSystemMenu() const { 121 return false; 122} 123 124int BrowserFrameAsh::GetMinimizeButtonOffset() const { 125 return 0; 126} 127 128BrowserFrameAsh::~BrowserFrameAsh() { 129} 130 131/////////////////////////////////////////////////////////////////////////////// 132// BrowserFrameAsh, private: 133 134void BrowserFrameAsh::SetWindowAutoManaged() { 135 if (browser_view_->browser()->type() != Browser::TYPE_POPUP || 136 browser_view_->browser()->is_app()) { 137 ash::wm::GetWindowState(GetNativeWindow())-> 138 set_window_position_managed(true); 139 } 140} 141