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" | 15 #include "modules/credentialmanager/CredentialManagerClient.h" |
16 #include "modules/credentialmanager/CredentialRequestOptions.h" | 16 #include "modules/credentialmanager/CredentialRequestOptions.h" |
17 #include "modules/credentialmanager/FederatedCredential.h" | 17 #include "modules/credentialmanager/FederatedCredential.h" |
18 #include "modules/credentialmanager/FederatedCredentialRequestOptions.h" | 18 #include "modules/credentialmanager/FederatedCredentialRequestOptions.h" |
19 #include "modules/credentialmanager/PasswordCredential.h" | 19 #include "modules/credentialmanager/PasswordCredential.h" |
20 #include "platform/weborigin/SecurityOrigin.h" | 20 #include "platform/weborigin/SecurityOrigin.h" |
21 #include "public/platform/Platform.h" | 21 #include "public/platform/Platform.h" |
22 #include "public/platform/WebCredential.h" | 22 #include "public/platform/WebCredential.h" |
23 #include "public/platform/WebCredentialManagerClient.h" | 23 #include "public/platform/WebCredentialManagerClient.h" |
24 #include "public/platform/WebCredentialManagerError.h" | 24 #include "public/platform/WebCredentialManagerError.h" |
25 #include "public/platform/WebFederatedCredential.h" | 25 #include "public/platform/WebFederatedCredential.h" |
26 #include "public/platform/WebPasswordCredential.h" | 26 #include "public/platform/WebPasswordCredential.h" |
27 | 27 |
28 namespace blink { | 28 namespace blink { |
29 | 29 |
30 static void rejectDueToCredentialManagerError(PassRefPtrWillBeRawPtr<ScriptPromi seResolver> resolver, WebCredentialManagerError* reason) | 30 static void rejectDueToCredentialManagerError(ScriptPromiseResolver* resolver, W ebCredentialManagerError* reason) |
31 { | 31 { |
32 switch (reason->errorType) { | 32 switch (reason->errorType) { |
33 case WebCredentialManagerError::ErrorTypeDisabled: | 33 case WebCredentialManagerError::ErrorTypeDisabled: |
34 resolver->reject(DOMException::create(InvalidStateError, "The credential manager is disabled.")); | 34 resolver->reject(DOMException::create(InvalidStateError, "The credential manager is disabled.")); |
35 break; | 35 break; |
36 case WebCredentialManagerError::ErrorTypeUnknown: | 36 case WebCredentialManagerError::ErrorTypeUnknown: |
37 default: | 37 default: |
38 resolver->reject(DOMException::create(NotReadableError, "An unknown erro r occured while talking to the credential manager.")); | 38 resolver->reject(DOMException::create(NotReadableError, "An unknown erro r occured while talking to the credential manager.")); |
39 break; | 39 break; |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 class NotificationCallbacks : public WebCredentialManagerClient::NotificationCal lbacks { | 43 class NotificationCallbacks : public WebCredentialManagerClient::NotificationCal lbacks { |
44 WTF_MAKE_NONCOPYABLE(NotificationCallbacks); | 44 WTF_MAKE_NONCOPYABLE(NotificationCallbacks); |
45 public: | 45 public: |
46 explicit NotificationCallbacks(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) : m_resolver(resolver) { } | 46 explicit NotificationCallbacks(ScriptPromiseResolver* resolver) : m_resolver (resolver) { } |
47 ~NotificationCallbacks() override { } | 47 ~NotificationCallbacks() override { } |
haraken
2015/07/17 01:40:36
Add ASSERT(!m_resolver).
| |
48 | 48 |
49 void onSuccess() override | 49 void onSuccess() override |
50 { | 50 { |
51 m_resolver->resolve(); | 51 m_resolver->resolve(); |
haraken
2015/07/17 01:40:36
Shall we clear m_resolver?
yhirano
2015/07/17 02:11:31
I'm not a great fan of the pattern. resolve() and
haraken
2015/07/17 02:18:35
I'm just curious about the programming rule. Some
yhirano
2015/07/17 03:18:39
As a user I generally don't like manual clearing w
| |
52 } | 52 } |
53 | 53 |
54 void onError(WebCredentialManagerError* reason) override | 54 void onError(WebCredentialManagerError* reason) override |
55 { | 55 { |
56 rejectDueToCredentialManagerError(m_resolver, reason); | 56 rejectDueToCredentialManagerError(m_resolver, reason); |
57 } | 57 } |
58 | 58 |
59 private: | 59 private: |
60 const RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; | 60 const Persistent<ScriptPromiseResolver> m_resolver; |
61 }; | 61 }; |
62 | 62 |
63 class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks { | 63 class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks { |
64 WTF_MAKE_NONCOPYABLE(RequestCallbacks); | 64 WTF_MAKE_NONCOPYABLE(RequestCallbacks); |
65 public: | 65 public: |
66 explicit RequestCallbacks(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> reso lver) : m_resolver(resolver) { } | 66 explicit RequestCallbacks(ScriptPromiseResolver* resolver) : m_resolver(reso lver) { } |
67 ~RequestCallbacks() override { } | 67 ~RequestCallbacks() override { } |
68 | 68 |
69 void onSuccess(WebCredential* credential) override | 69 void onSuccess(WebCredential* credential) override |
70 { | 70 { |
71 if (!credential) { | 71 if (!credential) { |
72 m_resolver->resolve(); | 72 m_resolver->resolve(); |
haraken
2015/07/17 01:40:35
Ditto. Same comment for other places.
| |
73 return; | 73 return; |
74 } | 74 } |
75 | 75 |
76 ASSERT(credential->isPasswordCredential() || credential->isFederatedCred ential()); | 76 ASSERT(credential->isPasswordCredential() || credential->isFederatedCred ential()); |
77 if (credential->isPasswordCredential()) | 77 if (credential->isPasswordCredential()) |
78 m_resolver->resolve(PasswordCredential::create(static_cast<WebPasswo rdCredential*>(credential))); | 78 m_resolver->resolve(PasswordCredential::create(static_cast<WebPasswo rdCredential*>(credential))); |
79 else | 79 else |
80 m_resolver->resolve(FederatedCredential::create(static_cast<WebFeder atedCredential*>(credential))); | 80 m_resolver->resolve(FederatedCredential::create(static_cast<WebFeder atedCredential*>(credential))); |
81 } | 81 } |
82 | 82 |
83 void onError(WebCredentialManagerError* reason) override | 83 void onError(WebCredentialManagerError* reason) override |
84 { | 84 { |
85 rejectDueToCredentialManagerError(m_resolver, reason); | 85 rejectDueToCredentialManagerError(m_resolver, reason); |
86 } | 86 } |
87 | 87 |
88 private: | 88 private: |
89 const RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; | 89 const Persistent<ScriptPromiseResolver> m_resolver; |
90 }; | 90 }; |
91 | 91 |
92 | 92 |
93 CredentialsContainer* CredentialsContainer::create() | 93 CredentialsContainer* CredentialsContainer::create() |
94 { | 94 { |
95 return new CredentialsContainer(); | 95 return new CredentialsContainer(); |
96 } | 96 } |
97 | 97 |
98 CredentialsContainer::CredentialsContainer() | 98 CredentialsContainer::CredentialsContainer() |
99 { | 99 { |
100 } | 100 } |
101 | 101 |
102 static bool checkBoilerplate(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resol ver) | 102 static bool checkBoilerplate(ScriptPromiseResolver* resolver) |
103 { | 103 { |
104 CredentialManagerClient* client = CredentialManagerClient::from(resolver->sc riptState()->executionContext()); | 104 CredentialManagerClient* client = CredentialManagerClient::from(resolver->sc riptState()->executionContext()); |
105 if (!client) { | 105 if (!client) { |
106 resolver->reject(DOMException::create(InvalidStateError, "Could not esta blish connection to the credential manager.")); | 106 resolver->reject(DOMException::create(InvalidStateError, "Could not esta blish connection to the credential manager.")); |
107 return false; | 107 return false; |
108 } | 108 } |
109 | 109 |
110 String errorMessage; | 110 String errorMessage; |
111 if (!resolver->scriptState()->executionContext()->isPrivilegedContext(errorM essage)) { | 111 if (!resolver->scriptState()->executionContext()->isPrivilegedContext(errorM essage)) { |
112 resolver->reject(DOMException::create(SecurityError, errorMessage)); | 112 resolver->reject(DOMException::create(SecurityError, errorMessage)); |
113 return false; | 113 return false; |
114 } | 114 } |
115 | 115 |
116 return true; | 116 return true; |
117 } | 117 } |
118 | 118 |
119 ScriptPromise CredentialsContainer::request(ScriptState* scriptState, const Cred entialRequestOptions& options) | 119 ScriptPromise CredentialsContainer::request(ScriptState* scriptState, const Cred entialRequestOptions& options) |
120 { | 120 { |
121 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | 121 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
122 ScriptPromise promise = resolver->promise(); | 122 ScriptPromise promise = resolver->promise(); |
123 if (!checkBoilerplate(resolver)) | 123 if (!checkBoilerplate(resolver)) |
124 return promise; | 124 return promise; |
125 | 125 |
126 Vector<KURL> providers; | 126 Vector<KURL> providers; |
127 if (options.hasFederated() && options.federated().hasProviders()) { | 127 if (options.hasFederated() && options.federated().hasProviders()) { |
128 for (const auto& string : options.federated().providers()) { | 128 for (const auto& string : options.federated().providers()) { |
129 KURL url = KURL(KURL(), string); | 129 KURL url = KURL(KURL(), string); |
130 if (url.isValid()) | 130 if (url.isValid()) |
131 providers.append(url); | 131 providers.append(url); |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 CredentialManagerClient::from(scriptState->executionContext())->dispatchRequ est(options.suppressUI(), providers, new RequestCallbacks(resolver)); | 135 CredentialManagerClient::from(scriptState->executionContext())->dispatchRequ est(options.suppressUI(), providers, new RequestCallbacks(resolver)); |
136 return promise; | 136 return promise; |
137 } | 137 } |
138 | 138 |
139 ScriptPromise CredentialsContainer::notifySignedIn(ScriptState* scriptState, Cre dential* credential) | 139 ScriptPromise CredentialsContainer::notifySignedIn(ScriptState* scriptState, Cre dential* credential) |
140 { | 140 { |
141 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | 141 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
142 ScriptPromise promise = resolver->promise(); | 142 ScriptPromise promise = resolver->promise(); |
143 if (!checkBoilerplate(resolver)) | 143 if (!checkBoilerplate(resolver)) |
144 return promise; | 144 return promise; |
145 | 145 |
146 CredentialManagerClient::from(scriptState->executionContext())->dispatchSign edIn(WebCredential::create(credential->platformCredential()), new NotificationCa llbacks(resolver)); | 146 CredentialManagerClient::from(scriptState->executionContext())->dispatchSign edIn(WebCredential::create(credential->platformCredential()), new NotificationCa llbacks(resolver)); |
147 return promise; | 147 return promise; |
148 } | 148 } |
149 | 149 |
150 ScriptPromise CredentialsContainer::requireUserMediation(ScriptState* scriptStat e) | 150 ScriptPromise CredentialsContainer::requireUserMediation(ScriptState* scriptStat e) |
151 { | 151 { |
152 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | 152 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
153 ScriptPromise promise = resolver->promise(); | 153 ScriptPromise promise = resolver->promise(); |
154 if (!checkBoilerplate(resolver)) | 154 if (!checkBoilerplate(resolver)) |
155 return promise; | 155 return promise; |
156 | 156 |
157 CredentialManagerClient::from(scriptState->executionContext())->dispatchRequ ireUserMediation(new NotificationCallbacks(resolver)); | 157 CredentialManagerClient::from(scriptState->executionContext())->dispatchRequ ireUserMediation(new NotificationCallbacks(resolver)); |
158 return promise; | 158 return promise; |
159 } | 159 } |
160 | 160 |
161 } // namespace blink | 161 } // namespace blink |
OLD | NEW |