| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/credentialmanager/CredentialsContainer.h" | 6 #include "modules/credentialmanager/CredentialsContainer.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "modules/credentialmanager/Credential.h" | 14 #include "modules/credentialmanager/Credential.h" |
| 15 #include "modules/credentialmanager/CredentialManagerClient.h" |
| 16 #include "modules/credentialmanager/LocalCredential.h" |
| 15 #include "platform/weborigin/SecurityOrigin.h" | 17 #include "platform/weborigin/SecurityOrigin.h" |
| 16 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
| 17 #include "public/platform/WebCredential.h" | 19 #include "public/platform/WebCredential.h" |
| 18 #include "public/platform/WebCredentialManagerClient.h" | 20 #include "public/platform/WebCredentialManagerClient.h" |
| 19 #include "public/platform/WebCredentialManagerError.h" | 21 #include "public/platform/WebCredentialManagerError.h" |
| 22 #include "public/platform/WebLocalCredential.h" |
| 20 | 23 |
| 21 namespace blink { | 24 namespace blink { |
| 22 | 25 |
| 23 static void rejectDueToCredentialManagerError(PassRefPtr<ScriptPromiseResolver>
resolver, WebCredentialManagerError* reason) | 26 static void rejectDueToCredentialManagerError(PassRefPtr<ScriptPromiseResolver>
resolver, WebCredentialManagerError* reason) |
| 24 { | 27 { |
| 25 | |
| 26 switch (reason->errorType) { | 28 switch (reason->errorType) { |
| 27 case WebCredentialManagerError::ErrorTypeDisabled: | 29 case WebCredentialManagerError::ErrorTypeDisabled: |
| 28 resolver->reject(DOMException::create(InvalidStateError, "The credential
manager is disabled.")); | 30 resolver->reject(DOMException::create(InvalidStateError, "The credential
manager is disabled.")); |
| 29 break; | 31 break; |
| 30 case WebCredentialManagerError::ErrorTypeUnknown: | 32 case WebCredentialManagerError::ErrorTypeUnknown: |
| 31 default: | 33 default: |
| 32 resolver->reject(DOMException::create(NotReadableError, "An unknown erro
r occured while talking to the credential manager.")); | 34 resolver->reject(DOMException::create(NotReadableError, "An unknown erro
r occured while talking to the credential manager.")); |
| 33 break; | 35 break; |
| 34 } | 36 } |
| 35 } | 37 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 55 }; | 57 }; |
| 56 | 58 |
| 57 class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks { | 59 class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks { |
| 58 WTF_MAKE_NONCOPYABLE(RequestCallbacks); | 60 WTF_MAKE_NONCOPYABLE(RequestCallbacks); |
| 59 public: | 61 public: |
| 60 explicit RequestCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_re
solver(resolver) { } | 62 explicit RequestCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_re
solver(resolver) { } |
| 61 virtual ~RequestCallbacks() { } | 63 virtual ~RequestCallbacks() { } |
| 62 | 64 |
| 63 virtual void onSuccess(WebCredential* credential) OVERRIDE | 65 virtual void onSuccess(WebCredential* credential) OVERRIDE |
| 64 { | 66 { |
| 65 // FIXME: Split this into Local/Federated types. | 67 if (!credential) { |
| 66 m_resolver->resolve(Credential::create(credential->id(), credential->nam
e(), credential->avatarURL())); | 68 m_resolver->resolve(); |
| 69 return; |
| 70 } |
| 71 |
| 72 // FIXME: Split this into Local/Federated types. Right now it's hard-cod
ed to be a LocalCredential. :( |
| 73 m_resolver->resolve(LocalCredential::create(static_cast<WebLocalCredenti
al*>(credential))); |
| 67 } | 74 } |
| 68 | 75 |
| 69 virtual void onError(WebCredentialManagerError* reason) OVERRIDE | 76 virtual void onError(WebCredentialManagerError* reason) OVERRIDE |
| 70 { | 77 { |
| 71 rejectDueToCredentialManagerError(m_resolver, reason); | 78 rejectDueToCredentialManagerError(m_resolver, reason); |
| 72 } | 79 } |
| 73 | 80 |
| 74 private: | 81 private: |
| 75 const RefPtr<ScriptPromiseResolver> m_resolver; | 82 const RefPtr<ScriptPromiseResolver> m_resolver; |
| 76 }; | 83 }; |
| 77 | 84 |
| 78 | 85 |
| 79 CredentialsContainer* CredentialsContainer::create() | 86 CredentialsContainer* CredentialsContainer::create() |
| 80 { | 87 { |
| 81 return new CredentialsContainer(); | 88 return new CredentialsContainer(); |
| 82 } | 89 } |
| 83 | 90 |
| 84 CredentialsContainer::CredentialsContainer() | 91 CredentialsContainer::CredentialsContainer() |
| 85 { | 92 { |
| 86 } | 93 } |
| 87 | 94 |
| 88 static bool canAccessCredentialManagerAPI(ScriptState* scriptState, PassRefPtr<S
criptPromiseResolver> resolver) | 95 static bool checkBoilerplate(PassRefPtr<ScriptPromiseResolver> resolver) |
| 89 { | 96 { |
| 90 SecurityOrigin* securityOrigin = scriptState->executionContext()->securityOr
igin(); | 97 CredentialManagerClient* client = CredentialManagerClient::from(resolver->sc
riptState()->executionContext()); |
| 98 if (!client) { |
| 99 resolver->reject(DOMException::create(InvalidStateError, "Could not esta
blish connection to the credential manager.")); |
| 100 return false; |
| 101 } |
| 102 |
| 103 SecurityOrigin* securityOrigin = resolver->scriptState()->executionContext()
->securityOrigin(); |
| 91 String errorMessage; | 104 String errorMessage; |
| 92 if (!securityOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) { | 105 if (!securityOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) { |
| 93 resolver->reject(DOMException::create(SecurityError, errorMessage)); | 106 resolver->reject(DOMException::create(SecurityError, errorMessage)); |
| 94 return false; | 107 return false; |
| 95 } | 108 } |
| 109 |
| 96 return true; | 110 return true; |
| 97 } | 111 } |
| 98 | 112 |
| 99 static ScriptPromise stubImplementation(ScriptState* scriptState) | |
| 100 { | |
| 101 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); | |
| 102 ScriptPromise promise = resolver->promise(); | |
| 103 | |
| 104 if (!canAccessCredentialManagerAPI(scriptState, resolver)) | |
| 105 return promise; | |
| 106 | |
| 107 resolver->resolve(); | |
| 108 return promise; | |
| 109 } | |
| 110 | |
| 111 ScriptPromise CredentialsContainer::request(ScriptState* scriptState, const Dict
ionary&) | 113 ScriptPromise CredentialsContainer::request(ScriptState* scriptState, const Dict
ionary&) |
| 112 { | 114 { |
| 113 return stubImplementation(scriptState); | 115 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 116 ScriptPromise promise = resolver->promise(); |
| 117 if (!checkBoilerplate(resolver)) |
| 118 return promise; |
| 119 |
| 120 WebVector<WebURL> tempVector; |
| 121 CredentialManagerClient::from(scriptState->executionContext())->dispatchRequ
est(false, tempVector, new RequestCallbacks(resolver)); |
| 122 return promise; |
| 114 } | 123 } |
| 115 | 124 |
| 116 ScriptPromise CredentialsContainer::notifySignedIn(ScriptState* scriptState, Cre
dential* credential) | 125 ScriptPromise CredentialsContainer::notifySignedIn(ScriptState* scriptState, Cre
dential* credential) |
| 117 { | 126 { |
| 118 return stubImplementation(scriptState); | 127 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 128 ScriptPromise promise = resolver->promise(); |
| 129 if (!checkBoilerplate(resolver)) |
| 130 return promise; |
| 131 |
| 132 CredentialManagerClient::from(scriptState->executionContext())->dispatchSign
edIn(WebCredential(credential->platformCredential()), new NotificationCallbacks(
resolver)); |
| 133 return promise; |
| 119 } | 134 } |
| 120 | 135 |
| 121 ScriptPromise CredentialsContainer::notifyFailedSignIn(ScriptState* scriptState,
Credential* credential) | 136 ScriptPromise CredentialsContainer::notifyFailedSignIn(ScriptState* scriptState,
Credential* credential) |
| 122 { | 137 { |
| 123 return stubImplementation(scriptState); | 138 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 139 ScriptPromise promise = resolver->promise(); |
| 140 if (!checkBoilerplate(resolver)) |
| 141 return promise; |
| 142 |
| 143 CredentialManagerClient::from(scriptState->executionContext())->dispatchFail
edSignIn(WebCredential(credential->platformCredential()), new NotificationCallba
cks(resolver)); |
| 144 return promise; |
| 124 } | 145 } |
| 125 | 146 |
| 126 ScriptPromise CredentialsContainer::notifySignedOut(ScriptState* scriptState) | 147 ScriptPromise CredentialsContainer::notifySignedOut(ScriptState* scriptState) |
| 127 { | 148 { |
| 128 return stubImplementation(scriptState); | 149 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 150 ScriptPromise promise = resolver->promise(); |
| 151 if (!checkBoilerplate(resolver)) |
| 152 return promise; |
| 153 |
| 154 CredentialManagerClient::from(scriptState->executionContext())->dispatchSign
edOut(new NotificationCallbacks(resolver)); |
| 155 return promise; |
| 129 } | 156 } |
| 130 | 157 |
| 131 } // namespace blink | 158 } // namespace blink |
| OLD | NEW |