Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: ios/clean/chrome/browser/ui/omnibox/location_bar_mediator.mm

Issue 2761343002: [ios] Adds LocationBarCoordinator. (Closed)
Patch Set: Rebased. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
marq (ping after 24h) 2017/04/05 09:07:18 Are these to-be-implemented?
rohitrao (ping after 24h) 2017/04/05 14:37:04 These are all TODOs. I don't know what needs to g
111 - (void)locationBarHasResignedFirstResponder {
112 }
113 - (void)locationBarBeganEdit {
114 }
115 - (void)locationBarChanged {
116 }
117
118 - (web::WebState*)getWebState {
119 return _webStateList->GetActiveWebState();
120 }
121
122 - (ToolbarModel*)toolbarModel {
123 return _toolbarModel->GetToolbarModel();
124 }
125
126 #pragma mark - WebStateListObserver
127
128 - (void)webStateList:(WebStateList*)webStateList
129 didChangeActiveWebState:(web::WebState*)newWebState
130 oldWebState:(web::WebState*)oldWebState
131 atIndex:(int)atIndex
132 userAction:(BOOL)userAction {
133 // If the omnibox is focused, force it to resign first responder when the
134 // active tab changes.
135 _locationBar->HideKeyboardAndEndEditing();
136
137 if (newWebState) {
138 _webStateObserver =
139 base::MakeUnique<web::WebStateObserverBridge>(newWebState, self);
140 } else {
141 _webStateObserver = nullptr;
142 }
143
144 _locationBar->OnToolbarUpdated();
145 }
146
147 #pragma mark - WebStateObserver
148
149 // WebState navigation events that could potentially affect the contents of the
150 // omnibox are caught below and used to drive updates to the UI.
151
152 - (void)webState:(web::WebState*)webState
153 didStartProvisionalNavigationForURL:(const GURL&)URL {
154 _locationBar->OnToolbarUpdated();
155 }
156
157 - (void)webState:(web::WebState*)webState
158 didCommitNavigationWithDetails:
159 (const web::LoadCommittedDetails&)load_details {
160 _locationBar->OnToolbarUpdated();
161 }
162
163 - (void)webState:(web::WebState*)webState
164 didFinishNavigation:(web::NavigationContext*)navigation {
165 _locationBar->OnToolbarUpdated();
166 }
167
168 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
169 _locationBar->OnToolbarUpdated();
170 }
171
172 - (void)webStateDidChangeVisibleSecurityState:(web::WebState*)webState {
173 _locationBar->OnToolbarUpdated();
174 }
175
176 - (void)webStateDestroyed:(web::WebState*)webState {
177 _webStateObserver = nullptr;
178 }
179
180 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698