diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-05 23:27:11 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-05 23:27:11 +0000 |
commit | e5ffa33eb3ee71b9b44dc11ee80910b7b852b427 (patch) | |
tree | 84cb0771fb2a95d58183927b2f5db1941c74e82e /ui | |
parent | 090f7313473bb61b6b294f1bfbb3b05f9140dec3 (diff) | |
download | chromium_src-e5ffa33eb3ee71b9b44dc11ee80910b7b852b427.zip chromium_src-e5ffa33eb3ee71b9b44dc11ee80910b7b852b427.tar.gz chromium_src-e5ffa33eb3ee71b9b44dc11ee80910b7b852b427.tar.bz2 |
Always call the class methods to save/restore contexts.
If the current context was ever nil, sending a "restore" message would be a no-op and would likely leave an out-of-scope context as current.
BUG=90140
TEST=repeatedly select items in an open file dialog in column mode; no crash
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=95611
Reverted: http://src.chromium.org/viewvc/chrome?view=rev&revision=95631
Review URL: http://codereview.chromium.org/7572031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95686 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/base/clipboard/clipboard_mac.mm | 3 | ||||
-rw-r--r-- | ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h | 14 | ||||
-rw-r--r-- | ui/gfx/scoped_ns_graphics_context_save_gstate_mac.mm | 26 |
3 files changed, 30 insertions, 13 deletions
diff --git a/ui/base/clipboard/clipboard_mac.mm b/ui/base/clipboard/clipboard_mac.mm index 4f6b88a..0437e6a 100644 --- a/ui/base/clipboard/clipboard_mac.mm +++ b/ui/base/clipboard/clipboard_mac.mm @@ -16,6 +16,7 @@ #import "third_party/mozilla/NSPasteboard+Utils.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/gfx/canvas_skia.h" +#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" #include "ui/gfx/size.h" namespace ui { @@ -250,6 +251,7 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const { if (!image.get()) return SkBitmap(); + gfx::ScopedNSGraphicsContextSaveGState scoped_state; [image setFlipped:YES]; int width = [image size].width; int height = [image size].height; @@ -265,7 +267,6 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const { fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; - [NSGraphicsContext restoreGraphicsState]; } return canvas.ExtractBitmap(); } diff --git a/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h b/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h index 05bf5f6..4826bcc 100644 --- a/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h +++ b/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h @@ -4,23 +4,27 @@ #ifndef UI_GFX_SCOPED_NS_GRAPHICS_CONTEXT_SAVE_GSTATE_MAC_H_ #define UI_GFX_SCOPED_NS_GRAPHICS_CONTEXT_SAVE_GSTATE_MAC_H_ +#pragma once -#include "ui/ui_api.h" #include "base/basictypes.h" -#include "base/memory/scoped_nsobject.h" +#include "ui/ui_api.h" +#if defined(__OBJC__) @class NSGraphicsContext; +#else +class NSGraphicsContext; +#endif namespace gfx { +// A class to save/restore the state of the current context. class UI_API ScopedNSGraphicsContextSaveGState { public: - // If |context| is nil, it will use the |+currentContext|. - explicit ScopedNSGraphicsContextSaveGState(NSGraphicsContext* context = nil); + ScopedNSGraphicsContextSaveGState(); ~ScopedNSGraphicsContextSaveGState(); private: - scoped_nsobject<NSGraphicsContext> context_; + NSGraphicsContext* context_; // weak DISALLOW_COPY_AND_ASSIGN(ScopedNSGraphicsContextSaveGState); }; diff --git a/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.mm b/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.mm index f3992ea..9dca120 100644 --- a/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.mm +++ b/ui/gfx/scoped_ns_graphics_context_save_gstate_mac.mm @@ -4,19 +4,31 @@ #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" -#include <AppKit/AppKit.h> +#import <AppKit/AppKit.h> + +#include "base/logging.h" +#include "base/mac/mac_util.h" namespace gfx { -ScopedNSGraphicsContextSaveGState::ScopedNSGraphicsContextSaveGState( - NSGraphicsContext* context) : context_([context retain]) { - if (!context_) - context_.reset([[NSGraphicsContext currentContext] retain]); - [context_ saveGraphicsState]; +ScopedNSGraphicsContextSaveGState::ScopedNSGraphicsContextSaveGState() + : context_([NSGraphicsContext currentContext]) { + [NSGraphicsContext saveGraphicsState]; } ScopedNSGraphicsContextSaveGState::~ScopedNSGraphicsContextSaveGState() { - [context_ restoreGraphicsState]; + [NSGraphicsContext restoreGraphicsState]; + if (!context_ && base::mac::IsOSLeopardOrEarlier()) { + // On 10.5 and earlier, there is a bug. If the current graphics context was + // nil when +[NSGraphicsContext saveGraphicsState] was called, then calling + // +[NSGraphicsContext restoreGraphicsState] will not restore a nil current + // context, but will leave the current context in place. Because allowing + // that stale context (which will be deallocated soon) to remain current + // will only lead to heartache and pain, the current context must be + // manually nilled out. + [NSGraphicsContext setCurrentContext:nil]; + } + DCHECK_EQ(context_, [NSGraphicsContext currentContext]); } } // namespace gfx |