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