OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "base/scoped_callback_factory.h" | |
9 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "base/utf_string_conversions.h" | |
12 #include "googleurl/src/gurl.h" | |
10 #include "third_party/WebKit/WebKit/chromium/public/WebFileInfo.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebFileInfo.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebFileSystemCallbacks.h" | |
11 #include "third_party/WebKit/WebKit/chromium/public/WebFileSystemEntry.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebFileSystemEntry.h" |
16 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | |
17 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" | |
12 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" |
13 #include "webkit/fileapi/file_system_callback_dispatcher.h" | 19 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
20 #include "webkit/fileapi/file_system_path_manager.h" | |
21 #include "webkit/fileapi/file_system_types.h" | |
22 #include "webkit/fileapi/sandboxed_file_system_context.h" | |
23 #include "webkit/fileapi/sandboxed_file_system_operation.h" | |
14 #include "webkit/glue/webkit_glue.h" | 24 #include "webkit/glue/webkit_glue.h" |
15 #include "webkit/tools/test_shell/simple_file_writer.h" | 25 #include "webkit/tools/test_shell/simple_file_writer.h" |
16 | 26 |
17 using WebKit::WebFileInfo; | 27 using WebKit::WebFileInfo; |
28 using WebKit::WebFileSystem; | |
18 using WebKit::WebFileSystemCallbacks; | 29 using WebKit::WebFileSystemCallbacks; |
19 using WebKit::WebFileSystemEntry; | 30 using WebKit::WebFileSystemEntry; |
20 using WebKit::WebFileWriter; | 31 using WebKit::WebFileWriter; |
21 using WebKit::WebFileWriterClient; | 32 using WebKit::WebFileWriterClient; |
33 using WebKit::WebFrame; | |
34 using WebKit::WebSecurityOrigin; | |
22 using WebKit::WebString; | 35 using WebKit::WebString; |
23 using WebKit::WebVector; | 36 using WebKit::WebVector; |
24 | 37 |
38 using fileapi::FileSystemCallbackDispatcher; | |
39 using fileapi::SandboxedFileSystemContext; | |
40 using fileapi::SandboxedFileSystemOperation; | |
41 | |
25 namespace { | 42 namespace { |
26 | 43 |
27 class TestShellFileSystemCallbackDispatcher | 44 class SimpleFileSystemCallbackDispatcher |
28 : public fileapi::FileSystemCallbackDispatcher { | 45 : public FileSystemCallbackDispatcher { |
29 public: | 46 public: |
30 TestShellFileSystemCallbackDispatcher( | 47 SimpleFileSystemCallbackDispatcher(WebFileSystemCallbacks* callbacks) |
31 SimpleFileSystem* file_system, | 48 : callbacks_(callbacks) { |
32 WebFileSystemCallbacks* callbacks) | |
33 : file_system_(file_system), | |
34 callbacks_(callbacks), | |
35 request_id_(-1) { | |
36 } | 49 } |
37 | 50 |
38 void set_request_id(int request_id) { request_id_ = request_id; } | 51 ~SimpleFileSystemCallbackDispatcher() { |
52 RemoveOperation(); | |
53 } | |
54 | |
55 void set_operation(SandboxedFileSystemOperation* operation) { | |
56 operation_.reset(operation); | |
57 } | |
39 | 58 |
40 virtual void DidSucceed() { | 59 virtual void DidSucceed() { |
41 callbacks_->didSucceed(); | 60 callbacks_->didSucceed(); |
42 file_system_->RemoveCompletedOperation(request_id_); | 61 RemoveOperation(); |
43 } | 62 } |
44 | 63 |
45 virtual void DidReadMetadata(const base::PlatformFileInfo& info) { | 64 virtual void DidReadMetadata(const base::PlatformFileInfo& info) { |
46 WebFileInfo web_file_info; | 65 WebFileInfo web_file_info; |
47 web_file_info.length = info.size; | 66 web_file_info.length = info.size; |
48 web_file_info.modificationTime = info.last_modified.ToDoubleT(); | 67 web_file_info.modificationTime = info.last_modified.ToDoubleT(); |
49 web_file_info.type = info.is_directory ? | 68 web_file_info.type = info.is_directory ? |
50 WebFileInfo::TypeDirectory : WebFileInfo::TypeFile; | 69 WebFileInfo::TypeDirectory : WebFileInfo::TypeFile; |
51 callbacks_->didReadMetadata(web_file_info); | 70 callbacks_->didReadMetadata(web_file_info); |
52 file_system_->RemoveCompletedOperation(request_id_); | 71 RemoveOperation(); |
53 } | 72 } |
54 | 73 |
55 virtual void DidReadDirectory( | 74 virtual void DidReadDirectory( |
56 const std::vector<base::FileUtilProxy::Entry>& entries, | 75 const std::vector<base::FileUtilProxy::Entry>& entries, |
57 bool has_more) { | 76 bool has_more) { |
58 std::vector<WebFileSystemEntry> web_entries_vector; | 77 std::vector<WebFileSystemEntry> web_entries_vector; |
59 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = | 78 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = |
60 entries.begin(); it != entries.end(); ++it) { | 79 entries.begin(); it != entries.end(); ++it) { |
61 WebFileSystemEntry entry; | 80 WebFileSystemEntry entry; |
62 entry.name = webkit_glue::FilePathStringToWebString(it->name); | 81 entry.name = webkit_glue::FilePathStringToWebString(it->name); |
63 entry.isDirectory = it->is_directory; | 82 entry.isDirectory = it->is_directory; |
64 web_entries_vector.push_back(entry); | 83 web_entries_vector.push_back(entry); |
65 } | 84 } |
66 WebVector<WebKit::WebFileSystemEntry> web_entries = | 85 WebVector<WebKit::WebFileSystemEntry> web_entries = |
67 web_entries_vector; | 86 web_entries_vector; |
68 callbacks_->didReadDirectory(web_entries, has_more); | 87 callbacks_->didReadDirectory(web_entries, has_more); |
69 file_system_->RemoveCompletedOperation(request_id_); | 88 RemoveOperation(); |
70 } | 89 } |
71 | 90 |
72 virtual void DidOpenFileSystem(const std::string&, const FilePath&) { | 91 virtual void DidOpenFileSystem( |
73 NOTREACHED(); | 92 const std::string& name, const FilePath& path) { |
93 if (path.empty()) | |
94 callbacks_->didFail(WebKit::WebFileErrorSecurity); | |
95 else | |
96 callbacks_->didOpenFileSystem( | |
97 UTF8ToUTF16(name), webkit_glue::FilePathToWebString(path)); | |
98 RemoveOperation(); | |
74 } | 99 } |
75 | 100 |
76 virtual void DidFail(base::PlatformFileError error_code) { | 101 virtual void DidFail(base::PlatformFileError error_code) { |
77 callbacks_->didFail( | 102 callbacks_->didFail( |
78 webkit_glue::PlatformFileErrorToWebFileError(error_code)); | 103 webkit_glue::PlatformFileErrorToWebFileError(error_code)); |
79 file_system_->RemoveCompletedOperation(request_id_); | 104 RemoveOperation(); |
80 } | 105 } |
81 | 106 |
82 virtual void DidWrite(int64, bool) { | 107 virtual void DidWrite(int64, bool) { |
83 NOTREACHED(); | 108 NOTREACHED(); |
84 } | 109 } |
85 | 110 |
86 private: | 111 private: |
87 SimpleFileSystem* file_system_; | 112 void RemoveOperation() { |
113 // We need to make sure operation_ is null when we delete the operation | |
114 // (which in turn deletes this dispatcher instance). | |
michaeln
2010/11/13 00:16:42
~SimpleFileSystemCallbackDispatcher() invokes Remo
kinuko
2010/11/13 00:38:14
Sounds right. It can be changed. Btw I have anot
kinuko
2010/11/19 20:44:38
Done.
| |
115 scoped_ptr<SandboxedFileSystemOperation> operation; | |
116 operation.swap(operation_); | |
117 operation.reset(); | |
118 } | |
119 | |
88 WebFileSystemCallbacks* callbacks_; | 120 WebFileSystemCallbacks* callbacks_; |
89 int request_id_; | 121 scoped_ptr<SandboxedFileSystemOperation> operation_; |
90 }; | 122 }; |
91 | 123 |
92 } // namespace | 124 } // namespace |
125 | |
126 SimpleFileSystem::SimpleFileSystem() { | |
127 if (file_system_dir_.CreateUniqueTempDir()) { | |
128 sandboxed_context_.reset(new SandboxedFileSystemContext( | |
129 base::MessageLoopProxy::CreateForCurrentThread(), | |
130 file_system_dir_.path(), | |
131 false /* incognito */, | |
132 true /* allow_file_access */, | |
133 false /* unlimited_quota */)); | |
134 } else { | |
135 LOG(WARNING) << "Failed to create a temp dir for the filesystem." | |
136 "FileSystem feature will be disabled."; | |
137 } | |
138 } | |
93 | 139 |
94 SimpleFileSystem::~SimpleFileSystem() { | 140 SimpleFileSystem::~SimpleFileSystem() { |
95 // Drop all the operations. | 141 } |
96 for (OperationsMap::const_iterator iter(&operations_); | 142 |
97 !iter.IsAtEnd(); iter.Advance()) | 143 void SimpleFileSystem::OpenFileSystem( |
98 operations_.Remove(iter.GetCurrentKey()); | 144 WebFrame* frame, WebFileSystem::Type web_filesystem_type, |
145 long long, bool create, | |
146 WebFileSystemCallbacks* callbacks) { | |
147 if (!frame || !sandboxed_context_.get()) { | |
148 // The FileSystem temp directory was not initialized successfully. | |
149 callbacks->didFail(WebKit::WebFileErrorSecurity); | |
150 return; | |
151 } | |
152 | |
153 fileapi::FileSystemType type; | |
154 if (web_filesystem_type == WebFileSystem::TypeTemporary) | |
155 type = fileapi::kFileSystemTypeTemporary; | |
156 else if (web_filesystem_type == WebFileSystem::TypePersistent) | |
157 type = fileapi::kFileSystemTypePersistent; | |
158 else { | |
159 // Unknown type filesystem is requested. | |
160 callbacks->didFail(WebKit::WebFileErrorSecurity); | |
161 return; | |
162 } | |
163 | |
164 GURL origin_url(frame->securityOrigin().toString()); | |
165 GetNewOperation(callbacks)->OpenFileSystem(origin_url, type, create); | |
99 } | 166 } |
100 | 167 |
101 void SimpleFileSystem::move( | 168 void SimpleFileSystem::move( |
102 const WebString& src_path, | 169 const WebString& src_path, |
103 const WebString& dest_path, WebFileSystemCallbacks* callbacks) { | 170 const WebString& dest_path, WebFileSystemCallbacks* callbacks) { |
104 FilePath dest_filepath(webkit_glue::WebStringToFilePath(dest_path)); | 171 FilePath dest_filepath(webkit_glue::WebStringToFilePath(dest_path)); |
105 FilePath src_filepath(webkit_glue::WebStringToFilePath(src_path)); | 172 FilePath src_filepath(webkit_glue::WebStringToFilePath(src_path)); |
106 | 173 |
107 GetNewOperation(callbacks)->Move(src_filepath, dest_filepath); | 174 GetNewOperation(callbacks)->Move(src_filepath, dest_filepath); |
108 } | 175 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 FilePath filepath(webkit_glue::WebStringToFilePath(path)); | 237 FilePath filepath(webkit_glue::WebStringToFilePath(path)); |
171 | 238 |
172 GetNewOperation(callbacks)->ReadDirectory(filepath); | 239 GetNewOperation(callbacks)->ReadDirectory(filepath); |
173 } | 240 } |
174 | 241 |
175 WebFileWriter* SimpleFileSystem::createFileWriter( | 242 WebFileWriter* SimpleFileSystem::createFileWriter( |
176 const WebString& path, WebFileWriterClient* client) { | 243 const WebString& path, WebFileWriterClient* client) { |
177 return new SimpleFileWriter(path, client); | 244 return new SimpleFileWriter(path, client); |
178 } | 245 } |
179 | 246 |
180 fileapi::FileSystemOperation* SimpleFileSystem::GetNewOperation( | 247 SandboxedFileSystemOperation* SimpleFileSystem::GetNewOperation( |
181 WebFileSystemCallbacks* callbacks) { | 248 WebFileSystemCallbacks* callbacks) { |
182 // This pointer will be owned by |operation|. | 249 SimpleFileSystemCallbackDispatcher* dispatcher = |
183 TestShellFileSystemCallbackDispatcher* dispatcher = | 250 new SimpleFileSystemCallbackDispatcher(callbacks); |
184 new TestShellFileSystemCallbackDispatcher(this, callbacks); | 251 SandboxedFileSystemOperation* operation = new SandboxedFileSystemOperation( |
185 fileapi::FileSystemOperation* operation = new fileapi::FileSystemOperation( | 252 dispatcher, base::MessageLoopProxy::CreateForCurrentThread(), |
186 dispatcher, base::MessageLoopProxy::CreateForCurrentThread()); | 253 sandboxed_context_.get()); |
187 int32 request_id = operations_.Add(operation); | 254 dispatcher->set_operation(operation); |
188 dispatcher->set_request_id(request_id); | |
189 return operation; | 255 return operation; |
190 } | 256 } |
191 | |
192 void SimpleFileSystem::RemoveCompletedOperation(int request_id) { | |
193 operations_.Remove(request_id); | |
194 } | |
OLD | NEW |