[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_desktop_window_tree_host_win.cc
blobf6393124dce2b34be8eb2f7a96d0c21c0b904e69
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.
5 #include "chrome/browser/ui/views/frame/browser_desktop_window_tree_host_win.h"
7 #include <dwmapi.h>
9 #include "chrome/browser/lifetime/application_lifetime.h"
10 #include "chrome/browser/themes/theme_service.h"
11 #include "chrome/browser/themes/theme_service_factory.h"
12 #include "chrome/browser/ui/views/frame/browser_frame.h"
13 #include "chrome/browser/ui/views/frame/browser_frame_common_win.h"
14 #include "chrome/browser/ui/views/frame/browser_view.h"
15 #include "chrome/browser/ui/views/frame/browser_window_property_manager_win.h"
16 #include "chrome/browser/ui/views/frame/system_menu_insertion_delegate_win.h"
17 #include "chrome/browser/ui/views/tabs/tab_strip.h"
18 #include "chrome/browser/ui/views/theme_image_mapper.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/theme_provider.h"
21 #include "ui/gfx/win/dpi.h"
22 #include "ui/views/controls/menu/native_menu_win.h"
24 #pragma comment(lib, "dwmapi.lib")
26 namespace {
28 const int kClientEdgeThickness = 3;
29 // We need to offset the DWMFrame into the toolbar so that the blackness
30 // doesn't show up on our rounded corners.
31 const int kDWMFrameTopOffset = 3;
33 // DesktopThemeProvider maps resource ids using MapThemeImage(). This is
34 // necessary for BrowserDesktopWindowTreeHostWin so that it uses the windows
35 // theme images rather than the ash theme images.
36 class DesktopThemeProvider : public ui::ThemeProvider {
37 public:
38 explicit DesktopThemeProvider(ui::ThemeProvider* delegate)
39 : delegate_(delegate) {
42 virtual bool UsingSystemTheme() const OVERRIDE {
43 return delegate_->UsingSystemTheme();
45 virtual gfx::ImageSkia* GetImageSkiaNamed(int id) const OVERRIDE {
46 return delegate_->GetImageSkiaNamed(
47 chrome::MapThemeImage(chrome::HOST_DESKTOP_TYPE_NATIVE, id));
49 virtual SkColor GetColor(int id) const OVERRIDE {
50 return delegate_->GetColor(id);
52 virtual int GetDisplayProperty(int id) const OVERRIDE {
53 return delegate_->GetDisplayProperty(id);
55 virtual bool ShouldUseNativeFrame() const OVERRIDE {
56 return delegate_->ShouldUseNativeFrame();
58 virtual bool HasCustomImage(int id) const OVERRIDE {
59 return delegate_->HasCustomImage(
60 chrome::MapThemeImage(chrome::HOST_DESKTOP_TYPE_NATIVE, id));
62 virtual base::RefCountedMemory* GetRawData(
63 int id,
64 ui::ScaleFactor scale_factor) const OVERRIDE {
65 return delegate_->GetRawData(id, scale_factor);
68 private:
69 ui::ThemeProvider* delegate_;
71 DISALLOW_COPY_AND_ASSIGN(DesktopThemeProvider);
74 } // namespace
76 ////////////////////////////////////////////////////////////////////////////////
77 // BrowserDesktopWindowTreeHostWin, public:
79 BrowserDesktopWindowTreeHostWin::BrowserDesktopWindowTreeHostWin(
80 views::internal::NativeWidgetDelegate* native_widget_delegate,
81 views::DesktopNativeWidgetAura* desktop_native_widget_aura,
82 BrowserView* browser_view,
83 BrowserFrame* browser_frame)
84 : DesktopWindowTreeHostWin(native_widget_delegate,
85 desktop_native_widget_aura),
86 browser_view_(browser_view),
87 browser_frame_(browser_frame),
88 did_gdi_clear_(false) {
89 scoped_ptr<ui::ThemeProvider> theme_provider(
90 new DesktopThemeProvider(ThemeServiceFactory::GetForProfile(
91 browser_view->browser()->profile())));
92 browser_frame->SetThemeProvider(theme_provider.Pass());
95 BrowserDesktopWindowTreeHostWin::~BrowserDesktopWindowTreeHostWin() {
98 views::NativeMenuWin* BrowserDesktopWindowTreeHostWin::GetSystemMenu() {
99 if (!system_menu_.get()) {
100 SystemMenuInsertionDelegateWin insertion_delegate;
101 system_menu_.reset(
102 new views::NativeMenuWin(browser_frame_->GetSystemMenuModel(),
103 GetHWND()));
104 system_menu_->Rebuild(&insertion_delegate);
106 return system_menu_.get();
109 ////////////////////////////////////////////////////////////////////////////////
110 // BrowserDesktopWindowTreeHostWin, BrowserDesktopWindowTreeHost implementation:
112 views::DesktopWindowTreeHost*
113 BrowserDesktopWindowTreeHostWin::AsDesktopWindowTreeHost() {
114 return this;
117 int BrowserDesktopWindowTreeHostWin::GetMinimizeButtonOffset() const {
118 return minimize_button_metrics_.GetMinimizeButtonOffsetX();
121 bool BrowserDesktopWindowTreeHostWin::UsesNativeSystemMenu() const {
122 return true;
125 ////////////////////////////////////////////////////////////////////////////////
126 // BrowserDesktopWindowTreeHostWin, views::DesktopWindowTreeHostWin overrides:
128 int BrowserDesktopWindowTreeHostWin::GetInitialShowState() const {
129 STARTUPINFO si = {0};
130 si.cb = sizeof(si);
131 si.dwFlags = STARTF_USESHOWWINDOW;
132 GetStartupInfo(&si);
133 return si.wShowWindow;
136 bool BrowserDesktopWindowTreeHostWin::GetClientAreaInsets(
137 gfx::Insets* insets) const {
138 // Use the default client insets for an opaque frame or a glass popup/app
139 // frame.
140 if (!GetWidget()->ShouldUseNativeFrame() ||
141 !browser_view_->IsBrowserTypeNormal()) {
142 return false;
145 int border_thickness =
146 GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
147 // In fullscreen mode, we have no frame. In restored mode, we draw our own
148 // client edge over part of the default frame.
149 if (GetWidget()->IsFullscreen())
150 border_thickness = 0;
151 else if (!IsMaximized())
152 border_thickness -= kClientEdgeThickness;
153 insets->Set(0, border_thickness, border_thickness, border_thickness);
154 return true;
157 void BrowserDesktopWindowTreeHostWin::HandleCreate() {
158 DesktopWindowTreeHostWin::HandleCreate();
159 browser_window_property_manager_ =
160 BrowserWindowPropertyManager::CreateBrowserWindowPropertyManager(
161 browser_view_);
162 if (browser_window_property_manager_)
163 browser_window_property_manager_->UpdateWindowProperties(GetHWND());
166 void BrowserDesktopWindowTreeHostWin::HandleFrameChanged() {
167 // Reinitialize the status bubble, since it needs to be initialized
168 // differently depending on whether or not DWM composition is enabled
169 browser_view_->InitStatusBubble();
171 // We need to update the glass region on or off before the base class adjusts
172 // the window region.
173 UpdateDWMFrame();
174 DesktopWindowTreeHostWin::HandleFrameChanged();
177 bool BrowserDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
178 WPARAM w_param,
179 LPARAM l_param,
180 LRESULT* result) {
181 switch (message) {
182 case WM_ACTIVATE:
183 if (LOWORD(w_param) != WA_INACTIVE)
184 minimize_button_metrics_.OnHWNDActivated();
185 return false;
186 case WM_ENDSESSION:
187 chrome::SessionEnding();
188 return true;
189 case WM_INITMENUPOPUP:
190 GetSystemMenu()->UpdateStates();
191 return true;
193 return DesktopWindowTreeHostWin::PreHandleMSG(
194 message, w_param, l_param, result);
197 void BrowserDesktopWindowTreeHostWin::PostHandleMSG(UINT message,
198 WPARAM w_param,
199 LPARAM l_param) {
200 switch (message) {
201 case WM_CREATE:
202 minimize_button_metrics_.Init(GetHWND());
203 break;
204 case WM_WINDOWPOSCHANGED: {
205 UpdateDWMFrame();
207 // Windows lies to us about the position of the minimize button before a
208 // window is visible. We use this position to place the OTR avatar in RTL
209 // mode, so when the window is shown, we need to re-layout and schedule a
210 // paint for the non-client frame view so that the icon top has the correct
211 // position when the window becomes visible. This fixes bugs where the icon
212 // appears to overlay the minimize button.
213 // Note that we will call Layout every time SetWindowPos is called with
214 // SWP_SHOWWINDOW, however callers typically are careful about not
215 // specifying this flag unless necessary to avoid flicker.
216 // This may be invoked during creation on XP and before the non_client_view
217 // has been created.
218 WINDOWPOS* window_pos = reinterpret_cast<WINDOWPOS*>(l_param);
219 if (window_pos->flags & SWP_SHOWWINDOW && GetWidget()->non_client_view()) {
220 GetWidget()->non_client_view()->Layout();
221 GetWidget()->non_client_view()->SchedulePaint();
223 break;
225 case WM_ERASEBKGND:
226 if (!did_gdi_clear_ && DesktopWindowTreeHostWin::ShouldUseNativeFrame()) {
227 // This is necessary to avoid white flashing in the titlebar area around
228 // the minimize/maximize/close buttons.
229 HDC dc = GetDC(GetHWND());
230 MARGINS margins = GetDWMFrameMargins();
231 RECT client_rect;
232 GetClientRect(GetHWND(), &client_rect);
233 HBRUSH brush = CreateSolidBrush(0);
234 RECT rect = { 0, 0, client_rect.right, margins.cyTopHeight };
235 FillRect(dc, &rect, brush);
236 DeleteObject(brush);
237 ReleaseDC(GetHWND(), dc);
238 did_gdi_clear_ = true;
240 break;
245 bool BrowserDesktopWindowTreeHostWin::IsUsingCustomFrame() const {
246 // We don't theme popup or app windows, so regardless of whether or not a
247 // theme is active for normal browser windows, we don't want to use the custom
248 // frame for popups/apps.
249 if (!browser_view_->IsBrowserTypeNormal() &&
250 !DesktopWindowTreeHostWin::IsUsingCustomFrame()) {
251 return false;
254 // Otherwise, we use the native frame when we're told we should by the theme
255 // provider (e.g. no custom theme is active).
256 return !GetWidget()->GetThemeProvider()->ShouldUseNativeFrame();
259 bool BrowserDesktopWindowTreeHostWin::ShouldUseNativeFrame() const {
260 if (!views::DesktopWindowTreeHostWin::ShouldUseNativeFrame())
261 return false;
262 // This function can get called when the Browser window is closed i.e. in the
263 // context of the BrowserView destructor.
264 if (!browser_view_->browser())
265 return false;
266 return chrome::ShouldUseNativeFrame(browser_view_,
267 GetWidget()->GetThemeProvider());
270 void BrowserDesktopWindowTreeHostWin::FrameTypeChanged() {
271 views::DesktopWindowTreeHostWin::FrameTypeChanged();
272 did_gdi_clear_ = false;
275 ////////////////////////////////////////////////////////////////////////////////
276 // BrowserDesktopWindowTreeHostWin, private:
278 void BrowserDesktopWindowTreeHostWin::UpdateDWMFrame() {
279 // For "normal" windows on Aero, we always need to reset the glass area
280 // correctly, even if we're not currently showing the native frame (e.g.
281 // because a theme is showing), so we explicitly check for that case rather
282 // than checking browser_frame_->ShouldUseNativeFrame() here. Using that here
283 // would mean we wouldn't reset the glass area to zero when moving from the
284 // native frame to an opaque frame, leading to graphical glitches behind the
285 // opaque frame. Instead, we use that function below to tell us whether the
286 // frame is currently native or opaque.
287 if (!GetWidget()->client_view() || !browser_view_->IsBrowserTypeNormal() ||
288 !DesktopWindowTreeHostWin::ShouldUseNativeFrame())
289 return;
291 MARGINS margins = GetDWMFrameMargins();
293 DwmExtendFrameIntoClientArea(GetHWND(), &margins);
296 MARGINS BrowserDesktopWindowTreeHostWin::GetDWMFrameMargins() const {
297 MARGINS margins = { 0 };
299 // If the opaque frame is visible, we use the default (zero) margins.
300 // Otherwise, we need to figure out how to extend the glass in.
301 if (GetWidget()->ShouldUseNativeFrame()) {
302 // In fullscreen mode, we don't extend glass into the client area at all,
303 // because the GDI-drawn text in the web content composited over it will
304 // become semi-transparent over any glass area.
305 if (!IsMaximized() && !GetWidget()->IsFullscreen()) {
306 margins.cxLeftWidth = kClientEdgeThickness + 1;
307 margins.cxRightWidth = kClientEdgeThickness + 1;
308 margins.cyBottomHeight = kClientEdgeThickness + 1;
309 margins.cyTopHeight = kClientEdgeThickness + 1;
311 // In maximized mode, we only have a titlebar strip of glass, no side/bottom
312 // borders.
313 if (!browser_view_->IsFullscreen()) {
314 gfx::Rect tabstrip_bounds(
315 browser_frame_->GetBoundsForTabStrip(browser_view_->tabstrip()));
316 tabstrip_bounds = gfx::win::DIPToScreenRect(tabstrip_bounds);
317 margins.cyTopHeight = tabstrip_bounds.bottom() + kDWMFrameTopOffset;
320 return margins;
323 ////////////////////////////////////////////////////////////////////////////////
324 // BrowserDesktopWindowTreeHost, public:
326 // static
327 BrowserDesktopWindowTreeHost*
328 BrowserDesktopWindowTreeHost::CreateBrowserDesktopWindowTreeHost(
329 views::internal::NativeWidgetDelegate* native_widget_delegate,
330 views::DesktopNativeWidgetAura* desktop_native_widget_aura,
331 BrowserView* browser_view,
332 BrowserFrame* browser_frame) {
333 return new BrowserDesktopWindowTreeHostWin(native_widget_delegate,
334 desktop_native_widget_aura,
335 browser_view,
336 browser_frame);