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.
5 #include "chrome/browser/ui/views/frame/browser_frame_ash.h"
7 #include "ash/wm/window_properties.h"
8 #include "ash/wm/window_state.h"
9 #include "ash/wm/window_state_delegate.h"
10 #include "ash/wm/window_util.h"
11 #include "chrome/browser/ui/browser_commands.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/views/frame/browser_shutdown.h"
14 #include "chrome/browser/ui/views/frame/browser_view.h"
15 #include "ui/aura/client/aura_constants.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_observer.h"
18 #include "ui/views/view.h"
24 // BrowserWindowStateDelegate class handles a user's fullscreen
25 // request (Shift+F4/F4).
26 class BrowserWindowStateDelegate
: public ash::wm::WindowStateDelegate
{
28 explicit BrowserWindowStateDelegate(Browser
* browser
)
32 virtual ~BrowserWindowStateDelegate(){}
34 // Overridden from ash::wm::WindowStateDelegate.
35 virtual bool ToggleFullscreen(ash::wm::WindowState
* window_state
) OVERRIDE
{
36 DCHECK(window_state
->IsFullscreen() || window_state
->CanMaximize());
37 // Windows which cannot be maximized should not be fullscreened.
38 if (!window_state
->IsFullscreen() && !window_state
->CanMaximize())
40 chrome::ToggleFullscreenMode(browser_
);
44 Browser
* browser_
; // not owned.
46 DISALLOW_COPY_AND_ASSIGN(BrowserWindowStateDelegate
);
51 ///////////////////////////////////////////////////////////////////////////////
52 // BrowserFrameAsh, public:
55 const char BrowserFrameAsh::kWindowName
[] = "BrowserFrameAsh";
57 BrowserFrameAsh::BrowserFrameAsh(BrowserFrame
* browser_frame
,
58 BrowserView
* browser_view
)
59 : views::NativeWidgetAura(browser_frame
),
60 browser_view_(browser_view
) {
61 GetNativeWindow()->SetName(kWindowName
);
62 Browser
* browser
= browser_view
->browser();
63 ash::wm::WindowState
* window_state
=
64 ash::wm::GetWindowState(GetNativeWindow());
65 window_state
->SetDelegate(
66 scoped_ptr
<ash::wm::WindowStateDelegate
>(
67 new BrowserWindowStateDelegate(browser
)).Pass());
69 // Turn on auto window management if we don't need an explicit bounds.
70 // This way the requested bounds are honored.
71 if (!browser
->bounds_overridden() && !browser
->is_session_restore())
72 SetWindowAutoManaged();
73 #if defined(OS_CHROMEOS)
74 // For legacy reasons v1 apps (like Secure Shell) are allowed to consume keys
75 // like brightness, volume, etc. Otherwise these keys are handled by the
76 // Ash window manager.
77 window_state
->set_can_consume_system_keys(browser
->is_app());
78 #endif // defined(OS_CHROMEOS)
81 ///////////////////////////////////////////////////////////////////////////////
82 // BrowserFrameAsh, views::NativeWidgetAura overrides:
84 void BrowserFrameAsh::OnWindowDestroying(aura::Window
* window
) {
85 // Destroy any remaining WebContents early on. Doing so may result in
86 // calling back to one of the Views/LayoutManagers or supporting classes of
87 // BrowserView. By destroying here we ensure all said classes are valid.
88 DestroyBrowserWebContents(browser_view_
->browser());
89 NativeWidgetAura::OnWindowDestroying(window
);
92 void BrowserFrameAsh::OnWindowTargetVisibilityChanged(bool visible
) {
94 // Once the window has been shown we know the requested bounds
95 // (if provided) have been honored and we can switch on window management.
96 SetWindowAutoManaged();
98 views::NativeWidgetAura::OnWindowTargetVisibilityChanged(visible
);
101 bool BrowserFrameAsh::ShouldSaveWindowPlacement() const {
102 return NULL
== GetWidget()->GetNativeWindow()->GetProperty(
103 ash::kRestoreBoundsOverrideKey
);
106 void BrowserFrameAsh::GetWindowPlacement(
108 ui::WindowShowState
* show_state
) const {
109 gfx::Rect
* override_bounds
= GetWidget()->GetNativeWindow()->GetProperty(
110 ash::kRestoreBoundsOverrideKey
);
111 if (override_bounds
&& !override_bounds
->IsEmpty()) {
112 *bounds
= *override_bounds
;
113 *show_state
= GetWidget()->GetNativeWindow()->GetProperty(
114 ash::kRestoreShowStateOverrideKey
);
116 *bounds
= GetWidget()->GetRestoredBounds();
117 *show_state
= GetWidget()->GetNativeWindow()->GetProperty(
118 aura::client::kShowStateKey
);
122 ////////////////////////////////////////////////////////////////////////////////
123 // BrowserFrameAsh, NativeBrowserFrame implementation:
125 views::NativeWidget
* BrowserFrameAsh::AsNativeWidget() {
129 const views::NativeWidget
* BrowserFrameAsh::AsNativeWidget() const {
133 bool BrowserFrameAsh::UsesNativeSystemMenu() const {
137 int BrowserFrameAsh::GetMinimizeButtonOffset() const {
141 BrowserFrameAsh::~BrowserFrameAsh() {
144 ///////////////////////////////////////////////////////////////////////////////
145 // BrowserFrameAsh, private:
147 void BrowserFrameAsh::SetWindowAutoManaged() {
148 if (!browser_view_
->browser()->is_type_popup() ||
149 browser_view_
->browser()->is_app()) {
150 ash::wm::GetWindowState(GetNativeWindow())->
151 set_window_position_managed(true);