1 // Copyright 2014 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/elevation_icon_setter.h"
7 #include "base/task_runner_util.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "ui/views/controls/button/label_button.h"
14 #include "base/win/win_util.h"
15 #include "base/win/windows_version.h"
16 #include "ui/gfx/icon_util.h"
17 #include "ui/gfx/win/dpi.h"
21 // Helpers --------------------------------------------------------------------
25 scoped_ptr
<SkBitmap
> GetElevationIcon() {
26 scoped_ptr
<SkBitmap
> icon
;
28 if ((base::win::GetVersion() < base::win::VERSION_VISTA
) ||
29 !base::win::UserAccountControlIsEnabled())
32 SHSTOCKICONINFO icon_info
= { sizeof(SHSTOCKICONINFO
) };
33 typedef HRESULT (STDAPICALLTYPE
*GetStockIconInfo
)(SHSTOCKICONID
,
36 // Even with the runtime guard above, we have to use GetProcAddress()
37 // here, because otherwise the loader will try to resolve the function
38 // address on startup, which will break on XP.
39 GetStockIconInfo func
= reinterpret_cast<GetStockIconInfo
>(
40 GetProcAddress(GetModuleHandle(L
"shell32.dll"), "SHGetStockIconInfo"));
41 // TODO(pkasting): Run on a background thread since this call spins a nested
43 if (FAILED((*func
)(SIID_SHIELD
, SHGSI_ICON
| SHGSI_SMALLICON
, &icon_info
)))
46 icon
.reset(IconUtil::CreateSkBitmapFromHICON(
48 gfx::Size(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
))));
49 DestroyIcon(icon_info
.hIcon
);
57 // ElevationIconSetter --------------------------------------------------------
59 ElevationIconSetter::ElevationIconSetter(views::LabelButton
* button
)
62 base::PostTaskAndReplyWithResult(
63 content::BrowserThread::GetBlockingPool(),
65 base::Bind(&GetElevationIcon
),
66 base::Bind(&ElevationIconSetter::SetButtonIcon
,
67 weak_factory_
.GetWeakPtr()));
70 ElevationIconSetter::~ElevationIconSetter() {
73 void ElevationIconSetter::SetButtonIcon(scoped_ptr
<SkBitmap
> icon
) {
75 float device_scale_factor
= 1.0f
;
77 // Windows gives us back a correctly-scaled image for the current DPI, so
78 // mark this image as having been scaled for the current DPI already.
79 device_scale_factor
= gfx::GetDPIScale();
82 views::Button::STATE_NORMAL
,
83 gfx::ImageSkia(gfx::ImageSkiaRep(*icon
, device_scale_factor
)));
84 button_
->SizeToPreferredSize();
85 if (button_
->parent())
86 button_
->parent()->Layout();