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" |
11 #include "base/memory/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
13 | 13 |
14 namespace ui { | 14 namespace ui { |
15 | 15 |
16 bool GrabViewSnapshot(gfx::NativeView view, | 16 bool GrabViewSnapshot(gfx::NativeView view, |
17 std::vector<unsigned char>* png_representation, | 17 std::vector<unsigned char>* png_representation, |
18 const gfx::Rect& snapshot_bounds) { | 18 const gfx::Rect& snapshot_bounds) { |
19 NSWindow* window = [view window]; | 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])); |
(...skipping 20 matching lines...) Expand all Loading... |
42 png_representation->clear(); | 42 png_representation->clear(); |
43 | 43 |
44 base::ScopedCFTypeRef<CGImageRef> windowSnapshot( | 44 base::ScopedCFTypeRef<CGImageRef> windowSnapshot( |
45 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(), | 45 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(), |
46 kCGWindowListOptionIncludingWindow, | 46 kCGWindowListOptionIncludingWindow, |
47 [window windowNumber], | 47 [window windowNumber], |
48 kCGWindowImageBoundsIgnoreFraming)); | 48 kCGWindowImageBoundsIgnoreFraming)); |
49 if (CGImageGetWidth(windowSnapshot) <= 0) | 49 if (CGImageGetWidth(windowSnapshot) <= 0) |
50 return false; | 50 return false; |
51 | 51 |
52 scoped_nsobject<NSBitmapImageRep> rep( | 52 base::scoped_nsobject<NSBitmapImageRep> rep( |
53 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); | 53 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); |
54 NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; | 54 NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; |
55 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); | 55 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); |
56 NSUInteger length = [data length]; | 56 NSUInteger length = [data length]; |
57 if (buf == NULL || length == 0) | 57 if (buf == NULL || length == 0) |
58 return false; | 58 return false; |
59 | 59 |
60 png_representation->assign(buf, buf + length); | 60 png_representation->assign(buf, buf + length); |
61 DCHECK(!png_representation->empty()); | 61 DCHECK(!png_representation->empty()); |
62 | 62 |
63 return true; | 63 return true; |
64 } | 64 } |
65 | 65 |
66 bool GrabWindowSnapshot(gfx::NativeWindow window, | 66 bool GrabWindowSnapshot(gfx::NativeWindow window, |
67 std::vector<unsigned char>* png_representation, | 67 std::vector<unsigned char>* png_representation, |
68 const gfx::Rect& snapshot_bounds) { | 68 const gfx::Rect& snapshot_bounds) { |
69 // 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 + |
70 // tabstrip. | 70 // tabstrip. |
71 return GrabViewSnapshot([[window contentView] superview], png_representation, | 71 return GrabViewSnapshot([[window contentView] superview], png_representation, |
72 snapshot_bounds); | 72 snapshot_bounds); |
73 } | 73 } |
74 | 74 |
75 } // namespace ui | 75 } // namespace ui |
OLD | NEW |