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