| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 #import "ios/clean/chrome/browser/ui/omnibox/location_bar_mediator.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "ios/chrome/browser/ui/toolbar/toolbar_model_delegate_ios.h" |
| 10 #include "ios/chrome/browser/ui/toolbar/toolbar_model_impl_ios.h" |
| 11 #include "ios/shared/chrome/browser/tabs/web_state_list.h" |
| 12 #include "ios/shared/chrome/browser/tabs/web_state_list_observer_bridge.h" |
| 13 #import "ios/shared/chrome/browser/ui/omnibox/location_bar_controller.h" |
| 14 #import "ios/web/public/navigation_manager.h" |
| 15 #import "ios/web/public/web_state/web_state.h" |
| 16 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
| 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
| 22 @interface LocationBarMediator ()<CRWWebStateObserver, |
| 23 LocationBarDelegate, |
| 24 WebStateListObserving> |
| 25 @property(nonatomic, readwrite, assign) WebStateList* webStateList; |
| 26 @end |
| 27 |
| 28 @implementation LocationBarMediator { |
| 29 // Observes the WebStateList so that this mediator can update the UI when the |
| 30 // active WebState changes. |
| 31 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 32 |
| 33 // Used to update the UI in response to WebState observer notifications. This |
| 34 // observer is always observing the currently-active WebState and may be |
| 35 // nullptr if no WebState is currently active. |
| 36 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; |
| 37 |
| 38 // The LocationBarController that wraps OmniboxViewIOS and |
| 39 // OmniboxTextFieldIOS. This mediator updates the UI through |_locationBar| |
| 40 // rather than through a consumer. |
| 41 std::unique_ptr<LocationBarController> _locationBar; |
| 42 |
| 43 // The ToolbarModelDelegate, backed by |_webStateList|. |
| 44 std::unique_ptr<ToolbarModelDelegateIOS> _toolbarModelDelegate; |
| 45 |
| 46 // The ToolbarModel that backs |_locationBar|. |
| 47 std::unique_ptr<ToolbarModelImplIOS> _toolbarModel; |
| 48 } |
| 49 |
| 50 @synthesize webStateList = _webStateList; |
| 51 |
| 52 - (instancetype)initWithWebStateList:(WebStateList*)webStateList { |
| 53 if ((self = [super init])) { |
| 54 _webStateList = webStateList; |
| 55 |
| 56 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
| 57 _webStateList->AddObserver(_webStateListObserver.get()); |
| 58 web::WebState* webState = _webStateList->GetActiveWebState(); |
| 59 if (webState) { |
| 60 _webStateObserver = |
| 61 base::MakeUnique<web::WebStateObserverBridge>(webState, self); |
| 62 } |
| 63 |
| 64 _toolbarModelDelegate = |
| 65 base::MakeUnique<ToolbarModelDelegateIOS>(webStateList); |
| 66 _toolbarModel = |
| 67 base::MakeUnique<ToolbarModelImplIOS>(_toolbarModelDelegate.get()); |
| 68 } |
| 69 return self; |
| 70 } |
| 71 |
| 72 - (void)dealloc { |
| 73 _webStateList->RemoveObserver(_webStateListObserver.get()); |
| 74 } |
| 75 |
| 76 - (void)setLocationBar:(std::unique_ptr<LocationBarController>)locationBar { |
| 77 _locationBar = std::move(locationBar); |
| 78 _locationBar->OnToolbarUpdated(); |
| 79 } |
| 80 |
| 81 #pragma mark - LocationBarDelegate |
| 82 |
| 83 - (void)loadGURLFromLocationBar:(const GURL&)url |
| 84 transition:(ui::PageTransition)transition { |
| 85 web::WebState* webState = _webStateList->GetActiveWebState(); |
| 86 DCHECK(webState); |
| 87 |
| 88 if (url.SchemeIs(url::kJavaScriptScheme)) { |
| 89 // TODO(crbug.com/708341): Percent-unescape the url content first. |
| 90 webState->ExecuteJavaScript(base::UTF8ToUTF16(url.GetContent())); |
| 91 return; |
| 92 } else { |
| 93 // TODO(crbug.com/708341): Use WebState::OpenURL() here, because that method |
| 94 // can handle different WindowOpenDispositions. The code below assumes |
| 95 // CURRENT_TAB. |
| 96 |
| 97 // When opening a URL, force the omnibox to resign first responder. This |
| 98 // will also close the popup. |
| 99 web::NavigationManager::WebLoadParams loadParams(url); |
| 100 loadParams.transition_type = transition; |
| 101 loadParams.is_renderer_initiated = false; |
| 102 webState->GetNavigationManager()->LoadURLWithParams(loadParams); |
| 103 } |
| 104 |
| 105 _locationBar->HideKeyboardAndEndEditing(); |
| 106 _locationBar->OnToolbarUpdated(); |
| 107 } |
| 108 |
| 109 - (void)locationBarHasBecomeFirstResponder { |
| 110 // TODO(crbug.com/708341): Implement this method or edit this comment with an |
| 111 // explanation of what this method needs to do. |
| 112 } |
| 113 |
| 114 - (void)locationBarHasResignedFirstResponder { |
| 115 // TODO(crbug.com/708341): Implement this method or edit this comment with an |
| 116 // explanation of what this method needs to do. |
| 117 } |
| 118 |
| 119 - (void)locationBarBeganEdit { |
| 120 // TODO(crbug.com/708341): Implement this method or edit this comment with an |
| 121 // explanation of what this method needs to do. |
| 122 } |
| 123 |
| 124 - (void)locationBarChanged { |
| 125 // TODO(crbug.com/708341): Implement this method or edit this comment with an |
| 126 // explanation of what this method needs to do. |
| 127 } |
| 128 |
| 129 - (web::WebState*)getWebState { |
| 130 return _webStateList->GetActiveWebState(); |
| 131 } |
| 132 |
| 133 - (ToolbarModel*)toolbarModel { |
| 134 return _toolbarModel->GetToolbarModel(); |
| 135 } |
| 136 |
| 137 #pragma mark - WebStateListObserver |
| 138 |
| 139 - (void)webStateList:(WebStateList*)webStateList |
| 140 didChangeActiveWebState:(web::WebState*)newWebState |
| 141 oldWebState:(web::WebState*)oldWebState |
| 142 atIndex:(int)atIndex |
| 143 userAction:(BOOL)userAction { |
| 144 // If the omnibox is focused, force it to resign first responder when the |
| 145 // active tab changes. |
| 146 _locationBar->HideKeyboardAndEndEditing(); |
| 147 |
| 148 if (newWebState) { |
| 149 _webStateObserver = |
| 150 base::MakeUnique<web::WebStateObserverBridge>(newWebState, self); |
| 151 } else { |
| 152 _webStateObserver = nullptr; |
| 153 } |
| 154 |
| 155 _locationBar->OnToolbarUpdated(); |
| 156 } |
| 157 |
| 158 #pragma mark - WebStateObserver |
| 159 |
| 160 // WebState navigation events that could potentially affect the contents of the |
| 161 // omnibox are caught below and used to drive updates to the UI. |
| 162 |
| 163 - (void)webState:(web::WebState*)webState |
| 164 didStartProvisionalNavigationForURL:(const GURL&)URL { |
| 165 _locationBar->OnToolbarUpdated(); |
| 166 } |
| 167 |
| 168 - (void)webState:(web::WebState*)webState |
| 169 didCommitNavigationWithDetails: |
| 170 (const web::LoadCommittedDetails&)load_details { |
| 171 _locationBar->OnToolbarUpdated(); |
| 172 } |
| 173 |
| 174 - (void)webState:(web::WebState*)webState |
| 175 didFinishNavigation:(web::NavigationContext*)navigation { |
| 176 _locationBar->OnToolbarUpdated(); |
| 177 } |
| 178 |
| 179 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 180 _locationBar->OnToolbarUpdated(); |
| 181 } |
| 182 |
| 183 - (void)webStateDidChangeVisibleSecurityState:(web::WebState*)webState { |
| 184 _locationBar->OnToolbarUpdated(); |
| 185 } |
| 186 |
| 187 - (void)webStateDestroyed:(web::WebState*)webState { |
| 188 _webStateObserver = nullptr; |
| 189 } |
| 190 |
| 191 @end |
| OLD | NEW |