FileList是文件组件的表示形式,是一个包装了系列File对象的对象。里面通过vector保持多个文件对象。
class FileList : public ScriptWrappable, public RefCounted<FileList> {
public:
// 需要用create创建该类对象
static Ref<FileList> create()
{ // adoptRef是RefPtr中的函数,实现管理该对象的引用问题。这些不影响理解。
return adoptRef(*new FileList);
}
static Ref<FileList> create(Vector<RefPtr<File>>&& files)
{ // adoptRef需要的是引用类型,需要要加*,WTFMove,WTF是web template framework简称,move是c++ std中提供的东西。
return adoptRef(*new FileList(WTFMove(files)));
}
// 返回文件长度,简单调用vector的size实现
unsigned length() const { return m_files.size(); }
// 返回某个文件对象,见cpp文件
WEBCORE_EXPORT File* item(unsigned index) const;
// 判断文件列表是否为空,简单调用vector对象的isEmpty
bool isEmpty() const { return m_files.isEmpty(); }
// 返回所有文件的路径,见cpp文件
Vector<String> paths() const;
private:
// 不需要显示调用构造函数创建该类对象
FileList();
FileList(Vector<RefPtr<File>>&& files)
// 初始化m_files变量
: m_files(WTFMove(files))
{ }
// FileLists can only be changed by their owners.
friend class DataTransfer;
friend class FileInputType;
// 追加文件
void append(RefPtr<File>&& file) { m_files.append(WTFMove(file)); }
// 删除所有文件
void clear() { m_files.clear(); }
// 是一个类似数组的vector,里面是一系列RefPtr对象,RefPtr对象负责管理File对象的引用问题。
Vector<RefPtr<File>> m_files;
};
FileList.cpp
namespace WebCore {
FileList::FileList()
{
}
// 返回某个文件对象
File* FileList::item(unsigned index) const
{
if (index >= m_files.size())
return 0;
return m_files[index].get();
}
// 返回所有文件的路径
Vector<String> FileList::paths() const
{
Vector<String> paths;
for (unsigned i = 0; i < m_files.size(); ++i)
paths.append(m_files[i]->path());
return paths;
}
} // namespace WebCore