[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_frame_ash.cc
blobeda4329ff3d461cdd5ef6c4276138b4767be7dea
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"
20 using aura::Window;
22 namespace {
24 // BrowserWindowStateDelegate class handles a user's fullscreen
25 // request (Shift+F4/F4).
26 class BrowserWindowStateDelegate : public ash::wm::WindowStateDelegate {
27 public:
28 explicit BrowserWindowStateDelegate(Browser* browser)
29 : browser_(browser) {
30 DCHECK(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())
39 return true;
40 chrome::ToggleFullscreenMode(browser_);
41 return true;
43 private:
44 Browser* browser_; // not owned.
46 DISALLOW_COPY_AND_ASSIGN(BrowserWindowStateDelegate);
49 } // namespace
51 ///////////////////////////////////////////////////////////////////////////////
52 // BrowserFrameAsh, public:
54 // static
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) {
93 if (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(
107 gfx::Rect* bounds,
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);
115 } else {
116 *bounds = GetWidget()->GetRestoredBounds();
117 *show_state = GetWidget()->GetNativeWindow()->GetProperty(
118 aura::client::kShowStateKey);
122 ////////////////////////////////////////////////////////////////////////////////
123 // BrowserFrameAsh, NativeBrowserFrame implementation:
125 views::NativeWidget* BrowserFrameAsh::AsNativeWidget() {
126 return this;
129 const views::NativeWidget* BrowserFrameAsh::AsNativeWidget() const {
130 return this;
133 bool BrowserFrameAsh::UsesNativeSystemMenu() const {
134 return false;
137 int BrowserFrameAsh::GetMinimizeButtonOffset() const {
138 return 0;
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);