OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/bookmarks/bookmark_position_cache.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/mac/objc_property_releaser.h" |
| 11 |
| 12 namespace { |
| 13 // The current version of the cached position. This number should be incremented |
| 14 // each time the NSCoding implementation changes. |
| 15 const int kVersion = 2; |
| 16 |
| 17 // The value 3 was used for items corresponding to managed bookmarks and |
| 18 // was removed during the transition from UIWebView to WKWebView. |
| 19 const int kMenuItemManaged = 3; |
| 20 |
| 21 NSString* kFolderKey = @"FolderKey"; |
| 22 NSString* kTypeKey = @"TypeKey"; |
| 23 NSString* kPositionKey = @"PositionKey"; |
| 24 NSString* kVersionKey = @"VersionKey"; |
| 25 } // namespace |
| 26 |
| 27 @interface BookmarkPositionCache () |
| 28 |
| 29 // This is the designated initializer. It does not perform any validation. |
| 30 - (instancetype)initWithFolderId:(int64_t)folderId |
| 31 position:(CGFloat)position |
| 32 type:(bookmarks::MenuItemType)type |
| 33 NS_DESIGNATED_INITIALIZER; |
| 34 |
| 35 - (instancetype)init NS_UNAVAILABLE; |
| 36 |
| 37 @end |
| 38 |
| 39 @implementation BookmarkPositionCache |
| 40 @synthesize folderId = _folderId; |
| 41 @synthesize position = _position; |
| 42 @synthesize type = _type; |
| 43 |
| 44 #pragma mark - Public Constructors |
| 45 |
| 46 + (BookmarkPositionCache*)cacheForMenuItemAllWithPosition:(CGFloat)position { |
| 47 return [[[BookmarkPositionCache alloc] |
| 48 initWithFolderId:0 |
| 49 position:position |
| 50 type:bookmarks::MenuItemAll] autorelease]; |
| 51 } |
| 52 |
| 53 + (BookmarkPositionCache*)cacheForMenuItemFolderWithPosition:(CGFloat)position |
| 54 folderId:(int64_t)folderId { |
| 55 return [[[BookmarkPositionCache alloc] |
| 56 initWithFolderId:folderId |
| 57 position:position |
| 58 type:bookmarks::MenuItemFolder] autorelease]; |
| 59 } |
| 60 |
| 61 #pragma mark - Designated Initializer |
| 62 |
| 63 - (instancetype)initWithFolderId:(int64_t)folderId |
| 64 position:(CGFloat)position |
| 65 type:(bookmarks::MenuItemType)type { |
| 66 self = [super init]; |
| 67 if (self) { |
| 68 _folderId = folderId; |
| 69 _position = position; |
| 70 _type = type; |
| 71 } |
| 72 return self; |
| 73 } |
| 74 |
| 75 #pragma mark - Superclass Overrides |
| 76 |
| 77 - (instancetype)init { |
| 78 NOTREACHED(); |
| 79 return nil; |
| 80 } |
| 81 |
| 82 - (BOOL)isEqual:(id)object { |
| 83 if (self == object) |
| 84 return YES; |
| 85 if (![object isKindOfClass:[BookmarkPositionCache class]]) |
| 86 return NO; |
| 87 BookmarkPositionCache* other = static_cast<BookmarkPositionCache*>(object); |
| 88 if (self.type != other.type) |
| 89 return NO; |
| 90 if (fabs(self.position - other.position) > 0.01) |
| 91 return NO; |
| 92 switch (self.type) { |
| 93 case bookmarks::MenuItemDivider: |
| 94 case bookmarks::MenuItemAll: |
| 95 case bookmarks::MenuItemSectionHeader: |
| 96 return YES; |
| 97 case bookmarks::MenuItemFolder: |
| 98 return self.folderId == other.folderId; |
| 99 } |
| 100 } |
| 101 |
| 102 - (NSUInteger)hash { |
| 103 return static_cast<NSUInteger>(self.type) ^ |
| 104 static_cast<NSUInteger>(self.folderId); |
| 105 } |
| 106 |
| 107 #pragma mark - NSCoding |
| 108 |
| 109 - (instancetype)initWithCoder:(NSCoder*)coder { |
| 110 int version = [coder decodeIntForKey:kVersionKey]; |
| 111 int typeInt = [coder decodeIntForKey:kTypeKey]; |
| 112 |
| 113 if (version != kVersion) { |
| 114 [self release]; |
| 115 return nil; |
| 116 } |
| 117 |
| 118 if (!bookmarks::NumberIsValidMenuItemType(typeInt)) { |
| 119 [self release]; |
| 120 return nil; |
| 121 } |
| 122 |
| 123 if (typeInt == kMenuItemManaged) { |
| 124 [self release]; |
| 125 return nil; |
| 126 } |
| 127 |
| 128 bookmarks::MenuItemType type = static_cast<bookmarks::MenuItemType>(typeInt); |
| 129 return [self initWithFolderId:[coder decodeInt64ForKey:kFolderKey] |
| 130 position:[coder decodeFloatForKey:kPositionKey] |
| 131 type:type]; |
| 132 } |
| 133 |
| 134 - (void)encodeWithCoder:(NSCoder*)coder { |
| 135 [coder encodeInt:kVersion forKey:kVersionKey]; |
| 136 [coder encodeInt:static_cast<int>(self.type) forKey:kTypeKey]; |
| 137 [coder encodeFloat:self.position forKey:kPositionKey]; |
| 138 [coder encodeInt64:self.folderId forKey:kFolderKey]; |
| 139 } |
| 140 |
| 141 @end |
OLD | NEW |