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/app/spotlight/topsites_spotlight_manager.h" | 5 #import "ios/chrome/app/spotlight/topsites_spotlight_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/ios/weak_nsobject.h" | |
10 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
11 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
12 #include "components/bookmarks/browser/bookmark_model.h" | 11 #include "components/bookmarks/browser/bookmark_model.h" |
13 #include "components/browser_sync/profile_sync_service.h" | 12 #include "components/browser_sync/profile_sync_service.h" |
14 #include "components/history/core/browser/history_types.h" | 13 #include "components/history/core/browser/history_types.h" |
15 #include "components/history/core/browser/top_sites.h" | 14 #include "components/history/core/browser/top_sites.h" |
16 #include "components/history/core/browser/top_sites_observer.h" | 15 #include "components/history/core/browser/top_sites_observer.h" |
17 #include "components/suggestions/suggestions_service.h" | 16 #include "components/suggestions/suggestions_service.h" |
18 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 17 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
19 #include "ios/chrome/browser/favicon/ios_chrome_large_icon_service_factory.h" | 18 #include "ios/chrome/browser/favicon/ios_chrome_large_icon_service_factory.h" |
20 #include "ios/chrome/browser/history/top_sites_factory.h" | 19 #include "ios/chrome/browser/history/top_sites_factory.h" |
21 #include "ios/chrome/browser/suggestions/suggestions_service_factory.h" | 20 #include "ios/chrome/browser/suggestions/suggestions_service_factory.h" |
22 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" | 21 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
23 #include "ios/chrome/browser/sync/sync_observer_bridge.h" | 22 #include "ios/chrome/browser/sync/sync_observer_bridge.h" |
24 #include "ios/chrome/browser/ui/ntp/google_landing_controller.h" | 23 #include "ios/chrome/browser/ui/ntp/google_landing_controller.h" |
25 | 24 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." |
| 27 #endif |
| 28 |
26 class SpotlightTopSitesBridge; | 29 class SpotlightTopSitesBridge; |
27 class SpotlightTopSitesCallbackBridge; | 30 class SpotlightTopSitesCallbackBridge; |
28 class SpotlightSuggestionsBridge; | 31 class SpotlightSuggestionsBridge; |
29 | 32 |
30 @interface TopSitesSpotlightManager ()<SyncObserverModelBridge> { | 33 @interface TopSitesSpotlightManager ()<SyncObserverModelBridge> { |
31 // Bridge to register for top sites changes. It's important that this instance | 34 // Bridge to register for top sites changes. It's important that this instance |
32 // variable is released before the _topSite one. | 35 // variable is released before the _topSite one. |
33 std::unique_ptr<SpotlightTopSitesBridge> _topSitesBridge; | 36 std::unique_ptr<SpotlightTopSitesBridge> _topSitesBridge; |
34 | 37 |
35 // Bridge to register for top sites callbacks. | 38 // Bridge to register for top sites callbacks. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 explicit SpotlightTopSitesCallbackBridge(TopSitesSpotlightManager* owner) | 93 explicit SpotlightTopSitesCallbackBridge(TopSitesSpotlightManager* owner) |
91 : owner_(owner) {} | 94 : owner_(owner) {} |
92 | 95 |
93 SpotlightTopSitesCallbackBridge() {} | 96 SpotlightTopSitesCallbackBridge() {} |
94 | 97 |
95 void OnMostVisitedURLsAvailable(const history::MostVisitedURLList& data) { | 98 void OnMostVisitedURLsAvailable(const history::MostVisitedURLList& data) { |
96 [owner_ onMostVisitedURLsAvailable:data]; | 99 [owner_ onMostVisitedURLsAvailable:data]; |
97 } | 100 } |
98 | 101 |
99 private: | 102 private: |
100 __unsafe_unretained TopSitesSpotlightManager* owner_; // weak, owns us | 103 __weak TopSitesSpotlightManager* owner_; |
101 }; | 104 }; |
102 | 105 |
103 class SpotlightTopSitesBridge : public history::TopSitesObserver { | 106 class SpotlightTopSitesBridge : public history::TopSitesObserver { |
104 public: | 107 public: |
105 explicit SpotlightTopSitesBridge(TopSitesSpotlightManager* owner) | 108 explicit SpotlightTopSitesBridge(TopSitesSpotlightManager* owner) |
106 : owner_(owner) { | 109 : owner_(owner) { |
107 owner.topSites->AddObserver(this); | 110 owner.topSites->AddObserver(this); |
108 }; | 111 }; |
109 | 112 |
110 ~SpotlightTopSitesBridge() override { | 113 ~SpotlightTopSitesBridge() override { |
111 owner_.topSites->RemoveObserver(this); | 114 owner_.topSites->RemoveObserver(this); |
112 }; | 115 }; |
113 | 116 |
114 void TopSitesLoaded(history::TopSites* top_sites) override {} | 117 void TopSitesLoaded(history::TopSites* top_sites) override {} |
115 | 118 |
116 void TopSitesChanged(history::TopSites* top_sites, | 119 void TopSitesChanged(history::TopSites* top_sites, |
117 ChangeReason change_reason) override { | 120 ChangeReason change_reason) override { |
118 [owner_ updateAllTopSitesSpotlightItems]; | 121 [owner_ updateAllTopSitesSpotlightItems]; |
119 } | 122 } |
120 | 123 |
121 private: | 124 private: |
122 __unsafe_unretained TopSitesSpotlightManager* owner_; // weak | 125 __weak TopSitesSpotlightManager* owner_; |
123 }; | 126 }; |
124 | 127 |
125 class SpotlightSuggestionsBridge | 128 class SpotlightSuggestionsBridge |
126 : public base::SupportsWeakPtr<SpotlightSuggestionsBridge> { | 129 : public base::SupportsWeakPtr<SpotlightSuggestionsBridge> { |
127 public: | 130 public: |
128 explicit SpotlightSuggestionsBridge(TopSitesSpotlightManager* owner) | 131 explicit SpotlightSuggestionsBridge(TopSitesSpotlightManager* owner) |
129 : owner_(owner) {} | 132 : owner_(owner) {} |
130 | 133 |
131 SpotlightSuggestionsBridge() {} | 134 SpotlightSuggestionsBridge() {} |
132 | 135 |
133 void OnSuggestionsProfileAvailable( | 136 void OnSuggestionsProfileAvailable( |
134 const suggestions::SuggestionsProfile& suggestions_profile) { | 137 const suggestions::SuggestionsProfile& suggestions_profile) { |
135 [owner_ onSuggestionsProfileAvailable:suggestions_profile]; | 138 [owner_ onSuggestionsProfileAvailable:suggestions_profile]; |
136 } | 139 } |
137 | 140 |
138 private: | 141 private: |
139 __unsafe_unretained TopSitesSpotlightManager* owner_; // weak, owns us | 142 __weak TopSitesSpotlightManager* owner_; |
140 }; | 143 }; |
141 | 144 |
142 @implementation TopSitesSpotlightManager | 145 @implementation TopSitesSpotlightManager |
143 @synthesize topSites = _topSites; | 146 @synthesize topSites = _topSites; |
144 | 147 |
145 + (TopSitesSpotlightManager*)topSitesSpotlightManagerWithBrowserState: | 148 + (TopSitesSpotlightManager*)topSitesSpotlightManagerWithBrowserState: |
146 (ios::ChromeBrowserState*)browserState { | 149 (ios::ChromeBrowserState*)browserState { |
147 return [[[TopSitesSpotlightManager alloc] | 150 return [[TopSitesSpotlightManager alloc] |
148 initWithLargeIconService:IOSChromeLargeIconServiceFactory:: | 151 initWithLargeIconService:IOSChromeLargeIconServiceFactory:: |
149 GetForBrowserState(browserState) | 152 GetForBrowserState(browserState) |
150 topSites:ios::TopSitesFactory::GetForBrowserState( | 153 topSites:ios::TopSitesFactory::GetForBrowserState( |
151 browserState) | 154 browserState) |
152 bookmarkModel:ios::BookmarkModelFactory::GetForBrowserState( | 155 bookmarkModel:ios::BookmarkModelFactory::GetForBrowserState( |
153 browserState) | 156 browserState) |
154 profileSyncService:IOSChromeProfileSyncServiceFactory:: | 157 profileSyncService:IOSChromeProfileSyncServiceFactory:: |
155 GetForBrowserState(browserState) | 158 GetForBrowserState(browserState) |
156 suggestionsService:suggestions::SuggestionsServiceFactory:: | 159 suggestionsService:suggestions::SuggestionsServiceFactory:: |
157 GetForBrowserState(browserState)] | 160 GetForBrowserState(browserState)]; |
158 autorelease]; | |
159 } | 161 } |
160 | 162 |
161 - (instancetype) | 163 - (instancetype) |
162 initWithLargeIconService:(favicon::LargeIconService*)largeIconService | 164 initWithLargeIconService:(favicon::LargeIconService*)largeIconService |
163 topSites:(scoped_refptr<history::TopSites>)topSites | 165 topSites:(scoped_refptr<history::TopSites>)topSites |
164 bookmarkModel:(bookmarks::BookmarkModel*)bookmarkModel | 166 bookmarkModel:(bookmarks::BookmarkModel*)bookmarkModel |
165 profileSyncService:(browser_sync::ProfileSyncService*)syncService | 167 profileSyncService:(browser_sync::ProfileSyncService*)syncService |
166 suggestionsService:(suggestions::SuggestionsService*)suggestionsService { | 168 suggestionsService:(suggestions::SuggestionsService*)suggestionsService { |
167 self = [super initWithLargeIconService:largeIconService | 169 self = [super initWithLargeIconService:largeIconService |
168 domain:spotlight::DOMAIN_TOPSITES]; | 170 domain:spotlight::DOMAIN_TOPSITES]; |
(...skipping 10 matching lines...) Expand all Loading... |
179 _suggestionsServiceResponseSubscription = _suggestionService->AddCallback( | 181 _suggestionsServiceResponseSubscription = _suggestionService->AddCallback( |
180 base::Bind(&SpotlightSuggestionsBridge::OnSuggestionsProfileAvailable, | 182 base::Bind(&SpotlightSuggestionsBridge::OnSuggestionsProfileAvailable, |
181 _suggestionsBridge->AsWeakPtr())); | 183 _suggestionsBridge->AsWeakPtr())); |
182 sync_observer_bridge_.reset(new SyncObserverBridge(self, syncService)); | 184 sync_observer_bridge_.reset(new SyncObserverBridge(self, syncService)); |
183 } | 185 } |
184 } | 186 } |
185 return self; | 187 return self; |
186 } | 188 } |
187 | 189 |
188 - (void)updateAllTopSitesSpotlightItems { | 190 - (void)updateAllTopSitesSpotlightItems { |
189 base::WeakNSObject<TopSitesSpotlightManager> weakSelf(self); | 191 __weak TopSitesSpotlightManager* weakSelf = self; |
190 [self clearAllSpotlightItems:^(NSError* error) { | 192 [self clearAllSpotlightItems:^(NSError* error) { |
191 dispatch_async(dispatch_get_main_queue(), ^{ | 193 dispatch_async(dispatch_get_main_queue(), ^{ |
192 [weakSelf addAllTopSitesSpotlightItems]; | 194 [weakSelf addAllTopSitesSpotlightItems]; |
193 }); | 195 }); |
194 }]; | 196 }]; |
195 } | 197 } |
196 | 198 |
197 - (void)addAllTopSitesSpotlightItems { | 199 - (void)addAllTopSitesSpotlightItems { |
198 if (_suggestionService) { | 200 if (_suggestionService) { |
199 [self addAllSuggestionsTopSitesItems]; | 201 [self addAllSuggestionsTopSitesItems]; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 } else { | 264 } else { |
263 [self addAllLocalTopSitesItems]; | 265 [self addAllLocalTopSitesItems]; |
264 } | 266 } |
265 } | 267 } |
266 | 268 |
267 - (void)reindexTopSites { | 269 - (void)reindexTopSites { |
268 if (_isReindexPending) { | 270 if (_isReindexPending) { |
269 return; | 271 return; |
270 } | 272 } |
271 _isReindexPending = true; | 273 _isReindexPending = true; |
272 base::WeakNSObject<TopSitesSpotlightManager> weakSelf(self); | 274 __weak TopSitesSpotlightManager* weakSelf = self; |
273 dispatch_after( | 275 dispatch_after( |
274 dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(1 * NSEC_PER_SEC)), | 276 dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(1 * NSEC_PER_SEC)), |
275 dispatch_get_main_queue(), ^{ | 277 dispatch_get_main_queue(), ^{ |
276 [weakSelf updateAllTopSitesSpotlightItems]; | 278 TopSitesSpotlightManager* strongSelf = weakSelf; |
277 weakSelf.get()->_isReindexPending = false; | 279 if (!strongSelf) { |
| 280 return; |
| 281 } |
| 282 [strongSelf updateAllTopSitesSpotlightItems]; |
| 283 strongSelf->_isReindexPending = false; |
278 }); | 284 }); |
279 } | 285 } |
280 | 286 |
281 #pragma mark - | 287 #pragma mark - |
282 #pragma mark SyncObserverModelBridge | 288 #pragma mark SyncObserverModelBridge |
283 | 289 |
284 - (void)onSyncStateChanged { | 290 - (void)onSyncStateChanged { |
285 [self updateAllTopSitesSpotlightItems]; | 291 [self updateAllTopSitesSpotlightItems]; |
286 } | 292 } |
287 | 293 |
288 @end | 294 @end |
OLD | NEW |