| 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 "modules/credentialmanager/CredentialsContainer.h" | 5 #include "modules/credentialmanager/CredentialsContainer.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 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" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 : public WebCredentialManagerClient::NotificationCallbacks { | 58 : public WebCredentialManagerClient::NotificationCallbacks { |
| 59 WTF_MAKE_NONCOPYABLE(NotificationCallbacks); | 59 WTF_MAKE_NONCOPYABLE(NotificationCallbacks); |
| 60 | 60 |
| 61 public: | 61 public: |
| 62 explicit NotificationCallbacks(ScriptPromiseResolver* resolver) | 62 explicit NotificationCallbacks(ScriptPromiseResolver* resolver) |
| 63 : resolver_(resolver) {} | 63 : resolver_(resolver) {} |
| 64 ~NotificationCallbacks() override {} | 64 ~NotificationCallbacks() override {} |
| 65 | 65 |
| 66 void OnSuccess() override { | 66 void OnSuccess() override { |
| 67 Frame* frame = | 67 Frame* frame = |
| 68 ToDocument(resolver_->GetScriptState()->GetExecutionContext()) | 68 ToDocument(ExecutionContext::From(resolver_->GetScriptState())) |
| 69 ->GetFrame(); | 69 ->GetFrame(); |
| 70 SECURITY_CHECK(!frame || frame == frame->Tree().Top()); | 70 SECURITY_CHECK(!frame || frame == frame->Tree().Top()); |
| 71 | 71 |
| 72 resolver_->Resolve(); | 72 resolver_->Resolve(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void OnError(WebCredentialManagerError reason) override { | 75 void OnError(WebCredentialManagerError reason) override { |
| 76 RejectDueToCredentialManagerError(resolver_, reason); | 76 RejectDueToCredentialManagerError(resolver_, reason); |
| 77 } | 77 } |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 const Persistent<ScriptPromiseResolver> resolver_; | 80 const Persistent<ScriptPromiseResolver> resolver_; |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks { | 83 class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks { |
| 84 WTF_MAKE_NONCOPYABLE(RequestCallbacks); | 84 WTF_MAKE_NONCOPYABLE(RequestCallbacks); |
| 85 | 85 |
| 86 public: | 86 public: |
| 87 explicit RequestCallbacks(ScriptPromiseResolver* resolver) | 87 explicit RequestCallbacks(ScriptPromiseResolver* resolver) |
| 88 : resolver_(resolver) {} | 88 : resolver_(resolver) {} |
| 89 ~RequestCallbacks() override {} | 89 ~RequestCallbacks() override {} |
| 90 | 90 |
| 91 void OnSuccess(std::unique_ptr<WebCredential> web_credential) override { | 91 void OnSuccess(std::unique_ptr<WebCredential> web_credential) override { |
| 92 Frame* frame = | 92 Frame* frame = |
| 93 ToDocument(resolver_->GetScriptState()->GetExecutionContext()) | 93 ToDocument(ExecutionContext::From(resolver_->GetScriptState())) |
| 94 ->GetFrame(); | 94 ->GetFrame(); |
| 95 SECURITY_CHECK(!frame || frame == frame->Tree().Top()); | 95 SECURITY_CHECK(!frame || frame == frame->Tree().Top()); |
| 96 | 96 |
| 97 std::unique_ptr<WebCredential> credential = | 97 std::unique_ptr<WebCredential> credential = |
| 98 WTF::WrapUnique(web_credential.release()); | 98 WTF::WrapUnique(web_credential.release()); |
| 99 if (!credential || !frame) { | 99 if (!credential || !frame) { |
| 100 resolver_->Resolve(); | 100 resolver_->Resolve(); |
| 101 return; | 101 return; |
| 102 } | 102 } |
| 103 | 103 |
| 104 ASSERT(credential->IsPasswordCredential() || | 104 ASSERT(credential->IsPasswordCredential() || |
| 105 credential->IsFederatedCredential()); | 105 credential->IsFederatedCredential()); |
| 106 UseCounter::Count(resolver_->GetScriptState()->GetExecutionContext(), | 106 UseCounter::Count(ExecutionContext::From(resolver_->GetScriptState()), |
| 107 UseCounter::kCredentialManagerGetReturnedCredential); | 107 UseCounter::kCredentialManagerGetReturnedCredential); |
| 108 if (credential->IsPasswordCredential()) | 108 if (credential->IsPasswordCredential()) |
| 109 resolver_->Resolve(PasswordCredential::Create( | 109 resolver_->Resolve(PasswordCredential::Create( |
| 110 static_cast<WebPasswordCredential*>(credential.get()))); | 110 static_cast<WebPasswordCredential*>(credential.get()))); |
| 111 else | 111 else |
| 112 resolver_->Resolve(FederatedCredential::Create( | 112 resolver_->Resolve(FederatedCredential::Create( |
| 113 static_cast<WebFederatedCredential*>(credential.get()))); | 113 static_cast<WebFederatedCredential*>(credential.get()))); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void OnError(WebCredentialManagerError reason) override { | 116 void OnError(WebCredentialManagerError reason) override { |
| 117 RejectDueToCredentialManagerError(resolver_, reason); | 117 RejectDueToCredentialManagerError(resolver_, reason); |
| 118 } | 118 } |
| 119 | 119 |
| 120 private: | 120 private: |
| 121 const Persistent<ScriptPromiseResolver> resolver_; | 121 const Persistent<ScriptPromiseResolver> resolver_; |
| 122 }; | 122 }; |
| 123 | 123 |
| 124 CredentialsContainer* CredentialsContainer::Create() { | 124 CredentialsContainer* CredentialsContainer::Create() { |
| 125 return new CredentialsContainer(); | 125 return new CredentialsContainer(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 CredentialsContainer::CredentialsContainer() {} | 128 CredentialsContainer::CredentialsContainer() {} |
| 129 | 129 |
| 130 static bool CheckBoilerplate(ScriptPromiseResolver* resolver) { | 130 static bool CheckBoilerplate(ScriptPromiseResolver* resolver) { |
| 131 Frame* frame = | 131 Frame* frame = ToDocument(ExecutionContext::From(resolver->GetScriptState())) |
| 132 ToDocument(resolver->GetScriptState()->GetExecutionContext())->GetFrame(); | 132 ->GetFrame(); |
| 133 if (!frame || frame != frame->Tree().Top()) { | 133 if (!frame || frame != frame->Tree().Top()) { |
| 134 resolver->Reject(DOMException::Create(kSecurityError, | 134 resolver->Reject(DOMException::Create(kSecurityError, |
| 135 "CredentialContainer methods may " | 135 "CredentialContainer methods may " |
| 136 "only be executed in a top-level " | 136 "only be executed in a top-level " |
| 137 "document.")); | 137 "document.")); |
| 138 return false; | 138 return false; |
| 139 } | 139 } |
| 140 | 140 |
| 141 String error_message; | 141 String error_message; |
| 142 if (!resolver->GetScriptState()->GetExecutionContext()->IsSecureContext( | 142 if (!ExecutionContext::From(resolver->GetScriptState()) |
| 143 error_message)) { | 143 ->IsSecureContext(error_message)) { |
| 144 resolver->Reject(DOMException::Create(kSecurityError, error_message)); | 144 resolver->Reject(DOMException::Create(kSecurityError, error_message)); |
| 145 return false; | 145 return false; |
| 146 } | 146 } |
| 147 | 147 |
| 148 CredentialManagerClient* client = CredentialManagerClient::From( | 148 CredentialManagerClient* client = CredentialManagerClient::From( |
| 149 resolver->GetScriptState()->GetExecutionContext()); | 149 ExecutionContext::From(resolver->GetScriptState())); |
| 150 if (!client) { | 150 if (!client) { |
| 151 resolver->Reject(DOMException::Create( | 151 resolver->Reject(DOMException::Create( |
| 152 kInvalidStateError, | 152 kInvalidStateError, |
| 153 "Could not establish connection to the credential manager.")); | 153 "Could not establish connection to the credential manager.")); |
| 154 return false; | 154 return false; |
| 155 } | 155 } |
| 156 | 156 |
| 157 return true; | 157 return true; |
| 158 } | 158 } |
| 159 | 159 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 174 // looping. In order to avoid this crazyness for the moment, we're making a | 174 // looping. In order to avoid this crazyness for the moment, we're making a |
| 175 // copy of the vector. https://crbug.com/587088 | 175 // copy of the vector. https://crbug.com/587088 |
| 176 const Vector<String> provider_strings = options.federated().providers(); | 176 const Vector<String> provider_strings = options.federated().providers(); |
| 177 for (const auto& string : provider_strings) { | 177 for (const auto& string : provider_strings) { |
| 178 KURL url = KURL(KURL(), string); | 178 KURL url = KURL(KURL(), string); |
| 179 if (url.IsValid()) | 179 if (url.IsValid()) |
| 180 providers.push_back(url); | 180 providers.push_back(url); |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 | 183 |
| 184 UseCounter::Count(script_state->GetExecutionContext(), | 184 UseCounter::Count(ExecutionContext::From(script_state), |
| 185 options.unmediated() | 185 options.unmediated() |
| 186 ? UseCounter::kCredentialManagerGetWithoutUI | 186 ? UseCounter::kCredentialManagerGetWithoutUI |
| 187 : UseCounter::kCredentialManagerGetWithUI); | 187 : UseCounter::kCredentialManagerGetWithUI); |
| 188 | 188 |
| 189 CredentialManagerClient::From(script_state->GetExecutionContext()) | 189 CredentialManagerClient::From(ExecutionContext::From(script_state)) |
| 190 ->DispatchGet(options.unmediated(), options.password(), providers, | 190 ->DispatchGet(options.unmediated(), options.password(), providers, |
| 191 new RequestCallbacks(resolver)); | 191 new RequestCallbacks(resolver)); |
| 192 return promise; | 192 return promise; |
| 193 } | 193 } |
| 194 | 194 |
| 195 ScriptPromise CredentialsContainer::store(ScriptState* script_state, | 195 ScriptPromise CredentialsContainer::store(ScriptState* script_state, |
| 196 Credential* credential) { | 196 Credential* credential) { |
| 197 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 197 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 198 ScriptPromise promise = resolver->Promise(); | 198 ScriptPromise promise = resolver->Promise(); |
| 199 if (!CheckBoilerplate(resolver)) | 199 if (!CheckBoilerplate(resolver)) |
| 200 return promise; | 200 return promise; |
| 201 | 201 |
| 202 auto web_credential = | 202 auto web_credential = |
| 203 WebCredential::Create(credential->GetPlatformCredential()); | 203 WebCredential::Create(credential->GetPlatformCredential()); |
| 204 CredentialManagerClient::From(script_state->GetExecutionContext()) | 204 CredentialManagerClient::From(ExecutionContext::From(script_state)) |
| 205 ->DispatchStore(*web_credential, new NotificationCallbacks(resolver)); | 205 ->DispatchStore(*web_credential, new NotificationCallbacks(resolver)); |
| 206 return promise; | 206 return promise; |
| 207 } | 207 } |
| 208 | 208 |
| 209 ScriptPromise CredentialsContainer::requireUserMediation( | 209 ScriptPromise CredentialsContainer::requireUserMediation( |
| 210 ScriptState* script_state) { | 210 ScriptState* script_state) { |
| 211 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 211 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 212 ScriptPromise promise = resolver->Promise(); | 212 ScriptPromise promise = resolver->Promise(); |
| 213 if (!CheckBoilerplate(resolver)) | 213 if (!CheckBoilerplate(resolver)) |
| 214 return promise; | 214 return promise; |
| 215 | 215 |
| 216 CredentialManagerClient::From(script_state->GetExecutionContext()) | 216 CredentialManagerClient::From(ExecutionContext::From(script_state)) |
| 217 ->DispatchRequireUserMediation(new NotificationCallbacks(resolver)); | 217 ->DispatchRequireUserMediation(new NotificationCallbacks(resolver)); |
| 218 return promise; | 218 return promise; |
| 219 } | 219 } |
| 220 | 220 |
| 221 } // namespace blink | 221 } // namespace blink |
| OLD | NEW |