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