| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/enhanced_bookmarks/image_store_util.h" | 5 #include "components/enhanced_bookmarks/image_store_util.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 // An implementation of RefCountedMemory, where the bytes are stored in a | 13 // An implementation of RefCountedMemory, where the bytes are stored in a |
| 14 // NSData. This class assumes the NSData is non mutable to avoid a copy. | 14 // NSData. This class assumes the NSData is non mutable to avoid a copy. |
| 15 class RefCountedNSDataMemory : public base::RefCountedMemory { | 15 class RefCountedNSDataMemory : public base::RefCountedMemory { |
| 16 public: | 16 public: |
| 17 explicit RefCountedNSDataMemory(NSData* memory) : data_([memory retain]) {} | 17 explicit RefCountedNSDataMemory(NSData* memory) : data_([memory retain]) {} |
| 18 | 18 |
| 19 virtual const unsigned char* front() const override { | 19 const unsigned char* front() const override { |
| 20 return reinterpret_cast<const unsigned char*>([data_ bytes]); | 20 return reinterpret_cast<const unsigned char*>([data_ bytes]); |
| 21 } | 21 } |
| 22 | 22 |
| 23 virtual size_t size() const override { | 23 size_t size() const override { return [data_ length]; } |
| 24 return [data_ length]; | |
| 25 } | |
| 26 | 24 |
| 27 private: | 25 private: |
| 28 virtual ~RefCountedNSDataMemory() {} | 26 ~RefCountedNSDataMemory() override {} |
| 29 | 27 |
| 30 base::scoped_nsobject<NSData> data_; | 28 base::scoped_nsobject<NSData> data_; |
| 31 DISALLOW_COPY_AND_ASSIGN(RefCountedNSDataMemory); | 29 DISALLOW_COPY_AND_ASSIGN(RefCountedNSDataMemory); |
| 32 }; | 30 }; |
| 33 } // namespace | 31 } // namespace |
| 34 | 32 |
| 35 namespace enhanced_bookmarks { | 33 namespace enhanced_bookmarks { |
| 36 | 34 |
| 37 // Encodes the UIImage representation of a gfx::Image. | 35 // Encodes the UIImage representation of a gfx::Image. |
| 38 scoped_refptr<base::RefCountedMemory> BytesForImage(const gfx::Image& image) { | 36 scoped_refptr<base::RefCountedMemory> BytesForImage(const gfx::Image& image) { |
| 39 DCHECK(image.HasRepresentation(gfx::Image::kImageRepCocoaTouch)); | 37 DCHECK(image.HasRepresentation(gfx::Image::kImageRepCocoaTouch)); |
| 40 return scoped_refptr<RefCountedNSDataMemory>(new RefCountedNSDataMemory( | 38 return scoped_refptr<RefCountedNSDataMemory>(new RefCountedNSDataMemory( |
| 41 [NSKeyedArchiver archivedDataWithRootObject:image.ToUIImage()])); | 39 [NSKeyedArchiver archivedDataWithRootObject:image.ToUIImage()])); |
| 42 } | 40 } |
| 43 | 41 |
| 44 // Decodes the UIImage in the bytes and returns a gfx::Image. | 42 // Decodes the UIImage in the bytes and returns a gfx::Image. |
| 45 gfx::Image ImageForBytes(const scoped_refptr<base::RefCountedMemory>& data) { | 43 gfx::Image ImageForBytes(const scoped_refptr<base::RefCountedMemory>& data) { |
| 46 return gfx::Image([[NSKeyedUnarchiver unarchiveObjectWithData: | 44 return gfx::Image([[NSKeyedUnarchiver unarchiveObjectWithData: |
| 47 [NSData dataWithBytes:data->front() length:data->size()]] retain]); | 45 [NSData dataWithBytes:data->front() length:data->size()]] retain]); |
| 48 } | 46 } |
| 49 | 47 |
| 50 } // namespace enhanced_bookmarks | 48 } // namespace enhanced_bookmarks |
| OLD | NEW |