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