OLD | NEW |
(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 #include "ios/web/public/web_state/js/credential_util.h" |
| 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 const char kCredentialsPendingRequestErrorType[] = "PendingRequestError"; |
| 26 const char kCredentialsPendingRequestErrorMessage[] = |
| 27 "There is already an outstanding request"; |
| 28 const char kCredentialsSecurityErrorType[] = "SecurityError"; |
| 29 const char kCredentialsPasswordStoreUnavailableErrorType[] = |
| 30 "PasswordStoreUnavailableError"; |
| 31 const char kCredentialsPasswordStoreUnavailableErrorMessage[] = |
| 32 "The password store is unavailable"; |
| 33 const char kCredentialsSecurityErrorMessageUntrustedOrigin[] = |
| 34 "The origin is untrusted"; |
| 35 |
| 36 @interface JSCredentialManager () |
| 37 |
| 38 // Evaluates the JavaScript in |script|, which should evaluate to a JavaScript |
| 39 // boolean value. That value will be passed to |completionHandler|. |
| 40 - (void)evaluateScript:(NSString*)script |
| 41 completionHandler:(void (^)(BOOL))completionHandler; |
| 42 |
| 43 @end |
| 44 |
| 45 @implementation JSCredentialManager |
| 46 |
| 47 - (void)resolvePromiseWithRequestID:(NSInteger)requestID |
| 48 credential:(const web::Credential&)credential |
| 49 completionHandler:(void (^)(BOOL))completionHandler { |
| 50 base::DictionaryValue credentialData; |
| 51 web::CredentialToDictionaryValue(credential, &credentialData); |
| 52 std::string credentialDataJSON; |
| 53 base::JSONWriter::Write(credentialData, &credentialDataJSON); |
| 54 NSString* script = [NSString |
| 55 stringWithFormat:@"__gCrWeb['credentialManager'].resolve(%ld, %@)", |
| 56 static_cast<long>(requestID), |
| 57 base::SysUTF8ToNSString(credentialDataJSON)]; |
| 58 [self evaluate:script |
| 59 stringResultHandler:^(NSString* result, NSError* error) { |
| 60 if (completionHandler) |
| 61 completionHandler(!error && [result isEqualToString:@"true"]); |
| 62 }]; |
| 63 } |
| 64 |
| 65 - (void)resolvePromiseWithRequestID:(NSInteger)requestID |
| 66 completionHandler:(void (^)(BOOL))completionHandler { |
| 67 NSString* script = |
| 68 [NSString stringWithFormat:@"__gCrWeb['credentialManager'].resolve(%ld)", |
| 69 static_cast<long>(requestID)]; |
| 70 [self evaluateScript:script completionHandler:completionHandler]; |
| 71 } |
| 72 |
| 73 - (void)rejectPromiseWithRequestID:(NSInteger)requestID |
| 74 errorType:(NSString*)errorType |
| 75 message:(NSString*)message |
| 76 completionHandler:(void (^)(BOOL))completionHandler { |
| 77 NSString* script = [NSString |
| 78 stringWithFormat:@"__gCrWeb['credentialManager'].reject(%ld, %@, %@)", |
| 79 static_cast<long>(requestID), JSONEscape(errorType), |
| 80 JSONEscape(message)]; |
| 81 [self evaluateScript:script completionHandler:completionHandler]; |
| 82 } |
| 83 |
| 84 - (void)evaluateScript:(NSString*)script |
| 85 completionHandler:(void (^)(BOOL))completionHandler { |
| 86 [self evaluate:script |
| 87 stringResultHandler:^(NSString* result, NSError* error) { |
| 88 if (completionHandler) |
| 89 completionHandler(!error && [result isEqualToString:@"true"]); |
| 90 }]; |
| 91 } |
| 92 |
| 93 #pragma mark - Protected methods |
| 94 |
| 95 - (NSString*)scriptPath { |
| 96 return @"credential_manager"; |
| 97 } |
| 98 |
| 99 - (NSString*)presenceBeacon { |
| 100 return @"__gCrWeb.credentialManager"; |
| 101 } |
| 102 |
| 103 @end |
OLD | NEW |