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/browser/fileapi/sandbox_file_system_backend.h" | 5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 #include "webkit/browser/fileapi/file_system_backend.h" | 17 #include "webkit/browser/fileapi/file_system_backend.h" |
18 #include "webkit/browser/fileapi/file_system_url.h" | 18 #include "webkit/browser/fileapi/file_system_url.h" |
19 #include "webkit/browser/fileapi/mock_file_system_options.h" | 19 #include "webkit/browser/fileapi/mock_file_system_options.h" |
20 #include "webkit/browser/fileapi/sandbox_context.h" | 20 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h" |
21 #include "webkit/common/fileapi/file_system_util.h" | 21 #include "webkit/common/fileapi/file_system_util.h" |
22 | 22 |
23 // PS stands for path separator. | 23 // PS stands for path separator. |
24 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 24 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
25 #define PS "\\" | 25 #define PS "\\" |
26 #else | 26 #else |
27 #define PS "/" | 27 #define PS "/" |
28 #endif | 28 #endif |
29 | 29 |
30 namespace fileapi { | 30 namespace fileapi { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 base::PlatformFileError error) { | 78 base::PlatformFileError error) { |
79 *error_out = error; | 79 *error_out = error; |
80 } | 80 } |
81 | 81 |
82 } // namespace | 82 } // namespace |
83 | 83 |
84 class SandboxFileSystemBackendTest : public testing::Test { | 84 class SandboxFileSystemBackendTest : public testing::Test { |
85 protected: | 85 protected: |
86 virtual void SetUp() { | 86 virtual void SetUp() { |
87 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 87 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
88 SetUpNewSandboxContext(CreateAllowFileAccessOptions()); | 88 SetUpNewDelegate(CreateAllowFileAccessOptions()); |
89 } | 89 } |
90 | 90 |
91 void SetUpNewSandboxContext(const FileSystemOptions& options) { | 91 void SetUpNewDelegate(const FileSystemOptions& options) { |
92 context_.reset(new SandboxContext( | 92 delegate_.reset(new SandboxFileSystemBackendDelegate( |
93 NULL /* quota_manager_proxy */, | 93 NULL /* quota_manager_proxy */, |
94 base::MessageLoopProxy::current().get(), | 94 base::MessageLoopProxy::current().get(), |
95 data_dir_.path(), | 95 data_dir_.path(), |
96 NULL /* special_storage_policy */, | 96 NULL /* special_storage_policy */, |
97 options)); | 97 options)); |
98 } | 98 } |
99 | 99 |
100 void SetUpNewBackend(const FileSystemOptions& options) { | 100 void SetUpNewBackend(const FileSystemOptions& options) { |
101 SetUpNewSandboxContext(options); | 101 SetUpNewDelegate(options); |
102 backend_.reset(new SandboxFileSystemBackend(context_.get())); | 102 backend_.reset(new SandboxFileSystemBackend(delegate_.get())); |
103 } | 103 } |
104 | 104 |
105 SandboxContext::OriginEnumerator* CreateOriginEnumerator() const { | 105 SandboxFileSystemBackendDelegate::OriginEnumerator* |
| 106 CreateOriginEnumerator() const { |
106 return backend_->CreateOriginEnumerator(); | 107 return backend_->CreateOriginEnumerator(); |
107 } | 108 } |
108 | 109 |
109 void CreateOriginTypeDirectory(const GURL& origin, | 110 void CreateOriginTypeDirectory(const GURL& origin, |
110 fileapi::FileSystemType type) { | 111 fileapi::FileSystemType type) { |
111 base::FilePath target = context_-> | 112 base::FilePath target = delegate_-> |
112 GetBaseDirectoryForOriginAndType(origin, type, true); | 113 GetBaseDirectoryForOriginAndType(origin, type, true); |
113 ASSERT_TRUE(!target.empty()); | 114 ASSERT_TRUE(!target.empty()); |
114 ASSERT_TRUE(base::DirectoryExists(target)); | 115 ASSERT_TRUE(base::DirectoryExists(target)); |
115 } | 116 } |
116 | 117 |
117 bool GetRootPath(const GURL& origin_url, | 118 bool GetRootPath(const GURL& origin_url, |
118 fileapi::FileSystemType type, | 119 fileapi::FileSystemType type, |
119 OpenFileSystemMode mode, | 120 OpenFileSystemMode mode, |
120 base::FilePath* root_path) { | 121 base::FilePath* root_path) { |
121 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 122 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
122 backend_->OpenFileSystem( | 123 backend_->OpenFileSystem( |
123 origin_url, type, mode, | 124 origin_url, type, mode, |
124 base::Bind(&DidOpenFileSystem, &error)); | 125 base::Bind(&DidOpenFileSystem, &error)); |
125 base::MessageLoop::current()->RunUntilIdle(); | 126 base::MessageLoop::current()->RunUntilIdle(); |
126 if (error != base::PLATFORM_FILE_OK) | 127 if (error != base::PLATFORM_FILE_OK) |
127 return false; | 128 return false; |
128 base::FilePath returned_root_path = | 129 base::FilePath returned_root_path = |
129 context_->GetBaseDirectoryForOriginAndType( | 130 delegate_->GetBaseDirectoryForOriginAndType( |
130 origin_url, type, false /* create */); | 131 origin_url, type, false /* create */); |
131 if (root_path) | 132 if (root_path) |
132 *root_path = returned_root_path; | 133 *root_path = returned_root_path; |
133 return !returned_root_path.empty(); | 134 return !returned_root_path.empty(); |
134 } | 135 } |
135 | 136 |
136 base::FilePath file_system_path() const { | 137 base::FilePath file_system_path() const { |
137 return data_dir_.path().Append(SandboxContext::kFileSystemDirectory); | 138 return data_dir_.path().Append( |
| 139 SandboxFileSystemBackendDelegate::kFileSystemDirectory); |
138 } | 140 } |
139 | 141 |
140 base::ScopedTempDir data_dir_; | 142 base::ScopedTempDir data_dir_; |
141 base::MessageLoop message_loop_; | 143 base::MessageLoop message_loop_; |
142 scoped_ptr<SandboxContext> context_; | 144 scoped_ptr<SandboxFileSystemBackendDelegate> delegate_; |
143 scoped_ptr<SandboxFileSystemBackend> backend_; | 145 scoped_ptr<SandboxFileSystemBackend> backend_; |
144 }; | 146 }; |
145 | 147 |
146 TEST_F(SandboxFileSystemBackendTest, Empty) { | 148 TEST_F(SandboxFileSystemBackendTest, Empty) { |
147 SetUpNewBackend(CreateAllowFileAccessOptions()); | 149 SetUpNewBackend(CreateAllowFileAccessOptions()); |
148 scoped_ptr<SandboxContext::OriginEnumerator> enumerator( | 150 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator( |
149 CreateOriginEnumerator()); | 151 CreateOriginEnumerator()); |
150 ASSERT_TRUE(enumerator->Next().is_empty()); | 152 ASSERT_TRUE(enumerator->Next().is_empty()); |
151 } | 153 } |
152 | 154 |
153 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) { | 155 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) { |
154 SetUpNewBackend(CreateAllowFileAccessOptions()); | 156 SetUpNewBackend(CreateAllowFileAccessOptions()); |
155 const char* temporary_origins[] = { | 157 const char* temporary_origins[] = { |
156 "http://www.bar.com/", | 158 "http://www.bar.com/", |
157 "http://www.foo.com/", | 159 "http://www.foo.com/", |
158 "http://www.foo.com:1/", | 160 "http://www.foo.com:1/", |
(...skipping 12 matching lines...) Expand all Loading... |
171 CreateOriginTypeDirectory(GURL(temporary_origins[i]), | 173 CreateOriginTypeDirectory(GURL(temporary_origins[i]), |
172 fileapi::kFileSystemTypeTemporary); | 174 fileapi::kFileSystemTypeTemporary); |
173 temporary_set.insert(GURL(temporary_origins[i])); | 175 temporary_set.insert(GURL(temporary_origins[i])); |
174 } | 176 } |
175 for (size_t i = 0; i < persistent_size; ++i) { | 177 for (size_t i = 0; i < persistent_size; ++i) { |
176 CreateOriginTypeDirectory(GURL(persistent_origins[i]), | 178 CreateOriginTypeDirectory(GURL(persistent_origins[i]), |
177 kFileSystemTypePersistent); | 179 kFileSystemTypePersistent); |
178 persistent_set.insert(GURL(persistent_origins[i])); | 180 persistent_set.insert(GURL(persistent_origins[i])); |
179 } | 181 } |
180 | 182 |
181 scoped_ptr<SandboxContext::OriginEnumerator> enumerator( | 183 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator( |
182 CreateOriginEnumerator()); | 184 CreateOriginEnumerator()); |
183 size_t temporary_actual_size = 0; | 185 size_t temporary_actual_size = 0; |
184 size_t persistent_actual_size = 0; | 186 size_t persistent_actual_size = 0; |
185 GURL current; | 187 GURL current; |
186 while (!(current = enumerator->Next()).is_empty()) { | 188 while (!(current = enumerator->Next()).is_empty()) { |
187 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec()); | 189 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec()); |
188 if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) { | 190 if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) { |
189 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); | 191 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); |
190 ++temporary_actual_size; | 192 ++temporary_actual_size; |
191 } | 193 } |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 316 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
315 &root_path)); | 317 &root_path)); |
316 base::FilePath expected = file_system_path().AppendASCII( | 318 base::FilePath expected = file_system_path().AppendASCII( |
317 kRootPathFileURITestCases[i].expected_path); | 319 kRootPathFileURITestCases[i].expected_path); |
318 EXPECT_EQ(expected.value(), root_path.value()); | 320 EXPECT_EQ(expected.value(), root_path.value()); |
319 EXPECT_TRUE(base::DirectoryExists(root_path)); | 321 EXPECT_TRUE(base::DirectoryExists(root_path)); |
320 } | 322 } |
321 } | 323 } |
322 | 324 |
323 } // namespace fileapi | 325 } // namespace fileapi |
OLD | NEW |