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/tab_switcher/tab_switcher_model.h" | 5 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_model.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "components/browser_sync/profile_sync_service.h" | 10 #include "components/browser_sync/profile_sync_service.h" |
11 #include "components/sessions/core/session_id.h" | 11 #include "components/sessions/core/session_id.h" |
12 #include "components/signin/core/browser/signin_manager.h" | 12 #include "components/signin/core/browser/signin_manager.h" |
13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
14 #include "ios/chrome/browser/signin/signin_manager_factory.h" | 14 #include "ios/chrome/browser/signin/signin_manager_factory.h" |
15 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" | 15 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
16 #include "ios/chrome/browser/sync/sync_setup_service.h" | 16 #include "ios/chrome/browser/sync/sync_setup_service.h" |
17 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" | 17 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" |
18 #import "ios/chrome/browser/tabs/tab.h" | 18 #import "ios/chrome/browser/tabs/tab.h" |
19 #import "ios/chrome/browser/tabs/tab_model.h" | 19 #import "ios/chrome/browser/tabs/tab_model.h" |
20 #include "ios/chrome/browser/ui/ntp/recent_tabs/synced_sessions.h" | 20 #include "ios/chrome/browser/ui/ntp/recent_tabs/synced_sessions.h" |
21 #import "ios/chrome/browser/ui/tab_switcher/session_changes.h" | 21 #import "ios/chrome/browser/ui/tab_switcher/session_changes.h" |
22 #import "ios/chrome/browser/ui/tab_switcher/tab_model_snapshot.h" | 22 #import "ios/chrome/browser/ui/tab_switcher/tab_model_snapshot.h" |
23 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_cache.h" | 23 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_cache.h" |
24 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_model_private.h" | 24 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_model_private.h" |
25 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_panel_cell.h" | 25 #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_panel_cell.h" |
26 | 26 |
27 namespace ios_internal { | 27 bool TabSwitcherSessionTypeIsLocalSession(TabSwitcherSessionType sessionType) { |
28 | 28 return sessionType == TabSwitcherSessionType::OFF_THE_RECORD_SESSION || |
29 bool IsLocalSession(SessionType sessionType) { | 29 sessionType == TabSwitcherSessionType::REGULAR_SESSION; |
30 return sessionType == SessionType::OFF_THE_RECORD_SESSION || | |
31 sessionType == SessionType::REGULAR_SESSION; | |
32 } | 30 } |
33 | 31 |
34 } // namespace ios_internal | |
35 | |
36 namespace { | 32 namespace { |
37 | 33 |
38 class TagAndIndex { | 34 class TagAndIndex { |
39 public: | 35 public: |
40 TagAndIndex(std::string const& tag, size_t index) | 36 TagAndIndex(std::string const& tag, size_t index) |
41 : tag_(tag), index_(index) {} | 37 : tag_(tag), index_(index) {} |
42 std::string tag_; | 38 std::string tag_; |
43 size_t index_; | 39 size_t index_; |
44 bool operator<(const TagAndIndex& other) const { return tag_ < other.tag_; } | 40 bool operator<(const TagAndIndex& other) const { return tag_ < other.tag_; } |
45 }; | 41 }; |
(...skipping 24 matching lines...) Expand all Loading... |
70 _syncedSessionsObserver; | 66 _syncedSessionsObserver; |
71 // Snapshots of the |_mainTabModel| and |_otrTabModel|. | 67 // Snapshots of the |_mainTabModel| and |_otrTabModel|. |
72 std::unique_ptr<TabModelSnapshot> _mainTabModelSnapshot; | 68 std::unique_ptr<TabModelSnapshot> _mainTabModelSnapshot; |
73 std::unique_ptr<TabModelSnapshot> _otrTabModelSnapshot; | 69 std::unique_ptr<TabModelSnapshot> _otrTabModelSnapshot; |
74 // The cache holding resized tabs snapshots. | 70 // The cache holding resized tabs snapshots. |
75 base::scoped_nsobject<TabSwitcherCache> _cache; | 71 base::scoped_nsobject<TabSwitcherCache> _cache; |
76 } | 72 } |
77 | 73 |
78 // Returns the type of the local session corresponding to the given |tabModel|. | 74 // Returns the type of the local session corresponding to the given |tabModel|. |
79 // |tabModel| MUST be equal to either |_mainTabModel|, or |_otrTabModel|. | 75 // |tabModel| MUST be equal to either |_mainTabModel|, or |_otrTabModel|. |
80 - (ios_internal::SessionType)typeOfLocalSessionForTabModel:(TabModel*)tabModel; | 76 - (TabSwitcherSessionType)typeOfLocalSessionForTabModel:(TabModel*)tabModel; |
81 @end | 77 @end |
82 | 78 |
83 @implementation TabSwitcherModel | 79 @implementation TabSwitcherModel |
84 | 80 |
85 @synthesize mainTabModel = _mainTabModel; | 81 @synthesize mainTabModel = _mainTabModel; |
86 @synthesize otrTabModel = _otrTabModel; | 82 @synthesize otrTabModel = _otrTabModel; |
87 | 83 |
88 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState | 84 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState |
89 delegate:(id<TabSwitcherModelDelegate>)delegate | 85 delegate:(id<TabSwitcherModelDelegate>)delegate |
90 mainTabModel:(TabModel*)mainTabModel | 86 mainTabModel:(TabModel*)mainTabModel |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 142 } |
147 | 143 |
148 - (NSInteger)distantSessionCount { | 144 - (NSInteger)distantSessionCount { |
149 return _syncedSessions->GetSessionCount(); | 145 return _syncedSessions->GetSessionCount(); |
150 } | 146 } |
151 | 147 |
152 - (ios::ChromeBrowserState*)browserState { | 148 - (ios::ChromeBrowserState*)browserState { |
153 return _browserState; | 149 return _browserState; |
154 } | 150 } |
155 | 151 |
156 - (TabModel*)tabModelForSessionOfType:(ios_internal::SessionType)type { | 152 - (TabModel*)tabModelForSessionOfType:(TabSwitcherSessionType)type { |
157 DCHECK(type == ios_internal::SessionType::OFF_THE_RECORD_SESSION || | 153 DCHECK(type == TabSwitcherSessionType::OFF_THE_RECORD_SESSION || |
158 type == ios_internal::SessionType::REGULAR_SESSION); | 154 type == TabSwitcherSessionType::REGULAR_SESSION); |
159 return type == ios_internal::SessionType::OFF_THE_RECORD_SESSION | 155 return type == TabSwitcherSessionType::OFF_THE_RECORD_SESSION ? _otrTabModel |
160 ? _otrTabModel | 156 : _mainTabModel; |
161 : _mainTabModel; | |
162 } | 157 } |
163 | 158 |
164 - (NSInteger)numberOfTabsInLocalSessionOfType:(ios_internal::SessionType)type { | 159 - (NSInteger)numberOfTabsInLocalSessionOfType:(TabSwitcherSessionType)type { |
165 TabModelSnapshot* tabModelSnapshot = [self tabModelSnapshotForSession:type]; | 160 TabModelSnapshot* tabModelSnapshot = [self tabModelSnapshotForSession:type]; |
166 return tabModelSnapshot->tabs().size(); | 161 return tabModelSnapshot->tabs().size(); |
167 } | 162 } |
168 | 163 |
169 - (Tab*)tabAtIndex:(NSUInteger)index | 164 - (Tab*)tabAtIndex:(NSUInteger)index |
170 inLocalSessionOfType:(ios_internal::SessionType)type { | 165 inLocalSessionOfType:(TabSwitcherSessionType)type { |
171 TabModelSnapshot* tabModelSnapshot = [self tabModelSnapshotForSession:type]; | 166 TabModelSnapshot* tabModelSnapshot = [self tabModelSnapshotForSession:type]; |
172 return tabModelSnapshot->tabs()[index]; | 167 return tabModelSnapshot->tabs()[index]; |
173 } | 168 } |
174 | 169 |
175 - (std::unique_ptr<TabModelSnapshot>)tabModelSnapshotForLocalSession: | 170 - (std::unique_ptr<TabModelSnapshot>)tabModelSnapshotForLocalSession: |
176 (ios_internal::SessionType)type { | 171 (TabSwitcherSessionType)type { |
177 TabModel* tm = nullptr; | 172 TabModel* tm = nullptr; |
178 switch (type) { | 173 switch (type) { |
179 case ios_internal::SessionType::OFF_THE_RECORD_SESSION: | 174 case TabSwitcherSessionType::OFF_THE_RECORD_SESSION: |
180 tm = _otrTabModel; | 175 tm = _otrTabModel; |
181 break; | 176 break; |
182 case ios_internal::SessionType::REGULAR_SESSION: | 177 case TabSwitcherSessionType::REGULAR_SESSION: |
183 tm = _mainTabModel; | 178 tm = _mainTabModel; |
184 break; | 179 break; |
185 default: | 180 default: |
186 NOTREACHED(); | 181 NOTREACHED(); |
187 break; | 182 break; |
188 } | 183 } |
189 return base::MakeUnique<TabModelSnapshot>(tm); | 184 return base::MakeUnique<TabModelSnapshot>(tm); |
190 } | 185 } |
191 | 186 |
192 - (std::unique_ptr<const synced_sessions::DistantSession>)distantSessionForTag: | 187 - (std::unique_ptr<const synced_sessions::DistantSession>)distantSessionForTag: |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 std::set<TagAndIndex> tagsOfOtherSessions; | 304 std::set<TagAndIndex> tagsOfOtherSessions; |
310 std::set_intersection( | 305 std::set_intersection( |
311 tagsOfNewSessions.begin(), tagsOfNewSessions.end(), | 306 tagsOfNewSessions.begin(), tagsOfNewSessions.end(), |
312 tagsOfOldSessions.begin(), tagsOfOldSessions.end(), | 307 tagsOfOldSessions.begin(), tagsOfOldSessions.end(), |
313 std::inserter(tagsOfOtherSessions, tagsOfOtherSessions.end())); | 308 std::inserter(tagsOfOtherSessions, tagsOfOtherSessions.end())); |
314 for (TagAndIndex const& tagAndIndexOfSession : tagsOfOtherSessions) { | 309 for (TagAndIndex const& tagAndIndexOfSession : tagsOfOtherSessions) { |
315 [delegate distantSessionMayNeedUpdate:tagAndIndexOfSession.tag_]; | 310 [delegate distantSessionMayNeedUpdate:tagAndIndexOfSession.tag_]; |
316 } | 311 } |
317 } | 312 } |
318 | 313 |
319 - (ios_internal::SessionType)typeOfLocalSessionForTabModel:(TabModel*)tabModel { | 314 - (TabSwitcherSessionType)typeOfLocalSessionForTabModel:(TabModel*)tabModel { |
320 DCHECK(tabModel == _mainTabModel || tabModel == _otrTabModel); | 315 DCHECK(tabModel == _mainTabModel || tabModel == _otrTabModel); |
321 if (tabModel == _otrTabModel) | 316 if (tabModel == _otrTabModel) |
322 return ios_internal::SessionType::OFF_THE_RECORD_SESSION; | 317 return TabSwitcherSessionType::OFF_THE_RECORD_SESSION; |
323 return ios_internal::SessionType::REGULAR_SESSION; | 318 return TabSwitcherSessionType::REGULAR_SESSION; |
324 } | 319 } |
325 | 320 |
326 - (TabModelSnapshot*)tabModelSnapshotForSession: | 321 - (TabModelSnapshot*)tabModelSnapshotForSession:(TabSwitcherSessionType)type { |
327 (ios_internal::SessionType)type { | |
328 switch (type) { | 322 switch (type) { |
329 case ios_internal::SessionType::OFF_THE_RECORD_SESSION: | 323 case TabSwitcherSessionType::OFF_THE_RECORD_SESSION: |
330 return _otrTabModelSnapshot.get(); | 324 return _otrTabModelSnapshot.get(); |
331 case ios_internal::SessionType::REGULAR_SESSION: | 325 case TabSwitcherSessionType::REGULAR_SESSION: |
332 return _mainTabModelSnapshot.get(); | 326 return _mainTabModelSnapshot.get(); |
333 default: | 327 default: |
334 NOTREACHED(); | 328 NOTREACHED(); |
335 return nullptr; | 329 return nullptr; |
336 break; | 330 break; |
337 } | 331 } |
338 } | 332 } |
339 | 333 |
340 - (void)setTabModelSnapshot:(std::unique_ptr<TabModelSnapshot>)tabModelSnapshot | 334 - (void)setTabModelSnapshot:(std::unique_ptr<TabModelSnapshot>)tabModelSnapshot |
341 forSession:(ios_internal::SessionType)type { | 335 forSession:(TabSwitcherSessionType)type { |
342 switch (type) { | 336 switch (type) { |
343 case ios_internal::SessionType::OFF_THE_RECORD_SESSION: | 337 case TabSwitcherSessionType::OFF_THE_RECORD_SESSION: |
344 _otrTabModelSnapshot = std::move(tabModelSnapshot); | 338 _otrTabModelSnapshot = std::move(tabModelSnapshot); |
345 break; | 339 break; |
346 case ios_internal::SessionType::REGULAR_SESSION: | 340 case TabSwitcherSessionType::REGULAR_SESSION: |
347 _mainTabModelSnapshot = std::move(tabModelSnapshot); | 341 _mainTabModelSnapshot = std::move(tabModelSnapshot); |
348 break; | 342 break; |
349 default: | 343 default: |
350 NOTREACHED(); | 344 NOTREACHED(); |
351 break; | 345 break; |
352 } | 346 } |
353 } | 347 } |
354 | 348 |
355 - (void)tabModelChanged:(TabModel*)tabModel { | 349 - (void)tabModelChanged:(TabModel*)tabModel { |
356 ios_internal::SessionType sessionType = | 350 TabSwitcherSessionType sessionType = |
357 [self typeOfLocalSessionForTabModel:tabModel]; | 351 [self typeOfLocalSessionForTabModel:tabModel]; |
358 [_delegate localSessionMayNeedUpdate:sessionType]; | 352 [_delegate localSessionMayNeedUpdate:sessionType]; |
359 } | 353 } |
360 | 354 |
361 #pragma mark - SyncedSessionsObserver | 355 #pragma mark - SyncedSessionsObserver |
362 | 356 |
363 - (void)reloadSessions { | 357 - (void)reloadSessions { |
364 [self syncedSessionsChanged]; | 358 [self syncedSessionsChanged]; |
365 } | 359 } |
366 | 360 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 [self tabModelChanged:model]; | 395 [self tabModelChanged:model]; |
402 } | 396 } |
403 | 397 |
404 - (void)tabModel:(TabModel*)model | 398 - (void)tabModel:(TabModel*)model |
405 didChangeTabSnapshot:(Tab*)tab | 399 didChangeTabSnapshot:(Tab*)tab |
406 withImage:(UIImage*)image { | 400 withImage:(UIImage*)image { |
407 [self tabModelChanged:model]; | 401 [self tabModelChanged:model]; |
408 } | 402 } |
409 | 403 |
410 @end | 404 @end |
OLD | NEW |