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"
20 // Helpers --------------------------------------------------------------------
24 scoped_ptr
<SkBitmap
> GetElevationIcon() {
25 scoped_ptr
<SkBitmap
> icon
;
27 if ((base::win::GetVersion() < base::win::VERSION_VISTA
) ||
28 !base::win::UserAccountControlIsEnabled())
31 SHSTOCKICONINFO icon_info
= { sizeof(SHSTOCKICONINFO
) };
32 typedef HRESULT (STDAPICALLTYPE
*GetStockIconInfo
)(SHSTOCKICONID
,
35 // Even with the runtime guard above, we have to use GetProcAddress()
36 // here, because otherwise the loader will try to resolve the function
37 // address on startup, which will break on XP.
38 GetStockIconInfo func
= reinterpret_cast<GetStockIconInfo
>(
39 GetProcAddress(GetModuleHandle(L
"shell32.dll"), "SHGetStockIconInfo"));
40 // TODO(pkasting): Run on a background thread since this call spins a nested
42 if (FAILED((*func
)(SIID_SHIELD
, SHGSI_ICON
| SHGSI_SMALLICON
, &icon_info
)))
45 icon
.reset(IconUtil::CreateSkBitmapFromHICON(
47 gfx::Size(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
))));
48 DestroyIcon(icon_info
.hIcon
);
56 // ElevationIconSetter --------------------------------------------------------
58 ElevationIconSetter::ElevationIconSetter(views::LabelButton
* button
)
61 base::PostTaskAndReplyWithResult(
62 content::BrowserThread::GetBlockingPool(),
64 base::Bind(&GetElevationIcon
),
65 base::Bind(&ElevationIconSetter::SetButtonIcon
,
66 weak_factory_
.GetWeakPtr()));
69 ElevationIconSetter::~ElevationIconSetter() {
72 void ElevationIconSetter::SetButtonIcon(scoped_ptr
<SkBitmap
> icon
) {
74 button_
->SetImage(views::Button::STATE_NORMAL
,
75 gfx::ImageSkia::CreateFrom1xBitmap(*icon
));
76 button_
->SizeToPreferredSize();
77 if (button_
->parent())
78 button_
->parent()->Layout();