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

Side by Side Diff: ios/chrome/browser/passwords/js_credential_manager.mm

Issue 1456983002: Move JS-related password manager code upstream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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 2015 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_credential_manager.h"
6
7 #include "base/json/json_writer.h"
8 #include "base/json/string_escape.h"
9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/values.h"
12 #import "ios/web/web_state/js/credential_util.h"
Eugene But (OOO till 7-30) 2015/11/18 17:06:48 s/import/include
vabr (Chromium) 2015/11/19 10:02:27 Done.
13
14 namespace {
15
16 // Sanitizes |JSON| and wraps it in quotes so it can be injected safely in
17 // JavaScript.
18 NSString* JSONEscape(NSString* JSON) {
19 return base::SysUTF8ToNSString(
20 base::GetQuotedJSONString(base::SysNSStringToUTF8(JSON)));
21 }
22
23 } // namespace
24
25 namespace ios {
26 namespace passwords {
27 const char kPendingRequestErrorType[] = "PendingRequestError";
28 const char kPendingRequestErrorMessage[] =
29 "There is already an outstanding request";
30 const char kSecurityErrorType[] = "SecurityError";
31 const char kPasswordStoreUnavailableErrorType[] =
32 "PasswordStoreUnavailableError";
33 const char kPasswordStoreUnavailableErrorMessage[] =
34 "The password store is unavailable";
35 const char kSecurityErrorMessageUntrustedOrigin[] = "The origin is untrusted";
36 } // namespace passwords
37 } // namespace ios
38
39 @interface JSCredentialManager ()
40
41 // Evaluates the JavaScript in |script|, which should evaluate to a JavaScript
42 // boolean value. That value will be passed to |completionHandler|.
43 - (void)evaluateScript:(NSString*)script
44 completionHandler:(void (^)(BOOL))completionHandler;
45
46 @end
47
48 @implementation JSCredentialManager
49
50 - (void)resolvePromiseWithRequestID:(NSInteger)requestID
51 credential:(const web::Credential&)credential
52 completionHandler:(void (^)(BOOL))completionHandler {
53 base::DictionaryValue credentialData;
54 web::CredentialToDictionaryValue(credential, &credentialData);
55 std::string credentialDataJSON;
56 base::JSONWriter::Write(credentialData, &credentialDataJSON);
57 NSString* script = [NSString
58 stringWithFormat:@"__gCrWeb['credentialManager'].resolve(%ld, %@)",
59 static_cast<long>(requestID),
60 base::SysUTF8ToNSString(credentialDataJSON)];
61 [self evaluate:script
62 stringResultHandler:^(NSString* result, NSError* error) {
63 if (completionHandler)
64 completionHandler(!error && [result isEqualToString:@"true"]);
65 }];
66 }
67
68 - (void)resolvePromiseWithRequestID:(NSInteger)requestID
69 completionHandler:(void (^)(BOOL))completionHandler {
70 NSString* script =
71 [NSString stringWithFormat:@"__gCrWeb['credentialManager'].resolve(%ld)",
72 static_cast<long>(requestID)];
73 [self evaluateScript:script completionHandler:completionHandler];
74 }
75
76 - (void)rejectPromiseWithRequestID:(NSInteger)requestID
77 errorType:(NSString*)errorType
78 message:(NSString*)message
79 completionHandler:(void (^)(BOOL))completionHandler {
80 NSString* script = [NSString
81 stringWithFormat:@"__gCrWeb['credentialManager'].reject(%ld, %@, %@)",
82 static_cast<long>(requestID), JSONEscape(errorType),
83 JSONEscape(message)];
84 [self evaluateScript:script completionHandler:completionHandler];
85 }
86
87 - (void)evaluateScript:(NSString*)script
88 completionHandler:(void (^)(BOOL))completionHandler {
89 [self evaluate:script
90 stringResultHandler:^(NSString* result, NSError* error) {
91 if (completionHandler)
92 completionHandler(!error && [result isEqualToString:@"true"]);
93 }];
94 }
95
96 #pragma mark - Protected methods
97
98 - (NSString*)scriptPath {
99 return @"credential_manager";
100 }
101
102 - (NSString*)presenceBeacon {
103 return @"__gCrWeb.credentialManager";
104 }
105
106 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698