| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/file_system_dir_url_request_job.h" | 5 #include "webkit/browser/fileapi/file_system_dir_url_request_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | 64 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
| 65 kValidExternalMountPoint, fileapi::kFileSystemTypeTest, | 65 kValidExternalMountPoint, fileapi::kFileSystemTypeTest, |
| 66 fileapi::FileSystemMountOption(), base::FilePath()); | 66 fileapi::FileSystemMountOption(), base::FilePath()); |
| 67 callback.Run(base::File::FILE_OK); | 67 callback.Run(base::File::FILE_OK); |
| 68 } else { | 68 } else { |
| 69 callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 69 callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
| 70 } | 70 } |
| 71 return true; | 71 return true; |
| 72 } | 72 } |
| 73 | 73 |
| 74 class FileSystemDirURLRequestJobFactory : public net::URLRequestJobFactory { |
| 75 public: |
| 76 FileSystemDirURLRequestJobFactory(const std::string& storage_domain, |
| 77 FileSystemContext* context) |
| 78 : storage_domain_(storage_domain), file_system_context_(context) { |
| 79 } |
| 80 |
| 81 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 82 const std::string& scheme, |
| 83 net::URLRequest* request, |
| 84 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 85 return new fileapi::FileSystemDirURLRequestJob( |
| 86 request, network_delegate, storage_domain_, file_system_context_); |
| 87 } |
| 88 |
| 89 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 90 return true; |
| 91 } |
| 92 |
| 93 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 94 return true; |
| 95 } |
| 96 |
| 97 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
| 98 return false; |
| 99 } |
| 100 |
| 101 private: |
| 102 std::string storage_domain_; |
| 103 FileSystemContext* file_system_context_; |
| 104 }; |
| 105 |
| 106 |
| 74 } // namespace | 107 } // namespace |
| 75 | 108 |
| 76 class FileSystemDirURLRequestJobTest : public testing::Test { | 109 class FileSystemDirURLRequestJobTest : public testing::Test { |
| 77 protected: | 110 protected: |
| 78 FileSystemDirURLRequestJobTest() | 111 FileSystemDirURLRequestJobTest() |
| 79 : weak_factory_(this) { | 112 : weak_factory_(this) { |
| 80 } | 113 } |
| 81 | 114 |
| 82 virtual void SetUp() OVERRIDE { | 115 virtual void SetUp() OVERRIDE { |
| 83 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 116 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 84 | 117 |
| 85 special_storage_policy_ = new MockSpecialStoragePolicy; | 118 special_storage_policy_ = new MockSpecialStoragePolicy; |
| 86 file_system_context_ = CreateFileSystemContextForTesting( | 119 file_system_context_ = CreateFileSystemContextForTesting( |
| 87 NULL, temp_dir_.path()); | 120 NULL, temp_dir_.path()); |
| 88 | 121 |
| 89 file_system_context_->OpenFileSystem( | 122 file_system_context_->OpenFileSystem( |
| 90 GURL("http://remote/"), fileapi::kFileSystemTypeTemporary, | 123 GURL("http://remote/"), fileapi::kFileSystemTypeTemporary, |
| 91 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 124 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 92 base::Bind(&FileSystemDirURLRequestJobTest::OnOpenFileSystem, | 125 base::Bind(&FileSystemDirURLRequestJobTest::OnOpenFileSystem, |
| 93 weak_factory_.GetWeakPtr())); | 126 weak_factory_.GetWeakPtr())); |
| 94 base::RunLoop().RunUntilIdle(); | 127 base::RunLoop().RunUntilIdle(); |
| 95 | |
| 96 net::URLRequest::Deprecated::RegisterProtocolFactory( | |
| 97 "filesystem", &FileSystemDirURLRequestJobFactory); | |
| 98 } | 128 } |
| 99 | 129 |
| 100 virtual void TearDown() OVERRIDE { | 130 virtual void TearDown() OVERRIDE { |
| 101 // NOTE: order matters, request must die before delegate | 131 // NOTE: order matters, request must die before delegate |
| 102 request_.reset(NULL); | 132 request_.reset(NULL); |
| 103 delegate_.reset(NULL); | 133 delegate_.reset(NULL); |
| 104 | |
| 105 net::URLRequest::Deprecated::RegisterProtocolFactory("filesystem", NULL); | |
| 106 ClearUnusedJob(); | |
| 107 } | 134 } |
| 108 | 135 |
| 109 void SetUpAutoMountContext(base::FilePath* mnt_point) { | 136 void SetUpAutoMountContext(base::FilePath* mnt_point) { |
| 110 *mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | 137 *mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); |
| 111 ASSERT_TRUE(base::CreateDirectory(*mnt_point)); | 138 ASSERT_TRUE(base::CreateDirectory(*mnt_point)); |
| 112 | 139 |
| 113 ScopedVector<fileapi::FileSystemBackend> additional_providers; | 140 ScopedVector<fileapi::FileSystemBackend> additional_providers; |
| 114 additional_providers.push_back(new TestFileSystemBackend( | 141 additional_providers.push_back(new TestFileSystemBackend( |
| 115 base::MessageLoopProxy::current().get(), *mnt_point)); | 142 base::MessageLoopProxy::current().get(), *mnt_point)); |
| 116 | 143 |
| 117 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | 144 std::vector<fileapi::URLRequestAutoMountHandler> handlers; |
| 118 handlers.push_back(base::Bind(&TestAutoMountForURLRequest)); | 145 handlers.push_back(base::Bind(&TestAutoMountForURLRequest)); |
| 119 | 146 |
| 120 file_system_context_ = CreateFileSystemContextWithAutoMountersForTesting( | 147 file_system_context_ = CreateFileSystemContextWithAutoMountersForTesting( |
| 121 NULL, additional_providers.Pass(), handlers, temp_dir_.path()); | 148 NULL, additional_providers.Pass(), handlers, temp_dir_.path()); |
| 122 } | 149 } |
| 123 | 150 |
| 124 void OnOpenFileSystem(const GURL& root_url, | 151 void OnOpenFileSystem(const GURL& root_url, |
| 125 const std::string& name, | 152 const std::string& name, |
| 126 base::File::Error result) { | 153 base::File::Error result) { |
| 127 ASSERT_EQ(base::File::FILE_OK, result); | 154 ASSERT_EQ(base::File::FILE_OK, result); |
| 128 } | 155 } |
| 129 | 156 |
| 130 void TestRequestHelper(const GURL& url, bool run_to_completion, | 157 void TestRequestHelper(const GURL& url, bool run_to_completion, |
| 131 FileSystemContext* file_system_context) { | 158 FileSystemContext* file_system_context) { |
| 132 delegate_.reset(new net::TestDelegate()); | 159 delegate_.reset(new net::TestDelegate()); |
| 133 delegate_->set_quit_on_redirect(true); | 160 delegate_->set_quit_on_redirect(true); |
| 161 job_factory_.reset(new FileSystemDirURLRequestJobFactory( |
| 162 url.GetOrigin().host(), file_system_context)); |
| 163 empty_context_.set_job_factory(job_factory_.get()); |
| 164 |
| 134 request_ = empty_context_.CreateRequest( | 165 request_ = empty_context_.CreateRequest( |
| 135 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); | 166 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); |
| 136 job_ = new fileapi::FileSystemDirURLRequestJob( | |
| 137 request_.get(), NULL, url.GetOrigin().host(), file_system_context); | |
| 138 | |
| 139 request_->Start(); | 167 request_->Start(); |
| 140 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 168 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
| 141 if (run_to_completion) | 169 if (run_to_completion) |
| 142 base::MessageLoop::current()->Run(); | 170 base::MessageLoop::current()->Run(); |
| 143 } | 171 } |
| 144 | 172 |
| 145 void TestRequest(const GURL& url) { | 173 void TestRequest(const GURL& url) { |
| 146 TestRequestHelper(url, true, file_system_context_.get()); | 174 TestRequestHelper(url, true, file_system_context_.get()); |
| 147 } | 175 } |
| 148 | 176 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 std::string date_str; | 261 std::string date_str; |
| 234 base::UTF16ToUTF8(date_ustr.getBuffer(), date_ustr.length(), &date_str); | 262 base::UTF16ToUTF8(date_ustr.getBuffer(), date_ustr.length(), &date_str); |
| 235 EXPECT_TRUE(base::Time::FromString(date_str.c_str(), &date)); | 263 EXPECT_TRUE(base::Time::FromString(date_str.c_str(), &date)); |
| 236 EXPECT_FALSE(date.is_null()); | 264 EXPECT_FALSE(date.is_null()); |
| 237 } | 265 } |
| 238 | 266 |
| 239 GURL CreateFileSystemURL(const std::string path) { | 267 GURL CreateFileSystemURL(const std::string path) { |
| 240 return GURL(kFileSystemURLPrefix + path); | 268 return GURL(kFileSystemURLPrefix + path); |
| 241 } | 269 } |
| 242 | 270 |
| 243 static net::URLRequestJob* FileSystemDirURLRequestJobFactory( | |
| 244 net::URLRequest* request, | |
| 245 net::NetworkDelegate* network_delegate, | |
| 246 const std::string& scheme) { | |
| 247 DCHECK(job_); | |
| 248 net::URLRequestJob* temp = job_; | |
| 249 job_ = NULL; | |
| 250 return temp; | |
| 251 } | |
| 252 | |
| 253 static void ClearUnusedJob() { | |
| 254 if (job_) { | |
| 255 scoped_refptr<net::URLRequestJob> deleter = job_; | |
| 256 job_ = NULL; | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 fileapi::FileSystemFileUtil* file_util() { | 271 fileapi::FileSystemFileUtil* file_util() { |
| 261 return file_system_context_->sandbox_delegate()->sync_file_util(); | 272 return file_system_context_->sandbox_delegate()->sync_file_util(); |
| 262 } | 273 } |
| 263 | 274 |
| 264 // Put the message loop at the top, so that it's the last thing deleted. | 275 // Put the message loop at the top, so that it's the last thing deleted. |
| 265 // Delete all MessageLoopProxy objects before the MessageLoop, to help prevent | 276 // Delete all MessageLoopProxy objects before the MessageLoop, to help prevent |
| 266 // leaks caused by tasks posted during shutdown. | 277 // leaks caused by tasks posted during shutdown. |
| 267 base::MessageLoopForIO message_loop_; | 278 base::MessageLoopForIO message_loop_; |
| 268 | 279 |
| 269 base::ScopedTempDir temp_dir_; | 280 base::ScopedTempDir temp_dir_; |
| 270 net::URLRequestContext empty_context_; | 281 net::URLRequestContext empty_context_; |
| 271 scoped_ptr<net::TestDelegate> delegate_; | 282 scoped_ptr<net::TestDelegate> delegate_; |
| 272 scoped_ptr<net::URLRequest> request_; | 283 scoped_ptr<net::URLRequest> request_; |
| 284 scoped_ptr<FileSystemDirURLRequestJobFactory> job_factory_; |
| 273 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; | 285 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; |
| 274 scoped_refptr<FileSystemContext> file_system_context_; | 286 scoped_refptr<FileSystemContext> file_system_context_; |
| 275 base::WeakPtrFactory<FileSystemDirURLRequestJobTest> weak_factory_; | 287 base::WeakPtrFactory<FileSystemDirURLRequestJobTest> weak_factory_; |
| 276 | |
| 277 static net::URLRequestJob* job_; | |
| 278 }; | 288 }; |
| 279 | 289 |
| 280 // static | |
| 281 net::URLRequestJob* FileSystemDirURLRequestJobTest::job_ = NULL; | |
| 282 | |
| 283 namespace { | 290 namespace { |
| 284 | 291 |
| 285 TEST_F(FileSystemDirURLRequestJobTest, DirectoryListing) { | 292 TEST_F(FileSystemDirURLRequestJobTest, DirectoryListing) { |
| 286 CreateDirectory("foo"); | 293 CreateDirectory("foo"); |
| 287 CreateDirectory("foo/bar"); | 294 CreateDirectory("foo/bar"); |
| 288 CreateDirectory("foo/bar/baz"); | 295 CreateDirectory("foo/bar/baz"); |
| 289 | 296 |
| 290 EnsureFileExists("foo/bar/hoge"); | 297 EnsureFileExists("foo/bar/hoge"); |
| 291 TruncateFile("foo/bar/hoge", 10); | 298 TruncateFile("foo/bar/hoge", 10); |
| 292 | 299 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 ASSERT_FALSE(request_->status().is_success()); | 433 ASSERT_FALSE(request_->status().is_success()); |
| 427 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 434 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 428 | 435 |
| 429 ASSERT_FALSE( | 436 ASSERT_FALSE( |
| 430 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | 437 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 431 kValidExternalMountPoint)); | 438 kValidExternalMountPoint)); |
| 432 } | 439 } |
| 433 | 440 |
| 434 } // namespace (anonymous) | 441 } // namespace (anonymous) |
| 435 } // namespace content | 442 } // namespace content |
| OLD | NEW |