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 "content/browser/download/download_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 |
9 #include <set> | 10 #include <set> |
10 #include <string> | 11 #include <string> |
11 #include <utility> | 12 #include <utility> |
12 | 13 |
13 #include "base/bind.h" | 14 #include "base/bind.h" |
14 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/guid.h" |
15 #include "base/macros.h" | 17 #include "base/macros.h" |
16 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
18 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
19 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
20 #include "base/strings/string16.h" | 22 #include "base/strings/string16.h" |
21 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
22 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
23 #include "build/build_config.h" | 25 #include "build/build_config.h" |
24 #include "content/browser/byte_stream.h" | 26 #include "content/browser/byte_stream.h" |
(...skipping 17 matching lines...) Expand all Loading... |
42 #include "testing/gmock_mutant.h" | 44 #include "testing/gmock_mutant.h" |
43 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
44 #include "url/origin.h" | 46 #include "url/origin.h" |
45 | 47 |
46 using ::testing::AllOf; | 48 using ::testing::AllOf; |
47 using ::testing::DoAll; | 49 using ::testing::DoAll; |
48 using ::testing::Eq; | 50 using ::testing::Eq; |
49 using ::testing::Ref; | 51 using ::testing::Ref; |
50 using ::testing::Return; | 52 using ::testing::Return; |
51 using ::testing::ReturnRef; | 53 using ::testing::ReturnRef; |
| 54 using ::testing::ReturnRefOfCopy; |
52 using ::testing::SetArgPointee; | 55 using ::testing::SetArgPointee; |
53 using ::testing::StrictMock; | 56 using ::testing::StrictMock; |
54 using ::testing::_; | 57 using ::testing::_; |
55 | 58 |
56 ACTION_TEMPLATE(RunCallback, | 59 ACTION_TEMPLATE(RunCallback, |
57 HAS_1_TEMPLATE_PARAMS(int, k), | 60 HAS_1_TEMPLATE_PARAMS(int, k), |
58 AND_1_VALUE_PARAMS(p0)) { | 61 AND_1_VALUE_PARAMS(p0)) { |
59 return ::std::tr1::get<k>(args).Run(p0); | 62 return ::std::tr1::get<k>(args).Run(p0); |
60 } | 63 } |
61 | 64 |
62 namespace content { | 65 namespace content { |
63 class ByteStreamReader; | 66 class ByteStreamReader; |
64 | 67 |
65 namespace { | 68 namespace { |
66 | 69 |
67 // Matches a DownloadCreateInfo* that points to the same object as |info| and | 70 // Matches a DownloadCreateInfo* that points to the same object as |info| and |
68 // has a |default_download_directory| that matches |download_directory|. | 71 // has a |default_download_directory| that matches |download_directory|. |
69 MATCHER_P2(DownloadCreateInfoWithDefaultPath, info, download_directory, "") { | 72 MATCHER_P2(DownloadCreateInfoWithDefaultPath, info, download_directory, "") { |
70 return arg == info && | 73 return arg == info && |
71 arg->default_download_directory == download_directory; | 74 arg->default_download_directory == download_directory; |
72 } | 75 } |
73 | 76 |
74 class MockDownloadItemImpl : public DownloadItemImpl { | 77 class MockDownloadItemImpl : public DownloadItemImpl { |
75 public: | 78 public: |
76 // Use history constructor for minimal base object. | 79 // Use history constructor for minimal base object. |
77 explicit MockDownloadItemImpl(DownloadItemImplDelegate* delegate) | 80 explicit MockDownloadItemImpl(DownloadItemImplDelegate* delegate) |
78 : DownloadItemImpl( | 81 : DownloadItemImpl(delegate, |
79 delegate, | 82 std::string("7d122682-55b5-4a47-a253-36cadc3e5bee"), |
80 content::DownloadItem::kInvalidId, | 83 content::DownloadItem::kInvalidId, |
81 base::FilePath(), | 84 base::FilePath(), |
82 base::FilePath(), | 85 base::FilePath(), |
83 std::vector<GURL>(), | 86 std::vector<GURL>(), |
84 GURL(), | 87 GURL(), |
85 "application/octet-stream", | 88 "application/octet-stream", |
86 "application/octet-stream", | 89 "application/octet-stream", |
87 base::Time(), | 90 base::Time(), |
88 base::Time(), | 91 base::Time(), |
89 std::string(), | 92 std::string(), |
90 std::string(), | 93 std::string(), |
91 0, | 94 0, |
92 0, | 95 0, |
93 DownloadItem::COMPLETE, | 96 DownloadItem::COMPLETE, |
94 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 97 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
95 DOWNLOAD_INTERRUPT_REASON_NONE, | 98 DOWNLOAD_INTERRUPT_REASON_NONE, |
96 false, | 99 false, |
97 net::BoundNetLog()) {} | 100 net::BoundNetLog()) {} |
98 virtual ~MockDownloadItemImpl() {} | 101 virtual ~MockDownloadItemImpl() {} |
99 | 102 |
100 MOCK_METHOD4(OnDownloadTargetDetermined, | 103 MOCK_METHOD4(OnDownloadTargetDetermined, |
101 void(const base::FilePath&, TargetDisposition, | 104 void(const base::FilePath&, TargetDisposition, |
102 DownloadDangerType, const base::FilePath&)); | 105 DownloadDangerType, const base::FilePath&)); |
103 MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*)); | 106 MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*)); |
104 MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*)); | 107 MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*)); |
105 MOCK_METHOD0(UpdateObservers, void()); | 108 MOCK_METHOD0(UpdateObservers, void()); |
106 MOCK_METHOD0(CanShowInFolder, bool()); | 109 MOCK_METHOD0(CanShowInFolder, bool()); |
107 MOCK_METHOD0(CanOpenDownload, bool()); | 110 MOCK_METHOD0(CanOpenDownload, bool()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 MOCK_CONST_METHOD0(GetContentDisposition, std::string()); | 149 MOCK_CONST_METHOD0(GetContentDisposition, std::string()); |
147 MOCK_CONST_METHOD0(GetMimeType, std::string()); | 150 MOCK_CONST_METHOD0(GetMimeType, std::string()); |
148 MOCK_CONST_METHOD0(GetOriginalMimeType, std::string()); | 151 MOCK_CONST_METHOD0(GetOriginalMimeType, std::string()); |
149 MOCK_CONST_METHOD0(GetReferrerCharset, std::string()); | 152 MOCK_CONST_METHOD0(GetReferrerCharset, std::string()); |
150 MOCK_CONST_METHOD0(GetRemoteAddress, std::string()); | 153 MOCK_CONST_METHOD0(GetRemoteAddress, std::string()); |
151 MOCK_CONST_METHOD0(GetTotalBytes, int64_t()); | 154 MOCK_CONST_METHOD0(GetTotalBytes, int64_t()); |
152 MOCK_CONST_METHOD0(GetReceivedBytes, int64_t()); | 155 MOCK_CONST_METHOD0(GetReceivedBytes, int64_t()); |
153 MOCK_CONST_METHOD0(GetHashState, const std::string&()); | 156 MOCK_CONST_METHOD0(GetHashState, const std::string&()); |
154 MOCK_CONST_METHOD0(GetHash, const std::string&()); | 157 MOCK_CONST_METHOD0(GetHash, const std::string&()); |
155 MOCK_CONST_METHOD0(GetId, uint32_t()); | 158 MOCK_CONST_METHOD0(GetId, uint32_t()); |
| 159 MOCK_CONST_METHOD0(GetGuid, const std::string&()); |
156 MOCK_CONST_METHOD0(GetStartTime, base::Time()); | 160 MOCK_CONST_METHOD0(GetStartTime, base::Time()); |
157 MOCK_CONST_METHOD0(GetEndTime, base::Time()); | 161 MOCK_CONST_METHOD0(GetEndTime, base::Time()); |
158 MOCK_METHOD0(GetDownloadManager, DownloadManager*()); | 162 MOCK_METHOD0(GetDownloadManager, DownloadManager*()); |
159 MOCK_CONST_METHOD0(IsPaused, bool()); | 163 MOCK_CONST_METHOD0(IsPaused, bool()); |
160 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool()); | 164 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool()); |
161 MOCK_METHOD1(SetOpenWhenComplete, void(bool)); | 165 MOCK_METHOD1(SetOpenWhenComplete, void(bool)); |
162 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool()); | 166 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool()); |
163 MOCK_CONST_METHOD0(GetDangerType, DownloadDangerType()); | 167 MOCK_CONST_METHOD0(GetDangerType, DownloadDangerType()); |
164 MOCK_CONST_METHOD0(IsDangerous, bool()); | 168 MOCK_CONST_METHOD0(IsDangerous, bool()); |
165 MOCK_METHOD0(GetAutoOpened, bool()); | 169 MOCK_METHOD0(GetAutoOpened, bool()); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // Generally used during teardown. | 234 // Generally used during teardown. |
231 MockDownloadItemImpl* PopItem(); | 235 MockDownloadItemImpl* PopItem(); |
232 | 236 |
233 // Should be called when the item of this id is removed so that | 237 // Should be called when the item of this id is removed so that |
234 // we don't keep dangling pointers. | 238 // we don't keep dangling pointers. |
235 void RemoveItem(int id); | 239 void RemoveItem(int id); |
236 | 240 |
237 // Overridden methods from DownloadItemFactory. | 241 // Overridden methods from DownloadItemFactory. |
238 DownloadItemImpl* CreatePersistedItem( | 242 DownloadItemImpl* CreatePersistedItem( |
239 DownloadItemImplDelegate* delegate, | 243 DownloadItemImplDelegate* delegate, |
| 244 const std::string& guid, |
240 uint32_t download_id, | 245 uint32_t download_id, |
241 const base::FilePath& current_path, | 246 const base::FilePath& current_path, |
242 const base::FilePath& target_path, | 247 const base::FilePath& target_path, |
243 const std::vector<GURL>& url_chain, | 248 const std::vector<GURL>& url_chain, |
244 const GURL& referrer_url, | 249 const GURL& referrer_url, |
245 const std::string& mime_type, | 250 const std::string& mime_type, |
246 const std::string& original_mime_type, | 251 const std::string& original_mime_type, |
247 const base::Time& start_time, | 252 const base::Time& start_time, |
248 const base::Time& end_time, | 253 const base::Time& end_time, |
249 const std::string& etag, | 254 const std::string& etag, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 return result; | 302 return result; |
298 } | 303 } |
299 | 304 |
300 void MockDownloadItemFactory::RemoveItem(int id) { | 305 void MockDownloadItemFactory::RemoveItem(int id) { |
301 DCHECK(items_.find(id) != items_.end()); | 306 DCHECK(items_.find(id) != items_.end()); |
302 items_.erase(id); | 307 items_.erase(id); |
303 } | 308 } |
304 | 309 |
305 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem( | 310 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem( |
306 DownloadItemImplDelegate* delegate, | 311 DownloadItemImplDelegate* delegate, |
| 312 const std::string& guid, |
307 uint32_t download_id, | 313 uint32_t download_id, |
308 const base::FilePath& current_path, | 314 const base::FilePath& current_path, |
309 const base::FilePath& target_path, | 315 const base::FilePath& target_path, |
310 const std::vector<GURL>& url_chain, | 316 const std::vector<GURL>& url_chain, |
311 const GURL& referrer_url, | 317 const GURL& referrer_url, |
312 const std::string& mime_type, | 318 const std::string& mime_type, |
313 const std::string& original_mime_type, | 319 const std::string& original_mime_type, |
314 const base::Time& start_time, | 320 const base::Time& start_time, |
315 const base::Time& end_time, | 321 const base::Time& end_time, |
316 const std::string& etag, | 322 const std::string& etag, |
317 const std::string& last_modified, | 323 const std::string& last_modified, |
318 int64_t received_bytes, | 324 int64_t received_bytes, |
319 int64_t total_bytes, | 325 int64_t total_bytes, |
320 DownloadItem::DownloadState state, | 326 DownloadItem::DownloadState state, |
321 DownloadDangerType danger_type, | 327 DownloadDangerType danger_type, |
322 DownloadInterruptReason interrupt_reason, | 328 DownloadInterruptReason interrupt_reason, |
323 bool opened, | 329 bool opened, |
324 const net::BoundNetLog& bound_net_log) { | 330 const net::BoundNetLog& bound_net_log) { |
325 DCHECK(items_.find(download_id) == items_.end()); | 331 DCHECK(items_.find(download_id) == items_.end()); |
326 MockDownloadItemImpl* result = | 332 MockDownloadItemImpl* result = |
327 new StrictMock<MockDownloadItemImpl>(&item_delegate_); | 333 new StrictMock<MockDownloadItemImpl>(&item_delegate_); |
328 EXPECT_CALL(*result, GetId()) | 334 EXPECT_CALL(*result, GetId()) |
329 .WillRepeatedly(Return(download_id)); | 335 .WillRepeatedly(Return(download_id)); |
| 336 EXPECT_CALL(*result, GetGuid()).WillRepeatedly(ReturnRefOfCopy(guid)); |
330 items_[download_id] = result; | 337 items_[download_id] = result; |
331 return result; | 338 return result; |
332 } | 339 } |
333 | 340 |
334 DownloadItemImpl* MockDownloadItemFactory::CreateActiveItem( | 341 DownloadItemImpl* MockDownloadItemFactory::CreateActiveItem( |
335 DownloadItemImplDelegate* delegate, | 342 DownloadItemImplDelegate* delegate, |
336 uint32_t download_id, | 343 uint32_t download_id, |
337 const DownloadCreateInfo& info, | 344 const DownloadCreateInfo& info, |
338 const net::BoundNetLog& bound_net_log) { | 345 const net::BoundNetLog& bound_net_log) { |
339 DCHECK(items_.find(download_id) == items_.end()); | 346 DCHECK(items_.find(download_id) == items_.end()); |
340 | 347 |
341 MockDownloadItemImpl* result = | 348 MockDownloadItemImpl* result = |
342 new StrictMock<MockDownloadItemImpl>(&item_delegate_); | 349 new StrictMock<MockDownloadItemImpl>(&item_delegate_); |
343 EXPECT_CALL(*result, GetId()) | 350 EXPECT_CALL(*result, GetId()) |
344 .WillRepeatedly(Return(download_id)); | 351 .WillRepeatedly(Return(download_id)); |
| 352 EXPECT_CALL(*result, GetGuid()) |
| 353 .WillRepeatedly( |
| 354 ReturnRefOfCopy(base::ToUpperASCII(base::GenerateGUID()))); |
345 items_[download_id] = result; | 355 items_[download_id] = result; |
346 | 356 |
347 // Active items are created and then immediately are called to start | 357 // Active items are created and then immediately are called to start |
348 // the download. | 358 // the download. |
349 EXPECT_CALL(*result, MockStart(_, _)); | 359 EXPECT_CALL(*result, MockStart(_, _)); |
350 | 360 |
351 return result; | 361 return result; |
352 } | 362 } |
353 | 363 |
354 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( | 364 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 EXPECT_CALL(GetMockDownloadItem(2), Remove()) | 715 EXPECT_CALL(GetMockDownloadItem(2), Remove()) |
706 .WillOnce(Return()); | 716 .WillOnce(Return()); |
707 EXPECT_CALL(GetMockDownloadItem(3), Remove()) | 717 EXPECT_CALL(GetMockDownloadItem(3), Remove()) |
708 .Times(0); | 718 .Times(0); |
709 | 719 |
710 download_manager_->RemoveAllDownloads(); | 720 download_manager_->RemoveAllDownloads(); |
711 // Because we're mocking the download item, the Remove call doesn't | 721 // Because we're mocking the download item, the Remove call doesn't |
712 // result in them being removed from the DownloadManager list. | 722 // result in them being removed from the DownloadManager list. |
713 } | 723 } |
714 | 724 |
| 725 TEST_F(DownloadManagerTest, GetDownloadByGuid) { |
| 726 for (uint32_t i = 0; i < 4; ++i) |
| 727 AddItemToManager(); |
| 728 |
| 729 MockDownloadItemImpl& item = GetMockDownloadItem(0); |
| 730 DownloadItem* result = download_manager_->GetDownloadByGuid(item.GetGuid()); |
| 731 ASSERT_TRUE(result); |
| 732 ASSERT_EQ(static_cast<DownloadItem*>(&item), result); |
| 733 |
| 734 ASSERT_FALSE(download_manager_->GetDownloadByGuid("")); |
| 735 |
| 736 const char kGuid[] = "8DF158E8-C980-4618-BB03-EBA3242EB48B"; |
| 737 DownloadItem* persisted_item = download_manager_->CreateDownloadItem( |
| 738 kGuid, |
| 739 10, |
| 740 base::FilePath(), |
| 741 base::FilePath(), |
| 742 std::vector<GURL>(), |
| 743 GURL("http://example.com/a"), |
| 744 "application/octet-stream", |
| 745 "application/octet-stream", |
| 746 base::Time::Now(), |
| 747 base::Time::Now(), |
| 748 std::string(), |
| 749 std::string(), |
| 750 10, |
| 751 10, |
| 752 DownloadItem::INTERRUPTED, |
| 753 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 754 DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
| 755 false); |
| 756 ASSERT_TRUE(persisted_item); |
| 757 |
| 758 ASSERT_EQ(persisted_item, download_manager_->GetDownloadByGuid(kGuid)); |
| 759 } |
| 760 |
715 namespace { | 761 namespace { |
716 | 762 |
717 base::Callback<bool(const GURL&)> GetSingleURLFilter(const GURL& url) { | 763 base::Callback<bool(const GURL&)> GetSingleURLFilter(const GURL& url) { |
718 return base::Bind(&GURL::operator==, base::Owned(new GURL(url))); | 764 return base::Bind(&GURL::operator==, base::Owned(new GURL(url))); |
719 } | 765 } |
720 | 766 |
721 } // namespace | 767 } // namespace |
722 | 768 |
723 // Confirm that only downloads with the specified URL are removed. | 769 // Confirm that only downloads with the specified URL are removed. |
724 TEST_F(DownloadManagerTest, RemoveDownloadsByURL) { | 770 TEST_F(DownloadManagerTest, RemoveDownloadsByURL) { |
725 base::Time now(base::Time::Now()); | 771 base::Time now(base::Time::Now()); |
726 for (uint32_t i = 0; i < 2; ++i) { | 772 for (uint32_t i = 0; i < 2; ++i) { |
727 MockDownloadItemImpl& item(AddItemToManager()); | 773 MockDownloadItemImpl& item(AddItemToManager()); |
728 EXPECT_CALL(item, GetStartTime()).WillRepeatedly(Return(now)); | 774 EXPECT_CALL(item, GetStartTime()).WillRepeatedly(Return(now)); |
729 EXPECT_CALL(item, GetState()) | 775 EXPECT_CALL(item, GetState()) |
730 .WillRepeatedly(Return(DownloadItem::COMPLETE)); | 776 .WillRepeatedly(Return(DownloadItem::COMPLETE)); |
731 } | 777 } |
732 | 778 |
733 EXPECT_CALL(GetMockDownloadItem(0), Remove()); | 779 EXPECT_CALL(GetMockDownloadItem(0), Remove()); |
734 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); | 780 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); |
735 | 781 |
736 base::Callback<bool(const GURL&)> url_filter = | 782 base::Callback<bool(const GURL&)> url_filter = |
737 GetSingleURLFilter(download_urls_[0]); | 783 GetSingleURLFilter(download_urls_[0]); |
738 int remove_count = download_manager_->RemoveDownloadsByURLAndTime( | 784 int remove_count = download_manager_->RemoveDownloadsByURLAndTime( |
739 url_filter, base::Time(), base::Time::Max()); | 785 url_filter, base::Time(), base::Time::Max()); |
740 EXPECT_EQ(remove_count, 1); | 786 EXPECT_EQ(remove_count, 1); |
741 } | 787 } |
742 | 788 |
743 } // namespace content | 789 } // namespace content |
OLD | NEW |