OLD | NEW |
| (Empty) |
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/window_icon_util.h" | |
6 | |
7 #include <X11/Xatom.h> | |
8 #include <X11/Xutil.h> | |
9 | |
10 #include "ui/gfx/x/x11_error_tracker.h" | |
11 #include "ui/gfx/x/x11_types.h" | |
12 | |
13 gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) { | |
14 DCHECK(id.type == content::DesktopMediaID::TYPE_WINDOW); | |
15 | |
16 Display* display = gfx::GetXDisplay(); | |
17 Atom property = XInternAtom(display, "_NET_WM_ICON", True); | |
18 Atom actual_type; | |
19 int actual_format; | |
20 unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty | |
21 unsigned long size; | |
22 long* data; | |
23 | |
24 // The |error_tracker| essentially provides an empty X error handler for | |
25 // the call of XGetWindowProperty. The motivation is to guard against crash | |
26 // for any reason that XGetWindowProperty fails. For example, at the time that | |
27 // XGetWindowProperty is called, the window handler (a.k.a |id.id|) may | |
28 // already be invalid due to the fact that the end user has closed the | |
29 // corresponding window, etc. | |
30 std::unique_ptr<gfx::X11ErrorTracker> error_tracker( | |
31 new gfx::X11ErrorTracker()); | |
32 int status = XGetWindowProperty(display, id.id, property, 0L, ~0L, False, | |
33 AnyPropertyType, &actual_type, &actual_format, | |
34 &size, &bytes_after, | |
35 reinterpret_cast<unsigned char**>(&data)); | |
36 error_tracker.reset(); | |
37 | |
38 if (status != Success) { | |
39 return gfx::ImageSkia(); | |
40 } | |
41 | |
42 // The format of |data| is concatenation of sections like | |
43 // [width, height, pixel data of size width * height], and the total bytes | |
44 // number of |data| is |size|. And here we are picking the largest icon. | |
45 int width = 0; | |
46 int height = 0; | |
47 int start = 0; | |
48 int i = 0; | |
49 while (i + 1 < static_cast<int>(size)) { | |
50 if ((i == 0 || static_cast<int>(data[i] * data[i + 1]) > width * height) && | |
51 (i + 1 + data[i] * data[i + 1] < static_cast<int>(size))) { | |
52 width = static_cast<int>(data[i]); | |
53 height = static_cast<int>(data[i + 1]); | |
54 start = i + 2; | |
55 } | |
56 i = i + 2 + static_cast<int>(data[i] * data[i + 1]); | |
57 } | |
58 | |
59 SkBitmap result; | |
60 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType); | |
61 result.allocPixels(info); | |
62 result.lockPixels(); | |
63 | |
64 uint32_t* pixels_data = reinterpret_cast<uint32_t*>(result.getPixels()); | |
65 | |
66 for (long y = 0; y < height; ++y) { | |
67 for (long x = 0; x < width; ++x) { | |
68 pixels_data[result.rowBytesAsPixels() * y + x] = | |
69 static_cast<uint32_t>(data[start + width * y + x]); | |
70 } | |
71 } | |
72 | |
73 XFree(data); | |
74 return gfx::ImageSkia::CreateFrom1xBitmap(result); | |
75 } | |
OLD | NEW |