Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(968)

Side by Side Diff: webkit/fileapi/media/native_media_file_util.cc

Issue 14247034: Move Media Galleries FileAPI code out of webkit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cr-14352004
Patch Set: Add android ifdef. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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.
4
5 #include "webkit/fileapi/media/native_media_file_util.h"
6
7 #include "webkit/fileapi/file_system_operation_context.h"
8 #include "webkit/fileapi/media/filtering_file_enumerator.h"
9 #include "webkit/fileapi/media/media_file_system_mount_point_provider.h"
10 #include "webkit/fileapi/media/media_path_filter.h"
11 #include "webkit/fileapi/native_file_util.h"
12
13 using base::PlatformFile;
14 using base::PlatformFileError;
15 using base::PlatformFileInfo;
16
17 namespace fileapi {
18
19 namespace {
20
21 MediaPathFilter* GetMediaPathFilter(FileSystemOperationContext* context) {
22 return context->GetUserValue<MediaPathFilter*>(
23 MediaFileSystemMountPointProvider::kMediaPathFilterKey);
24 }
25
26 } // namespace
27
28 NativeMediaFileUtil::NativeMediaFileUtil() {
29 }
30
31 PlatformFileError NativeMediaFileUtil::CreateOrOpen(
32 FileSystemOperationContext* context,
33 const FileSystemURL& url,
34 int file_flags,
35 PlatformFile* file_handle,
36 bool* created) {
37 // Only called by NaCl, which should not have access to media file systems.
38 return base::PLATFORM_FILE_ERROR_SECURITY;
39 }
40
41 PlatformFileError NativeMediaFileUtil::EnsureFileExists(
42 FileSystemOperationContext* context,
43 const FileSystemURL& url, bool* created) {
44 base::FilePath file_path;
45 PlatformFileError error = GetFilteredLocalFilePath(context, url, &file_path);
46 if (error != base::PLATFORM_FILE_OK)
47 return error;
48 return NativeFileUtil::EnsureFileExists(file_path, created);
49 }
50
51 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator>
52 NativeMediaFileUtil::CreateFileEnumerator(
53 FileSystemOperationContext* context,
54 const FileSystemURL& root_url) {
55 DCHECK(context);
56 return make_scoped_ptr(new FilteringFileEnumerator(
57 IsolatedFileUtil::CreateFileEnumerator(context, root_url),
58 GetMediaPathFilter(context)))
59 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>();
60 }
61
62 PlatformFileError NativeMediaFileUtil::Touch(
63 FileSystemOperationContext* context,
64 const FileSystemURL& url,
65 const base::Time& last_access_time,
66 const base::Time& last_modified_time) {
67 base::FilePath file_path;
68 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory(
69 context,
70 url,
71 // Touch fails for non-existent paths and filtered paths.
72 base::PLATFORM_FILE_ERROR_FAILED,
73 &file_path);
74 if (error != base::PLATFORM_FILE_OK)
75 return error;
76 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time);
77 }
78
79 PlatformFileError NativeMediaFileUtil::Truncate(
80 FileSystemOperationContext* context,
81 const FileSystemURL& url,
82 int64 length) {
83 base::FilePath file_path;
84 PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory(
85 context,
86 url,
87 // Cannot truncate paths that do not exist, or are filtered.
88 base::PLATFORM_FILE_ERROR_NOT_FOUND,
89 &file_path);
90 if (error != base::PLATFORM_FILE_OK)
91 return error;
92 return NativeFileUtil::Truncate(file_path, length);
93 }
94
95 PlatformFileError NativeMediaFileUtil::CopyOrMoveFile(
96 FileSystemOperationContext* context,
97 const FileSystemURL& src_url,
98 const FileSystemURL& dest_url,
99 bool copy) {
100 base::FilePath src_file_path;
101 PlatformFileError error =
102 GetFilteredLocalFilePathForExistingFileOrDirectory(
103 context, src_url,
104 base::PLATFORM_FILE_ERROR_NOT_FOUND,
105 &src_file_path);
106 if (error != base::PLATFORM_FILE_OK)
107 return error;
108 if (NativeFileUtil::DirectoryExists(src_file_path))
109 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
110
111 base::FilePath dest_file_path;
112 error = GetLocalFilePath(context, dest_url, &dest_file_path);
113 if (error != base::PLATFORM_FILE_OK)
114 return error;
115 PlatformFileInfo file_info;
116 error = NativeFileUtil::GetFileInfo(dest_file_path, &file_info);
117 if (error != base::PLATFORM_FILE_OK &&
118 error != base::PLATFORM_FILE_ERROR_NOT_FOUND)
119 return error;
120 if (error == base::PLATFORM_FILE_OK && file_info.is_directory)
121 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
122 if (!GetMediaPathFilter(context)->Match(dest_file_path))
123 return base::PLATFORM_FILE_ERROR_SECURITY;
124
125 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy);
126 }
127
128 PlatformFileError NativeMediaFileUtil::CopyInForeignFile(
129 FileSystemOperationContext* context,
130 const base::FilePath& src_file_path,
131 const FileSystemURL& dest_url) {
132 if (src_file_path.empty())
133 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
134
135 base::FilePath dest_file_path;
136 PlatformFileError error =
137 GetFilteredLocalFilePath(context, dest_url, &dest_file_path);
138 if (error != base::PLATFORM_FILE_OK)
139 return error;
140 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true);
141 }
142
143 PlatformFileError NativeMediaFileUtil::DeleteFile(
144 FileSystemOperationContext* context,
145 const FileSystemURL& url) {
146 base::FilePath file_path;
147 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
148 if (error != base::PLATFORM_FILE_OK)
149 return error;
150 PlatformFileInfo file_info;
151 error = NativeFileUtil::GetFileInfo(file_path, &file_info);
152 if (error != base::PLATFORM_FILE_OK)
153 return error;
154 if (file_info.is_directory)
155 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
156 if (!GetMediaPathFilter(context)->Match(file_path))
157 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
158 return NativeFileUtil::DeleteFile(file_path);
159 }
160
161 PlatformFileError NativeMediaFileUtil::GetFileInfo(
162 FileSystemOperationContext* context,
163 const FileSystemURL& url,
164 PlatformFileInfo* file_info,
165 base::FilePath* platform_path) {
166 DCHECK(context);
167 DCHECK(GetMediaPathFilter(context));
168 DCHECK(file_info);
169 DCHECK(platform_path);
170
171 base::PlatformFileError error =
172 IsolatedFileUtil::GetFileInfo(context, url, file_info, platform_path);
173 if (error != base::PLATFORM_FILE_OK)
174 return error;
175
176 if (file_info->is_directory ||
177 GetMediaPathFilter(context)->Match(*platform_path)) {
178 return base::PLATFORM_FILE_OK;
179 }
180 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
181 }
182
183 PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath(
184 FileSystemOperationContext* context,
185 const FileSystemURL& file_system_url,
186 base::FilePath* local_file_path) {
187 base::FilePath file_path;
188 PlatformFileError error =
189 IsolatedFileUtil::GetLocalFilePath(context, file_system_url, &file_path);
190 if (error != base::PLATFORM_FILE_OK)
191 return error;
192 if (!GetMediaPathFilter(context)->Match(file_path))
193 return base::PLATFORM_FILE_ERROR_SECURITY;
194
195 *local_file_path = file_path;
196 return base::PLATFORM_FILE_OK;
197 }
198
199 PlatformFileError
200 NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory(
201 FileSystemOperationContext* context,
202 const FileSystemURL& file_system_url,
203 PlatformFileError failure_error,
204 base::FilePath* local_file_path) {
205 base::FilePath file_path;
206 PlatformFileError error =
207 GetLocalFilePath(context, file_system_url, &file_path);
208 if (error != base::PLATFORM_FILE_OK)
209 return error;
210
211 if (!file_util::PathExists(file_path))
212 return failure_error;
213 PlatformFileInfo file_info;
214 if (!file_util::GetFileInfo(file_path, &file_info))
215 return base::PLATFORM_FILE_ERROR_FAILED;
216
217 if (!file_info.is_directory &&
218 !GetMediaPathFilter(context)->Match(file_path)) {
219 return failure_error;
220 }
221
222 *local_file_path = file_path;
223 return base::PLATFORM_FILE_OK;
224 }
225
226 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/media/native_media_file_util.h ('k') | webkit/fileapi/media/native_media_file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698