| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 <Foundation/Foundation.h> | |
| 6 | |
| 7 #include "ios/chrome/browser/reading_list/reading_list_model_storage_defaults.h" | |
| 8 | |
| 9 #include "base/mac/foundation_util.h" | |
| 10 #include "base/strings/sys_string_conversions.h" | |
| 11 #import "net/base/mac/url_conversions.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 NSString* const kReadingListReadElements = @"ReadingListReadElements"; | |
| 16 NSString* const kReadingListUnreadElements = @"ReadingListUnreadElements"; | |
| 17 NSString* const kReadingListUnseenState = @"ReadingListUnseenState"; | |
| 18 NSString* const kReadingListEntryTitleKey = @"title"; | |
| 19 NSString* const kReadingListEntryURLKey = @"URL"; | |
| 20 NSString* const kReadingListEntryStateKey = @"state"; | |
| 21 NSString* const kReadingListEntryDistilledURLKey = @"distilledURL"; | |
| 22 | |
| 23 ReadingListEntry DecodeReadingListEntry(NSData* data) { | |
| 24 NSError* error = nil; | |
| 25 NSDictionary* dictionary = | |
| 26 [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:data error:&error]; | |
| 27 NSString* title = base::mac::ObjCCastStrict<NSString>( | |
| 28 [dictionary objectForKey:kReadingListEntryTitleKey]); | |
| 29 NSURL* url = base::mac::ObjCCastStrict<NSURL>( | |
| 30 [dictionary objectForKey:kReadingListEntryURLKey]); | |
| 31 auto state(static_cast<ReadingListEntry::DistillationState>( | |
| 32 [[dictionary objectForKey:kReadingListEntryStateKey] intValue])); | |
| 33 DCHECK(title && url); | |
| 34 GURL gurl(net::GURLWithNSURL(url)); | |
| 35 DCHECK(gurl.is_valid()); | |
| 36 ReadingListEntry entry(gurl, base::SysNSStringToUTF8(title)); | |
| 37 | |
| 38 switch (state) { | |
| 39 case ReadingListEntry::PROCESSED: { | |
| 40 NSURL* distilled_url = base::mac::ObjCCastStrict<NSURL>( | |
| 41 [dictionary objectForKey:kReadingListEntryDistilledURLKey]); | |
| 42 DCHECK(distilled_url); | |
| 43 GURL distilled_gurl(net::GURLWithNSURL(distilled_url)); | |
| 44 DCHECK(distilled_gurl.is_valid()); | |
| 45 entry.SetDistilledURL(distilled_gurl); | |
| 46 break; | |
| 47 } | |
| 48 case ReadingListEntry::PROCESSING: | |
| 49 case ReadingListEntry::WILL_RETRY: | |
| 50 case ReadingListEntry::ERROR: | |
| 51 entry.SetDistilledState(state); | |
| 52 break; | |
| 53 case ReadingListEntry::WAITING: | |
| 54 break; | |
| 55 } | |
| 56 | |
| 57 return entry; | |
| 58 } | |
| 59 | |
| 60 NSData* EncodeReadingListEntry(const ReadingListEntry& entry) { | |
| 61 NSMutableDictionary* dictionary = | |
| 62 [NSMutableDictionary dictionaryWithDictionary:@{ | |
| 63 kReadingListEntryTitleKey : base::SysUTF8ToNSString(entry.Title()), | |
| 64 kReadingListEntryURLKey : net::NSURLWithGURL(entry.URL()), | |
| 65 kReadingListEntryStateKey : | |
| 66 [NSNumber numberWithInt:entry.DistilledState()] | |
| 67 }]; | |
| 68 | |
| 69 const GURL distilled_gurl(entry.DistilledURL()); | |
| 70 if (distilled_gurl.is_valid()) { | |
| 71 NSURL* distilled_url = net::NSURLWithGURL(distilled_gurl); | |
| 72 if (distilled_url) | |
| 73 [dictionary setObject:distilled_url | |
| 74 forKey:kReadingListEntryDistilledURLKey]; | |
| 75 } | |
| 76 return [NSKeyedArchiver archivedDataWithRootObject:dictionary]; | |
| 77 } | |
| 78 | |
| 79 } // namespace | |
| 80 | |
| 81 ReadingListModelStorageDefaults::ReadingListModelStorageDefaults() { | |
| 82 backend_ = [[NSUserDefaults standardUserDefaults] retain]; | |
| 83 } | |
| 84 | |
| 85 ReadingListModelStorageDefaults::ReadingListModelStorageDefaults( | |
| 86 NSUserDefaults* backend) { | |
| 87 DCHECK(backend); | |
| 88 backend_ = [backend retain]; | |
| 89 } | |
| 90 | |
| 91 ReadingListModelStorageDefaults::~ReadingListModelStorageDefaults() { | |
| 92 [backend_ release]; | |
| 93 } | |
| 94 | |
| 95 std::vector<ReadingListEntry> | |
| 96 ReadingListModelStorageDefaults::LoadPersistentReadList() { | |
| 97 std::vector<ReadingListEntry> read; | |
| 98 NSArray* readList = base::mac::ObjCCastStrict<NSArray>( | |
| 99 [backend_ objectForKey:kReadingListReadElements]); | |
| 100 if (readList) { | |
| 101 for (NSData* entryData : readList) { | |
| 102 read.push_back(DecodeReadingListEntry(entryData)); | |
| 103 } | |
| 104 } | |
| 105 return read; | |
| 106 } | |
| 107 | |
| 108 std::vector<ReadingListEntry> | |
| 109 ReadingListModelStorageDefaults::LoadPersistentUnreadList() { | |
| 110 std::vector<ReadingListEntry> unread; | |
| 111 NSArray* unreadList = base::mac::ObjCCastStrict<NSArray>( | |
| 112 [backend_ objectForKey:kReadingListUnreadElements]); | |
| 113 if (unreadList) { | |
| 114 for (NSData* entryData : unreadList) { | |
| 115 unread.push_back(DecodeReadingListEntry(entryData)); | |
| 116 } | |
| 117 } | |
| 118 return unread; | |
| 119 } | |
| 120 | |
| 121 bool ReadingListModelStorageDefaults::LoadPersistentHasUnseen() { | |
| 122 return [[backend_ objectForKey:kReadingListUnseenState] boolValue]; | |
| 123 } | |
| 124 | |
| 125 void ReadingListModelStorageDefaults::SavePersistentReadList( | |
| 126 const std::vector<ReadingListEntry>& read) { | |
| 127 NSMutableArray* read_list = [NSMutableArray arrayWithCapacity:read.size()]; | |
| 128 for (const ReadingListEntry& entry : read) { | |
| 129 [read_list addObject:EncodeReadingListEntry(entry)]; | |
| 130 } | |
| 131 [backend_ setObject:read_list forKey:kReadingListReadElements]; | |
| 132 } | |
| 133 | |
| 134 void ReadingListModelStorageDefaults::SavePersistentUnreadList( | |
| 135 const std::vector<ReadingListEntry>& unread) { | |
| 136 NSMutableArray* unread_list = | |
| 137 [NSMutableArray arrayWithCapacity:unread.size()]; | |
| 138 for (const ReadingListEntry& entry : unread) { | |
| 139 [unread_list addObject:EncodeReadingListEntry(entry)]; | |
| 140 } | |
| 141 [backend_ setObject:unread_list forKey:kReadingListUnreadElements]; | |
| 142 } | |
| 143 | |
| 144 void ReadingListModelStorageDefaults::SavePersistentHasUnseen(bool has_unseen) { | |
| 145 [backend_ setObject:[NSNumber numberWithBool:has_unseen] | |
| 146 forKey:kReadingListUnseenState]; | |
| 147 } | |
| OLD | NEW |