Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/payments/PaymentInstruments.h" | 5 #include "modules/payments/PaymentInstruments.h" |
| 6 | 6 |
| 7 #include <utility> | |
| 8 | |
| 9 #include "bindings/core/v8/ExceptionState.h" | |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 10 #include "bindings/core/v8/ScriptPromise.h" |
| 11 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
| 12 #include "bindings/core/v8/V8StringResource.h" | |
| 13 #include "core/dom/DOMException.h" | |
| 8 #include "modules/payments/PaymentInstrument.h" | 14 #include "modules/payments/PaymentInstrument.h" |
| 15 #include "modules/payments/PaymentManager.h" | |
| 16 #include "platform/wtf/Vector.h" | |
| 9 | 17 |
| 10 namespace blink { | 18 namespace blink { |
| 19 namespace { | |
| 11 | 20 |
| 12 PaymentInstruments::PaymentInstruments() {} | 21 static const char kPaymentManagerUnavailable[] = "Payment manager unavailable"; |
| 22 | |
| 23 bool rejectError(ScriptPromiseResolver* resolver, | |
| 24 payments::mojom::blink::PaymentHandlerStatus status) { | |
| 25 switch (status) { | |
| 26 case payments::mojom::blink::PaymentHandlerStatus::SUCCESS: | |
| 27 return false; | |
| 28 case payments::mojom::blink::PaymentHandlerStatus::NOT_IMPLEMENTED: | |
| 29 resolver->Reject( | |
| 30 DOMException::Create(kNotSupportedError, "Not implemented yet")); | |
| 31 return true; | |
| 32 case payments::mojom::blink::PaymentHandlerStatus::NOT_FOUND: | |
| 33 resolver->Reject(DOMException::Create(kNotFoundError, | |
| 34 "There is no stored instrument")); | |
| 35 return true; | |
| 36 case payments::mojom::blink::PaymentHandlerStatus::NO_ACTIVE_WORKER: | |
| 37 resolver->Reject(DOMException::Create(kInvalidStateError, | |
| 38 "There is no stored instrument")); | |
| 39 return true; | |
| 40 case payments::mojom::blink::PaymentHandlerStatus::STORAGE_OPERATION_FAILED: | |
| 41 resolver->Reject(DOMException::Create(kInvalidStateError, | |
| 42 "There is no stored instrument")); | |
| 43 return true; | |
| 44 } | |
| 45 NOTREACHED(); | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 PaymentInstruments::PaymentInstruments( | |
| 52 const payments::mojom::blink::PaymentManagerPtr& manager) | |
| 53 : manager_(manager) {} | |
| 13 | 54 |
| 14 ScriptPromise PaymentInstruments::deleteInstrument( | 55 ScriptPromise PaymentInstruments::deleteInstrument( |
| 15 const String& instrument_key) { | 56 const String& instrument_key) { |
| 16 NOTIMPLEMENTED(); | 57 NOTIMPLEMENTED(); |
| 17 return ScriptPromise(); | 58 return ScriptPromise(); |
| 18 } | 59 } |
| 19 | 60 |
| 20 ScriptPromise PaymentInstruments::get(const String& instrument_key) { | 61 ScriptPromise PaymentInstruments::get(ScriptState* script_state, |
| 21 NOTIMPLEMENTED(); | 62 const String& instrument_key) { |
| 22 return ScriptPromise(); | 63 if (!manager_.is_bound()) { |
| 64 return ScriptPromise::RejectWithDOMException( | |
| 65 script_state, | |
| 66 DOMException::Create(kInvalidStateError, kPaymentManagerUnavailable)); | |
| 67 } | |
| 68 | |
| 69 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | |
| 70 ScriptPromise promise = resolver->Promise(); | |
| 71 | |
| 72 manager_->GetPaymentInstrument( | |
| 73 instrument_key, ConvertToBaseCallback(WTF::Bind( | |
| 74 &PaymentInstruments::onGetPaymentInstrument, | |
| 75 WrapPersistent(this), WrapPersistent(resolver)))); | |
| 76 return promise; | |
| 23 } | 77 } |
| 24 | 78 |
| 25 ScriptPromise PaymentInstruments::keys() { | 79 ScriptPromise PaymentInstruments::keys() { |
| 26 NOTIMPLEMENTED(); | 80 NOTIMPLEMENTED(); |
| 27 return ScriptPromise(); | 81 return ScriptPromise(); |
| 28 } | 82 } |
| 29 | 83 |
| 30 ScriptPromise PaymentInstruments::has(const String& instrument_key) { | 84 ScriptPromise PaymentInstruments::has(const String& instrument_key) { |
| 31 NOTIMPLEMENTED(); | 85 NOTIMPLEMENTED(); |
| 32 return ScriptPromise(); | 86 return ScriptPromise(); |
| 33 } | 87 } |
| 34 | 88 |
| 35 ScriptPromise PaymentInstruments::set(const String& instrument_key, | 89 ScriptPromise PaymentInstruments::set(ScriptState* script_state, |
| 36 const PaymentInstrument& details) { | 90 const String& instrument_key, |
| 37 NOTIMPLEMENTED(); | 91 const PaymentInstrument& details, |
| 38 return ScriptPromise(); | 92 ExceptionState& exception_state) { |
| 93 if (!manager_.is_bound()) { | |
| 94 return ScriptPromise::RejectWithDOMException( | |
| 95 script_state, | |
| 96 DOMException::Create(kInvalidStateError, kPaymentManagerUnavailable)); | |
| 97 } | |
| 98 | |
| 99 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | |
| 100 ScriptPromise promise = resolver->Promise(); | |
| 101 | |
| 102 payments::mojom::blink::PaymentInstrumentPtr instrument = | |
| 103 payments::mojom::blink::PaymentInstrument::New(); | |
| 104 instrument->name = details.hasName() ? details.name() : WTF::g_empty_string; | |
| 105 if (details.hasEnabledMethods()) { | |
| 106 instrument->enabled_methods = | |
| 107 WTF::Vector<WTF::String>(details.enabledMethods()); | |
|
haraken
2017/04/13 01:32:31
Why do we need the static cast?
zino
2017/04/15 03:30:10
Done.
| |
| 108 } | |
| 109 | |
| 110 if (details.hasCapabilities()) { | |
| 111 v8::Local<v8::String> value; | |
| 112 if (!v8::JSON::Stringify(script_state->GetContext(), | |
| 113 details.capabilities().V8Value().As<v8::Object>()) | |
| 114 .ToLocal(&value)) { | |
| 115 exception_state.ThrowTypeError( | |
| 116 "Capabilities should be a JSON-serializable object"); | |
| 117 return exception_state.Reject(script_state); | |
| 118 } | |
| 119 instrument->stringified_capabilities = | |
| 120 V8StringToWebCoreString<String>(value, kDoNotExternalize); | |
|
haraken
2017/04/13 01:32:31
ToCoreString
zino
2017/04/15 03:30:10
Done.
| |
| 121 } else { | |
| 122 instrument->stringified_capabilities = WTF::g_empty_string; | |
| 123 } | |
| 124 | |
| 125 manager_->SetPaymentInstrument( | |
| 126 instrument_key, std::move(instrument), | |
| 127 ConvertToBaseCallback( | |
| 128 WTF::Bind(&PaymentInstruments::onSetPaymentInstrument, | |
| 129 WrapPersistent(this), WrapPersistent(resolver)))); | |
| 130 return promise; | |
| 39 } | 131 } |
| 40 | 132 |
| 41 DEFINE_TRACE(PaymentInstruments) {} | 133 DEFINE_TRACE(PaymentInstruments) {} |
| 42 | 134 |
| 135 void PaymentInstruments::onGetPaymentInstrument( | |
| 136 ScriptPromiseResolver* resolver, | |
| 137 payments::mojom::blink::PaymentInstrumentPtr stored_instrument, | |
| 138 payments::mojom::blink::PaymentHandlerStatus status) { | |
| 139 DCHECK(resolver); | |
| 140 if (rejectError(resolver, status)) | |
| 141 return; | |
| 142 PaymentInstrument instrument; | |
| 143 instrument.setName(stored_instrument->name); | |
| 144 Vector<WTF::String> enabled_methods; | |
|
haraken
2017/04/13 01:32:31
Drop WTF::.
zino
2017/04/15 03:30:10
Done.
| |
| 145 for (const auto& method : stored_instrument->enabled_methods) { | |
| 146 enabled_methods.push_back(method); | |
| 147 } | |
| 148 | |
| 149 instrument.setEnabledMethods(enabled_methods); | |
| 150 if (!stored_instrument->stringified_capabilities.IsEmpty()) { | |
| 151 ScriptState::Scope scope(resolver->GetScriptState()); | |
| 152 ExceptionState exception_state(resolver->GetScriptState()->GetIsolate(), | |
| 153 ExceptionState::kGetterContext, | |
| 154 "PaymentInstruments", "get"); | |
| 155 instrument.setCapabilities( | |
| 156 ScriptValue(resolver->GetScriptState(), | |
| 157 FromJSONString(resolver->GetScriptState()->GetIsolate(), | |
| 158 stored_instrument->stringified_capabilities, | |
| 159 exception_state))); | |
| 160 if (exception_state.HadException()) { | |
| 161 resolver->Reject(exception_state.GetException()); | |
|
haraken
2017/04/13 01:32:31
I guess you need to call exception_state.Clear().
zino
2017/04/15 03:30:10
Done.
I used exception_state.Reject instead.
The
| |
| 162 return; | |
| 163 } | |
| 164 } | |
| 165 resolver->Resolve(instrument); | |
| 166 } | |
| 167 | |
| 168 void PaymentInstruments::onSetPaymentInstrument( | |
| 169 ScriptPromiseResolver* resolver, | |
| 170 payments::mojom::blink::PaymentHandlerStatus status) { | |
| 171 DCHECK(resolver); | |
| 172 if (rejectError(resolver, status)) | |
| 173 return; | |
| 174 resolver->Resolve(); | |
| 175 } | |
| 176 | |
| 43 } // namespace blink | 177 } // namespace blink |
| OLD | NEW |