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 "bindings/modules/v8/UnionTypesModules.h" |
12 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
13 #include "core/fileapi/Blob.h" | 13 #include "core/fileapi/Blob.h" |
14 #include "platform/blob/BlobData.h" | 14 #include "platform/blob/BlobData.h" |
15 #include "wtf/text/TextEncoding.h" | 15 #include "wtf/text/TextEncoding.h" |
16 #include <v8.h> | 16 #include <v8.h> |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 PushMessageData* PushMessageData::create(const String& messageString) | 20 PushMessageData* PushMessageData::create(const String& messageString, ExceptionS
tate& exceptionState) |
21 { | 21 { |
22 return PushMessageData::create(ArrayBufferOrArrayBufferViewOrUSVString::from
USVString(messageString)); | 22 return PushMessageData::create(ArrayBufferOrArrayBufferViewOrUSVString::from
USVString(messageString), exceptionState); |
23 } | 23 } |
24 | 24 |
25 PushMessageData* PushMessageData::create(const ArrayBufferOrArrayBufferViewOrUSV
String& messageData) | 25 PushMessageData* PushMessageData::create(const ArrayBufferOrArrayBufferViewOrUSV
String& messageData, ExceptionState& exceptionState) |
26 { | 26 { |
27 if (messageData.isArrayBuffer() || messageData.isArrayBufferView()) { | 27 if (messageData.isArrayBuffer() || messageData.isArrayBufferView()) { |
28 RefPtr<DOMArrayBuffer> buffer = messageData.isArrayBufferView() | 28 RefPtr<DOMArrayBuffer> buffer = messageData.isArrayBufferView() |
29 ? messageData.getAsArrayBufferView()->buffer() | 29 ? messageData.getAsArrayBufferView()->bufferOrNull() |
30 : messageData.getAsArrayBuffer(); | 30 : messageData.getAsArrayBuffer(); |
31 | 31 |
| 32 // TODO(junov): crbug.com/536816 |
| 33 // Instead of crashing when buffer allocation fails, we should consider |
| 34 // throwing a RangeError exception. It is what the ECMAScript spec says
to do: |
| 35 // http://ecma-international.org/ecma-262/6.0/#sec-createbytedatablock |
| 36 // However the PushMessageData specification does not state that such |
| 37 // exceptions should be re-thrown. |
| 38 RELEASE_ASSERT(buffer); // This is essentially an out-of-memory crash |
32 return new PushMessageData(static_cast<const char*>(buffer->data()), buf
fer->byteLength()); | 39 return new PushMessageData(static_cast<const char*>(buffer->data()), buf
fer->byteLength()); |
33 } | 40 } |
34 | 41 |
35 if (messageData.isUSVString()) { | 42 if (messageData.isUSVString()) { |
36 CString encodedString = UTF8Encoding().encode(messageData.getAsUSVString
(), WTF::EntitiesForUnencodables); | 43 CString encodedString = UTF8Encoding().encode(messageData.getAsUSVString
(), WTF::EntitiesForUnencodables); |
37 return new PushMessageData(encodedString.data(), encodedString.length())
; | 44 return new PushMessageData(encodedString.data(), encodedString.length())
; |
38 } | 45 } |
39 | 46 |
40 ASSERT(messageData.isNull()); | 47 ASSERT(messageData.isNull()); |
41 return new PushMessageData(); | 48 return new PushMessageData(); |
42 } | 49 } |
43 | 50 |
44 PushMessageData::PushMessageData() | 51 PushMessageData::PushMessageData() |
45 { | 52 { |
46 } | 53 } |
47 | 54 |
48 PushMessageData::PushMessageData(const char* data, unsigned bytesSize) | 55 PushMessageData::PushMessageData(const char* data, unsigned bytesSize) |
49 { | 56 { |
50 m_data.append(data, bytesSize); | 57 m_data.append(data, bytesSize); |
51 } | 58 } |
52 | 59 |
53 PushMessageData::~PushMessageData() | 60 PushMessageData::~PushMessageData() |
54 { | 61 { |
55 } | 62 } |
56 | 63 |
57 PassRefPtr<DOMArrayBuffer> PushMessageData::arrayBuffer() const | 64 PassRefPtr<DOMArrayBuffer> PushMessageData::arrayBuffer() const |
58 { | 65 { |
59 return DOMArrayBuffer::create(m_data.data(), m_data.size()); | 66 // TODO(junov): crbug.com/536816 |
| 67 // Use createOrNull instead of deprecatedCreateOrCrash. Requires |
| 68 // defining behavior for when allocation fails. ECMAScript spec says |
| 69 // allocation failure should throw a RangeError exception, but the |
| 70 // spec for PushMessageData.arrayBuffer() does not state that |
| 71 // such exceptions should be re-thrown. So for now, we just crash. |
| 72 RefPtr<DOMArrayBuffer> buffer = DOMArrayBuffer::deprecatedCreateOrCrash(m_da
ta.data(), m_data.size()); |
| 73 return buffer.release(); |
60 } | 74 } |
61 | 75 |
62 Blob* PushMessageData::blob() const | 76 Blob* PushMessageData::blob() const |
63 { | 77 { |
64 OwnPtr<BlobData> blobData = BlobData::create(); | 78 OwnPtr<BlobData> blobData = BlobData::create(); |
65 blobData->appendBytes(m_data.data(), m_data.size()); | 79 blobData->appendBytes(m_data.data(), m_data.size()); |
66 | 80 |
67 // Note that the content type of the Blob object is deliberately not being | 81 // Note that the content type of the Blob object is deliberately not being |
68 // provided, following the specification. | 82 // provided, following the specification. |
69 | 83 |
(...skipping 21 matching lines...) Expand all Loading... |
91 String PushMessageData::text() const | 105 String PushMessageData::text() const |
92 { | 106 { |
93 return UTF8Encoding().decode(m_data.data(), m_data.size()); | 107 return UTF8Encoding().decode(m_data.data(), m_data.size()); |
94 } | 108 } |
95 | 109 |
96 DEFINE_TRACE(PushMessageData) | 110 DEFINE_TRACE(PushMessageData) |
97 { | 111 { |
98 } | 112 } |
99 | 113 |
100 } // namespace blink | 114 } // namespace blink |
OLD | NEW |