1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/autofill/form_suggestion_controller.h"
#include "base/ios/ios_util.h"
#include "base/ios/weak_nsobject.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_block.h"
#include "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_field_trial_ios.h"
#include "components/autofill/core/browser/autofill_popup_delegate.h"
#import "components/autofill/ios/browser/form_suggestion.h"
#import "ios/chrome/browser/autofill/form_input_accessory_view_controller.h"
#import "ios/chrome/browser/autofill/form_suggestion_provider.h"
#import "ios/chrome/browser/autofill/form_suggestion_view.h"
#import "ios/chrome/browser/passwords/password_generation_utils.h"
#include "ios/chrome/browser/ui/ui_util.h"
#import "ios/web/public/url_scheme_util.h"
#import "ios/web/public/web_state/crw_web_view_proxy.h"
#import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
#import "ios/web/public/web_state/web_state.h"
namespace {
// Struct that describes suggestion state.
struct AutofillSuggestionState {
AutofillSuggestionState(const std::string& form_name,
const std::string& field_name,
const std::string& typed_value);
// The name of the form for autofill.
std::string form_name;
// The name of the field for autofill.
std::string field_name;
// The user-typed value in the field.
std::string typed_value;
// The suggestions for the form field. An array of |FormSuggestion|.
base::scoped_nsobject<NSArray> suggestions;
};
AutofillSuggestionState::AutofillSuggestionState(const std::string& form_name,
const std::string& field_name,
const std::string& typed_value)
: form_name(form_name), field_name(field_name), typed_value(typed_value) {
}
} // namespace
@interface FormSuggestionController () <FormInputAccessoryViewProvider> {
// Form navigation delegate.
base::WeakNSProtocol<id<FormInputAccessoryViewDelegate>> _delegate;
// Callback to update the accessory view.
base::mac::ScopedBlock<AccessoryViewReadyCompletion>
accessoryViewUpdateBlock_;
// Autofill suggestion state.
scoped_ptr<AutofillSuggestionState> _suggestionState;
// Providers for suggestions, sorted according to the order in which
// they should be asked for suggestions, with highest priority in front.
base::scoped_nsobject<NSArray> _suggestionProviders;
// Access to WebView from the CRWWebController.
base::scoped_nsprotocol<id<CRWWebViewProxy>> _webViewProxy;
}
// Returns an autoreleased input accessory view that shows |suggestions|.
- (UIView*)suggestionViewWithSuggestions:(NSArray*)suggestions;
// Updates keyboard for |suggestionState|.
- (void)updateKeyboard:(AutofillSuggestionState*)suggestionState;
// Updates keyboard with |suggestions|.
- (void)updateKeyboardWithSuggestions:(NSArray*)suggestions;
// Clears state in between page loads.
- (void)resetSuggestionState;
@end
@implementation FormSuggestionController {
// Bridge to observe the web state from Objective-C.
scoped_ptr<web::WebStateObserverBridge> _webStateObserverBridge;
// Manager for FormSuggestion JavaScripts.
base::scoped_nsobject<JsSuggestionManager> _jsSuggestionManager;
// The provider for the current set of suggestions.
id<FormSuggestionProvider> _provider; // weak
}
- (instancetype)initWithWebState:(web::WebState*)webState
providers:(NSArray*)providers
JsSuggestionManager:(JsSuggestionManager*)jsSuggestionManager {
self = [super init];
if (self) {
_webStateObserverBridge.reset(
new web::WebStateObserverBridge(webState, self));
_webViewProxy.reset([webState->GetWebViewProxy() retain]);
_jsSuggestionManager.reset([jsSuggestionManager retain]);
_suggestionProviders.reset([providers copy]);
}
return self;
}
- (instancetype)initWithWebState:(web::WebState*)webState
providers:(NSArray*)providers {
JsSuggestionManager* jsSuggestionManager =
base::mac::ObjCCast<JsSuggestionManager>(
[webState->GetJSInjectionReceiver()
instanceOfClass:[JsSuggestionManager class]]);
return [self initWithWebState:webState
providers:providers
JsSuggestionManager:jsSuggestionManager];
}
- (void)onNoSuggestionsAvailable {
}
- (void)detachFromWebState {
_webStateObserverBridge.reset();
}
#pragma mark -
#pragma mark CRWWebStateObserver
- (void)webStateDestroyed:(web::WebState*)webState {
[self detachFromWebState];
}
- (void)webStateDidLoadPage:(web::WebState*)webState {
[self processPage:webState];
}
- (void)processPage:(web::WebState*)webState {
[self resetSuggestionState];
web::URLVerificationTrustLevel trustLevel =
web::URLVerificationTrustLevel::kNone;
const GURL pageURL(webState->GetCurrentURL(&trustLevel));
if (trustLevel != web::URLVerificationTrustLevel::kAbsolute) {
DLOG(WARNING) << "Page load not handled on untrusted page";
return;
}
if (web::UrlHasWebScheme(pageURL) && webState->ContentIsHTML())
[_jsSuggestionManager inject];
}
- (void)setWebViewProxy:(id<CRWWebViewProxy>)webViewProxy {
_webViewProxy.reset([webViewProxy retain]);
}
- (void)retrieveSuggestionsForFormNamed:(const std::string&)formName
fieldName:(const std::string&)fieldName
type:(const std::string&)type
webState:(web::WebState*)webState {
base::WeakNSObject<FormSuggestionController> weakSelf(self);
base::scoped_nsobject<NSString> strongFormName(
[base::SysUTF8ToNSString(formName) copy]);
base::scoped_nsobject<NSString> strongFieldName(
[base::SysUTF8ToNSString(fieldName) copy]);
base::scoped_nsobject<NSString> strongType(
[base::SysUTF8ToNSString(type) copy]);
base::scoped_nsobject<NSString> strongValue(
[base::SysUTF8ToNSString(_suggestionState.get()->typed_value) copy]);
// Build a block for each provider that will invoke its completion with YES
// if the provider can provide suggestions for the specified form/field/type
// and NO otherwise.
base::scoped_nsobject<NSMutableArray> findProviderBlocks(
[[NSMutableArray alloc] init]);
for (NSUInteger i = 0; i < [_suggestionProviders count]; i++) {
base::mac::ScopedBlock<passwords::PipelineBlock> block(
^(void (^completion)(BOOL success)) {
// Access all the providers through |self| to guarantee that both
// |self| and all the providers exist when the block is executed.
// |_suggestionProviders| is immutable, so the subscripting is
// always valid.
base::scoped_nsobject<FormSuggestionController> strongSelf(
[weakSelf retain]);
if (!strongSelf)
return;
id<FormSuggestionProvider> provider =
strongSelf.get()->_suggestionProviders[i];
[provider checkIfSuggestionsAvailableForForm:strongFormName
field:strongFieldName
type:strongType
typedValue:strongValue
webState:webState
completionHandler:completion];
},
base::scoped_policy::RETAIN);
[findProviderBlocks addObject:block];
}
// Once the suggestions are retrieved, update the suggestions UI.
SuggestionsReadyCompletion readyCompletion =
^(NSArray* suggestions, id<FormSuggestionProvider> provider) {
[weakSelf onSuggestionsReady:suggestions provider:provider];
};
// Once a provider is found, use it to retrieve suggestions.
passwords::PipelineCompletionBlock completion = ^(NSUInteger providerIndex) {
if (providerIndex == NSNotFound) {
[weakSelf onNoSuggestionsAvailable];
return;
}
base::scoped_nsobject<FormSuggestionController> strongSelf(
[weakSelf retain]);
if (!strongSelf)
return;
id<FormSuggestionProvider> provider =
strongSelf.get()->_suggestionProviders[providerIndex];
[provider retrieveSuggestionsForForm:strongFormName
field:strongFieldName
type:strongType
typedValue:strongValue
webState:webState
completionHandler:readyCompletion];
};
// Run all the blocks in |findProviderBlocks| until one invokes its
// completion with YES. The first one to do so will be passed to
// |completion|.
passwords::RunSearchPipeline(findProviderBlocks, completion);
}
- (void)onSuggestionsReady:(NSArray*)suggestions
provider:(id<FormSuggestionProvider>)provider {
// TODO(ios): crbug.com/249916. If we can also pass in the form/field for
// which |suggestions| are, we should check here if |suggestions| are for
// the current active element. If not, reset |_suggestionState|.
if (!_suggestionState) {
// The suggestion state was reset in between the call to Autofill API (e.g.
// OnQueryFormFieldAutofill) and this method being called back. Results are
// therefore no longer relevant.
return;
}
_provider = provider;
_suggestionState->suggestions.reset([suggestions copy]);
[self updateKeyboard:_suggestionState.get()];
}
- (void)resetSuggestionState {
_provider = nil;
_suggestionState.reset();
}
- (void)clearSuggestions {
// Note that other parts of the suggestionsState are not reset.
if (!_suggestionState.get())
return;
_suggestionState->suggestions.reset([[NSArray alloc] init]);
[self updateKeyboard:_suggestionState.get()];
}
- (void)updateKeyboard:(AutofillSuggestionState*)suggestionState {
if (!suggestionState) {
if (accessoryViewUpdateBlock_)
accessoryViewUpdateBlock_.get()(nil, self);
} else {
[self updateKeyboardWithSuggestions:suggestionState->suggestions];
}
}
- (void)updateKeyboardWithSuggestions:(NSArray*)suggestions {
if (accessoryViewUpdateBlock_) {
accessoryViewUpdateBlock_.get()(
[self suggestionViewWithSuggestions:suggestions], self);
}
}
- (UIView*)suggestionViewWithSuggestions:(NSArray*)suggestions {
CGRect frame = [_webViewProxy keyboardAccessory].frame;
// Force the desired height on iPads running iOS 9 or later where the height
// of the inputAccessoryView is 0.
if (base::ios::IsRunningOnIOS9OrLater() && IsIPadIdiom()) {
frame.size.height = autofill::kInputAccessoryHeight;
}
base::scoped_nsobject<FormSuggestionView> view([[FormSuggestionView alloc]
initWithFrame:frame
client:self
suggestions:suggestions]);
return view.autorelease();
}
- (void)didSelectSuggestion:(FormSuggestion*)suggestion {
if (!_suggestionState)
return;
// Send the suggestion to the provider. Upon completion advance the cursor
// for single-field Autofill, or close the keyboard for full-form Autofill.
base::WeakNSObject<FormSuggestionController> weakSelf(self);
[_provider
didSelectSuggestion:suggestion
forField:base::SysUTF8ToNSString(_suggestionState->field_name)
form:base::SysUTF8ToNSString(_suggestionState->form_name)
completionHandler:^{
if (autofill::AutofillFieldTrialIOS::IsFullFormAutofillEnabled()) {
[[weakSelf accessoryViewDelegate] closeKeyboardWithoutButtonPress];
} else {
[[weakSelf accessoryViewDelegate]
selectNextElementWithoutButtonPress];
}
}];
_provider = nil;
}
- (id<FormInputAccessoryViewProvider>)accessoryViewProvider {
return self;
}
#pragma mark FormInputAccessoryViewProvider
- (id<FormInputAccessoryViewDelegate>)accessoryViewDelegate {
return _delegate.get();
}
- (void)setAccessoryViewDelegate:(id<FormInputAccessoryViewDelegate>)delegate {
_delegate.reset(delegate);
}
- (void)
checkIfAccessoryViewIsAvailableForFormNamed:(const std::string&)formName
fieldName:(const std::string&)fieldName
webState:(web::WebState*)webState
completionHandler:
(AccessoryViewAvailableCompletion)
completionHandler {
[self processPage:webState];
completionHandler(YES);
}
- (void)retrieveAccessoryViewForFormNamed:(const std::string&)formName
fieldName:(const std::string&)fieldName
value:(const std::string&)value
type:(const std::string&)type
webState:(web::WebState*)webState
accessoryViewUpdateBlock:
(AccessoryViewReadyCompletion)accessoryViewUpdateBlock {
_suggestionState.reset(
new AutofillSuggestionState(formName, fieldName, value));
accessoryViewUpdateBlock([self suggestionViewWithSuggestions:@[]], self);
accessoryViewUpdateBlock_.reset([accessoryViewUpdateBlock copy]);
[self retrieveSuggestionsForFormNamed:formName
fieldName:fieldName
type:type
webState:webState];
}
- (void)inputAccessoryViewControllerDidReset:
(FormInputAccessoryViewController*)controller {
accessoryViewUpdateBlock_.reset();
[self resetSuggestionState];
}
- (void)resizeAccessoryView {
[self updateKeyboard:_suggestionState.get()];
}
- (BOOL)getLogKeyboardAccessoryMetrics {
return YES;
}
@end
|