Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: ios/chrome/browser/ui/settings/sync_encryption_collection_view_controller.mm

Issue 2587023002: Upstream Chrome on iOS source code [8/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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/settings/sync_encryption_collection_view_controll er.h"
6
7 #include "base/ios/weak_nsobject.h"
8 #import "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "components/browser_sync/profile_sync_service.h"
11 #include "components/google/core/browser/google_util.h"
12 #include "components/strings/grit/components_strings.h"
13 #include "components/sync/base/sync_prefs.h"
14 #include "ios/chrome/browser/application_context.h"
15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
16 #include "ios/chrome/browser/chrome_url_constants.h"
17 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h"
18 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h"
19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item .h"
20 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h"
21 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
22 #import "ios/chrome/browser/ui/settings/cells/encryption_item.h"
23 #import "ios/chrome/browser/ui/settings/settings_utils.h"
24 #import "ios/chrome/browser/ui/settings/sync_create_passphrase_collection_view_c ontroller.h"
25 #import "ios/chrome/browser/ui/settings/sync_encryption_passphrase_collection_vi ew_controller.h"
26 #include "ios/chrome/grit/ios_strings.h"
27 #include "ui/base/l10n/l10n_util_mac.h"
28 #include "url/gurl.h"
29
30 namespace {
31
32 typedef NS_ENUM(NSInteger, SectionIdentifier) {
33 SectionIdentifierEncryption = kSectionIdentifierEnumZero,
34 SectionIdentifierFooter,
35 };
36
37 typedef NS_ENUM(NSInteger, ItemType) {
38 ItemTypeAccount = kItemTypeEnumZero,
39 ItemTypePassphrase,
40 ItemTypeFooter,
41 };
42
43 } // namespace
44
45 @interface SyncEncryptionCollectionViewController () {
46 ios::ChromeBrowserState* _browserState;
47 }
48 // Returns an account item.
49 - (CollectionViewItem*)accountItem;
50
51 // Returns a passphrase item.
52 - (CollectionViewItem*)passphraseItem;
53
54 // Returns a footer item with a link.
55 - (CollectionViewItem*)footerItem;
56 @end
57
58 @implementation SyncEncryptionCollectionViewController
59
60 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState {
61 DCHECK(browserState);
62 self = [super initWithStyle:CollectionViewControllerStyleAppBar];
63 if (self) {
64 self.title = l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_TITLE);
65 _browserState = browserState;
66 [self loadModel];
67 }
68 return self;
69 }
70
71 #pragma mark - SettingsRootCollectionViewController
72
73 - (void)loadModel {
74 [super loadModel];
75 CollectionViewModel* model = self.collectionViewModel;
76
77 [model addSectionWithIdentifier:SectionIdentifierEncryption];
78 [model addItem:[self accountItem]
79 toSectionWithIdentifier:SectionIdentifierEncryption];
80 [model addItem:[self passphraseItem]
81 toSectionWithIdentifier:SectionIdentifierEncryption];
82
83 browser_sync::ProfileSyncService* service =
84 IOSChromeProfileSyncServiceFactory::GetForBrowserState(_browserState);
85 if (service->IsEngineInitialized() && service->IsUsingSecondaryPassphrase()) {
86 [model addSectionWithIdentifier:SectionIdentifierFooter];
87 [model addItem:[self footerItem]
88 toSectionWithIdentifier:SectionIdentifierFooter];
89 }
90 }
91
92 #pragma mark - Items
93
94 - (CollectionViewItem*)accountItem {
95 DCHECK(browser_sync::ProfileSyncService::IsSyncAllowedByFlag());
96 BOOL checkedAndEnabled = YES;
97 browser_sync::ProfileSyncService* service =
98 IOSChromeProfileSyncServiceFactory::GetForBrowserState(_browserState);
99 if (service->IsEngineInitialized() && service->IsUsingSecondaryPassphrase()) {
100 checkedAndEnabled = NO;
101 }
102 NSString* text = l10n_util::GetNSString(IDS_SYNC_BASIC_ENCRYPTION_DATA);
103 return [self itemWithType:ItemTypeAccount
104 text:text
105 checked:checkedAndEnabled
106 enabled:checkedAndEnabled];
107 }
108
109 - (CollectionViewItem*)passphraseItem {
110 DCHECK(browser_sync::ProfileSyncService::IsSyncAllowedByFlag());
111 BOOL checked = NO;
112 BOOL enabled = NO;
113 browser_sync::ProfileSyncService* service =
114 IOSChromeProfileSyncServiceFactory::GetForBrowserState(_browserState);
115 if (service->IsEngineInitialized() && service->IsUsingSecondaryPassphrase()) {
116 checked = YES;
117 } else {
118 enabled = YES;
119 }
120 NSString* text = l10n_util::GetNSString(IDS_SYNC_FULL_ENCRYPTION_DATA);
121 return [self itemWithType:ItemTypePassphrase
122 text:text
123 checked:checked
124 enabled:enabled];
125 }
126
127 - (CollectionViewItem*)footerItem {
128 CollectionViewFooterItem* footerItem = [[[CollectionViewFooterItem alloc]
129 initWithType:ItemTypeFooter] autorelease];
130 footerItem.text =
131 l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_PASSPHRASE_HINT);
132 footerItem.linkURL = google_util::AppendGoogleLocaleParam(
133 GURL(kSyncGoogleDashboardURL),
134 GetApplicationContext()->GetApplicationLocale());
135 footerItem.linkDelegate = self;
136 return footerItem;
137 }
138
139 #pragma mark - MDCCollectionViewStylingDelegate
140
141 - (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView
142 cellStyleForSection:(NSInteger)section {
143 NSInteger sectionIdentifier =
144 [self.collectionViewModel sectionIdentifierForSection:section];
145 switch (sectionIdentifier) {
146 case SectionIdentifierFooter:
147 // Display the Learn More footer in the default style with no "card" UI
148 // and no section padding.
149 return MDCCollectionViewCellStyleDefault;
150 default:
151 return self.styler.cellStyle;
152 }
153 }
154
155 - (BOOL)collectionView:(UICollectionView*)collectionView
156 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath {
157 NSInteger sectionIdentifier =
158 [self.collectionViewModel sectionIdentifierForSection:indexPath.section];
159 switch (sectionIdentifier) {
160 case SectionIdentifierFooter:
161 // Display the Learn More footer without any background image or
162 // shadowing.
163 return YES;
164 default:
165 return NO;
166 }
167 }
168
169 - (CGFloat)collectionView:(UICollectionView*)collectionView
170 cellHeightAtIndexPath:(NSIndexPath*)indexPath {
171 CollectionViewItem* item =
172 [self.collectionViewModel itemAtIndexPath:indexPath];
173 return [MDCCollectionViewCell
174 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
175 forItem:item];
176 }
177
178 #pragma mark - UICollectionViewDelegate
179
180 - (void)collectionView:(UICollectionView*)collectionView
181 didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
182 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
183 DCHECK_EQ(indexPath.section,
184 [self.collectionViewModel
185 sectionForSectionIdentifier:SectionIdentifierEncryption]);
186
187 CollectionViewItem* item =
188 [self.collectionViewModel itemAtIndexPath:indexPath];
189 if ([item respondsToSelector:@selector(isEnabled)] &&
190 ![item performSelector:@selector(isEnabled)]) {
191 // Don't perform any action if the cell isn't enabled.
192 return;
193 }
194
195 switch (item.type) {
196 case ItemTypePassphrase: {
197 DCHECK(browser_sync::ProfileSyncService::IsSyncAllowedByFlag());
198 browser_sync::ProfileSyncService* service =
199 IOSChromeProfileSyncServiceFactory::GetForBrowserState(_browserState);
200 if (service->IsEngineInitialized() &&
201 !service->IsUsingSecondaryPassphrase()) {
202 base::scoped_nsobject<SyncCreatePassphraseCollectionViewController>
203 controller([[SyncCreatePassphraseCollectionViewController alloc]
204 initWithBrowserState:_browserState]);
205 [self.navigationController pushViewController:controller animated:YES];
206 }
207 break;
208 }
209 case ItemTypeAccount:
210 case ItemTypeFooter:
211 default:
212 break;
213 }
214 }
215
216 #pragma mark - Private methods
217
218 - (CollectionViewItem*)itemWithType:(NSInteger)type
219 text:(NSString*)text
220 checked:(BOOL)checked
221 enabled:(BOOL)enabled {
222 EncryptionItem* item =
223 [[[EncryptionItem alloc] initWithType:type] autorelease];
224 item.text = text;
225 item.accessoryType = checked ? MDCCollectionViewCellAccessoryCheckmark
226 : MDCCollectionViewCellAccessoryNone;
227 item.enabled = enabled;
228 return item;
229 }
230
231 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698