OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 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/chrome/browser/passwords/js_password_manager.h" | |
6 | |
7 #include "base/json/string_escape.h" | |
8 #include "base/logging.h" | |
9 #include "base/strings/sys_string_conversions.h" | |
10 | |
11 namespace { | |
12 // Santizies |str| and wraps it in quotes so it can be injected safely in | |
13 // JavaScript. | |
14 NSString* JSONEscape(NSString* str) { | |
Eugene But (OOO till 7-30)
2015/11/18 17:06:48
NIT: s/str/JSON here and in the comment
vabr (Chromium)
2015/11/19 10:02:27
Also here, I do not understand why the capitalisat
vabr (Chromium)
2015/11/19 16:45:02
Change to JSONString to match the others.
| |
15 return base::SysUTF8ToNSString( | |
16 base::GetQuotedJSONString(base::SysNSStringToUTF8(str))); | |
17 } | |
18 } // namespace | |
19 | |
20 @interface JsPasswordManager () | |
21 // Injects a script that does two things: | |
22 // 1. Injects password controller JavaScript in the page. | |
23 // 2. Extracts the _submitted_ password form data from the DOM on the page. | |
24 // The result is returned in |completionHandler|. | |
25 // |completionHandler| cannot be nil. | |
26 - (void)evaluateExtraScript:(NSString*)script | |
27 completionHandler:(void (^)(NSString*))completionHandler; | |
28 @end | |
29 | |
30 @implementation JsPasswordManager | |
31 | |
32 - (void)findPasswordFormsWithCompletionHandler: | |
33 (void (^)(NSString*))completionHandler { | |
34 DCHECK(completionHandler); | |
35 [self evaluateExtraScript:@"__gCrWeb.findPasswordForms()" | |
36 completionHandler:completionHandler]; | |
37 } | |
38 | |
39 - (void)extractForm:(NSString*)formName | |
40 completionHandler:(void (^)(NSString*))completionHandler { | |
Eugene But (OOO till 7-30)
2015/11/18 17:06:48
Indent 4 spaces
vabr (Chromium)
2015/11/19 10:02:27
Cannot do, it conflicts with git cl format, and th
Eugene But (OOO till 7-30)
2015/11/19 15:13:21
This is a bug in clang format and presubmit messag
vabr (Chromium)
2015/11/19 16:45:02
Fair enough, done.
| |
41 DCHECK(completionHandler); | |
42 NSString* extra = [NSString | |
43 stringWithFormat:@"__gCrWeb.getPasswordForm(%@)", JSONEscape(formName)]; | |
44 [self evaluateExtraScript:extra completionHandler:completionHandler]; | |
45 } | |
46 | |
47 - (void)fillPasswordForm:(NSString*)jsonString | |
48 withUsername:(NSString*)username | |
49 password:(NSString*)password | |
50 completionHandler:(void (^)(BOOL))completionHandler { | |
51 DCHECK(completionHandler); | |
52 NSString* script = [NSString | |
53 stringWithFormat:@"__gCrWeb.fillPasswordForm(%@, %@, %@)", jsonString, | |
54 JSONEscape(username), JSONEscape(password)]; | |
55 [self evaluate:script | |
56 stringResultHandler:^(NSString* result, NSError* error) { | |
57 completionHandler(!error && [result isEqualToString:@"true"]); | |
58 }]; | |
59 } | |
60 | |
61 - (void)clearAutofilledPasswordsInForm:(NSString*)formName | |
62 completionHandler:(void (^)(BOOL))completionHandler { | |
63 NSString* script = | |
64 [NSString stringWithFormat:@"__gCrWeb.clearAutofilledPasswords(%@)", | |
65 JSONEscape(formName)]; | |
66 [self evaluate:script | |
67 stringResultHandler:^(NSString* result, NSError* error) { | |
68 completionHandler(!error && [result isEqualToString:@"true"]); | |
69 }]; | |
70 } | |
71 | |
72 - (void)fillPasswordForm:(NSString*)formName | |
73 withGeneratedPassword:(NSString*)password | |
74 completionHandler:(void (^)(BOOL))completionHandler { | |
75 NSString* script = | |
76 [NSString stringWithFormat: | |
77 @"__gCrWeb.fillPasswordFormWithGeneratedPassword(%@, %@)", | |
78 JSONEscape(formName), JSONEscape(password)]; | |
79 [self evaluate:script | |
80 stringResultHandler:^(NSString* result, NSError* error) { | |
81 if (completionHandler) | |
82 completionHandler(!error && [result isEqualToString:@"true"]); | |
83 }]; | |
84 } | |
85 | |
86 #pragma mark - | |
87 #pragma mark ProtectedMethods | |
88 | |
89 - (NSString*)scriptPath { | |
90 return @"password_controller"; | |
91 } | |
92 | |
93 #pragma mark - | |
94 #pragma mark Private | |
95 | |
96 - (void)evaluateExtraScript:(NSString*)script | |
97 completionHandler:(void (^)(NSString*))completionHandler { | |
98 DCHECK(completionHandler); | |
99 [self injectDependenciesIfMissing]; | |
100 NSString* js = [[self injectionContent] stringByAppendingString:script]; | |
Eugene But (OOO till 7-30)
2015/11/18 17:06:48
s/js/JS
vabr (Chromium)
2015/11/19 10:02:27
Same push-back as for jsonString in ios/chrome/bro
vabr (Chromium)
2015/11/19 16:45:02
Done.
| |
101 [self evaluate:js | |
102 stringResultHandler:^(NSString* result, NSError*) { | |
103 completionHandler(result); | |
104 }]; | |
105 } | |
106 | |
107 @end | |
OLD | NEW |