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 #import "ios/chrome/browser/ui/bookmarks/bookmark_position_cache.h" | 5 #import "ios/chrome/browser/ui/bookmarks/bookmark_position_cache.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/objc_property_releaser.h" | |
11 | 10 |
12 namespace { | 11 namespace { |
13 // The current version of the cached position. This number should be incremented | 12 // The current version of the cached position. This number should be incremented |
14 // each time the NSCoding implementation changes. | 13 // each time the NSCoding implementation changes. |
15 const int kVersion = 2; | 14 const int kVersion = 2; |
16 | 15 |
17 // The value 3 was used for items corresponding to managed bookmarks and | 16 // The value 3 was used for items corresponding to managed bookmarks and |
18 // was removed during the transition from UIWebView to WKWebView. | 17 // was removed during the transition from UIWebView to WKWebView. |
19 const int kMenuItemManaged = 3; | 18 const int kMenuItemManaged = 3; |
20 | 19 |
(...skipping 17 matching lines...) Expand all Loading... |
38 | 37 |
39 @implementation BookmarkPositionCache | 38 @implementation BookmarkPositionCache |
40 @synthesize folderId = _folderId; | 39 @synthesize folderId = _folderId; |
41 @synthesize position = _position; | 40 @synthesize position = _position; |
42 @synthesize type = _type; | 41 @synthesize type = _type; |
43 | 42 |
44 #pragma mark - Public Constructors | 43 #pragma mark - Public Constructors |
45 | 44 |
46 + (BookmarkPositionCache*)cacheForMenuItemFolderWithPosition:(CGFloat)position | 45 + (BookmarkPositionCache*)cacheForMenuItemFolderWithPosition:(CGFloat)position |
47 folderId:(int64_t)folderId { | 46 folderId:(int64_t)folderId { |
48 return [[[BookmarkPositionCache alloc] | 47 return [[BookmarkPositionCache alloc] |
49 initWithFolderId:folderId | 48 initWithFolderId:folderId |
50 position:position | 49 position:position |
51 type:bookmarks::MenuItemFolder] autorelease]; | 50 type:bookmarks::MenuItemFolder]; |
52 } | 51 } |
53 | 52 |
54 #pragma mark - Designated Initializer | 53 #pragma mark - Designated Initializer |
55 | 54 |
56 - (instancetype)initWithFolderId:(int64_t)folderId | 55 - (instancetype)initWithFolderId:(int64_t)folderId |
57 position:(CGFloat)position | 56 position:(CGFloat)position |
58 type:(bookmarks::MenuItemType)type { | 57 type:(bookmarks::MenuItemType)type { |
59 self = [super init]; | 58 self = [super init]; |
60 if (self) { | 59 if (self) { |
61 _folderId = folderId; | 60 _folderId = folderId; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 static_cast<NSUInteger>(self.folderId); | 95 static_cast<NSUInteger>(self.folderId); |
97 } | 96 } |
98 | 97 |
99 #pragma mark - NSCoding | 98 #pragma mark - NSCoding |
100 | 99 |
101 - (instancetype)initWithCoder:(NSCoder*)coder { | 100 - (instancetype)initWithCoder:(NSCoder*)coder { |
102 int version = [coder decodeIntForKey:kVersionKey]; | 101 int version = [coder decodeIntForKey:kVersionKey]; |
103 int typeInt = [coder decodeIntForKey:kTypeKey]; | 102 int typeInt = [coder decodeIntForKey:kTypeKey]; |
104 | 103 |
105 if (version != kVersion) { | 104 if (version != kVersion) { |
106 [self release]; | |
107 return nil; | 105 return nil; |
108 } | 106 } |
109 | 107 |
110 if (!bookmarks::NumberIsValidMenuItemType(typeInt)) { | 108 if (!bookmarks::NumberIsValidMenuItemType(typeInt)) { |
111 [self release]; | |
112 return nil; | 109 return nil; |
113 } | 110 } |
114 | 111 |
115 if (typeInt == kMenuItemManaged) { | 112 if (typeInt == kMenuItemManaged) { |
116 [self release]; | |
117 return nil; | 113 return nil; |
118 } | 114 } |
119 | 115 |
120 bookmarks::MenuItemType type = static_cast<bookmarks::MenuItemType>(typeInt); | 116 bookmarks::MenuItemType type = static_cast<bookmarks::MenuItemType>(typeInt); |
121 return [self initWithFolderId:[coder decodeInt64ForKey:kFolderKey] | 117 return [self initWithFolderId:[coder decodeInt64ForKey:kFolderKey] |
122 position:[coder decodeFloatForKey:kPositionKey] | 118 position:[coder decodeFloatForKey:kPositionKey] |
123 type:type]; | 119 type:type]; |
124 } | 120 } |
125 | 121 |
126 - (void)encodeWithCoder:(NSCoder*)coder { | 122 - (void)encodeWithCoder:(NSCoder*)coder { |
127 [coder encodeInt:kVersion forKey:kVersionKey]; | 123 [coder encodeInt:kVersion forKey:kVersionKey]; |
128 [coder encodeInt:static_cast<int>(self.type) forKey:kTypeKey]; | 124 [coder encodeInt:static_cast<int>(self.type) forKey:kTypeKey]; |
129 [coder encodeFloat:self.position forKey:kPositionKey]; | 125 [coder encodeFloat:self.position forKey:kPositionKey]; |
130 [coder encodeInt64:self.folderId forKey:kFolderKey]; | 126 [coder encodeInt64:self.folderId forKey:kFolderKey]; |
131 } | 127 } |
132 | 128 |
133 @end | 129 @end |
OLD | NEW |