| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 | 47 |
| 48 class TestMultiBufferDataProvider : public ResourceMultiBufferDataProvider { | 48 class TestMultiBufferDataProvider : public ResourceMultiBufferDataProvider { |
| 49 public: | 49 public: |
| 50 TestMultiBufferDataProvider(UrlData* url_data, MultiBuffer::BlockId pos) | 50 TestMultiBufferDataProvider(UrlData* url_data, MultiBuffer::BlockId pos) |
| 51 : ResourceMultiBufferDataProvider(url_data, pos), loading_(false) { | 51 : ResourceMultiBufferDataProvider(url_data, pos), loading_(false) { |
| 52 CHECK(test_data_providers.insert(this).second); | 52 CHECK(test_data_providers.insert(this).second); |
| 53 } | 53 } |
| 54 ~TestMultiBufferDataProvider() override { | 54 ~TestMultiBufferDataProvider() override { |
| 55 CHECK_EQ(static_cast<size_t>(1), test_data_providers.erase(this)); | 55 CHECK_EQ(static_cast<size_t>(1), test_data_providers.erase(this)); |
| 56 } | 56 } |
| 57 void SetLoadingToFalse() { |
| 58 // Check that we have not been destroyed first. |
| 59 if (test_data_providers.find(this) != test_data_providers.end()) { |
| 60 loading_ = false; |
| 61 } |
| 62 } |
| 57 void Start() override { | 63 void Start() override { |
| 58 // Create a mock active loader. | 64 // Create a mock active loader. |
| 59 // Keep track of active loading state via loadAsynchronously() and cancel(). | 65 // Keep track of active loading state via loadAsynchronously() and cancel(). |
| 60 NiceMock<MockWebURLLoader>* url_loader = new NiceMock<MockWebURLLoader>(); | 66 NiceMock<MockWebURLLoader>* url_loader = new NiceMock<MockWebURLLoader>(); |
| 61 ON_CALL(*url_loader, cancel()).WillByDefault(Assign(&loading_, false)); | 67 ON_CALL(*url_loader, cancel()) |
| 68 .WillByDefault( |
| 69 Invoke(this, &TestMultiBufferDataProvider::SetLoadingToFalse)); |
| 62 loading_ = true; | 70 loading_ = true; |
| 63 active_loader_.reset( | 71 active_loader_.reset( |
| 64 new ActiveLoader(scoped_ptr<WebURLLoader>(url_loader))); | 72 new ActiveLoader(scoped_ptr<WebURLLoader>(url_loader))); |
| 65 if (!on_start_.is_null()) { | 73 if (!on_start_.is_null()) { |
| 66 on_start_.Run(); | 74 on_start_.Run(); |
| 67 } | 75 } |
| 68 } | 76 } |
| 69 | 77 |
| 70 bool loading() const { return loading_; } | 78 bool loading() const { return loading_; } |
| 71 void RunOnStart(base::Closure cb) { on_start_ = cb; } | 79 void RunOnStart(base::Closure cb) { on_start_ = cb; } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 MOCK_METHOD2(AddBufferedByteRange, void(int64_t start, int64_t end)); | 178 MOCK_METHOD2(AddBufferedByteRange, void(int64_t start, int64_t end)); |
| 171 | 179 |
| 172 private: | 180 private: |
| 173 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSourceHost); | 181 DISALLOW_COPY_AND_ASSIGN(MockBufferedDataSourceHost); |
| 174 }; | 182 }; |
| 175 | 183 |
| 176 class MockMultibufferDataSource : public MultibufferDataSource { | 184 class MockMultibufferDataSource : public MultibufferDataSource { |
| 177 public: | 185 public: |
| 178 MockMultibufferDataSource( | 186 MockMultibufferDataSource( |
| 179 const GURL& url, | 187 const GURL& url, |
| 188 UrlData::CORSMode cors_mode, |
| 180 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 189 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 181 linked_ptr<UrlIndex> url_index, | 190 linked_ptr<UrlIndex> url_index, |
| 182 WebLocalFrame* frame, | 191 WebLocalFrame* frame, |
| 183 BufferedDataSourceHost* host) | 192 BufferedDataSourceHost* host) |
| 184 : MultibufferDataSource( | 193 : MultibufferDataSource( |
| 185 url, | 194 url, |
| 186 UrlData::CORS_UNSPECIFIED, | 195 cors_mode, |
| 187 task_runner, | 196 task_runner, |
| 188 url_index, | 197 url_index, |
| 189 frame, | 198 frame, |
| 190 new media::MediaLog(), | 199 new media::MediaLog(), |
| 191 host, | 200 host, |
| 192 base::Bind(&MockMultibufferDataSource::set_downloading, | 201 base::Bind(&MockMultibufferDataSource::set_downloading, |
| 193 base::Unretained(this))), | 202 base::Unretained(this))), |
| 194 downloading_(false) {} | 203 downloading_(false) {} |
| 195 | 204 |
| 196 bool downloading() { return downloading_; } | 205 bool downloading() { return downloading_; } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 224 view_->setMainFrame(frame_); | 233 view_->setMainFrame(frame_); |
| 225 } | 234 } |
| 226 | 235 |
| 227 virtual ~MultibufferDataSourceTest() { | 236 virtual ~MultibufferDataSourceTest() { |
| 228 view_->close(); | 237 view_->close(); |
| 229 frame_->close(); | 238 frame_->close(); |
| 230 } | 239 } |
| 231 | 240 |
| 232 MOCK_METHOD1(OnInitialize, void(bool)); | 241 MOCK_METHOD1(OnInitialize, void(bool)); |
| 233 | 242 |
| 234 void Initialize(const char* url, bool expected) { | 243 void InitializeWithCORS(const char* url, |
| 244 bool expected, |
| 245 UrlData::CORSMode cors_mode) { |
| 235 GURL gurl(url); | 246 GURL gurl(url); |
| 236 data_source_.reset(new MockMultibufferDataSource( | 247 data_source_.reset(new MockMultibufferDataSource( |
| 237 gurl, message_loop_.task_runner(), url_index_, | 248 gurl, cors_mode, message_loop_.task_runner(), url_index_, |
| 238 view_->mainFrame()->toWebLocalFrame(), &host_)); | 249 view_->mainFrame()->toWebLocalFrame(), &host_)); |
| 239 data_source_->SetPreload(preload_); | 250 data_source_->SetPreload(preload_); |
| 240 | 251 |
| 241 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); | 252 response_generator_.reset(new TestResponseGenerator(gurl, kFileSize)); |
| 242 EXPECT_CALL(*this, OnInitialize(expected)); | 253 EXPECT_CALL(*this, OnInitialize(expected)); |
| 243 data_source_->Initialize(base::Bind( | 254 data_source_->Initialize(base::Bind( |
| 244 &MultibufferDataSourceTest::OnInitialize, base::Unretained(this))); | 255 &MultibufferDataSourceTest::OnInitialize, base::Unretained(this))); |
| 245 message_loop_.RunUntilIdle(); | 256 message_loop_.RunUntilIdle(); |
| 246 | 257 |
| 247 // Not really loading until after OnInitialize is called. | 258 // Not really loading until after OnInitialize is called. |
| 248 EXPECT_EQ(data_source_->downloading(), false); | 259 EXPECT_EQ(data_source_->downloading(), false); |
| 249 } | 260 } |
| 250 | 261 |
| 262 void Initialize(const char* url, bool expected) { |
| 263 InitializeWithCORS(url, expected, UrlData::CORS_UNSPECIFIED); |
| 264 } |
| 265 |
| 251 // Helper to initialize tests with a valid 200 response. | 266 // Helper to initialize tests with a valid 200 response. |
| 252 void InitializeWith200Response() { | 267 void InitializeWith200Response() { |
| 253 Initialize(kHttpUrl, true); | 268 Initialize(kHttpUrl, true); |
| 254 | 269 |
| 255 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); | 270 EXPECT_CALL(host_, SetTotalBytes(response_generator_->content_length())); |
| 256 Respond(response_generator_->Generate200()); | 271 Respond(response_generator_->Generate200()); |
| 257 | 272 |
| 258 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); | 273 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); |
| 259 ReceiveData(kDataSize); | 274 ReceiveData(kDataSize); |
| 260 } | 275 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 ReceiveData(kDataSize); | 379 ReceiveData(kDataSize); |
| 365 FinishLoading(); | 380 FinishLoading(); |
| 366 Stop(); | 381 Stop(); |
| 367 } | 382 } |
| 368 | 383 |
| 369 void ExecuteMixedResponseFailureTest(const WebURLResponse& response1, | 384 void ExecuteMixedResponseFailureTest(const WebURLResponse& response1, |
| 370 const WebURLResponse& response2) { | 385 const WebURLResponse& response2) { |
| 371 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); | 386 EXPECT_CALL(host_, SetTotalBytes(kFileSize)); |
| 372 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); | 387 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize)); |
| 373 EXPECT_CALL(*this, ReadCallback(kDataSize)); | 388 EXPECT_CALL(*this, ReadCallback(kDataSize)); |
| 374 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)); | 389 // Stop() will also cause the readback to be called with kReadError, but |
| 390 // we want to make sure it was called before Stop(). |
| 391 bool failed_ = false; |
| 392 EXPECT_CALL(*this, ReadCallback(media::DataSource::kReadError)) |
| 393 .WillOnce(Assign(&failed_, true)); |
| 375 | 394 |
| 376 Respond(response1); | 395 Respond(response1); |
| 377 ReceiveData(kDataSize); | 396 ReceiveData(kDataSize); |
| 378 ReadAt(0); | 397 ReadAt(0); |
| 379 EXPECT_TRUE(loading()); | 398 EXPECT_TRUE(loading()); |
| 380 | 399 |
| 381 FinishLoading(); | 400 FinishLoading(); |
| 382 Restart(); | 401 Restart(); |
| 383 ReadAt(kDataSize); | 402 ReadAt(kDataSize); |
| 384 Respond(response2); | 403 Respond(response2); |
| 404 EXPECT_TRUE(failed_); |
| 385 Stop(); | 405 Stop(); |
| 386 } | 406 } |
| 387 | 407 |
| 388 void CheckCapacityDefer() { | 408 void CheckCapacityDefer() { |
| 389 EXPECT_EQ(2 << 20, preload_low()); | 409 EXPECT_EQ(2 << 20, preload_low()); |
| 390 EXPECT_EQ(3 << 20, preload_high()); | 410 EXPECT_EQ(3 << 20, preload_high()); |
| 391 } | 411 } |
| 392 | 412 |
| 393 void CheckReadThenDefer() { | 413 void CheckReadThenDefer() { |
| 394 EXPECT_EQ(0, preload_low()); | 414 EXPECT_EQ(0, preload_low()); |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 response_generator_->GeneratePartial206(0, kDataSize - 1); | 785 response_generator_->GeneratePartial206(0, kDataSize - 1); |
| 766 response1.setWasFetchedViaServiceWorker(true); | 786 response1.setWasFetchedViaServiceWorker(true); |
| 767 response1.setOriginalURLViaServiceWorker(GURL(kHttpDifferentOriginUrl)); | 787 response1.setOriginalURLViaServiceWorker(GURL(kHttpDifferentOriginUrl)); |
| 768 WebURLResponse response2 = | 788 WebURLResponse response2 = |
| 769 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1); | 789 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1); |
| 770 // The origin URL of response1 and response2 are different. So an error should | 790 // The origin URL of response1 and response2 are different. So an error should |
| 771 // occur. | 791 // occur. |
| 772 ExecuteMixedResponseFailureTest(response1, response2); | 792 ExecuteMixedResponseFailureTest(response1, response2); |
| 773 } | 793 } |
| 774 | 794 |
| 795 TEST_F(MultibufferDataSourceTest, |
| 796 Http_MixedResponse_ServiceWorkerProxiedAndDifferentOriginResponseCORS) { |
| 797 InitializeWithCORS(kHttpUrl, true, UrlData::CORS_ANONYMOUS); |
| 798 WebURLResponse response1 = |
| 799 response_generator_->GeneratePartial206(0, kDataSize - 1); |
| 800 response1.setWasFetchedViaServiceWorker(true); |
| 801 response1.setOriginalURLViaServiceWorker(GURL(kHttpDifferentOriginUrl)); |
| 802 WebURLResponse response2 = |
| 803 response_generator_->GeneratePartial206(kDataSize, kDataSize * 2 - 1); |
| 804 // The origin URL of response1 and response2 are different, but a CORS check |
| 805 // has been passed for each request, so expect success. |
| 806 ExecuteMixedResponseSuccessTest(response1, response2); |
| 807 } |
| 808 |
| 775 TEST_F(MultibufferDataSourceTest, File_Retry) { | 809 TEST_F(MultibufferDataSourceTest, File_Retry) { |
| 776 InitializeWithFileResponse(); | 810 InitializeWithFileResponse(); |
| 777 | 811 |
| 778 // Read to advance our position. | 812 // Read to advance our position. |
| 779 EXPECT_CALL(*this, ReadCallback(kDataSize)); | 813 EXPECT_CALL(*this, ReadCallback(kDataSize)); |
| 780 ReadAt(0); | 814 ReadAt(0); |
| 781 | 815 |
| 782 // Issue a pending read but terminate the connection to force a retry. | 816 // Issue a pending read but terminate the connection to force a retry. |
| 783 ReadAt(kDataSize); | 817 ReadAt(kDataSize); |
| 784 FinishLoading(); | 818 FinishLoading(); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 960 EXPECT_CALL(host_, | 994 EXPECT_CALL(host_, |
| 961 AddBufferedByteRange(kDataSize, kDataSize + kDataSize / 2)); | 995 AddBufferedByteRange(kDataSize, kDataSize + kDataSize / 2)); |
| 962 ReceiveData(kDataSize / 2); | 996 ReceiveData(kDataSize / 2); |
| 963 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); | 997 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); |
| 964 ReceiveData(kDataSize / 2); | 998 ReceiveData(kDataSize / 2); |
| 965 | 999 |
| 966 EXPECT_TRUE(data_source_->downloading()); | 1000 EXPECT_TRUE(data_source_->downloading()); |
| 967 | 1001 |
| 968 StrictMock<MockBufferedDataSourceHost> host2; | 1002 StrictMock<MockBufferedDataSourceHost> host2; |
| 969 MockMultibufferDataSource source2( | 1003 MockMultibufferDataSource source2( |
| 970 GURL(kHttpUrl), message_loop_.task_runner(), url_index_, | 1004 GURL(kHttpUrl), UrlData::CORS_UNSPECIFIED, message_loop_.task_runner(), |
| 971 view_->mainFrame()->toWebLocalFrame(), &host2); | 1005 url_index_, view_->mainFrame()->toWebLocalFrame(), &host2); |
| 972 source2.SetPreload(preload_); | 1006 source2.SetPreload(preload_); |
| 973 | 1007 |
| 974 EXPECT_CALL(*this, OnInitialize(true)); | 1008 EXPECT_CALL(*this, OnInitialize(true)); |
| 975 | 1009 |
| 976 // This call would not be expected if we were not sharing data. | 1010 // This call would not be expected if we were not sharing data. |
| 977 EXPECT_CALL(host2, SetTotalBytes(response_generator_->content_length())); | 1011 EXPECT_CALL(host2, SetTotalBytes(response_generator_->content_length())); |
| 978 source2.Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, | 1012 source2.Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, |
| 979 base::Unretained(this))); | 1013 base::Unretained(this))); |
| 980 message_loop_.RunUntilIdle(); | 1014 message_loop_.RunUntilIdle(); |
| 981 | 1015 |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1254 bytes_received += kDataSize; | 1288 bytes_received += kDataSize; |
| 1255 } | 1289 } |
| 1256 EXPECT_GT(bytes_received, 0); | 1290 EXPECT_GT(bytes_received, 0); |
| 1257 EXPECT_LT(bytes_received + kDataSize, kFileSize); | 1291 EXPECT_LT(bytes_received + kDataSize, kFileSize); |
| 1258 EXPECT_FALSE(active_loader_allownull()); | 1292 EXPECT_FALSE(active_loader_allownull()); |
| 1259 } | 1293 } |
| 1260 | 1294 |
| 1261 TEST_F(MultibufferDataSourceTest, SeekPastEOF) { | 1295 TEST_F(MultibufferDataSourceTest, SeekPastEOF) { |
| 1262 GURL gurl(kHttpUrl); | 1296 GURL gurl(kHttpUrl); |
| 1263 data_source_.reset(new MockMultibufferDataSource( | 1297 data_source_.reset(new MockMultibufferDataSource( |
| 1264 gurl, message_loop_.task_runner(), url_index_, | 1298 gurl, UrlData::CORS_UNSPECIFIED, message_loop_.task_runner(), url_index_, |
| 1265 view_->mainFrame()->toWebLocalFrame(), &host_)); | 1299 view_->mainFrame()->toWebLocalFrame(), &host_)); |
| 1266 data_source_->SetPreload(preload_); | 1300 data_source_->SetPreload(preload_); |
| 1267 | 1301 |
| 1268 response_generator_.reset(new TestResponseGenerator(gurl, kDataSize + 1)); | 1302 response_generator_.reset(new TestResponseGenerator(gurl, kDataSize + 1)); |
| 1269 EXPECT_CALL(*this, OnInitialize(true)); | 1303 EXPECT_CALL(*this, OnInitialize(true)); |
| 1270 data_source_->Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, | 1304 data_source_->Initialize(base::Bind(&MultibufferDataSourceTest::OnInitialize, |
| 1271 base::Unretained(this))); | 1305 base::Unretained(this))); |
| 1272 message_loop_.RunUntilIdle(); | 1306 message_loop_.RunUntilIdle(); |
| 1273 | 1307 |
| 1274 // Not really loading until after OnInitialize is called. | 1308 // Not really loading until after OnInitialize is called. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1365 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); | 1399 EXPECT_CALL(host_, AddBufferedByteRange(0, kDataSize * 2)); |
| 1366 FinishLoading(); | 1400 FinishLoading(); |
| 1367 | 1401 |
| 1368 // Done loading, now we should know the length. | 1402 // Done loading, now we should know the length. |
| 1369 EXPECT_TRUE(data_source_->GetSize(&len)); | 1403 EXPECT_TRUE(data_source_->GetSize(&len)); |
| 1370 EXPECT_EQ(kDataSize, len); | 1404 EXPECT_EQ(kDataSize, len); |
| 1371 Stop(); | 1405 Stop(); |
| 1372 } | 1406 } |
| 1373 | 1407 |
| 1374 } // namespace media | 1408 } // namespace media |
| OLD | NEW |