OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/ui/window_snapshot/window_snapshot.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/mac/scoped_cftyperef.h" | |
11 #include "base/memory/scoped_nsobject.h" | |
12 #include "ui/gfx/rect.h" | |
13 | |
14 namespace chrome { | |
15 namespace internal { | |
16 | |
17 bool GrabWindowSnapshot(gfx::NativeWindow window, | |
18 std::vector<unsigned char>* png_representation, | |
19 const gfx::Rect& snapshot_bounds) { | |
20 NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; | |
21 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame])); | |
22 gfx::Rect window_bounds = gfx::Rect(NSRectToCGRect([window frame])); | |
23 | |
24 // Flip window coordinates based on the primary screen. | |
25 window_bounds.set_y( | |
26 screen_bounds.height() - window_bounds.y() - window_bounds.height()); | |
27 | |
28 // Convert snapshot bounds relative to window into bounds relative to | |
29 // screen. | |
30 gfx::Rect screen_snapshot_bounds = snapshot_bounds; | |
31 screen_snapshot_bounds.Offset(window_bounds.OffsetFromOrigin()); | |
32 | |
33 DCHECK_LE(screen_snapshot_bounds.right(), window_bounds.right()); | |
34 DCHECK_LE(screen_snapshot_bounds.bottom(), window_bounds.bottom()); | |
35 | |
36 png_representation->clear(); | |
37 | |
38 // Make sure to grab the "window frame" view so we get current tab + | |
39 // tabstrip. | |
40 NSView* view = [[window contentView] superview]; | |
41 base::mac::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage( | |
42 screen_snapshot_bounds.ToCGRect(), kCGWindowListOptionIncludingWindow, | |
43 [[view window] windowNumber], kCGWindowImageBoundsIgnoreFraming)); | |
44 if (CGImageGetWidth(windowSnapshot) <= 0) | |
45 return false; | |
46 | |
47 scoped_nsobject<NSBitmapImageRep> rep( | |
48 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); | |
49 NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; | |
50 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); | |
51 NSUInteger length = [data length]; | |
52 if (buf == NULL || length == 0) | |
53 return false; | |
54 | |
55 png_representation->assign(buf, buf + length); | |
56 DCHECK(!png_representation->empty()); | |
57 | |
58 return true; | |
59 } | |
60 | |
61 } // namespace internal | |
62 } // namespace chrome | |
OLD | NEW |