1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/frame/browser_desktop_window_tree_host_win.h"
#include <dwmapi.h>
#include "base/process/process_handle.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/views/frame/browser_frame.h"
#include "chrome/browser/ui/views/frame/browser_frame_common_win.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/browser_window_property_manager_win.h"
#include "chrome/browser/ui/views/frame/system_menu_insertion_delegate_win.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/theme_image_mapper.h"
#include "chrome/common/chrome_constants.h"
#include "components/browser_watcher/exit_funnel_win.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/win/dpi.h"
#include "ui/views/controls/menu/native_menu_win.h"
#pragma comment(lib, "dwmapi.lib")
namespace {
const int kClientEdgeThickness = 3;
// We need to offset the DWMFrame into the toolbar so that the blackness
// doesn't show up on our rounded corners.
const int kDWMFrameTopOffset = 3;
// DesktopThemeProvider maps resource ids using MapThemeImage(). This is
// necessary for BrowserDesktopWindowTreeHostWin so that it uses the windows
// theme images rather than the ash theme images.
class DesktopThemeProvider : public ui::ThemeProvider {
public:
explicit DesktopThemeProvider(ui::ThemeProvider* delegate)
: delegate_(delegate) {
}
virtual bool UsingSystemTheme() const override {
return delegate_->UsingSystemTheme();
}
virtual gfx::ImageSkia* GetImageSkiaNamed(int id) const override {
return delegate_->GetImageSkiaNamed(
chrome::MapThemeImage(chrome::HOST_DESKTOP_TYPE_NATIVE, id));
}
virtual SkColor GetColor(int id) const override {
return delegate_->GetColor(id);
}
virtual int GetDisplayProperty(int id) const override {
return delegate_->GetDisplayProperty(id);
}
virtual bool ShouldUseNativeFrame() const override {
return delegate_->ShouldUseNativeFrame();
}
virtual bool HasCustomImage(int id) const override {
return delegate_->HasCustomImage(
chrome::MapThemeImage(chrome::HOST_DESKTOP_TYPE_NATIVE, id));
}
virtual base::RefCountedMemory* GetRawData(
int id,
ui::ScaleFactor scale_factor) const override {
return delegate_->GetRawData(id, scale_factor);
}
private:
ui::ThemeProvider* delegate_;
DISALLOW_COPY_AND_ASSIGN(DesktopThemeProvider);
};
// See http://crbug.com/412384.
void TraceSessionEnding(LPARAM lparam) {
browser_watcher::ExitFunnel funnel;
if (!funnel.Init(chrome::kBrowserExitCodesRegistryPath,
base::GetCurrentProcessHandle())) {
return;
}
// This exit path is the prime suspect for most our unclean shutdowns.
// Trace all the possible options to WM_ENDSESSION. This may result in
// multiple events for a single shutdown, but that's fine.
funnel.RecordEvent(L"WM_ENDSESSION");
if (lparam & ENDSESSION_CLOSEAPP)
funnel.RecordEvent(L"ES_CloseApp");
if (lparam & ENDSESSION_CRITICAL)
funnel.RecordEvent(L"ES_Critical");
if (lparam & ENDSESSION_LOGOFF)
funnel.RecordEvent(L"ES_Logoff");
const LPARAM kKnownBits =
ENDSESSION_CLOSEAPP | ENDSESSION_CRITICAL | ENDSESSION_LOGOFF;
if (lparam & ~kKnownBits)
funnel.RecordEvent(L"ES_Other");
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, public:
BrowserDesktopWindowTreeHostWin::BrowserDesktopWindowTreeHostWin(
views::internal::NativeWidgetDelegate* native_widget_delegate,
views::DesktopNativeWidgetAura* desktop_native_widget_aura,
BrowserView* browser_view,
BrowserFrame* browser_frame)
: DesktopWindowTreeHostWin(native_widget_delegate,
desktop_native_widget_aura),
browser_view_(browser_view),
browser_frame_(browser_frame),
did_gdi_clear_(false) {
scoped_ptr<ui::ThemeProvider> theme_provider(
new DesktopThemeProvider(ThemeServiceFactory::GetForProfile(
browser_view->browser()->profile())));
browser_frame->SetThemeProvider(theme_provider.Pass());
}
BrowserDesktopWindowTreeHostWin::~BrowserDesktopWindowTreeHostWin() {
}
views::NativeMenuWin* BrowserDesktopWindowTreeHostWin::GetSystemMenu() {
if (!system_menu_.get()) {
SystemMenuInsertionDelegateWin insertion_delegate;
system_menu_.reset(
new views::NativeMenuWin(browser_frame_->GetSystemMenuModel(),
GetHWND()));
system_menu_->Rebuild(&insertion_delegate);
}
return system_menu_.get();
}
////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, BrowserDesktopWindowTreeHost implementation:
views::DesktopWindowTreeHost*
BrowserDesktopWindowTreeHostWin::AsDesktopWindowTreeHost() {
return this;
}
int BrowserDesktopWindowTreeHostWin::GetMinimizeButtonOffset() const {
return minimize_button_metrics_.GetMinimizeButtonOffsetX();
}
bool BrowserDesktopWindowTreeHostWin::UsesNativeSystemMenu() const {
return true;
}
////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, views::DesktopWindowTreeHostWin overrides:
int BrowserDesktopWindowTreeHostWin::GetInitialShowState() const {
STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
GetStartupInfo(&si);
return si.wShowWindow;
}
bool BrowserDesktopWindowTreeHostWin::GetClientAreaInsets(
gfx::Insets* insets) const {
// Use the default client insets for an opaque frame or a glass popup/app
// frame.
if (!GetWidget()->ShouldUseNativeFrame() ||
!browser_view_->IsBrowserTypeNormal()) {
return false;
}
int border_thickness = GetSystemMetrics(SM_CXSIZEFRAME);
// In fullscreen mode, we have no frame. In restored mode, we draw our own
// client edge over part of the default frame.
if (GetWidget()->IsFullscreen())
border_thickness = 0;
else if (!IsMaximized())
border_thickness -= kClientEdgeThickness;
insets->Set(0, border_thickness, border_thickness, border_thickness);
return true;
}
void BrowserDesktopWindowTreeHostWin::HandleCreate() {
DesktopWindowTreeHostWin::HandleCreate();
browser_window_property_manager_ =
BrowserWindowPropertyManager::CreateBrowserWindowPropertyManager(
browser_view_);
if (browser_window_property_manager_)
browser_window_property_manager_->UpdateWindowProperties(GetHWND());
}
void BrowserDesktopWindowTreeHostWin::HandleFrameChanged() {
// Reinitialize the status bubble, since it needs to be initialized
// differently depending on whether or not DWM composition is enabled
browser_view_->InitStatusBubble();
// We need to update the glass region on or off before the base class adjusts
// the window region.
UpdateDWMFrame();
DesktopWindowTreeHostWin::HandleFrameChanged();
}
bool BrowserDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
WPARAM w_param,
LPARAM l_param,
LRESULT* result) {
switch (message) {
case WM_ACTIVATE:
if (LOWORD(w_param) != WA_INACTIVE)
minimize_button_metrics_.OnHWNDActivated();
return false;
case WM_ENDSESSION:
TraceSessionEnding(l_param);
chrome::SessionEnding();
return true;
case WM_INITMENUPOPUP:
GetSystemMenu()->UpdateStates();
return true;
}
return DesktopWindowTreeHostWin::PreHandleMSG(
message, w_param, l_param, result);
}
void BrowserDesktopWindowTreeHostWin::PostHandleMSG(UINT message,
WPARAM w_param,
LPARAM l_param) {
switch (message) {
case WM_CREATE:
minimize_button_metrics_.Init(GetHWND());
break;
case WM_WINDOWPOSCHANGED: {
UpdateDWMFrame();
// Windows lies to us about the position of the minimize button before a
// window is visible. We use this position to place the OTR avatar in RTL
// mode, so when the window is shown, we need to re-layout and schedule a
// paint for the non-client frame view so that the icon top has the correct
// position when the window becomes visible. This fixes bugs where the icon
// appears to overlay the minimize button.
// Note that we will call Layout every time SetWindowPos is called with
// SWP_SHOWWINDOW, however callers typically are careful about not
// specifying this flag unless necessary to avoid flicker.
// This may be invoked during creation on XP and before the non_client_view
// has been created.
WINDOWPOS* window_pos = reinterpret_cast<WINDOWPOS*>(l_param);
if (window_pos->flags & SWP_SHOWWINDOW && GetWidget()->non_client_view()) {
GetWidget()->non_client_view()->Layout();
GetWidget()->non_client_view()->SchedulePaint();
}
break;
}
case WM_ERASEBKGND:
if (!did_gdi_clear_ && DesktopWindowTreeHostWin::ShouldUseNativeFrame()) {
// This is necessary to avoid white flashing in the titlebar area around
// the minimize/maximize/close buttons.
HDC dc = GetDC(GetHWND());
MARGINS margins = GetDWMFrameMargins();
RECT client_rect;
GetClientRect(GetHWND(), &client_rect);
HBRUSH brush = CreateSolidBrush(0);
RECT rect = { 0, 0, client_rect.right, margins.cyTopHeight };
FillRect(dc, &rect, brush);
DeleteObject(brush);
ReleaseDC(GetHWND(), dc);
did_gdi_clear_ = true;
}
break;
}
}
bool BrowserDesktopWindowTreeHostWin::IsUsingCustomFrame() const {
// We don't theme popup or app windows, so regardless of whether or not a
// theme is active for normal browser windows, we don't want to use the custom
// frame for popups/apps.
if (!browser_view_->IsBrowserTypeNormal() &&
!DesktopWindowTreeHostWin::IsUsingCustomFrame()) {
return false;
}
// Otherwise, we use the native frame when we're told we should by the theme
// provider (e.g. no custom theme is active).
return !GetWidget()->GetThemeProvider()->ShouldUseNativeFrame();
}
bool BrowserDesktopWindowTreeHostWin::ShouldUseNativeFrame() const {
if (!views::DesktopWindowTreeHostWin::ShouldUseNativeFrame())
return false;
// This function can get called when the Browser window is closed i.e. in the
// context of the BrowserView destructor.
if (!browser_view_->browser())
return false;
return chrome::ShouldUseNativeFrame(browser_view_,
GetWidget()->GetThemeProvider());
}
void BrowserDesktopWindowTreeHostWin::FrameTypeChanged() {
views::DesktopWindowTreeHostWin::FrameTypeChanged();
did_gdi_clear_ = false;
}
////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHostWin, private:
void BrowserDesktopWindowTreeHostWin::UpdateDWMFrame() {
// For "normal" windows on Aero, we always need to reset the glass area
// correctly, even if we're not currently showing the native frame (e.g.
// because a theme is showing), so we explicitly check for that case rather
// than checking browser_frame_->ShouldUseNativeFrame() here. Using that here
// would mean we wouldn't reset the glass area to zero when moving from the
// native frame to an opaque frame, leading to graphical glitches behind the
// opaque frame. Instead, we use that function below to tell us whether the
// frame is currently native or opaque.
if (!GetWidget()->client_view() || !browser_view_->IsBrowserTypeNormal() ||
!DesktopWindowTreeHostWin::ShouldUseNativeFrame())
return;
MARGINS margins = GetDWMFrameMargins();
DwmExtendFrameIntoClientArea(GetHWND(), &margins);
}
MARGINS BrowserDesktopWindowTreeHostWin::GetDWMFrameMargins() const {
MARGINS margins = { 0 };
// If the opaque frame is visible, we use the default (zero) margins.
// Otherwise, we need to figure out how to extend the glass in.
if (GetWidget()->ShouldUseNativeFrame()) {
// In fullscreen mode, we don't extend glass into the client area at all,
// because the GDI-drawn text in the web content composited over it will
// become semi-transparent over any glass area.
if (!IsMaximized() && !GetWidget()->IsFullscreen()) {
margins.cxLeftWidth = kClientEdgeThickness + 1;
margins.cxRightWidth = kClientEdgeThickness + 1;
margins.cyBottomHeight = kClientEdgeThickness + 1;
margins.cyTopHeight = kClientEdgeThickness + 1;
}
// In maximized mode, we only have a titlebar strip of glass, no side/bottom
// borders.
if (!browser_view_->IsFullscreen()) {
gfx::Rect tabstrip_bounds(
browser_frame_->GetBoundsForTabStrip(browser_view_->tabstrip()));
tabstrip_bounds = gfx::win::DIPToScreenRect(tabstrip_bounds);
margins.cyTopHeight = tabstrip_bounds.bottom() + kDWMFrameTopOffset;
}
}
return margins;
}
////////////////////////////////////////////////////////////////////////////////
// BrowserDesktopWindowTreeHost, public:
// static
BrowserDesktopWindowTreeHost*
BrowserDesktopWindowTreeHost::CreateBrowserDesktopWindowTreeHost(
views::internal::NativeWidgetDelegate* native_widget_delegate,
views::DesktopNativeWidgetAura* desktop_native_widget_aura,
BrowserView* browser_view,
BrowserFrame* browser_frame) {
return new BrowserDesktopWindowTreeHostWin(native_widget_delegate,
desktop_native_widget_aura,
browser_view,
browser_frame);
}
|