Chromium Code Reviews| Index: chrome/browser/media/window_icon_util_x11.cc |
| diff --git a/chrome/browser/media/window_icon_util_x11.cc b/chrome/browser/media/window_icon_util_x11.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a1c9942599a1bb5e202845a5e199a00cc534c4f1 |
| --- /dev/null |
| +++ b/chrome/browser/media/window_icon_util_x11.cc |
| @@ -0,0 +1,57 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media/window_icon_util.h" |
| + |
| +#include <X11/Xatom.h> |
| +#include <X11/Xutil.h> |
| + |
| +#include "ui/gfx/x/x11_types.h" |
| + |
| +gfx::ImageSkia WindowIconUtil::GetWindowIcon(content::DesktopMediaID id) { |
| + Display* display = gfx::GetXDisplay(); |
| + Atom property = XInternAtom(display, "_NET_WM_ICON", True); |
| + Atom actual_type; |
| + int actual_format; |
| + unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty |
| + unsigned long size; |
| + long* data; |
| + |
| + int status = XGetWindowProperty(display, id.id, property, 0L, |
| + ~0L, False, AnyPropertyType, &actual_type, |
| + &actual_format, &size, &bytes_after, |
| + reinterpret_cast<unsigned char**>(&data)); |
| + if (status != Success) { |
| + return gfx::ImageSkia(); |
| + } |
| + |
| + int width = 0; |
| + int height = 0; |
| + int start = 0; |
| + int i = 0; |
| + while (i < static_cast<int>(size)) { |
|
Sergey Ulanov
2016/08/18 05:38:19
Please add a comment above to explain what happens
qiangchen
2016/08/18 20:07:46
Done.
|
| + if (i == 0 || static_cast<int>(data[i] * data[i + 1]) > width * height) { |
|
Sergey Ulanov
2016/08/18 05:38:18
data[i+1] may be outside of the buffer size. Need
qiangchen
2016/08/18 20:07:46
Done.
|
| + width = static_cast<int>(data[i]); |
| + height = static_cast<int>(data[i + 1]); |
| + start = i + 2; |
|
Sergey Ulanov
2016/08/18 05:38:19
verify here that start + 2 + width*height < size
qiangchen
2016/08/18 20:07:46
Done.
|
| + } |
| + i = i + 2 + static_cast<int>(data[i] * data[i + 1]); |
| + } |
| + |
| + SkBitmap result; |
| + result.allocN32Pixels(width, height, false); |
| + result.lockPixels(); |
| + |
| + uint32_t* pixels_data = reinterpret_cast<uint32_t*>(result.getPixels()); |
| + |
| + for (long y = 0; y < height; ++y) { |
| + for (long x = 0; x < width; ++x) { |
| + pixels_data[result.rowBytesAsPixels() * y + x] = SkPreMultiplyColor( |
|
Sergey Ulanov
2016/08/18 05:38:19
nit: you can allocate SkBitmap of type kUnpremul_S
qiangchen
2016/08/18 20:07:46
Create SkBitmap with Unpremul_SkAlphaType.
But me
|
| + static_cast<uint32_t>(data[start + width * y + x])); |
| + } |
| + } |
| + |
| + XFree(data); |
| + return gfx::ImageSkia::CreateFrom1xBitmap(result); |
| +} |