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/push_messaging/PushMessageData.h" | 6 #include "modules/push_messaging/PushMessageData.h" |
7 | 7 |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
| 11 #include "bindings/modules/v8/UnionTypesModules.h" |
11 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
12 #include "core/fileapi/Blob.h" | 13 #include "core/fileapi/Blob.h" |
13 #include "platform/blob/BlobData.h" | 14 #include "platform/blob/BlobData.h" |
| 15 #include "wtf/text/TextEncoding.h" |
14 #include <v8.h> | 16 #include <v8.h> |
15 | 17 |
16 namespace blink { | 18 namespace blink { |
17 | 19 |
| 20 PushMessageData* PushMessageData::create(const String& messageString) |
| 21 { |
| 22 return PushMessageData::create(ArrayBufferOrArrayBufferViewOrUSVString::from
USVString(messageString)); |
| 23 } |
| 24 |
| 25 PushMessageData* PushMessageData::create(const ArrayBufferOrArrayBufferViewOrUSV
String& messageData) |
| 26 { |
| 27 if (messageData.isArrayBuffer() || messageData.isArrayBufferView()) { |
| 28 RefPtr<DOMArrayBuffer> buffer = messageData.isArrayBufferView() |
| 29 ? messageData.getAsArrayBufferView()->buffer() |
| 30 : messageData.getAsArrayBuffer(); |
| 31 |
| 32 return new PushMessageData(static_cast<const char*>(buffer->data()), buf
fer->byteLength()); |
| 33 } |
| 34 |
| 35 if (messageData.isUSVString()) { |
| 36 CString encodedString = UTF8Encoding().normalizeAndEncode(messageData.ge
tAsUSVString(), WTF::EntitiesForUnencodables); |
| 37 return new PushMessageData(encodedString.data(), encodedString.length())
; |
| 38 } |
| 39 |
| 40 ASSERT(messageData.isNull()); |
| 41 return new PushMessageData(); |
| 42 } |
| 43 |
18 PushMessageData::PushMessageData() | 44 PushMessageData::PushMessageData() |
19 { | 45 { |
20 } | 46 } |
21 | 47 |
22 PushMessageData::PushMessageData(const String& messageData) | 48 PushMessageData::PushMessageData(const char* data, unsigned bytesSize) |
23 : m_messageData(messageData) | |
24 { | 49 { |
| 50 m_data.append(data, bytesSize); |
25 } | 51 } |
26 | 52 |
27 PushMessageData::~PushMessageData() | 53 PushMessageData::~PushMessageData() |
28 { | 54 { |
29 } | 55 } |
30 | 56 |
31 PassRefPtr<DOMArrayBuffer> PushMessageData::arrayBuffer() const | 57 PassRefPtr<DOMArrayBuffer> PushMessageData::arrayBuffer() const |
32 { | 58 { |
33 return DOMArrayBuffer::create(m_messageData.characters8(), m_messageData.len
gth()); | 59 return DOMArrayBuffer::create(m_data.data(), m_data.size()); |
34 } | 60 } |
35 | 61 |
36 Blob* PushMessageData::blob() const | 62 Blob* PushMessageData::blob() const |
37 { | 63 { |
38 OwnPtr<BlobData> blobData = BlobData::create(); | 64 OwnPtr<BlobData> blobData = BlobData::create(); |
39 blobData->appendText(m_messageData, false); | 65 blobData->appendBytes(m_data.data(), m_data.size()); |
40 | 66 |
41 // Note that the content type of the Blob object is deliberately not being | 67 // Note that the content type of the Blob object is deliberately not being |
42 // provided, following the specification. | 68 // provided, following the specification. |
43 | 69 |
44 const long long blobSize = blobData->length(); | 70 const long long byteLength = blobData->length(); |
45 return Blob::create(BlobDataHandle::create(blobData.release(), blobSize)); | 71 return Blob::create(BlobDataHandle::create(blobData.release(), byteLength)); |
46 } | 72 } |
47 | 73 |
48 ScriptValue PushMessageData::json(ScriptState* scriptState, ExceptionState& exce
ptionState) const | 74 ScriptValue PushMessageData::json(ScriptState* scriptState, ExceptionState& exce
ptionState) const |
49 { | 75 { |
50 v8::Isolate* isolate = scriptState->isolate(); | 76 v8::Isolate* isolate = scriptState->isolate(); |
51 | 77 |
52 ScriptState::Scope scope(scriptState); | 78 ScriptState::Scope scope(scriptState); |
53 v8::Local<v8::String> dataString = v8String(isolate, m_messageData); | 79 v8::Local<v8::String> dataString = v8String(isolate, text()); |
54 | 80 |
55 v8::TryCatch block; | 81 v8::TryCatch block; |
56 v8::Local<v8::Value> parsed; | 82 v8::Local<v8::Value> parsed; |
57 if (!v8Call(v8::JSON::Parse(isolate, dataString), parsed, block)) { | 83 if (!v8Call(v8::JSON::Parse(isolate, dataString), parsed, block)) { |
58 exceptionState.rethrowV8Exception(block.Exception()); | 84 exceptionState.rethrowV8Exception(block.Exception()); |
59 return ScriptValue(); | 85 return ScriptValue(); |
60 } | 86 } |
61 | 87 |
62 return ScriptValue(scriptState, parsed); | 88 return ScriptValue(scriptState, parsed); |
63 } | 89 } |
64 | 90 |
65 const String& PushMessageData::text() const | 91 String PushMessageData::text() const |
66 { | 92 { |
67 return m_messageData; | 93 return UTF8Encoding().decode(m_data.data(), m_data.size()); |
68 } | 94 } |
69 | 95 |
70 DEFINE_TRACE(PushMessageData) | 96 DEFINE_TRACE(PushMessageData) |
71 { | 97 { |
72 } | 98 } |
73 | 99 |
74 } // namespace blink | 100 } // namespace blink |
OLD | NEW |