1 // Copyright 2016 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.
4 
5 #include "chrome/browser/media/webrtc/window_icon_util.h"
6 
7 #include "ui/gfx/icon_util.h"
8 
GetWindowIcon(content::DesktopMediaID id)9 gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) {
10   DCHECK(id.type == content::DesktopMediaID::TYPE_WINDOW);
11 
12   HWND hwnd = reinterpret_cast<HWND>(id.id);
13   HICON icon_handle = 0;
14 
15   SendMessageTimeout(hwnd, WM_GETICON, ICON_BIG, 0, SMTO_ABORTIFHUNG, 5,
16                      (PDWORD_PTR)&icon_handle);
17   if (!icon_handle)
18     icon_handle = reinterpret_cast<HICON>(GetClassLongPtr(hwnd, GCLP_HICON));
19 
20   if (!icon_handle) {
21     SendMessageTimeout(hwnd, WM_GETICON, ICON_SMALL, 0, SMTO_ABORTIFHUNG, 5,
22                        (PDWORD_PTR)&icon_handle);
23   }
24   if (!icon_handle) {
25     SendMessageTimeout(hwnd, WM_GETICON, ICON_SMALL2, 0, SMTO_ABORTIFHUNG, 5,
26                        (PDWORD_PTR)&icon_handle);
27   }
28   if (!icon_handle)
29     icon_handle = reinterpret_cast<HICON>(GetClassLongPtr(hwnd, GCLP_HICONSM));
30 
31   if (!icon_handle)
32     return gfx::ImageSkia();
33 
34   const SkBitmap icon_bitmap = IconUtil::CreateSkBitmapFromHICON(icon_handle);
35 
36   if (icon_bitmap.isNull())
37     return gfx::ImageSkia();
38 
39   return gfx::ImageSkia::CreateFrom1xBitmap(icon_bitmap);
40 }
41