| 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_url_request_job.h" | 5 #include "webkit/browser/fileapi/file_system_url_request_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | 73 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
| 74 kValidExternalMountPoint, fileapi::kFileSystemTypeTest, | 74 kValidExternalMountPoint, fileapi::kFileSystemTypeTest, |
| 75 fileapi::FileSystemMountOption(), base::FilePath()); | 75 fileapi::FileSystemMountOption(), base::FilePath()); |
| 76 callback.Run(base::File::FILE_OK); | 76 callback.Run(base::File::FILE_OK); |
| 77 } else { | 77 } else { |
| 78 callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 78 callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
| 79 } | 79 } |
| 80 return true; | 80 return true; |
| 81 } | 81 } |
| 82 | 82 |
| 83 class FileSystemURLRequestJobFactory : public net::URLRequestJobFactory { |
| 84 public: |
| 85 FileSystemURLRequestJobFactory(const std::string& storage_domain, |
| 86 FileSystemContext* context) |
| 87 : storage_domain_(storage_domain), file_system_context_(context) { |
| 88 } |
| 89 |
| 90 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 91 const std::string& scheme, |
| 92 net::URLRequest* request, |
| 93 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 94 return new fileapi::FileSystemURLRequestJob( |
| 95 request, network_delegate, storage_domain_, file_system_context_); |
| 96 } |
| 97 |
| 98 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 99 return true; |
| 100 } |
| 101 |
| 102 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 103 return true; |
| 104 } |
| 105 |
| 106 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
| 107 return false; |
| 108 } |
| 109 |
| 110 private: |
| 111 std::string storage_domain_; |
| 112 FileSystemContext* file_system_context_; |
| 113 }; |
| 114 |
| 83 } // namespace | 115 } // namespace |
| 84 | 116 |
| 85 class FileSystemURLRequestJobTest : public testing::Test { | 117 class FileSystemURLRequestJobTest : public testing::Test { |
| 86 protected: | 118 protected: |
| 87 FileSystemURLRequestJobTest() : weak_factory_(this) { | 119 FileSystemURLRequestJobTest() : weak_factory_(this) { |
| 88 } | 120 } |
| 89 | 121 |
| 90 virtual void SetUp() OVERRIDE { | 122 virtual void SetUp() OVERRIDE { |
| 91 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 123 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 92 | 124 |
| 93 // We use the main thread so that we can get the root path synchronously. | 125 // We use the main thread so that we can get the root path synchronously. |
| 94 // TODO(adamk): Run this on the FILE thread we've created as well. | 126 // TODO(adamk): Run this on the FILE thread we've created as well. |
| 95 file_system_context_ = | 127 file_system_context_ = |
| 96 CreateFileSystemContextForTesting(NULL, temp_dir_.path()); | 128 CreateFileSystemContextForTesting(NULL, temp_dir_.path()); |
| 97 | 129 |
| 98 file_system_context_->OpenFileSystem( | 130 file_system_context_->OpenFileSystem( |
| 99 GURL("http://remote/"), fileapi::kFileSystemTypeTemporary, | 131 GURL("http://remote/"), fileapi::kFileSystemTypeTemporary, |
| 100 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 132 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 101 base::Bind(&FileSystemURLRequestJobTest::OnOpenFileSystem, | 133 base::Bind(&FileSystemURLRequestJobTest::OnOpenFileSystem, |
| 102 weak_factory_.GetWeakPtr())); | 134 weak_factory_.GetWeakPtr())); |
| 103 base::RunLoop().RunUntilIdle(); | 135 base::RunLoop().RunUntilIdle(); |
| 104 | |
| 105 net::URLRequest::Deprecated::RegisterProtocolFactory( | |
| 106 "filesystem", &FileSystemURLRequestJobFactory); | |
| 107 } | 136 } |
| 108 | 137 |
| 109 virtual void TearDown() OVERRIDE { | 138 virtual void TearDown() OVERRIDE { |
| 110 net::URLRequest::Deprecated::RegisterProtocolFactory("filesystem", NULL); | |
| 111 ClearUnusedJob(); | |
| 112 if (pending_job_.get()) { | |
| 113 pending_job_->Kill(); | |
| 114 pending_job_ = NULL; | |
| 115 } | |
| 116 // FileReader posts a task to close the file in destructor. | 139 // FileReader posts a task to close the file in destructor. |
| 117 base::RunLoop().RunUntilIdle(); | 140 base::RunLoop().RunUntilIdle(); |
| 118 } | 141 } |
| 119 | 142 |
| 120 void SetUpAutoMountContext() { | 143 void SetUpAutoMountContext() { |
| 121 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | 144 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); |
| 122 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | 145 ASSERT_TRUE(base::CreateDirectory(mnt_point)); |
| 123 | 146 |
| 124 ScopedVector<fileapi::FileSystemBackend> additional_providers; | 147 ScopedVector<fileapi::FileSystemBackend> additional_providers; |
| 125 additional_providers.push_back(new TestFileSystemBackend( | 148 additional_providers.push_back(new TestFileSystemBackend( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 143 } | 166 } |
| 144 | 167 |
| 145 void TestRequestHelper(const GURL& url, | 168 void TestRequestHelper(const GURL& url, |
| 146 const net::HttpRequestHeaders* headers, | 169 const net::HttpRequestHeaders* headers, |
| 147 bool run_to_completion, | 170 bool run_to_completion, |
| 148 FileSystemContext* file_system_context) { | 171 FileSystemContext* file_system_context) { |
| 149 delegate_.reset(new net::TestDelegate()); | 172 delegate_.reset(new net::TestDelegate()); |
| 150 // Make delegate_ exit the MessageLoop when the request is done. | 173 // Make delegate_ exit the MessageLoop when the request is done. |
| 151 delegate_->set_quit_on_complete(true); | 174 delegate_->set_quit_on_complete(true); |
| 152 delegate_->set_quit_on_redirect(true); | 175 delegate_->set_quit_on_redirect(true); |
| 176 |
| 177 job_factory_.reset(new FileSystemURLRequestJobFactory( |
| 178 url.GetOrigin().host(), file_system_context)); |
| 179 empty_context_.set_job_factory(job_factory_.get()); |
| 180 |
| 153 request_ = empty_context_.CreateRequest( | 181 request_ = empty_context_.CreateRequest( |
| 154 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); | 182 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL); |
| 155 if (headers) | 183 if (headers) |
| 156 request_->SetExtraRequestHeaders(*headers); | 184 request_->SetExtraRequestHeaders(*headers); |
| 157 ASSERT_TRUE(!job_); | |
| 158 job_ = new FileSystemURLRequestJob( | |
| 159 request_.get(), NULL, url.GetOrigin().host(), file_system_context); | |
| 160 pending_job_ = job_; | |
| 161 | 185 |
| 162 request_->Start(); | 186 request_->Start(); |
| 163 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 187 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
| 164 if (run_to_completion) | 188 if (run_to_completion) |
| 165 base::MessageLoop::current()->Run(); | 189 base::MessageLoop::current()->Run(); |
| 166 } | 190 } |
| 167 | 191 |
| 168 void TestRequest(const GURL& url) { | 192 void TestRequest(const GURL& url) { |
| 169 TestRequestHelper(url, NULL, true, file_system_context_.get()); | 193 TestRequestHelper(url, NULL, true, file_system_context_.get()); |
| 170 } | 194 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 200 base::FilePath().AppendASCII(file_name)); | 224 base::FilePath().AppendASCII(file_name)); |
| 201 ASSERT_EQ(base::File::FILE_OK, | 225 ASSERT_EQ(base::File::FILE_OK, |
| 202 AsyncFileTestHelper::CreateFileWithData( | 226 AsyncFileTestHelper::CreateFileWithData( |
| 203 file_system_context_, url, buf, buf_size)); | 227 file_system_context_, url, buf, buf_size)); |
| 204 } | 228 } |
| 205 | 229 |
| 206 GURL CreateFileSystemURL(const std::string& path) { | 230 GURL CreateFileSystemURL(const std::string& path) { |
| 207 return GURL(kFileSystemURLPrefix + path); | 231 return GURL(kFileSystemURLPrefix + path); |
| 208 } | 232 } |
| 209 | 233 |
| 210 static net::URLRequestJob* FileSystemURLRequestJobFactory( | |
| 211 net::URLRequest* request, | |
| 212 net::NetworkDelegate* network_delegate, | |
| 213 const std::string& scheme) { | |
| 214 DCHECK(job_); | |
| 215 net::URLRequestJob* temp = job_; | |
| 216 job_ = NULL; | |
| 217 return temp; | |
| 218 } | |
| 219 | |
| 220 static void ClearUnusedJob() { | |
| 221 if (job_) { | |
| 222 scoped_refptr<net::URLRequestJob> deleter = job_; | |
| 223 job_ = NULL; | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 // Put the message loop at the top, so that it's the last thing deleted. | 234 // Put the message loop at the top, so that it's the last thing deleted. |
| 228 base::MessageLoopForIO message_loop_; | 235 base::MessageLoopForIO message_loop_; |
| 229 | 236 |
| 230 base::ScopedTempDir temp_dir_; | 237 base::ScopedTempDir temp_dir_; |
| 231 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 238 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
| 232 base::WeakPtrFactory<FileSystemURLRequestJobTest> weak_factory_; | 239 base::WeakPtrFactory<FileSystemURLRequestJobTest> weak_factory_; |
| 233 | 240 |
| 234 net::URLRequestContext empty_context_; | 241 net::URLRequestContext empty_context_; |
| 242 scoped_ptr<FileSystemURLRequestJobFactory> job_factory_; |
| 235 | 243 |
| 236 // NOTE: order matters, request must die before delegate | 244 // NOTE: order matters, request must die before delegate |
| 237 scoped_ptr<net::TestDelegate> delegate_; | 245 scoped_ptr<net::TestDelegate> delegate_; |
| 238 scoped_ptr<net::URLRequest> request_; | 246 scoped_ptr<net::URLRequest> request_; |
| 239 | |
| 240 scoped_refptr<net::URLRequestJob> pending_job_; | |
| 241 static net::URLRequestJob* job_; | |
| 242 }; | 247 }; |
| 243 | 248 |
| 244 // static | |
| 245 net::URLRequestJob* FileSystemURLRequestJobTest::job_ = NULL; | |
| 246 | |
| 247 namespace { | 249 namespace { |
| 248 | 250 |
| 249 TEST_F(FileSystemURLRequestJobTest, FileTest) { | 251 TEST_F(FileSystemURLRequestJobTest, FileTest) { |
| 250 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); | 252 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); |
| 251 TestRequest(CreateFileSystemURL("file1.dat")); | 253 TestRequest(CreateFileSystemURL("file1.dat")); |
| 252 | 254 |
| 253 ASSERT_FALSE(request_->is_pending()); | 255 ASSERT_FALSE(request_->is_pending()); |
| 254 EXPECT_EQ(1, delegate_->response_started_count()); | 256 EXPECT_EQ(1, delegate_->response_started_count()); |
| 255 EXPECT_FALSE(delegate_->received_data_before_response()); | 257 EXPECT_FALSE(delegate_->received_data_before_response()); |
| 256 EXPECT_EQ(kTestFileData, delegate_->data_received()); | 258 EXPECT_EQ(kTestFileData, delegate_->data_received()); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 EXPECT_TRUE(delegate_->request_failed()); | 455 EXPECT_TRUE(delegate_->request_failed()); |
| 454 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 456 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 455 | 457 |
| 456 ASSERT_FALSE( | 458 ASSERT_FALSE( |
| 457 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | 459 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 458 kValidExternalMountPoint)); | 460 kValidExternalMountPoint)); |
| 459 } | 461 } |
| 460 | 462 |
| 461 } // namespace | 463 } // namespace |
| 462 } // namespace content | 464 } // namespace content |
| OLD | NEW |