Don't preload rarely seen large images
[chromium-blink-merge.git] / storage / browser / fileapi / dragged_file_util.cc
blobff581fe326f04681eda3480754e3229785e060fc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "storage/browser/fileapi/dragged_file_util.h"
7 #include <string>
8 #include <vector>
10 #include "base/files/file_util.h"
11 #include "storage/browser/blob/shareable_file_reference.h"
12 #include "storage/browser/fileapi/file_system_context.h"
13 #include "storage/browser/fileapi/file_system_operation_context.h"
14 #include "storage/browser/fileapi/file_system_url.h"
15 #include "storage/browser/fileapi/isolated_context.h"
16 #include "storage/browser/fileapi/native_file_util.h"
18 namespace storage {
20 typedef IsolatedContext::MountPointInfo FileInfo;
22 namespace {
24 // Simply enumerate each path from a given fileinfo set.
25 // Used to enumerate top-level paths of an isolated filesystem.
26 class SetFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator {
27 public:
28 explicit SetFileEnumerator(const std::vector<FileInfo>& files)
29 : files_(files) {
30 file_iter_ = files_.begin();
32 ~SetFileEnumerator() override {}
34 // AbstractFileEnumerator overrides.
35 base::FilePath Next() override {
36 if (file_iter_ == files_.end())
37 return base::FilePath();
38 base::FilePath platform_file = (file_iter_++)->path;
39 NativeFileUtil::GetFileInfo(platform_file, &file_info_);
40 return platform_file;
42 int64 Size() override { return file_info_.size; }
43 bool IsDirectory() override { return file_info_.is_directory; }
44 base::Time LastModifiedTime() override { return file_info_.last_modified; }
46 private:
47 std::vector<FileInfo> files_;
48 std::vector<FileInfo>::const_iterator file_iter_;
49 base::File::Info file_info_;
52 } // namespace
54 //-------------------------------------------------------------------------
56 DraggedFileUtil::DraggedFileUtil() {}
58 base::File::Error DraggedFileUtil::GetFileInfo(
59 FileSystemOperationContext* context,
60 const FileSystemURL& url,
61 base::File::Info* file_info,
62 base::FilePath* platform_path) {
63 DCHECK(file_info);
64 std::string filesystem_id;
65 DCHECK(url.is_valid());
66 if (url.path().empty()) {
67 // The root directory case.
68 // For now we leave three time fields (modified/accessed/creation time)
69 // NULL as it is not really clear what to be set for this virtual directory.
70 // TODO(kinuko): Maybe we want to set the time when this filesystem is
71 // created (i.e. when the files/directories are dropped).
72 file_info->is_directory = true;
73 file_info->is_symbolic_link = false;
74 file_info->size = 0;
75 return base::File::FILE_OK;
77 base::File::Error error =
78 NativeFileUtil::GetFileInfo(url.path(), file_info);
79 if (base::IsLink(url.path()) && !base::FilePath().IsParent(url.path())) {
80 // Don't follow symlinks unless it's the one that are selected by the user.
81 return base::File::FILE_ERROR_NOT_FOUND;
83 if (error == base::File::FILE_OK)
84 *platform_path = url.path();
85 return error;
88 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator>
89 DraggedFileUtil::CreateFileEnumerator(
90 FileSystemOperationContext* context,
91 const FileSystemURL& root) {
92 DCHECK(root.is_valid());
93 if (!root.path().empty())
94 return LocalFileUtil::CreateFileEnumerator(context, root);
96 // Root path case.
97 std::vector<FileInfo> toplevels;
98 IsolatedContext::GetInstance()->GetDraggedFileInfo(
99 root.filesystem_id(), &toplevels);
100 return scoped_ptr<AbstractFileEnumerator>(new SetFileEnumerator(toplevels));
103 } // namespace storage