summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/passwords/js_credential_manager.mm
blob: abdbd41d7426b21254a00d98ea1155efc5b2404e (plain)
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
// 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"
#include "ios/web/public/web_state/js/credential_util.h"

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

const char kCredentialsPendingRequestErrorType[] = "PendingRequestError";
const char kCredentialsPendingRequestErrorMessage[] =
    "There is already an outstanding request";
const char kCredentialsSecurityErrorType[] = "SecurityError";
const char kCredentialsPasswordStoreUnavailableErrorType[] =
    "PasswordStoreUnavailableError";
const char kCredentialsPasswordStoreUnavailableErrorMessage[] =
    "The password store is unavailable";
const char kCredentialsSecurityErrorMessageUntrustedOrigin[] =
    "The origin is untrusted";

@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