Chromium Code Reviews| Index: webkit/tools/test_shell/simple_file_system.cc |
| diff --git a/webkit/tools/test_shell/simple_file_system.cc b/webkit/tools/test_shell/simple_file_system.cc |
| index 520f709caffbd72f61b12ef8885301b0ca24708e..493d98407b07ae66b6eb21603a08522db068b1a1 100644 |
| --- a/webkit/tools/test_shell/simple_file_system.cc |
| +++ b/webkit/tools/test_shell/simple_file_system.cc |
| @@ -6,40 +6,59 @@ |
| #include "base/file_path.h" |
| #include "base/message_loop_proxy.h" |
| +#include "base/scoped_callback_factory.h" |
| #include "base/time.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "googleurl/src/gurl.h" |
| #include "third_party/WebKit/WebKit/chromium/public/WebFileInfo.h" |
| +#include "third_party/WebKit/WebKit/chromium/public/WebFileSystemCallbacks.h" |
| #include "third_party/WebKit/WebKit/chromium/public/WebFileSystemEntry.h" |
| +#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
| +#include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" |
| #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" |
| #include "webkit/fileapi/file_system_callback_dispatcher.h" |
| +#include "webkit/fileapi/file_system_path_manager.h" |
| +#include "webkit/fileapi/file_system_types.h" |
| +#include "webkit/fileapi/sandboxed_file_system_context.h" |
| +#include "webkit/fileapi/sandboxed_file_system_operation.h" |
| #include "webkit/glue/webkit_glue.h" |
| #include "webkit/tools/test_shell/simple_file_writer.h" |
| using WebKit::WebFileInfo; |
| +using WebKit::WebFileSystem; |
| using WebKit::WebFileSystemCallbacks; |
| using WebKit::WebFileSystemEntry; |
| using WebKit::WebFileWriter; |
| using WebKit::WebFileWriterClient; |
| +using WebKit::WebFrame; |
| +using WebKit::WebSecurityOrigin; |
| using WebKit::WebString; |
| using WebKit::WebVector; |
| +using fileapi::FileSystemCallbackDispatcher; |
| +using fileapi::SandboxedFileSystemContext; |
| +using fileapi::SandboxedFileSystemOperation; |
| + |
| namespace { |
| -class TestShellFileSystemCallbackDispatcher |
| - : public fileapi::FileSystemCallbackDispatcher { |
| +class SimpleFileSystemCallbackDispatcher |
| + : public FileSystemCallbackDispatcher { |
| public: |
| - TestShellFileSystemCallbackDispatcher( |
| - SimpleFileSystem* file_system, |
| - WebFileSystemCallbacks* callbacks) |
| - : file_system_(file_system), |
| - callbacks_(callbacks), |
| - request_id_(-1) { |
| + SimpleFileSystemCallbackDispatcher(WebFileSystemCallbacks* callbacks) |
| + : callbacks_(callbacks) { |
| } |
| - void set_request_id(int request_id) { request_id_ = request_id; } |
| + ~SimpleFileSystemCallbackDispatcher() { |
| + RemoveOperation(); |
| + } |
| + |
| + void set_operation(SandboxedFileSystemOperation* operation) { |
| + operation_.reset(operation); |
| + } |
| virtual void DidSucceed() { |
| callbacks_->didSucceed(); |
| - file_system_->RemoveCompletedOperation(request_id_); |
| + RemoveOperation(); |
| } |
| virtual void DidReadMetadata(const base::PlatformFileInfo& info) { |
| @@ -49,7 +68,7 @@ class TestShellFileSystemCallbackDispatcher |
| web_file_info.type = info.is_directory ? |
| WebFileInfo::TypeDirectory : WebFileInfo::TypeFile; |
| callbacks_->didReadMetadata(web_file_info); |
| - file_system_->RemoveCompletedOperation(request_id_); |
| + RemoveOperation(); |
| } |
| virtual void DidReadDirectory( |
| @@ -66,17 +85,23 @@ class TestShellFileSystemCallbackDispatcher |
| WebVector<WebKit::WebFileSystemEntry> web_entries = |
| web_entries_vector; |
| callbacks_->didReadDirectory(web_entries, has_more); |
| - file_system_->RemoveCompletedOperation(request_id_); |
| + RemoveOperation(); |
| } |
| - virtual void DidOpenFileSystem(const std::string&, const FilePath&) { |
| - NOTREACHED(); |
| + virtual void DidOpenFileSystem( |
| + const std::string& name, const FilePath& path) { |
| + if (path.empty()) |
| + callbacks_->didFail(WebKit::WebFileErrorSecurity); |
| + else |
| + callbacks_->didOpenFileSystem( |
| + UTF8ToUTF16(name), webkit_glue::FilePathToWebString(path)); |
| + RemoveOperation(); |
| } |
| virtual void DidFail(base::PlatformFileError error_code) { |
| callbacks_->didFail( |
| webkit_glue::PlatformFileErrorToWebFileError(error_code)); |
| - file_system_->RemoveCompletedOperation(request_id_); |
| + RemoveOperation(); |
| } |
| virtual void DidWrite(int64, bool) { |
| @@ -84,18 +109,60 @@ class TestShellFileSystemCallbackDispatcher |
| } |
| private: |
| - SimpleFileSystem* file_system_; |
| + void RemoveOperation() { |
| + // We need to make sure operation_ is null when we delete the operation |
| + // (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.
|
| + scoped_ptr<SandboxedFileSystemOperation> operation; |
| + operation.swap(operation_); |
| + operation.reset(); |
| + } |
| + |
| WebFileSystemCallbacks* callbacks_; |
| - int request_id_; |
| + scoped_ptr<SandboxedFileSystemOperation> operation_; |
| }; |
| -} // namespace |
| +} // namespace |
| + |
| +SimpleFileSystem::SimpleFileSystem() { |
| + if (file_system_dir_.CreateUniqueTempDir()) { |
| + sandboxed_context_.reset(new SandboxedFileSystemContext( |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + file_system_dir_.path(), |
| + false /* incognito */, |
| + true /* allow_file_access */, |
| + false /* unlimited_quota */)); |
| + } else { |
| + LOG(WARNING) << "Failed to create a temp dir for the filesystem." |
| + "FileSystem feature will be disabled."; |
| + } |
| +} |
| SimpleFileSystem::~SimpleFileSystem() { |
| - // Drop all the operations. |
| - for (OperationsMap::const_iterator iter(&operations_); |
| - !iter.IsAtEnd(); iter.Advance()) |
| - operations_.Remove(iter.GetCurrentKey()); |
| +} |
| + |
| +void SimpleFileSystem::OpenFileSystem( |
| + WebFrame* frame, WebFileSystem::Type web_filesystem_type, |
| + long long, bool create, |
| + WebFileSystemCallbacks* callbacks) { |
| + if (!frame || !sandboxed_context_.get()) { |
| + // The FileSystem temp directory was not initialized successfully. |
| + callbacks->didFail(WebKit::WebFileErrorSecurity); |
| + return; |
| + } |
| + |
| + fileapi::FileSystemType type; |
| + if (web_filesystem_type == WebFileSystem::TypeTemporary) |
| + type = fileapi::kFileSystemTypeTemporary; |
| + else if (web_filesystem_type == WebFileSystem::TypePersistent) |
| + type = fileapi::kFileSystemTypePersistent; |
| + else { |
| + // Unknown type filesystem is requested. |
| + callbacks->didFail(WebKit::WebFileErrorSecurity); |
| + return; |
| + } |
| + |
| + GURL origin_url(frame->securityOrigin().toString()); |
| + GetNewOperation(callbacks)->OpenFileSystem(origin_url, type, create); |
| } |
| void SimpleFileSystem::move( |
| @@ -177,18 +244,13 @@ WebFileWriter* SimpleFileSystem::createFileWriter( |
| return new SimpleFileWriter(path, client); |
| } |
| -fileapi::FileSystemOperation* SimpleFileSystem::GetNewOperation( |
| +SandboxedFileSystemOperation* SimpleFileSystem::GetNewOperation( |
| WebFileSystemCallbacks* callbacks) { |
| - // This pointer will be owned by |operation|. |
| - TestShellFileSystemCallbackDispatcher* dispatcher = |
| - new TestShellFileSystemCallbackDispatcher(this, callbacks); |
| - fileapi::FileSystemOperation* operation = new fileapi::FileSystemOperation( |
| - dispatcher, base::MessageLoopProxy::CreateForCurrentThread()); |
| - int32 request_id = operations_.Add(operation); |
| - dispatcher->set_request_id(request_id); |
| + SimpleFileSystemCallbackDispatcher* dispatcher = |
| + new SimpleFileSystemCallbackDispatcher(callbacks); |
| + SandboxedFileSystemOperation* operation = new SandboxedFileSystemOperation( |
| + dispatcher, base::MessageLoopProxy::CreateForCurrentThread(), |
| + sandboxed_context_.get()); |
| + dispatcher->set_operation(operation); |
| return operation; |
| } |
| - |
| -void SimpleFileSystem::RemoveCompletedOperation(int request_id) { |
| - operations_.Remove(request_id); |
| -} |