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

Side by Side Diff: webkit/tools/test_shell/simple_file_system.cc

Issue 9380040: Revert 121620 - Refactor FileSystemOperation to take callback for each method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "webkit/tools/test_shell/simple_file_system.h" 5 #include "webkit/tools/test_shell/simple_file_system.h"
6 6
7 #include "base/bind.h"
8 #include "base/file_path.h" 7 #include "base/file_path.h"
9 #include "base/message_loop.h" 8 #include "base/message_loop.h"
10 #include "base/message_loop_proxy.h" 9 #include "base/message_loop_proxy.h"
11 #include "base/time.h" 10 #include "base/time.h"
12 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
13 #include "googleurl/src/gurl.h" 12 #include "googleurl/src/gurl.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileInfo.h" 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileInfo.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemCallback s.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemCallback s.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemEntry.h" 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemEntry.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
21 #include "webkit/fileapi/file_system_callback_dispatcher.h"
22 #include "webkit/fileapi/file_system_context.h"
23 #include "webkit/fileapi/file_system_operation_interface.h"
24 #include "webkit/fileapi/file_system_types.h"
22 #include "webkit/fileapi/mock_file_system_options.h" 25 #include "webkit/fileapi/mock_file_system_options.h"
23 #include "webkit/glue/webkit_glue.h" 26 #include "webkit/glue/webkit_glue.h"
24 #include "webkit/tools/test_shell/simple_file_writer.h" 27 #include "webkit/tools/test_shell/simple_file_writer.h"
25 28
26 using base::WeakPtr; 29 using base::WeakPtr;
27 30
28 using WebKit::WebFileInfo; 31 using WebKit::WebFileInfo;
29 using WebKit::WebFileSystem; 32 using WebKit::WebFileSystem;
30 using WebKit::WebFileSystemCallbacks; 33 using WebKit::WebFileSystemCallbacks;
31 using WebKit::WebFileSystemEntry; 34 using WebKit::WebFileSystemEntry;
32 using WebKit::WebFileWriter; 35 using WebKit::WebFileWriter;
33 using WebKit::WebFileWriterClient; 36 using WebKit::WebFileWriterClient;
34 using WebKit::WebFrame; 37 using WebKit::WebFrame;
35 using WebKit::WebSecurityOrigin; 38 using WebKit::WebSecurityOrigin;
36 using WebKit::WebString; 39 using WebKit::WebString;
37 using WebKit::WebURL; 40 using WebKit::WebURL;
38 using WebKit::WebVector; 41 using WebKit::WebVector;
39 42
43 using fileapi::FileSystemCallbackDispatcher;
40 using fileapi::FileSystemContext; 44 using fileapi::FileSystemContext;
41 using fileapi::FileSystemOperationInterface; 45 using fileapi::FileSystemOperationInterface;
42 46
47 namespace {
48
49 class SimpleFileSystemCallbackDispatcher
50 : public FileSystemCallbackDispatcher {
51 public:
52 // An instance of this class must be created by Create()
53 // (so that we do not leak ownerships).
54 static scoped_ptr<FileSystemCallbackDispatcher> Create(
55 const WeakPtr<SimpleFileSystem>& file_system,
56 WebFileSystemCallbacks* callbacks) {
57 return scoped_ptr<FileSystemCallbackDispatcher>(
58 new SimpleFileSystemCallbackDispatcher(file_system, callbacks));
59 }
60
61 ~SimpleFileSystemCallbackDispatcher() {
62 }
63
64 virtual void DidSucceed() {
65 DCHECK(file_system_);
66 callbacks_->didSucceed();
67 }
68
69 virtual void DidReadMetadata(const base::PlatformFileInfo& info,
70 const FilePath& platform_path) {
71 DCHECK(file_system_);
72 WebFileInfo web_file_info;
73 web_file_info.length = info.size;
74 web_file_info.modificationTime = info.last_modified.ToDoubleT();
75 web_file_info.type = info.is_directory ?
76 WebFileInfo::TypeDirectory : WebFileInfo::TypeFile;
77 web_file_info.platformPath =
78 webkit_glue::FilePathToWebString(platform_path);
79 callbacks_->didReadMetadata(web_file_info);
80 }
81
82 virtual void DidReadDirectory(
83 const std::vector<base::FileUtilProxy::Entry>& entries,
84 bool has_more) {
85 DCHECK(file_system_);
86 std::vector<WebFileSystemEntry> web_entries_vector;
87 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it =
88 entries.begin(); it != entries.end(); ++it) {
89 WebFileSystemEntry entry;
90 entry.name = webkit_glue::FilePathStringToWebString(it->name);
91 entry.isDirectory = it->is_directory;
92 web_entries_vector.push_back(entry);
93 }
94 WebVector<WebKit::WebFileSystemEntry> web_entries =
95 web_entries_vector;
96 callbacks_->didReadDirectory(web_entries, has_more);
97 }
98
99 virtual void DidOpenFileSystem(
100 const std::string& name, const GURL& root) {
101 DCHECK(file_system_);
102 if (!root.is_valid())
103 callbacks_->didFail(WebKit::WebFileErrorSecurity);
104 else
105 callbacks_->didOpenFileSystem(WebString::fromUTF8(name), root);
106 }
107
108 virtual void DidFail(base::PlatformFileError error_code) {
109 DCHECK(file_system_);
110 callbacks_->didFail(
111 webkit_glue::PlatformFileErrorToWebFileError(error_code));
112 }
113
114 virtual void DidWrite(int64, bool) {
115 NOTREACHED();
116 }
117
118 private:
119 SimpleFileSystemCallbackDispatcher(
120 const WeakPtr<SimpleFileSystem>& file_system,
121 WebFileSystemCallbacks* callbacks)
122 : file_system_(file_system),
123 callbacks_(callbacks) {
124 }
125
126 WeakPtr<SimpleFileSystem> file_system_;
127 WebFileSystemCallbacks* callbacks_;
128 };
129
130 } // namespace
131
43 SimpleFileSystem::SimpleFileSystem() { 132 SimpleFileSystem::SimpleFileSystem() {
44 if (file_system_dir_.CreateUniqueTempDir()) { 133 if (file_system_dir_.CreateUniqueTempDir()) {
45 file_system_context_ = new FileSystemContext( 134 file_system_context_ = new FileSystemContext(
46 base::MessageLoopProxy::current(), 135 base::MessageLoopProxy::current(),
47 base::MessageLoopProxy::current(), 136 base::MessageLoopProxy::current(),
48 NULL /* special storage policy */, 137 NULL /* special storage policy */,
49 NULL /* quota manager */, 138 NULL /* quota manager */,
50 file_system_dir_.path(), 139 file_system_dir_.path(),
51 fileapi::CreateAllowFileAccessOptions()); 140 fileapi::CreateAllowFileAccessOptions());
52 } else { 141 } else {
(...skipping 23 matching lines...) Expand all
76 else if (web_filesystem_type == WebFileSystem::TypeExternal) 165 else if (web_filesystem_type == WebFileSystem::TypeExternal)
77 type = fileapi::kFileSystemTypeExternal; 166 type = fileapi::kFileSystemTypeExternal;
78 else { 167 else {
79 // Unknown type filesystem is requested. 168 // Unknown type filesystem is requested.
80 callbacks->didFail(WebKit::WebFileErrorSecurity); 169 callbacks->didFail(WebKit::WebFileErrorSecurity);
81 return; 170 return;
82 } 171 }
83 172
84 GURL origin_url(frame->document().securityOrigin().toString()); 173 GURL origin_url(frame->document().securityOrigin().toString());
85 file_system_context_->OpenFileSystem( 174 file_system_context_->OpenFileSystem(
86 origin_url, type, create, OpenFileSystemHandler(callbacks)); 175 origin_url, type, create,
176 SimpleFileSystemCallbackDispatcher::Create(AsWeakPtr(), callbacks));
87 } 177 }
88 178
89 void SimpleFileSystem::move( 179 void SimpleFileSystem::move(
90 const WebURL& src_path, 180 const WebURL& src_path,
91 const WebURL& dest_path, WebFileSystemCallbacks* callbacks) { 181 const WebURL& dest_path, WebFileSystemCallbacks* callbacks) {
92 GetNewOperation(src_path)->Move(GURL(src_path), GURL(dest_path), 182 GetNewOperation(src_path, callbacks)->Move(GURL(src_path), GURL(dest_path));
93 FinishHandler(callbacks));
94 } 183 }
95 184
96 void SimpleFileSystem::copy( 185 void SimpleFileSystem::copy(
97 const WebURL& src_path, const WebURL& dest_path, 186 const WebURL& src_path, const WebURL& dest_path,
98 WebFileSystemCallbacks* callbacks) { 187 WebFileSystemCallbacks* callbacks) {
99 GetNewOperation(src_path)->Copy(GURL(src_path), GURL(dest_path), 188 GetNewOperation(src_path, callbacks)->Copy(GURL(src_path), GURL(dest_path));
100 FinishHandler(callbacks));
101 } 189 }
102 190
103 void SimpleFileSystem::remove( 191 void SimpleFileSystem::remove(
104 const WebURL& path, WebFileSystemCallbacks* callbacks) { 192 const WebURL& path, WebFileSystemCallbacks* callbacks) {
105 GetNewOperation(path)->Remove(path, false /* recursive */, 193 GetNewOperation(path, callbacks)->Remove(path, false /* recursive */);
106 FinishHandler(callbacks));
107 } 194 }
108 195
109 void SimpleFileSystem::removeRecursively( 196 void SimpleFileSystem::removeRecursively(
110 const WebURL& path, WebFileSystemCallbacks* callbacks) { 197 const WebURL& path, WebFileSystemCallbacks* callbacks) {
111 GetNewOperation(path)->Remove(path, true /* recursive */, 198 GetNewOperation(path, callbacks)->Remove(path, true /* recursive */);
112 FinishHandler(callbacks));
113 } 199 }
114 200
115 void SimpleFileSystem::readMetadata( 201 void SimpleFileSystem::readMetadata(
116 const WebURL& path, WebFileSystemCallbacks* callbacks) { 202 const WebURL& path, WebFileSystemCallbacks* callbacks) {
117 GetNewOperation(path)->GetMetadata(path, GetMetadataHandler(callbacks)); 203 GetNewOperation(path, callbacks)->GetMetadata(path);
118 } 204 }
119 205
120 void SimpleFileSystem::createFile( 206 void SimpleFileSystem::createFile(
121 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) { 207 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
122 GetNewOperation(path)->CreateFile(path, exclusive, FinishHandler(callbacks)); 208 GetNewOperation(path, callbacks)->CreateFile(path, exclusive);
123 } 209 }
124 210
125 void SimpleFileSystem::createDirectory( 211 void SimpleFileSystem::createDirectory(
126 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) { 212 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) {
127 GetNewOperation(path)->CreateDirectory(path, exclusive, false, 213 GetNewOperation(path, callbacks)->CreateDirectory(path, exclusive, false);
128 FinishHandler(callbacks));
129 } 214 }
130 215
131 void SimpleFileSystem::fileExists( 216 void SimpleFileSystem::fileExists(
132 const WebURL& path, WebFileSystemCallbacks* callbacks) { 217 const WebURL& path, WebFileSystemCallbacks* callbacks) {
133 GetNewOperation(path)->FileExists(path, FinishHandler(callbacks)); 218 GetNewOperation(path, callbacks)->FileExists(path);
134 } 219 }
135 220
136 void SimpleFileSystem::directoryExists( 221 void SimpleFileSystem::directoryExists(
137 const WebURL& path, WebFileSystemCallbacks* callbacks) { 222 const WebURL& path, WebFileSystemCallbacks* callbacks) {
138 GetNewOperation(path)->DirectoryExists(path, FinishHandler(callbacks)); 223 GetNewOperation(path, callbacks)->DirectoryExists(path);
139 } 224 }
140 225
141 void SimpleFileSystem::readDirectory( 226 void SimpleFileSystem::readDirectory(
142 const WebURL& path, WebFileSystemCallbacks* callbacks) { 227 const WebURL& path, WebFileSystemCallbacks* callbacks) {
143 GetNewOperation(path)->ReadDirectory(path, ReadDirectoryHandler(callbacks)); 228 GetNewOperation(path, callbacks)->ReadDirectory(path);
144 } 229 }
145 230
146 WebFileWriter* SimpleFileSystem::createFileWriter( 231 WebFileWriter* SimpleFileSystem::createFileWriter(
147 const WebURL& path, WebFileWriterClient* client) { 232 const WebURL& path, WebFileWriterClient* client) {
148 return new SimpleFileWriter(path, client, file_system_context_.get()); 233 return new SimpleFileWriter(path, client, file_system_context_.get());
149 } 234 }
150 235
151 FileSystemOperationInterface* SimpleFileSystem::GetNewOperation( 236 FileSystemOperationInterface* SimpleFileSystem::GetNewOperation(
152 const WebURL& url) { 237 const WebURL& url, WebFileSystemCallbacks* callbacks) {
153 return file_system_context_->CreateFileSystemOperation( 238 return file_system_context_->CreateFileSystemOperation(
154 GURL(url), 239 GURL(url),
240 SimpleFileSystemCallbackDispatcher::Create(AsWeakPtr(), callbacks),
155 base::MessageLoopProxy::current()); 241 base::MessageLoopProxy::current());
156 } 242 }
157
158 FileSystemOperationInterface::StatusCallback
159 SimpleFileSystem::FinishHandler(WebFileSystemCallbacks* callbacks) {
160 return base::Bind(&SimpleFileSystem::DidFinish,
161 AsWeakPtr(), base::Unretained(callbacks));
162 }
163
164 FileSystemOperationInterface::ReadDirectoryCallback
165 SimpleFileSystem::ReadDirectoryHandler(WebFileSystemCallbacks* callbacks) {
166 return base::Bind(&SimpleFileSystem::DidReadDirectory,
167 AsWeakPtr(), base::Unretained(callbacks));
168 }
169
170 FileSystemOperationInterface::GetMetadataCallback
171 SimpleFileSystem::GetMetadataHandler(WebFileSystemCallbacks* callbacks) {
172 return base::Bind(&SimpleFileSystem::DidGetMetadata,
173 AsWeakPtr(), base::Unretained(callbacks));
174 }
175
176 FileSystemContext::OpenFileSystemCallback
177 SimpleFileSystem::OpenFileSystemHandler(WebFileSystemCallbacks* callbacks) {
178 return base::Bind(&SimpleFileSystem::DidOpenFileSystem,
179 AsWeakPtr(), base::Unretained(callbacks));
180 }
181
182 void SimpleFileSystem::DidFinish(WebFileSystemCallbacks* callbacks,
183 base::PlatformFileError result) {
184 if (result == base::PLATFORM_FILE_OK)
185 callbacks->didSucceed();
186 else
187 callbacks->didFail(webkit_glue::PlatformFileErrorToWebFileError(result));
188 }
189
190 void SimpleFileSystem::DidGetMetadata(WebFileSystemCallbacks* callbacks,
191 base::PlatformFileError result,
192 const base::PlatformFileInfo& info,
193 const FilePath& platform_path) {
194 if (result == base::PLATFORM_FILE_OK) {
195 WebFileInfo web_file_info;
196 web_file_info.length = info.size;
197 web_file_info.modificationTime = info.last_modified.ToDoubleT();
198 web_file_info.type = info.is_directory ?
199 WebFileInfo::TypeDirectory : WebFileInfo::TypeFile;
200 web_file_info.platformPath =
201 webkit_glue::FilePathToWebString(platform_path);
202 callbacks->didReadMetadata(web_file_info);
203 } else {
204 callbacks->didFail(webkit_glue::PlatformFileErrorToWebFileError(result));
205 }
206 }
207
208 void SimpleFileSystem::DidReadDirectory(
209 WebFileSystemCallbacks* callbacks,
210 base::PlatformFileError result,
211 const std::vector<base::FileUtilProxy::Entry>& entries,
212 bool has_more) {
213 if (result == base::PLATFORM_FILE_OK) {
214 std::vector<WebFileSystemEntry> web_entries_vector;
215 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it =
216 entries.begin(); it != entries.end(); ++it) {
217 WebFileSystemEntry entry;
218 entry.name = webkit_glue::FilePathStringToWebString(it->name);
219 entry.isDirectory = it->is_directory;
220 web_entries_vector.push_back(entry);
221 }
222 WebVector<WebKit::WebFileSystemEntry> web_entries = web_entries_vector;
223 callbacks->didReadDirectory(web_entries, has_more);
224 } else {
225 callbacks->didFail(webkit_glue::PlatformFileErrorToWebFileError(result));
226 }
227 }
228
229 void SimpleFileSystem::DidOpenFileSystem(
230 WebFileSystemCallbacks* callbacks,
231 base::PlatformFileError result,
232 const std::string& name, const GURL& root) {
233 if (result == base::PLATFORM_FILE_OK) {
234 if (!root.is_valid())
235 callbacks->didFail(WebKit::WebFileErrorSecurity);
236 else
237 callbacks->didOpenFileSystem(WebString::fromUTF8(name), root);
238 } else {
239 callbacks->didFail(webkit_glue::PlatformFileErrorToWebFileError(result));
240 }
241 }
OLDNEW
« no previous file with comments | « webkit/tools/test_shell/simple_file_system.h ('k') | webkit/tools/test_shell/simple_file_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698