OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/settings/search_engine_settings_collection_view_c
ontroller.h" | 5 #import "ios/chrome/browser/ui/settings/search_engine_settings_collection_view_c
ontroller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #import "base/ios/weak_nsobject.h" | |
10 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
11 #import "base/mac/scoped_nsobject.h" | |
12 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
13 #include "components/search_engines/template_url_service.h" | 11 #include "components/search_engines/template_url_service.h" |
14 #include "components/search_engines/template_url_service_observer.h" | 12 #include "components/search_engines/template_url_service_observer.h" |
15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
16 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
17 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
18 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 16 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
19 #include "ios/chrome/grit/ios_strings.h" | 17 #include "ios/chrome/grit/ios_strings.h" |
20 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" | 18 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" |
21 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
22 | 20 |
| 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 22 #error "This file requires ARC support." |
| 23 #endif |
| 24 |
23 @interface SearchEngineSettingsCollectionViewController () | 25 @interface SearchEngineSettingsCollectionViewController () |
24 - (void)onChange; | 26 - (void)onChange; |
25 @end | 27 @end |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 31 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
30 SectionIdentifierSearchEngines = kSectionIdentifierEnumZero, | 32 SectionIdentifierSearchEngines = kSectionIdentifierEnumZero, |
31 }; | 33 }; |
32 | 34 |
33 typedef NS_ENUM(NSInteger, ItemType) { | 35 typedef NS_ENUM(NSInteger, ItemType) { |
34 ItemTypeSearchEnginesEngine = kItemTypeEnumZero, | 36 ItemTypeSearchEnginesEngine = kItemTypeEnumZero, |
35 }; | 37 }; |
36 | 38 |
37 // Observer used to reload the Search Engine collection view once the | 39 // Observer used to reload the Search Engine collection view once the |
38 // TemplateURLService changes, either on first load or due to a | 40 // TemplateURLService changes, either on first load or due to a |
39 // policy change. | 41 // policy change. |
40 class SearchEngineObserver : public TemplateURLServiceObserver { | 42 class SearchEngineObserver : public TemplateURLServiceObserver { |
41 public: | 43 public: |
42 SearchEngineObserver(SearchEngineSettingsCollectionViewController* owner, | 44 SearchEngineObserver(SearchEngineSettingsCollectionViewController* owner, |
43 TemplateURLService* urlService); | 45 TemplateURLService* urlService); |
44 ~SearchEngineObserver() override; | 46 ~SearchEngineObserver() override; |
45 void OnTemplateURLServiceChanged() override; | 47 void OnTemplateURLServiceChanged() override; |
46 | 48 |
47 private: | 49 private: |
48 base::WeakNSObject<SearchEngineSettingsCollectionViewController> owner_; | 50 __weak SearchEngineSettingsCollectionViewController* owner_; |
49 TemplateURLService* templateURLService_; // weak | 51 TemplateURLService* templateURLService_; // weak |
50 }; | 52 }; |
51 | 53 |
52 SearchEngineObserver::SearchEngineObserver( | 54 SearchEngineObserver::SearchEngineObserver( |
53 SearchEngineSettingsCollectionViewController* owner, | 55 SearchEngineSettingsCollectionViewController* owner, |
54 TemplateURLService* urlService) | 56 TemplateURLService* urlService) |
55 : owner_(owner), templateURLService_(urlService) { | 57 : owner_(owner), templateURLService_(urlService) { |
56 templateURLService_->AddObserver(this); | 58 templateURLService_->AddObserver(this); |
57 } | 59 } |
58 | 60 |
59 SearchEngineObserver::~SearchEngineObserver() { | 61 SearchEngineObserver::~SearchEngineObserver() { |
60 templateURLService_->RemoveObserver(this); | 62 templateURLService_->RemoveObserver(this); |
61 } | 63 } |
62 | 64 |
63 void SearchEngineObserver::OnTemplateURLServiceChanged() { | 65 void SearchEngineObserver::OnTemplateURLServiceChanged() { |
64 base::scoped_nsobject<SearchEngineSettingsCollectionViewController> | 66 SearchEngineSettingsCollectionViewController* strongOwner = owner_; |
65 strongOwner([owner_.get() retain]); | |
66 [strongOwner onChange]; | 67 [strongOwner onChange]; |
67 } | 68 } |
68 | 69 |
69 } // namespace | 70 } // namespace |
70 | 71 |
71 @implementation SearchEngineSettingsCollectionViewController { | 72 @implementation SearchEngineSettingsCollectionViewController { |
72 TemplateURLService* templateURLService_; // weak | 73 TemplateURLService* templateURLService_; // weak |
73 std::unique_ptr<SearchEngineObserver> observer_; | 74 std::unique_ptr<SearchEngineObserver> observer_; |
74 // Prevent unnecessary notifications when we write to the setting. | 75 // Prevent unnecessary notifications when we write to the setting. |
75 BOOL updatingBackend_; | 76 BOOL updatingBackend_; |
(...skipping 24 matching lines...) Expand all Loading... |
100 // Do not add any sections if there are no search engines. | 101 // Do not add any sections if there are no search engines. |
101 if (![values count]) { | 102 if (![values count]) { |
102 return; | 103 return; |
103 } | 104 } |
104 | 105 |
105 [model addSectionWithIdentifier:SectionIdentifierSearchEngines]; | 106 [model addSectionWithIdentifier:SectionIdentifierSearchEngines]; |
106 for (NSUInteger i = 0; i < values.count; i++) { | 107 for (NSUInteger i = 0; i < values.count; i++) { |
107 NSString* value = values[i]; | 108 NSString* value = values[i]; |
108 BOOL checked = [value isEqualToString:[self currentValue]]; | 109 BOOL checked = [value isEqualToString:[self currentValue]]; |
109 | 110 |
110 base::scoped_nsobject<CollectionViewTextItem> engine( | 111 CollectionViewTextItem* engine = [[CollectionViewTextItem alloc] |
111 [[CollectionViewTextItem alloc] | 112 initWithType:ItemTypeSearchEnginesEngine]; |
112 initWithType:ItemTypeSearchEnginesEngine]); | |
113 [engine setText:value]; | 113 [engine setText:value]; |
114 if (checked) { | 114 if (checked) { |
115 [engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; | 115 [engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; |
116 } | 116 } |
117 [model addItem:engine | 117 [model addItem:engine |
118 toSectionWithIdentifier:SectionIdentifierSearchEngines]; | 118 toSectionWithIdentifier:SectionIdentifierSearchEngines]; |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
122 #pragma mark UICollectionViewDelegate | 122 #pragma mark UICollectionViewDelegate |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 templateURLService_->SetUserSelectedDefaultSearchProvider(urls[index]); | 194 templateURLService_->SetUserSelectedDefaultSearchProvider(urls[index]); |
195 updatingBackend_ = NO; | 195 updatingBackend_ = NO; |
196 } | 196 } |
197 | 197 |
198 - (void)onChange { | 198 - (void)onChange { |
199 if (!updatingBackend_) | 199 if (!updatingBackend_) |
200 [self reloadData]; | 200 [self reloadData]; |
201 } | 201 } |
202 | 202 |
203 @end | 203 @end |
OLD | NEW |