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