blob: ab790872e0db1c88dd5602bf31d0f384e2623306 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
// Copyright (c) 2011 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/ui/window_snapshot/window_snapshot.h"
#import <Cocoa/Cocoa.h>
#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_nsobject.h"
#include "ui/gfx/rect.h"
namespace browser {
gfx::Rect GrabWindowSnapshot(gfx::NativeWindow window,
std::vector<unsigned char>* png_representation) {
png_representation->clear();
// Make sure to grab the "window frame" view so we get current tab +
// tabstrip.
NSView* view = [[window contentView] superview];
base::mac::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage(
CGRectNull, kCGWindowListOptionIncludingWindow,
[[view window] windowNumber], kCGWindowImageBoundsIgnoreFraming));
if (CGImageGetWidth(windowSnapshot) <= 0)
return gfx::Rect();
scoped_nsobject<NSBitmapImageRep> rep(
[[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]);
NSData* data = [rep representationUsingType:NSPNGFileType properties:nil];
const unsigned char* buf = static_cast<const unsigned char*>([data bytes]);
NSUInteger length = [data length];
if (buf == NULL || length == 0)
return gfx::Rect();
png_representation->assign(buf, buf + length);
DCHECK(!png_representation->empty());
return gfx::Rect(static_cast<int>([rep pixelsWide]),
static_cast<int>([rep pixelsHigh]));
}
} // namespace browser
|