Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/nfc/NFC.h" | 5 #include "modules/nfc/NFC.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "bindings/core/v8/V8ArrayBuffer.h" | 8 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 9 #include "bindings/core/v8/V8StringResource.h" | 9 #include "bindings/core/v8/V8StringResource.h" |
| 10 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 } | 113 } |
| 114 | 114 |
| 115 void setMediaType(NFCRecordPtr& recordPtr, | 115 void setMediaType(NFCRecordPtr& recordPtr, |
| 116 const WTF::String& recordMediaType, | 116 const WTF::String& recordMediaType, |
| 117 const WTF::String& defaultMediaType) { | 117 const WTF::String& defaultMediaType) { |
| 118 recordPtr->media_type = | 118 recordPtr->media_type = |
| 119 recordMediaType.isEmpty() ? defaultMediaType : recordMediaType; | 119 recordMediaType.isEmpty() ? defaultMediaType : recordMediaType; |
| 120 } | 120 } |
| 121 | 121 |
| 122 template <> | 122 template <> |
| 123 struct TypeConverter<mojo::WTFArray<uint8_t>, WTF::String> { | 123 struct TypeConverter<WTF::Vector<uint8_t>, WTF::String> { |
| 124 static mojo::WTFArray<uint8_t> Convert(const WTF::String& string) { | 124 static WTF::Vector<uint8_t> Convert(const WTF::String& string) { |
| 125 WTF::CString utf8String = string.utf8(); | 125 WTF::CString utf8String = string.utf8(); |
| 126 WTF::Vector<uint8_t> array; | 126 WTF::Vector<uint8_t> array; |
| 127 array.append(utf8String.data(), utf8String.length()); | 127 array.append(utf8String.data(), utf8String.length()); |
| 128 return mojo::WTFArray<uint8_t>(std::move(array)); | 128 return WTF::Vector<uint8_t>(std::move(array)); |
|
yzshen1
2017/01/03 21:33:28
you could return array directly.
Sam McNally
2017/01/04 00:30:16
Done.
| |
| 129 } | 129 } |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 template <> | 132 template <> |
| 133 struct TypeConverter<mojo::WTFArray<uint8_t>, blink::DOMArrayBuffer*> { | 133 struct TypeConverter<WTF::Vector<uint8_t>, blink::DOMArrayBuffer*> { |
| 134 static mojo::WTFArray<uint8_t> Convert(blink::DOMArrayBuffer* buffer) { | 134 static WTF::Vector<uint8_t> Convert(blink::DOMArrayBuffer* buffer) { |
| 135 WTF::Vector<uint8_t> array; | 135 WTF::Vector<uint8_t> array; |
| 136 array.append(static_cast<uint8_t*>(buffer->data()), buffer->byteLength()); | 136 array.append(static_cast<uint8_t*>(buffer->data()), buffer->byteLength()); |
| 137 return mojo::WTFArray<uint8_t>(std::move(array)); | 137 return WTF::Vector<uint8_t>(std::move(array)); |
|
yzshen1
2017/01/03 21:33:28
ditto.
Sam McNally
2017/01/04 00:30:16
Done.
| |
| 138 } | 138 } |
| 139 }; | 139 }; |
| 140 | 140 |
| 141 template <> | 141 template <> |
| 142 struct TypeConverter<NFCRecordPtr, WTF::String> { | 142 struct TypeConverter<NFCRecordPtr, WTF::String> { |
| 143 static NFCRecordPtr Convert(const WTF::String& string) { | 143 static NFCRecordPtr Convert(const WTF::String& string) { |
| 144 NFCRecordPtr record = NFCRecord::New(); | 144 NFCRecordPtr record = NFCRecord::New(); |
| 145 record->record_type = NFCRecordType::TEXT; | 145 record->record_type = NFCRecordType::TEXT; |
| 146 record->media_type = kPlainTextMimeType; | 146 record->media_type = kPlainTextMimeType; |
| 147 record->media_type.append(kCharSetUTF8); | 147 record->media_type.append(kCharSetUTF8); |
| 148 record->data = mojo::WTFArray<uint8_t>::From(string).PassStorage(); | 148 record->data = mojo::ConvertTo<WTF::Vector<uint8_t>>(string); |
| 149 return record; | 149 return record; |
| 150 } | 150 } |
| 151 }; | 151 }; |
| 152 | 152 |
| 153 template <> | 153 template <> |
| 154 struct TypeConverter<NFCRecordPtr, blink::DOMArrayBuffer*> { | 154 struct TypeConverter<NFCRecordPtr, blink::DOMArrayBuffer*> { |
| 155 static NFCRecordPtr Convert(blink::DOMArrayBuffer* buffer) { | 155 static NFCRecordPtr Convert(blink::DOMArrayBuffer* buffer) { |
| 156 NFCRecordPtr record = NFCRecord::New(); | 156 NFCRecordPtr record = NFCRecord::New(); |
| 157 record->record_type = NFCRecordType::OPAQUE_RECORD; | 157 record->record_type = NFCRecordType::OPAQUE_RECORD; |
| 158 record->media_type = kOpaqueMimeType; | 158 record->media_type = kOpaqueMimeType; |
| 159 record->data = mojo::WTFArray<uint8_t>::From(buffer).PassStorage(); | 159 record->data = mojo::ConvertTo<WTF::Vector<uint8_t>>(buffer); |
| 160 return record; | 160 return record; |
| 161 } | 161 } |
| 162 }; | 162 }; |
| 163 | 163 |
| 164 template <> | 164 template <> |
| 165 struct TypeConverter<NFCMessagePtr, WTF::String> { | 165 struct TypeConverter<NFCMessagePtr, WTF::String> { |
| 166 static NFCMessagePtr Convert(const WTF::String& string) { | 166 static NFCMessagePtr Convert(const WTF::String& string) { |
| 167 NFCMessagePtr message = NFCMessage::New(); | 167 NFCMessagePtr message = NFCMessage::New(); |
| 168 message->data.append(NFCRecord::From(string)); | 168 message->data.append(NFCRecord::From(string)); |
| 169 return message; | 169 return message; |
| 170 } | 170 } |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 template <> | 173 template <> |
| 174 struct TypeConverter<mojo::WTFArray<uint8_t>, blink::ScriptValue> { | 174 struct TypeConverter<WTF::Optional<WTF::Vector<uint8_t>>, blink::ScriptValue> { |
| 175 static mojo::WTFArray<uint8_t> Convert( | 175 static WTF::Optional<WTF::Vector<uint8_t>> Convert( |
| 176 const blink::ScriptValue& scriptValue) { | 176 const blink::ScriptValue& scriptValue) { |
| 177 v8::Local<v8::Value> value = scriptValue.v8Value(); | 177 v8::Local<v8::Value> value = scriptValue.v8Value(); |
| 178 | 178 |
| 179 if (value->IsNumber()) | 179 if (value->IsNumber()) |
| 180 return mojo::WTFArray<uint8_t>::From( | 180 return mojo::ConvertTo<WTF::Vector<uint8_t>>( |
| 181 WTF::String::number(value.As<v8::Number>()->Value())); | 181 WTF::String::number(value.As<v8::Number>()->Value())); |
| 182 | 182 |
| 183 if (value->IsString()) { | 183 if (value->IsString()) { |
| 184 blink::V8StringResource<> stringResource = value; | 184 blink::V8StringResource<> stringResource = value; |
| 185 if (stringResource.prepare()) | 185 if (stringResource.prepare()) { |
| 186 return mojo::WTFArray<uint8_t>::From<WTF::String>(stringResource); | 186 return mojo::ConvertTo<WTF::Vector<uint8_t>>( |
| 187 WTF::String(stringResource)); | |
| 188 } | |
| 187 } | 189 } |
| 188 | 190 |
| 189 if (value->IsObject() && !value->IsArray() && !value->IsArrayBuffer()) { | 191 if (value->IsObject() && !value->IsArray() && !value->IsArrayBuffer()) { |
| 190 v8::Local<v8::String> jsonString; | 192 v8::Local<v8::String> jsonString; |
| 191 if (v8::JSON::Stringify(scriptValue.context(), value.As<v8::Object>()) | 193 if (v8::JSON::Stringify(scriptValue.context(), value.As<v8::Object>()) |
| 192 .ToLocal(&jsonString)) { | 194 .ToLocal(&jsonString)) { |
| 193 WTF::String wtfString = blink::v8StringToWebCoreString<WTF::String>( | 195 WTF::String wtfString = blink::v8StringToWebCoreString<WTF::String>( |
| 194 jsonString, blink::DoNotExternalize); | 196 jsonString, blink::DoNotExternalize); |
| 195 return mojo::WTFArray<uint8_t>::From(wtfString); | 197 return mojo::ConvertTo<WTF::Vector<uint8_t>>(wtfString); |
| 196 } | 198 } |
| 197 } | 199 } |
| 198 | 200 |
| 199 if (value->IsArrayBuffer()) | 201 if (value->IsArrayBuffer()) |
| 200 return mojo::WTFArray<uint8_t>::From( | 202 return mojo::ConvertTo<WTF::Vector<uint8_t>>( |
| 201 blink::V8ArrayBuffer::toImpl(value.As<v8::Object>())); | 203 blink::V8ArrayBuffer::toImpl(value.As<v8::Object>())); |
| 202 | 204 |
| 203 return nullptr; | 205 return WTF::nullopt; |
| 204 } | 206 } |
| 205 }; | 207 }; |
| 206 | 208 |
| 207 template <> | 209 template <> |
| 208 struct TypeConverter<NFCRecordPtr, blink::NFCRecord> { | 210 struct TypeConverter<NFCRecordPtr, blink::NFCRecord> { |
| 209 static NFCRecordPtr Convert(const blink::NFCRecord& record) { | 211 static NFCRecordPtr Convert(const blink::NFCRecord& record) { |
| 210 NFCRecordPtr recordPtr = NFCRecord::New(); | 212 NFCRecordPtr recordPtr = NFCRecord::New(); |
| 211 | 213 |
| 212 if (record.hasRecordType()) | 214 if (record.hasRecordType()) |
| 213 recordPtr->record_type = toNFCRecordType(record.recordType()); | 215 recordPtr->record_type = toNFCRecordType(record.recordType()); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 229 setMediaType(recordPtr, record.mediaType(), kJsonMimeType); | 231 setMediaType(recordPtr, record.mediaType(), kJsonMimeType); |
| 230 break; | 232 break; |
| 231 case NFCRecordType::OPAQUE_RECORD: | 233 case NFCRecordType::OPAQUE_RECORD: |
| 232 setMediaType(recordPtr, record.mediaType(), kOpaqueMimeType); | 234 setMediaType(recordPtr, record.mediaType(), kOpaqueMimeType); |
| 233 break; | 235 break; |
| 234 default: | 236 default: |
| 235 NOTREACHED(); | 237 NOTREACHED(); |
| 236 break; | 238 break; |
| 237 } | 239 } |
| 238 | 240 |
| 239 auto recordData = mojo::WTFArray<uint8_t>::From(record.data()); | 241 auto recordData = |
| 242 mojo::ConvertTo<WTF::Optional<WTF::Vector<uint8_t>>>(record.data()); | |
|
haraken
2016/12/29 11:42:35
Why do we need Optional? Maybe can we simply check
Sam McNally
2016/12/30 00:21:14
We need to be able to distinguish am invalid value
| |
| 240 // If JS object cannot be converted to uint8_t array, return null, | 243 // If JS object cannot be converted to uint8_t array, return null, |
| 241 // interrupt NFCMessage conversion algorithm and reject promise with | 244 // interrupt NFCMessage conversion algorithm and reject promise with |
| 242 // SyntaxError exception. | 245 // SyntaxError exception. |
| 243 if (recordData.is_null()) | 246 if (!recordData) |
| 244 return nullptr; | 247 return nullptr; |
| 245 | 248 |
| 246 recordPtr->data = recordData.PassStorage(); | 249 recordPtr->data = recordData.value(); |
| 247 return recordPtr; | 250 return recordPtr; |
| 248 } | 251 } |
| 249 }; | 252 }; |
| 250 | 253 |
| 251 template <> | 254 template <> |
| 252 struct TypeConverter<NFCMessagePtr, blink::NFCMessage> { | 255 struct TypeConverter<NFCMessagePtr, blink::NFCMessage> { |
| 253 static NFCMessagePtr Convert(const blink::NFCMessage& message) { | 256 static NFCMessagePtr Convert(const blink::NFCMessage& message) { |
| 254 NFCMessagePtr messagePtr = NFCMessage::New(); | 257 NFCMessagePtr messagePtr = NFCMessage::New(); |
| 255 messagePtr->url = message.url(); | 258 messagePtr->url = message.url(); |
| 256 messagePtr->data.resize(message.data().size()); | 259 messagePtr->data.resize(message.data().size()); |
| (...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 815 } | 818 } |
| 816 | 819 |
| 817 DEFINE_TRACE(NFC) { | 820 DEFINE_TRACE(NFC) { |
| 818 PageVisibilityObserver::trace(visitor); | 821 PageVisibilityObserver::trace(visitor); |
| 819 ContextLifecycleObserver::trace(visitor); | 822 ContextLifecycleObserver::trace(visitor); |
| 820 visitor->trace(m_requests); | 823 visitor->trace(m_requests); |
| 821 visitor->trace(m_callbacks); | 824 visitor->trace(m_callbacks); |
| 822 } | 825 } |
| 823 | 826 |
| 824 } // namespace blink | 827 } // namespace blink |
| OLD | NEW |