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/file_system_file_stream_reader.h" | 5 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 size_t size, | 36 size_t size, |
37 int* result) { | 37 int* result) { |
38 ASSERT_TRUE(reader != NULL); | 38 ASSERT_TRUE(reader != NULL); |
39 ASSERT_TRUE(result != NULL); | 39 ASSERT_TRUE(result != NULL); |
40 *result = net::OK; | 40 *result = net::OK; |
41 net::TestCompletionCallback callback; | 41 net::TestCompletionCallback callback; |
42 size_t total_bytes_read = 0; | 42 size_t total_bytes_read = 0; |
43 while (total_bytes_read < size) { | 43 while (total_bytes_read < size) { |
44 scoped_refptr<net::IOBufferWithSize> buf( | 44 scoped_refptr<net::IOBufferWithSize> buf( |
45 new net::IOBufferWithSize(size - total_bytes_read)); | 45 new net::IOBufferWithSize(size - total_bytes_read)); |
46 int rv = reader->Read(buf, buf->size(), callback.callback()); | 46 int rv = reader->Read(buf.get(), buf->size(), callback.callback()); |
47 if (rv == net::ERR_IO_PENDING) | 47 if (rv == net::ERR_IO_PENDING) |
48 rv = callback.WaitForResult(); | 48 rv = callback.WaitForResult(); |
49 if (rv < 0) | 49 if (rv < 0) |
50 *result = rv; | 50 *result = rv; |
51 if (rv <= 0) | 51 if (rv <= 0) |
52 break; | 52 break; |
53 total_bytes_read += rv; | 53 total_bytes_read += rv; |
54 data->append(buf->data(), rv); | 54 data->append(buf->data(), rv); |
55 } | 55 } |
56 } | 56 } |
(...skipping 25 matching lines...) Expand all Loading... |
82 | 82 |
83 virtual void TearDown() OVERRIDE { | 83 virtual void TearDown() OVERRIDE { |
84 base::MessageLoop::current()->RunUntilIdle(); | 84 base::MessageLoop::current()->RunUntilIdle(); |
85 } | 85 } |
86 | 86 |
87 protected: | 87 protected: |
88 FileSystemFileStreamReader* CreateFileReader( | 88 FileSystemFileStreamReader* CreateFileReader( |
89 const std::string& file_name, | 89 const std::string& file_name, |
90 int64 initial_offset, | 90 int64 initial_offset, |
91 const base::Time& expected_modification_time) { | 91 const base::Time& expected_modification_time) { |
92 return new FileSystemFileStreamReader(file_system_context_, | 92 return new FileSystemFileStreamReader(file_system_context_.get(), |
93 GetFileSystemURL(file_name), | 93 GetFileSystemURL(file_name), |
94 initial_offset, | 94 initial_offset, |
95 expected_modification_time); | 95 expected_modification_time); |
96 } | 96 } |
97 | 97 |
98 base::Time test_file_modification_time() const { | 98 base::Time test_file_modification_time() const { |
99 return test_file_modification_time_; | 99 return test_file_modification_time_; |
100 } | 100 } |
101 | 101 |
102 void WriteFile(const std::string& file_name, | 102 void WriteFile(const std::string& file_name, |
103 const char* buf, | 103 const char* buf, |
104 int buf_size, | 104 int buf_size, |
105 base::Time* modification_time) { | 105 base::Time* modification_time) { |
106 FileSystemFileUtil* file_util = file_system_context_-> | 106 FileSystemFileUtil* file_util = file_system_context_-> |
107 sandbox_provider()->GetFileUtil(kFileSystemTypeTemporary); | 107 sandbox_provider()->GetFileUtil(kFileSystemTypeTemporary); |
108 FileSystemURL url = GetFileSystemURL(file_name); | 108 FileSystemURL url = GetFileSystemURL(file_name); |
109 | 109 |
110 FileSystemOperationContext context(file_system_context_); | 110 FileSystemOperationContext context(file_system_context_.get()); |
111 context.set_allowed_bytes_growth(1024); | 111 context.set_allowed_bytes_growth(1024); |
112 | 112 |
113 base::PlatformFile handle = base::kInvalidPlatformFileValue; | 113 base::PlatformFile handle = base::kInvalidPlatformFileValue; |
114 bool created = false; | 114 bool created = false; |
115 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->CreateOrOpen( | 115 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->CreateOrOpen( |
116 &context, | 116 &context, |
117 url, | 117 url, |
118 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, | 118 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
119 &handle, | 119 &handle, |
120 &created)); | 120 &created)); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 ASSERT_EQ(&kTestData[3], data); | 264 ASSERT_EQ(&kTestData[3], data); |
265 } | 265 } |
266 | 266 |
267 TEST_F(FileSystemFileStreamReaderTest, DeleteWithUnfinishedRead) { | 267 TEST_F(FileSystemFileStreamReaderTest, DeleteWithUnfinishedRead) { |
268 scoped_ptr<FileSystemFileStreamReader> reader( | 268 scoped_ptr<FileSystemFileStreamReader> reader( |
269 CreateFileReader(kTestFileName, 0, base::Time())); | 269 CreateFileReader(kTestFileName, 0, base::Time())); |
270 | 270 |
271 net::TestCompletionCallback callback; | 271 net::TestCompletionCallback callback; |
272 scoped_refptr<net::IOBufferWithSize> buf( | 272 scoped_refptr<net::IOBufferWithSize> buf( |
273 new net::IOBufferWithSize(kTestDataSize)); | 273 new net::IOBufferWithSize(kTestDataSize)); |
274 int rv = reader->Read(buf, buf->size(), base::Bind(&NeverCalled)); | 274 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled)); |
275 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); | 275 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); |
276 | 276 |
277 // Delete immediately. | 277 // Delete immediately. |
278 // Should not crash; nor should NeverCalled be callback. | 278 // Should not crash; nor should NeverCalled be callback. |
279 reader.reset(); | 279 reader.reset(); |
280 } | 280 } |
281 | 281 |
282 } // namespace fileapi | 282 } // namespace fileapi |
OLD | NEW |