browser_frame_ash.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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 // For legacy reasons v1 apps (like Secure Shell) are allowed to consume keys 74 // like brightness, volume, etc. Otherwise these keys are handled by the 75 // Ash window manager. 76 window_state->set_can_consume_system_keys(browser->is_app()); 77#endif // defined(OS_CHROMEOS) 78} 79 80/////////////////////////////////////////////////////////////////////////////// 81// BrowserFrameAsh, views::NativeWidgetAura overrides: 82 83void BrowserFrameAsh::OnWindowDestroying(aura::Window* window) { 84 // Destroy any remaining WebContents early on. Doing so may result in 85 // calling back to one of the Views/LayoutManagers or supporting classes of 86 // BrowserView. By destroying here we ensure all said classes are valid. 87 DestroyBrowserWebContents(browser_view_->browser()); 88 NativeWidgetAura::OnWindowDestroying(window); 89} 90 91void BrowserFrameAsh::OnWindowTargetVisibilityChanged(bool visible) { 92 if (visible) { 93 // Once the window has been shown we know the requested bounds 94 // (if provided) have been honored and we can switch on window management. 95 SetWindowAutoManaged(); 96 } 97 views::NativeWidgetAura::OnWindowTargetVisibilityChanged(visible); 98} 99 100//////////////////////////////////////////////////////////////////////////////// 101// BrowserFrameAsh, NativeBrowserFrame implementation: 102 103views::NativeWidget* BrowserFrameAsh::AsNativeWidget() { 104 return this; 105} 106 107const views::NativeWidget* BrowserFrameAsh::AsNativeWidget() const { 108 return this; 109} 110 111bool BrowserFrameAsh::UsesNativeSystemMenu() const { 112 return false; 113} 114 115int BrowserFrameAsh::GetMinimizeButtonOffset() const { 116 return 0; 117} 118 119BrowserFrameAsh::~BrowserFrameAsh() { 120} 121 122/////////////////////////////////////////////////////////////////////////////// 123// BrowserFrameAsh, private: 124 125void BrowserFrameAsh::SetWindowAutoManaged() { 126 if (!browser_view_->browser()->is_type_popup() || 127 browser_view_->browser()->is_app()) { 128 ash::wm::GetWindowState(GetNativeWindow())-> 129 set_window_position_managed(true); 130 } 131} 132