OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media/window_icon_util.h" | 5 #include "chrome/browser/media/window_icon_util.h" |
6 | 6 |
7 #include <X11/Xatom.h> | 7 #include <X11/Xatom.h> |
8 #include <X11/Xutil.h> | 8 #include <X11/Xutil.h> |
9 | 9 |
10 #include "ui/gfx/x/x11_types.h" | 10 #include "ui/gfx/x/x11_types.h" |
11 | 11 |
12 // The error handler is for XGetWindowProperty. Otherwise, XGetWindowProperty | |
sky
2016/08/25 15:44:51
Are the changes in this file related to this patch
qiangchen
2016/08/25 17:07:59
Yes. As I tested in real case, sometimes crash. Th
sky
2016/08/25 19:44:17
Having to replace the error handler and ignore the
qiangchen
2016/08/25 20:46:58
The |id.id| in the call of XGetWindowProperty is a
qiangchen
2016/08/25 21:08:44
A little more investigation when I try to SetError
| |
13 // may throw an exception, and thus crash Chrome. A possibility is that when | |
14 // GetWindowIcon is called after the window is already closed. Anyway, failure | |
15 // to get an icon is not severe enough to worth blocking the normal workflow or | |
16 // crashing Chrome. | |
17 int ErrorHandler(Display* display, XErrorEvent* event) { | |
18 return 0; | |
19 } | |
20 | |
12 gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) { | 21 gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) { |
13 DCHECK(id.type == content::DesktopMediaID::TYPE_WINDOW); | 22 DCHECK(id.type == content::DesktopMediaID::TYPE_WINDOW); |
14 | 23 |
15 Display* display = gfx::GetXDisplay(); | 24 Display* display = gfx::GetXDisplay(); |
16 Atom property = XInternAtom(display, "_NET_WM_ICON", True); | 25 Atom property = XInternAtom(display, "_NET_WM_ICON", True); |
17 Atom actual_type; | 26 Atom actual_type; |
18 int actual_format; | 27 int actual_format; |
19 unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty | 28 unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty |
20 unsigned long size; | 29 unsigned long size; |
21 long* data; | 30 long* data; |
22 | 31 |
32 XErrorHandler old_handler = XSetErrorHandler(&ErrorHandler); | |
23 int status = XGetWindowProperty(display, id.id, property, 0L, ~0L, False, | 33 int status = XGetWindowProperty(display, id.id, property, 0L, ~0L, False, |
24 AnyPropertyType, &actual_type, &actual_format, | 34 AnyPropertyType, &actual_type, &actual_format, |
25 &size, &bytes_after, | 35 &size, &bytes_after, |
26 reinterpret_cast<unsigned char**>(&data)); | 36 reinterpret_cast<unsigned char**>(&data)); |
27 if (status != Success) { | 37 if (status != Success) { |
28 return gfx::ImageSkia(); | 38 return gfx::ImageSkia(); |
29 } | 39 } |
40 XSetErrorHandler(old_handler); | |
30 | 41 |
31 // The format of |data| is concatenation of sections like | 42 // The format of |data| is concatenation of sections like |
32 // [width, height, pixel data of size width * height], and the total bytes | 43 // [width, height, pixel data of size width * height], and the total bytes |
33 // number of |data| is |size|. And here we are picking the largest icon. | 44 // number of |data| is |size|. And here we are picking the largest icon. |
34 int width = 0; | 45 int width = 0; |
35 int height = 0; | 46 int height = 0; |
36 int start = 0; | 47 int start = 0; |
37 int i = 0; | 48 int i = 0; |
38 while (i + 1 < static_cast<int>(size)) { | 49 while (i + 1 < static_cast<int>(size)) { |
39 if ((i == 0 || static_cast<int>(data[i] * data[i + 1]) > width * height) && | 50 if ((i == 0 || static_cast<int>(data[i] * data[i + 1]) > width * height) && |
(...skipping 15 matching lines...) Expand all Loading... | |
55 for (long y = 0; y < height; ++y) { | 66 for (long y = 0; y < height; ++y) { |
56 for (long x = 0; x < width; ++x) { | 67 for (long x = 0; x < width; ++x) { |
57 pixels_data[result.rowBytesAsPixels() * y + x] = | 68 pixels_data[result.rowBytesAsPixels() * y + x] = |
58 static_cast<uint32_t>(data[start + width * y + x]); | 69 static_cast<uint32_t>(data[start + width * y + x]); |
59 } | 70 } |
60 } | 71 } |
61 | 72 |
62 XFree(data); | 73 XFree(data); |
63 return gfx::ImageSkia::CreateFrom1xBitmap(result); | 74 return gfx::ImageSkia::CreateFrom1xBitmap(result); |
64 } | 75 } |
OLD | NEW |