| OLD | NEW |
| 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" |
| 7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 10 #include "base/time.h" | 11 #include "base/time.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileInfo.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileInfo.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemCallback
s.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemCallback
s.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemEntry.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileSystemEntry.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 21 #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" | |
| 25 #include "webkit/fileapi/mock_file_system_options.h" | 22 #include "webkit/fileapi/mock_file_system_options.h" |
| 26 #include "webkit/glue/webkit_glue.h" | 23 #include "webkit/glue/webkit_glue.h" |
| 27 #include "webkit/tools/test_shell/simple_file_writer.h" | 24 #include "webkit/tools/test_shell/simple_file_writer.h" |
| 28 | 25 |
| 29 using base::WeakPtr; | 26 using base::WeakPtr; |
| 30 | 27 |
| 31 using WebKit::WebFileInfo; | 28 using WebKit::WebFileInfo; |
| 32 using WebKit::WebFileSystem; | 29 using WebKit::WebFileSystem; |
| 33 using WebKit::WebFileSystemCallbacks; | 30 using WebKit::WebFileSystemCallbacks; |
| 34 using WebKit::WebFileSystemEntry; | 31 using WebKit::WebFileSystemEntry; |
| 35 using WebKit::WebFileWriter; | 32 using WebKit::WebFileWriter; |
| 36 using WebKit::WebFileWriterClient; | 33 using WebKit::WebFileWriterClient; |
| 37 using WebKit::WebFrame; | 34 using WebKit::WebFrame; |
| 38 using WebKit::WebSecurityOrigin; | 35 using WebKit::WebSecurityOrigin; |
| 39 using WebKit::WebString; | 36 using WebKit::WebString; |
| 40 using WebKit::WebURL; | 37 using WebKit::WebURL; |
| 41 using WebKit::WebVector; | 38 using WebKit::WebVector; |
| 42 | 39 |
| 43 using fileapi::FileSystemCallbackDispatcher; | |
| 44 using fileapi::FileSystemContext; | 40 using fileapi::FileSystemContext; |
| 45 using fileapi::FileSystemOperationInterface; | 41 using fileapi::FileSystemOperationInterface; |
| 46 | 42 |
| 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 | |
| 132 SimpleFileSystem::SimpleFileSystem() { | 43 SimpleFileSystem::SimpleFileSystem() { |
| 133 if (file_system_dir_.CreateUniqueTempDir()) { | 44 if (file_system_dir_.CreateUniqueTempDir()) { |
| 134 file_system_context_ = new FileSystemContext( | 45 file_system_context_ = new FileSystemContext( |
| 135 base::MessageLoopProxy::current(), | 46 base::MessageLoopProxy::current(), |
| 136 base::MessageLoopProxy::current(), | 47 base::MessageLoopProxy::current(), |
| 137 NULL /* special storage policy */, | 48 NULL /* special storage policy */, |
| 138 NULL /* quota manager */, | 49 NULL /* quota manager */, |
| 139 file_system_dir_.path(), | 50 file_system_dir_.path(), |
| 140 fileapi::CreateAllowFileAccessOptions()); | 51 fileapi::CreateAllowFileAccessOptions()); |
| 141 } else { | 52 } else { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 165 else if (web_filesystem_type == WebFileSystem::TypeExternal) | 76 else if (web_filesystem_type == WebFileSystem::TypeExternal) |
| 166 type = fileapi::kFileSystemTypeExternal; | 77 type = fileapi::kFileSystemTypeExternal; |
| 167 else { | 78 else { |
| 168 // Unknown type filesystem is requested. | 79 // Unknown type filesystem is requested. |
| 169 callbacks->didFail(WebKit::WebFileErrorSecurity); | 80 callbacks->didFail(WebKit::WebFileErrorSecurity); |
| 170 return; | 81 return; |
| 171 } | 82 } |
| 172 | 83 |
| 173 GURL origin_url(frame->document().securityOrigin().toString()); | 84 GURL origin_url(frame->document().securityOrigin().toString()); |
| 174 file_system_context_->OpenFileSystem( | 85 file_system_context_->OpenFileSystem( |
| 175 origin_url, type, create, | 86 origin_url, type, create, OpenFileSystemHandler(callbacks)); |
| 176 SimpleFileSystemCallbackDispatcher::Create(AsWeakPtr(), callbacks)); | |
| 177 } | 87 } |
| 178 | 88 |
| 179 void SimpleFileSystem::move( | 89 void SimpleFileSystem::move( |
| 180 const WebURL& src_path, | 90 const WebURL& src_path, |
| 181 const WebURL& dest_path, WebFileSystemCallbacks* callbacks) { | 91 const WebURL& dest_path, WebFileSystemCallbacks* callbacks) { |
| 182 GetNewOperation(src_path, callbacks)->Move(GURL(src_path), GURL(dest_path)); | 92 GetNewOperation(src_path)->Move(GURL(src_path), GURL(dest_path), |
| 93 FinishHandler(callbacks)); |
| 183 } | 94 } |
| 184 | 95 |
| 185 void SimpleFileSystem::copy( | 96 void SimpleFileSystem::copy( |
| 186 const WebURL& src_path, const WebURL& dest_path, | 97 const WebURL& src_path, const WebURL& dest_path, |
| 187 WebFileSystemCallbacks* callbacks) { | 98 WebFileSystemCallbacks* callbacks) { |
| 188 GetNewOperation(src_path, callbacks)->Copy(GURL(src_path), GURL(dest_path)); | 99 GetNewOperation(src_path)->Copy(GURL(src_path), GURL(dest_path), |
| 100 FinishHandler(callbacks)); |
| 189 } | 101 } |
| 190 | 102 |
| 191 void SimpleFileSystem::remove( | 103 void SimpleFileSystem::remove( |
| 192 const WebURL& path, WebFileSystemCallbacks* callbacks) { | 104 const WebURL& path, WebFileSystemCallbacks* callbacks) { |
| 193 GetNewOperation(path, callbacks)->Remove(path, false /* recursive */); | 105 GetNewOperation(path)->Remove(path, false /* recursive */, |
| 106 FinishHandler(callbacks)); |
| 194 } | 107 } |
| 195 | 108 |
| 196 void SimpleFileSystem::removeRecursively( | 109 void SimpleFileSystem::removeRecursively( |
| 197 const WebURL& path, WebFileSystemCallbacks* callbacks) { | 110 const WebURL& path, WebFileSystemCallbacks* callbacks) { |
| 198 GetNewOperation(path, callbacks)->Remove(path, true /* recursive */); | 111 GetNewOperation(path)->Remove(path, true /* recursive */, |
| 112 FinishHandler(callbacks)); |
| 199 } | 113 } |
| 200 | 114 |
| 201 void SimpleFileSystem::readMetadata( | 115 void SimpleFileSystem::readMetadata( |
| 202 const WebURL& path, WebFileSystemCallbacks* callbacks) { | 116 const WebURL& path, WebFileSystemCallbacks* callbacks) { |
| 203 GetNewOperation(path, callbacks)->GetMetadata(path); | 117 GetNewOperation(path)->GetMetadata(path, GetMetadataHandler(callbacks)); |
| 204 } | 118 } |
| 205 | 119 |
| 206 void SimpleFileSystem::createFile( | 120 void SimpleFileSystem::createFile( |
| 207 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) { | 121 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) { |
| 208 GetNewOperation(path, callbacks)->CreateFile(path, exclusive); | 122 GetNewOperation(path)->CreateFile(path, exclusive, FinishHandler(callbacks)); |
| 209 } | 123 } |
| 210 | 124 |
| 211 void SimpleFileSystem::createDirectory( | 125 void SimpleFileSystem::createDirectory( |
| 212 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) { | 126 const WebURL& path, bool exclusive, WebFileSystemCallbacks* callbacks) { |
| 213 GetNewOperation(path, callbacks)->CreateDirectory(path, exclusive, false); | 127 GetNewOperation(path)->CreateDirectory(path, exclusive, false, |
| 128 FinishHandler(callbacks)); |
| 214 } | 129 } |
| 215 | 130 |
| 216 void SimpleFileSystem::fileExists( | 131 void SimpleFileSystem::fileExists( |
| 217 const WebURL& path, WebFileSystemCallbacks* callbacks) { | 132 const WebURL& path, WebFileSystemCallbacks* callbacks) { |
| 218 GetNewOperation(path, callbacks)->FileExists(path); | 133 GetNewOperation(path)->FileExists(path, FinishHandler(callbacks)); |
| 219 } | 134 } |
| 220 | 135 |
| 221 void SimpleFileSystem::directoryExists( | 136 void SimpleFileSystem::directoryExists( |
| 222 const WebURL& path, WebFileSystemCallbacks* callbacks) { | 137 const WebURL& path, WebFileSystemCallbacks* callbacks) { |
| 223 GetNewOperation(path, callbacks)->DirectoryExists(path); | 138 GetNewOperation(path)->DirectoryExists(path, FinishHandler(callbacks)); |
| 224 } | 139 } |
| 225 | 140 |
| 226 void SimpleFileSystem::readDirectory( | 141 void SimpleFileSystem::readDirectory( |
| 227 const WebURL& path, WebFileSystemCallbacks* callbacks) { | 142 const WebURL& path, WebFileSystemCallbacks* callbacks) { |
| 228 GetNewOperation(path, callbacks)->ReadDirectory(path); | 143 GetNewOperation(path)->ReadDirectory(path, ReadDirectoryHandler(callbacks)); |
| 229 } | 144 } |
| 230 | 145 |
| 231 WebFileWriter* SimpleFileSystem::createFileWriter( | 146 WebFileWriter* SimpleFileSystem::createFileWriter( |
| 232 const WebURL& path, WebFileWriterClient* client) { | 147 const WebURL& path, WebFileWriterClient* client) { |
| 233 return new SimpleFileWriter(path, client, file_system_context_.get()); | 148 return new SimpleFileWriter(path, client, file_system_context_.get()); |
| 234 } | 149 } |
| 235 | 150 |
| 236 FileSystemOperationInterface* SimpleFileSystem::GetNewOperation( | 151 FileSystemOperationInterface* SimpleFileSystem::GetNewOperation( |
| 237 const WebURL& url, WebFileSystemCallbacks* callbacks) { | 152 const WebURL& url) { |
| 238 return file_system_context_->CreateFileSystemOperation( | 153 return file_system_context_->CreateFileSystemOperation( |
| 239 GURL(url), | 154 GURL(url), |
| 240 SimpleFileSystemCallbackDispatcher::Create(AsWeakPtr(), callbacks), | |
| 241 base::MessageLoopProxy::current()); | 155 base::MessageLoopProxy::current()); |
| 242 } | 156 } |
| 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 } |
| OLD | NEW |