Chromium Code Reviews| Index: ios/chrome/browser/passwords/js_credential_manager.mm |
| diff --git a/ios/chrome/browser/passwords/js_credential_manager.mm b/ios/chrome/browser/passwords/js_credential_manager.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5432153c33a1bb1641857f27af7d34e847624f73 |
| --- /dev/null |
| +++ b/ios/chrome/browser/passwords/js_credential_manager.mm |
| @@ -0,0 +1,106 @@ |
| +// Copyright 2015 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/passwords/js_credential_manager.h" |
| + |
| +#include "base/json/json_writer.h" |
| +#include "base/json/string_escape.h" |
| +#include "base/logging.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "base/values.h" |
| +#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.
|
| + |
| +namespace { |
| + |
| +// Sanitizes |JSON| and wraps it in quotes so it can be injected safely in |
| +// JavaScript. |
| +NSString* JSONEscape(NSString* JSON) { |
| + return base::SysUTF8ToNSString( |
| + base::GetQuotedJSONString(base::SysNSStringToUTF8(JSON))); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace ios { |
| +namespace passwords { |
| +const char kPendingRequestErrorType[] = "PendingRequestError"; |
| +const char kPendingRequestErrorMessage[] = |
| + "There is already an outstanding request"; |
| +const char kSecurityErrorType[] = "SecurityError"; |
| +const char kPasswordStoreUnavailableErrorType[] = |
| + "PasswordStoreUnavailableError"; |
| +const char kPasswordStoreUnavailableErrorMessage[] = |
| + "The password store is unavailable"; |
| +const char kSecurityErrorMessageUntrustedOrigin[] = "The origin is untrusted"; |
| +} // namespace passwords |
| +} // namespace ios |
| + |
| +@interface JSCredentialManager () |
| + |
| +// Evaluates the JavaScript in |script|, which should evaluate to a JavaScript |
| +// boolean value. That value will be passed to |completionHandler|. |
| +- (void)evaluateScript:(NSString*)script |
| + completionHandler:(void (^)(BOOL))completionHandler; |
| + |
| +@end |
| + |
| +@implementation JSCredentialManager |
| + |
| +- (void)resolvePromiseWithRequestID:(NSInteger)requestID |
| + credential:(const web::Credential&)credential |
| + completionHandler:(void (^)(BOOL))completionHandler { |
| + base::DictionaryValue credentialData; |
| + web::CredentialToDictionaryValue(credential, &credentialData); |
| + std::string credentialDataJSON; |
| + base::JSONWriter::Write(credentialData, &credentialDataJSON); |
| + NSString* script = [NSString |
| + stringWithFormat:@"__gCrWeb['credentialManager'].resolve(%ld, %@)", |
| + static_cast<long>(requestID), |
| + base::SysUTF8ToNSString(credentialDataJSON)]; |
| + [self evaluate:script |
| + stringResultHandler:^(NSString* result, NSError* error) { |
| + if (completionHandler) |
| + completionHandler(!error && [result isEqualToString:@"true"]); |
| + }]; |
| +} |
| + |
| +- (void)resolvePromiseWithRequestID:(NSInteger)requestID |
| + completionHandler:(void (^)(BOOL))completionHandler { |
| + NSString* script = |
| + [NSString stringWithFormat:@"__gCrWeb['credentialManager'].resolve(%ld)", |
| + static_cast<long>(requestID)]; |
| + [self evaluateScript:script completionHandler:completionHandler]; |
| +} |
| + |
| +- (void)rejectPromiseWithRequestID:(NSInteger)requestID |
| + errorType:(NSString*)errorType |
| + message:(NSString*)message |
| + completionHandler:(void (^)(BOOL))completionHandler { |
| + NSString* script = [NSString |
| + stringWithFormat:@"__gCrWeb['credentialManager'].reject(%ld, %@, %@)", |
| + static_cast<long>(requestID), JSONEscape(errorType), |
| + JSONEscape(message)]; |
| + [self evaluateScript:script completionHandler:completionHandler]; |
| +} |
| + |
| +- (void)evaluateScript:(NSString*)script |
| + completionHandler:(void (^)(BOOL))completionHandler { |
| + [self evaluate:script |
| + stringResultHandler:^(NSString* result, NSError* error) { |
| + if (completionHandler) |
| + completionHandler(!error && [result isEqualToString:@"true"]); |
| + }]; |
| +} |
| + |
| +#pragma mark - Protected methods |
| + |
| +- (NSString*)scriptPath { |
| + return @"credential_manager"; |
| +} |
| + |
| +- (NSString*)presenceBeacon { |
| + return @"__gCrWeb.credentialManager"; |
| +} |
| + |
| +@end |