Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: ui/snapshot/snapshot_mac.mm

Issue 2592983002: [devtools] Support different encodings for Page.CaptureScreenshot. (Closed)
Patch Set: Wait for load in CaptureScreenshotTest to fix android bot. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/callback.h" 9 #include "base/callback.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/mac/scoped_cftyperef.h" 11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/mac/sdk_forward_declarations.h" 12 #include "base/mac/sdk_forward_declarations.h"
14 #include "base/task_runner.h" 13 #include "base/task_runner.h"
15 #include "ui/gfx/geometry/rect.h" 14 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/image/image.h" 15 #include "ui/gfx/image/image.h"
17 16
18 namespace ui { 17 namespace ui {
19 18
20 bool GrabViewSnapshot(gfx::NativeView view, 19 bool GrabViewSnapshot(gfx::NativeView view,
21 std::vector<unsigned char>* png_representation, 20 gfx::Image* image,
22 const gfx::Rect& snapshot_bounds) { 21 const gfx::Rect& snapshot_bounds) {
23 NSWindow* window = [view window]; 22 NSWindow* window = [view window];
24 NSScreen* screen = [[NSScreen screens] firstObject]; 23 NSScreen* screen = [[NSScreen screens] firstObject];
25 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame])); 24 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame]));
26 25
27 26
28 // Get the view bounds relative to the screen 27 // Get the view bounds relative to the screen
29 NSRect frame = [view convertRect:[view bounds] toView:nil]; 28 NSRect frame = [view convertRect:[view bounds] toView:nil];
30 frame = [window convertRectToScreen:frame]; 29 frame = [window convertRectToScreen:frame];
31 30
32 gfx::Rect view_bounds = gfx::Rect(NSRectToCGRect(frame)); 31 gfx::Rect view_bounds = gfx::Rect(NSRectToCGRect(frame));
33 32
34 // Flip window coordinates based on the primary screen. 33 // Flip window coordinates based on the primary screen.
35 view_bounds.set_y( 34 view_bounds.set_y(
36 screen_bounds.height() - view_bounds.y() - view_bounds.height()); 35 screen_bounds.height() - view_bounds.y() - view_bounds.height());
37 36
38 // Convert snapshot bounds relative to window into bounds relative to 37 // Convert snapshot bounds relative to window into bounds relative to
39 // screen. 38 // screen.
40 gfx::Rect screen_snapshot_bounds = snapshot_bounds; 39 gfx::Rect screen_snapshot_bounds = snapshot_bounds;
41 screen_snapshot_bounds.Offset(view_bounds.OffsetFromOrigin()); 40 screen_snapshot_bounds.Offset(view_bounds.OffsetFromOrigin());
42 41
43 DCHECK_LE(screen_snapshot_bounds.right(), view_bounds.right()); 42 DCHECK_LE(screen_snapshot_bounds.right(), view_bounds.right());
44 DCHECK_LE(screen_snapshot_bounds.bottom(), view_bounds.bottom()); 43 DCHECK_LE(screen_snapshot_bounds.bottom(), view_bounds.bottom());
45 44
46 png_representation->clear();
47
48 base::ScopedCFTypeRef<CGImageRef> windowSnapshot( 45 base::ScopedCFTypeRef<CGImageRef> windowSnapshot(
49 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(), 46 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(),
50 kCGWindowListOptionIncludingWindow, 47 kCGWindowListOptionIncludingWindow,
51 [window windowNumber], 48 [window windowNumber],
52 kCGWindowImageBoundsIgnoreFraming)); 49 kCGWindowImageBoundsIgnoreFraming));
53 if (CGImageGetWidth(windowSnapshot) <= 0) 50 if (CGImageGetWidth(windowSnapshot) <= 0)
54 return false; 51 return false;
55 52
56 base::scoped_nsobject<NSBitmapImageRep> rep( 53 NSImage* nsImage =
57 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); 54 [[NSImage alloc] initWithCGImage:windowSnapshot size:NSZeroSize];
58 NSData* data = [rep representationUsingType:NSPNGFileType properties:@{}]; 55 *image = gfx::Image(nsImage);
59 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]);
60 NSUInteger length = [data length];
61 if (buf == NULL || length == 0)
62 return false;
63
64 png_representation->assign(buf, buf + length);
65 DCHECK(!png_representation->empty());
66
67 return true; 56 return true;
68 } 57 }
69 58
70 bool GrabWindowSnapshot(gfx::NativeWindow window, 59 bool GrabWindowSnapshot(gfx::NativeWindow window,
71 std::vector<unsigned char>* png_representation, 60 gfx::Image* image,
72 const gfx::Rect& snapshot_bounds) { 61 const gfx::Rect& snapshot_bounds) {
73 // Make sure to grab the "window frame" view so we get current tab + 62 // Make sure to grab the "window frame" view so we get current tab +
74 // tabstrip. 63 // tabstrip.
75 return GrabViewSnapshot([[window contentView] superview], png_representation, 64 return GrabViewSnapshot([[window contentView] superview], image,
76 snapshot_bounds); 65 snapshot_bounds);
77 } 66 }
78 67
79 void GrabWindowSnapshotAndScaleAsync( 68 void GrabWindowSnapshotAndScaleAsync(
80 gfx::NativeWindow window, 69 gfx::NativeWindow window,
81 const gfx::Rect& snapshot_bounds, 70 const gfx::Rect& snapshot_bounds,
82 const gfx::Size& target_size, 71 const gfx::Size& target_size,
83 scoped_refptr<base::TaskRunner> background_task_runner, 72 scoped_refptr<base::TaskRunner> background_task_runner,
84 GrabWindowSnapshotAsyncCallback callback) { 73 GrabWindowSnapshotAsyncCallback callback) {
85 callback.Run(gfx::Image()); 74 callback.Run(gfx::Image());
86 } 75 }
87 76
88 void GrabViewSnapshotAsync( 77 void GrabViewSnapshotAsync(gfx::NativeView view,
89 gfx::NativeView view, 78 const gfx::Rect& source_rect,
90 const gfx::Rect& source_rect, 79 const GrabWindowSnapshotAsyncCallback& callback) {
91 scoped_refptr<base::TaskRunner> background_task_runner, 80 callback.Run(gfx::Image());
92 const GrabWindowSnapshotAsyncPNGCallback& callback) {
93 callback.Run(scoped_refptr<base::RefCountedBytes>());
94 } 81 }
95 82
96 void GrabWindowSnapshotAsync( 83 void GrabWindowSnapshotAsync(gfx::NativeWindow window,
97 gfx::NativeWindow window, 84 const gfx::Rect& source_rect,
98 const gfx::Rect& source_rect, 85 const GrabWindowSnapshotAsyncCallback& callback) {
99 scoped_refptr<base::TaskRunner> background_task_runner,
100 const GrabWindowSnapshotAsyncPNGCallback& callback) {
101 return GrabViewSnapshotAsync([[window contentView] superview], source_rect, 86 return GrabViewSnapshotAsync([[window contentView] superview], source_rect,
102 background_task_runner, callback); 87 callback);
103 } 88 }
104 89
105 } // namespace ui 90 } // namespace ui
OLDNEW
« ui/snapshot/snapshot_aura_unittest.cc ('K') | « ui/snapshot/snapshot_ios.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698