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 // ====== New Architecture ===== |
| 6 // = This code is only used in the new iOS Chrome architecture. = |
| 7 // ============================================================================ |
| 8 |
| 9 #import "ios/chrome/browser/ui/tab_grid/tab_grid_coordinator.h" |
| 10 |
| 11 #include <memory> |
| 12 |
| 13 #include "base/strings/sys_string_conversions.h" |
| 14 #import "ios/chrome/browser/browser_coordinator+internal.h" |
| 15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 16 #import "ios/chrome/browser/ui/settings/settings_coordinator.h" |
| 17 #import "ios/chrome/browser/ui/tab/tab_coordinator.h" |
| 18 #import "ios/chrome/browser/ui/tab_grid/tab_grid_view_controller.h" |
| 19 #import "ios/web/public/navigation_manager.h" |
| 20 #include "ios/web/public/web_state/web_state.h" |
| 21 #import "net/base/mac/url_conversions.h" |
| 22 #include "ui/base/page_transition_types.h" |
| 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
| 28 @interface TabGridCoordinator ()<TabGridDataSource, |
| 29 TabGridActionDelegate, |
| 30 SettingsActionDelegate> |
| 31 @property(nonatomic, strong) TabGridViewController* viewController; |
| 32 @property(nonatomic, weak) SettingsCoordinator* settingsCoordinator; |
| 33 @end |
| 34 |
| 35 @implementation TabGridCoordinator { |
| 36 std::unique_ptr<web::WebState> _placeholderWebState; |
| 37 } |
| 38 |
| 39 @synthesize viewController = _viewController; |
| 40 @synthesize settingsCoordinator = _settingsCoordinator; |
| 41 |
| 42 #pragma mark - BrowserCoordinator |
| 43 |
| 44 - (void)start { |
| 45 self.viewController = [[TabGridViewController alloc] init]; |
| 46 self.viewController.dataSource = self; |
| 47 self.viewController.actionDelegate = self; |
| 48 |
| 49 // |rootViewController| is nullable, so this is by design a no-op if it hasn't |
| 50 // been set. This may be true in a unit test, or if this coordinator is being |
| 51 // used as a root coordinator. |
| 52 [self.rootViewController presentViewController:self.viewController |
| 53 animated:YES |
| 54 completion:nil]; |
| 55 } |
| 56 |
| 57 #pragma mark - TabGridDataSource |
| 58 |
| 59 - (NSUInteger)numberOfTabsInTabGrid { |
| 60 return 1; |
| 61 } |
| 62 |
| 63 - (NSString*)titleAtIndex:(NSInteger)index { |
| 64 // Placeholder implementation: ignore |index| and return the placeholder |
| 65 // web state, lazily creating it if needed. |
| 66 if (!_placeholderWebState.get()) { |
| 67 web::WebState::CreateParams webStateCreateParams(self.browserState); |
| 68 _placeholderWebState = web::WebState::Create(webStateCreateParams); |
| 69 _placeholderWebState->SetWebUsageEnabled(true); |
| 70 } |
| 71 GURL url = _placeholderWebState.get()->GetVisibleURL(); |
| 72 NSString* urlText = @"<New Tab>"; |
| 73 if (!url.is_valid()) { |
| 74 urlText = base::SysUTF8ToNSString(url.spec()); |
| 75 } |
| 76 return urlText; |
| 77 } |
| 78 |
| 79 #pragma mark - TabGridActionDelegate |
| 80 |
| 81 - (void)showTabAtIndexPath:(NSIndexPath*)indexPath { |
| 82 DCHECK(_placeholderWebState); |
| 83 |
| 84 TabCoordinator* tabCoordinator = [[TabCoordinator alloc] init]; |
| 85 tabCoordinator.webState = _placeholderWebState.get(); |
| 86 tabCoordinator.presentationKey = indexPath; |
| 87 [self addChildCoordinator:tabCoordinator]; |
| 88 [tabCoordinator start]; |
| 89 } |
| 90 |
| 91 - (void)showTabGrid { |
| 92 // This object should only ever have at most one child. |
| 93 DCHECK_LE(self.children.count, 1UL); |
| 94 BrowserCoordinator* child = [self.children anyObject]; |
| 95 [child stop]; |
| 96 [self removeChildCoordinator:child]; |
| 97 } |
| 98 |
| 99 #pragma mark - TabGridActionDelegate |
| 100 |
| 101 - (void)showSettings { |
| 102 SettingsCoordinator* settingsCoordinator = [[SettingsCoordinator alloc] init]; |
| 103 settingsCoordinator.actionDelegate = self; |
| 104 [self addOverlayCoordinator:settingsCoordinator]; |
| 105 self.settingsCoordinator = settingsCoordinator; |
| 106 [settingsCoordinator start]; |
| 107 } |
| 108 |
| 109 #pragma mark - SettingsActionDelegate |
| 110 |
| 111 - (void)closeSettings { |
| 112 [self.settingsCoordinator stop]; |
| 113 [self.settingsCoordinator.parentCoordinator |
| 114 removeChildCoordinator:self.settingsCoordinator]; |
| 115 // self.settingsCoordinator should be presumed to be nil after this point. |
| 116 } |
| 117 |
| 118 #pragma mark - URLOpening |
| 119 |
| 120 - (void)openURL:(NSURL*)URL { |
| 121 [self.overlayCoordinator stop]; |
| 122 [self removeOverlayCoordinator]; |
| 123 web::NavigationManager::WebLoadParams params(net::GURLWithNSURL(URL)); |
| 124 params.transition_type = ui::PAGE_TRANSITION_LINK; |
| 125 _placeholderWebState->GetNavigationManager()->LoadURLWithParams(params); |
| 126 if (!self.children.count) { |
| 127 // Placeholder — since there's only one tab in the grid, just open |
| 128 // the tab at index path (0,0). |
| 129 [self showTabAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 130 } |
| 131 } |
| 132 |
| 133 @end |
OLD | NEW |