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 "ui/snapshot/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" |
(...skipping 23 matching lines...) Expand all Loading... |
34 // Convert snapshot bounds relative to window into bounds relative to | 34 // Convert snapshot bounds relative to window into bounds relative to |
35 // screen. | 35 // screen. |
36 gfx::Rect screen_snapshot_bounds = snapshot_bounds; | 36 gfx::Rect screen_snapshot_bounds = snapshot_bounds; |
37 screen_snapshot_bounds.Offset(view_bounds.OffsetFromOrigin()); | 37 screen_snapshot_bounds.Offset(view_bounds.OffsetFromOrigin()); |
38 | 38 |
39 DCHECK_LE(screen_snapshot_bounds.right(), view_bounds.right()); | 39 DCHECK_LE(screen_snapshot_bounds.right(), view_bounds.right()); |
40 DCHECK_LE(screen_snapshot_bounds.bottom(), view_bounds.bottom()); | 40 DCHECK_LE(screen_snapshot_bounds.bottom(), view_bounds.bottom()); |
41 | 41 |
42 png_representation->clear(); | 42 png_representation->clear(); |
43 | 43 |
44 base::mac::ScopedCFTypeRef<CGImageRef> windowSnapshot(CGWindowListCreateImage( | 44 base::ScopedCFTypeRef<CGImageRef> windowSnapshot( |
45 screen_snapshot_bounds.ToCGRect(), kCGWindowListOptionIncludingWindow, | 45 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(), |
46 [window windowNumber], kCGWindowImageBoundsIgnoreFraming)); | 46 kCGWindowListOptionIncludingWindow, |
| 47 [window windowNumber], |
| 48 kCGWindowImageBoundsIgnoreFraming)); |
47 if (CGImageGetWidth(windowSnapshot) <= 0) | 49 if (CGImageGetWidth(windowSnapshot) <= 0) |
48 return false; | 50 return false; |
49 | 51 |
50 scoped_nsobject<NSBitmapImageRep> rep( | 52 scoped_nsobject<NSBitmapImageRep> rep( |
51 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); | 53 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); |
52 NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; | 54 NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; |
53 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); | 55 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); |
54 NSUInteger length = [data length]; | 56 NSUInteger length = [data length]; |
55 if (buf == NULL || length == 0) | 57 if (buf == NULL || length == 0) |
56 return false; | 58 return false; |
57 | 59 |
58 png_representation->assign(buf, buf + length); | 60 png_representation->assign(buf, buf + length); |
59 DCHECK(!png_representation->empty()); | 61 DCHECK(!png_representation->empty()); |
60 | 62 |
61 return true; | 63 return true; |
62 } | 64 } |
63 | 65 |
64 bool GrabWindowSnapshot(gfx::NativeWindow window, | 66 bool GrabWindowSnapshot(gfx::NativeWindow window, |
65 std::vector<unsigned char>* png_representation, | 67 std::vector<unsigned char>* png_representation, |
66 const gfx::Rect& snapshot_bounds) { | 68 const gfx::Rect& snapshot_bounds) { |
67 // Make sure to grab the "window frame" view so we get current tab + | 69 // Make sure to grab the "window frame" view so we get current tab + |
68 // tabstrip. | 70 // tabstrip. |
69 return GrabViewSnapshot([[window contentView] superview], png_representation, | 71 return GrabViewSnapshot([[window contentView] superview], png_representation, |
70 snapshot_bounds); | 72 snapshot_bounds); |
71 } | 73 } |
72 | 74 |
73 } // namespace ui | 75 } // namespace ui |
OLD | NEW |