1 | /* |
2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * * Neither the name of Google Inc. nor the names of its |
16 | * contributors may be used to endorse or promote products derived from |
17 | * this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #pragma once |
33 | |
34 | #include "DataTransfer.h" |
35 | #include "ExceptionOr.h" |
36 | #include "ScriptWrappable.h" |
37 | #include <wtf/Forward.h> |
38 | #include <wtf/Ref.h> |
39 | #include <wtf/RefCounted.h> |
40 | #include <wtf/WeakPtr.h> |
41 | |
42 | namespace WebCore { |
43 | |
44 | class DataTransferItem; |
45 | class File; |
46 | |
47 | class DataTransferItemList final : public ScriptWrappable, public CanMakeWeakPtr<DataTransferItemList> { |
48 | WTF_MAKE_NONCOPYABLE(DataTransferItemList); |
49 | WTF_MAKE_ISO_ALLOCATED(DataTransferItemList); |
50 | public: |
51 | DataTransferItemList(DataTransfer&); |
52 | ~DataTransferItemList(); |
53 | |
54 | // DataTransfer owns DataTransferItemList, and DataTransfer is kept alive as long as DataTransferItemList is alive. |
55 | void ref() { m_dataTransfer.ref(); } |
56 | void deref() { m_dataTransfer.deref(); } |
57 | DataTransfer& dataTransfer() { return m_dataTransfer; } |
58 | |
59 | // DOM API |
60 | unsigned length() const; |
61 | RefPtr<DataTransferItem> item(unsigned index); |
62 | ExceptionOr<RefPtr<DataTransferItem>> add(const String& data, const String& type); |
63 | RefPtr<DataTransferItem> add(Ref<File>&&); |
64 | ExceptionOr<void> remove(unsigned index); |
65 | void clear(); |
66 | |
67 | void didClearStringData(const String& type); |
68 | void didSetStringData(const String& type); |
69 | bool hasItems() const { return m_items.hasValue(); } |
70 | const Vector<Ref<DataTransferItem>>& items() const |
71 | { |
72 | ASSERT(m_items); |
73 | return *m_items; |
74 | } |
75 | |
76 | private: |
77 | Vector<Ref<DataTransferItem>>& ensureItems() const; |
78 | |
79 | DataTransfer& m_dataTransfer; |
80 | mutable Optional<Vector<Ref<DataTransferItem>>> m_items; |
81 | }; |
82 | |
83 | } // namespace WebCore |
84 | |