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 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // has a |default_download_directory| that matches |download_directory|. | 68 // has a |default_download_directory| that matches |download_directory|. |
69 MATCHER_P2(DownloadCreateInfoWithDefaultPath, info, download_directory, "") { | 69 MATCHER_P2(DownloadCreateInfoWithDefaultPath, info, download_directory, "") { |
70 return arg == info && | 70 return arg == info && |
71 arg->default_download_directory == download_directory; | 71 arg->default_download_directory == download_directory; |
72 } | 72 } |
73 | 73 |
74 class MockDownloadItemImpl : public DownloadItemImpl { | 74 class MockDownloadItemImpl : public DownloadItemImpl { |
75 public: | 75 public: |
76 // Use history constructor for minimal base object. | 76 // Use history constructor for minimal base object. |
77 explicit MockDownloadItemImpl(DownloadItemImplDelegate* delegate) | 77 explicit MockDownloadItemImpl(DownloadItemImplDelegate* delegate) |
78 : DownloadItemImpl( | 78 : DownloadItemImpl(delegate, |
79 delegate, | 79 content::DownloadItem::kInvalidId, |
80 content::DownloadItem::kInvalidId, | 80 base::FilePath(), |
81 base::FilePath(), | 81 base::FilePath(), |
82 base::FilePath(), | 82 std::vector<GURL>(), |
83 std::vector<GURL>(), | 83 GURL(), |
84 GURL(), | 84 "application/octet-stream", |
85 "application/octet-stream", | 85 "application/octet-stream", |
86 "application/octet-stream", | 86 base::Time(), |
87 base::Time(), | 87 base::Time(), |
88 base::Time(), | 88 std::string(), |
89 std::string(), | 89 std::string(), |
90 std::string(), | 90 0, |
91 0, | 91 0, |
92 0, | 92 std::string(), |
93 DownloadItem::COMPLETE, | 93 DownloadItem::COMPLETE, |
94 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 94 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
95 DOWNLOAD_INTERRUPT_REASON_NONE, | 95 DOWNLOAD_INTERRUPT_REASON_NONE, |
96 false, | 96 false, |
97 net::BoundNetLog()) {} | 97 net::BoundNetLog()) {} |
98 virtual ~MockDownloadItemImpl() {} | 98 virtual ~MockDownloadItemImpl() {} |
99 | 99 |
100 MOCK_METHOD4(OnDownloadTargetDetermined, | 100 MOCK_METHOD4(OnDownloadTargetDetermined, |
101 void(const base::FilePath&, TargetDisposition, | 101 void(const base::FilePath&, TargetDisposition, |
102 DownloadDangerType, const base::FilePath&)); | 102 DownloadDangerType, const base::FilePath&)); |
103 MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*)); | 103 MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*)); |
104 MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*)); | 104 MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*)); |
105 MOCK_METHOD0(UpdateObservers, void()); | 105 MOCK_METHOD0(UpdateObservers, void()); |
106 MOCK_METHOD0(CanShowInFolder, bool()); | 106 MOCK_METHOD0(CanShowInFolder, bool()); |
107 MOCK_METHOD0(CanOpenDownload, bool()); | 107 MOCK_METHOD0(CanOpenDownload, bool()); |
108 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); | 108 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); |
109 MOCK_METHOD0(OpenDownload, void()); | 109 MOCK_METHOD0(OpenDownload, void()); |
110 MOCK_METHOD0(ShowDownloadInShell, void()); | 110 MOCK_METHOD0(ShowDownloadInShell, void()); |
111 MOCK_METHOD0(ValidateDangerousDownload, void()); | 111 MOCK_METHOD0(ValidateDangerousDownload, void()); |
112 MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&)); | 112 MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&)); |
113 MOCK_METHOD3(UpdateProgress, void(int64_t, int64_t, const std::string&)); | 113 MOCK_METHOD3(UpdateProgress, void(int64_t, int64_t, const std::string&)); |
114 MOCK_METHOD1(Cancel, void(bool)); | 114 MOCK_METHOD1(Cancel, void(bool)); |
115 MOCK_METHOD0(MarkAsComplete, void()); | 115 MOCK_METHOD0(MarkAsComplete, void()); |
116 MOCK_METHOD1(OnAllDataSaved, void(const std::string&)); | 116 void OnAllDataSaved(int64_t, scoped_ptr<crypto::SecureHash>) override { |
| 117 NOTREACHED(); |
| 118 } |
117 MOCK_METHOD0(OnDownloadedFileRemoved, void()); | 119 MOCK_METHOD0(OnDownloadedFileRemoved, void()); |
118 void Start(scoped_ptr<DownloadFile> download_file, | 120 void Start(scoped_ptr<DownloadFile> download_file, |
119 scoped_ptr<DownloadRequestHandleInterface> req_handle, | 121 scoped_ptr<DownloadRequestHandleInterface> req_handle, |
120 const DownloadCreateInfo& create_info) override { | 122 const DownloadCreateInfo& create_info) override { |
121 MockStart(download_file.get(), req_handle.get()); | 123 MockStart(download_file.get(), req_handle.get()); |
122 } | 124 } |
123 | 125 |
124 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*)); | 126 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*)); |
125 | 127 |
126 MOCK_METHOD0(Remove, void()); | 128 MOCK_METHOD0(Remove, void()); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 MOCK_METHOD0(Shutdown, void()); | 194 MOCK_METHOD0(Shutdown, void()); |
193 MOCK_METHOD1(GetNextId, void(const DownloadIdCallback&)); | 195 MOCK_METHOD1(GetNextId, void(const DownloadIdCallback&)); |
194 MOCK_METHOD2(DetermineDownloadTarget, | 196 MOCK_METHOD2(DetermineDownloadTarget, |
195 bool(DownloadItem* item, | 197 bool(DownloadItem* item, |
196 const DownloadTargetCallback&)); | 198 const DownloadTargetCallback&)); |
197 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const base::FilePath&)); | 199 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const base::FilePath&)); |
198 MOCK_METHOD2(ShouldCompleteDownload, | 200 MOCK_METHOD2(ShouldCompleteDownload, |
199 bool(DownloadItem*, const base::Closure&)); | 201 bool(DownloadItem*, const base::Closure&)); |
200 MOCK_METHOD2(ShouldOpenDownload, | 202 MOCK_METHOD2(ShouldOpenDownload, |
201 bool(DownloadItem*, const DownloadOpenDelayedCallback&)); | 203 bool(DownloadItem*, const DownloadOpenDelayedCallback&)); |
202 MOCK_METHOD0(GenerateFileHash, bool()); | |
203 MOCK_METHOD4(GetSaveDir, void(BrowserContext*, | 204 MOCK_METHOD4(GetSaveDir, void(BrowserContext*, |
204 base::FilePath*, base::FilePath*, bool*)); | 205 base::FilePath*, base::FilePath*, bool*)); |
205 MOCK_METHOD5(ChooseSavePath, void( | 206 MOCK_METHOD5(ChooseSavePath, void( |
206 WebContents*, const base::FilePath&, const base::FilePath::StringType&, | 207 WebContents*, const base::FilePath&, const base::FilePath::StringType&, |
207 bool, const SavePackagePathPickedCallback&)); | 208 bool, const SavePackagePathPickedCallback&)); |
208 MOCK_CONST_METHOD0(ApplicationClientIdForFileScanning, std::string()); | 209 MOCK_CONST_METHOD0(ApplicationClientIdForFileScanning, std::string()); |
209 }; | 210 }; |
210 | 211 |
211 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} | 212 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} |
212 | 213 |
(...skipping 30 matching lines...) Expand all Loading... |
243 const std::vector<GURL>& url_chain, | 244 const std::vector<GURL>& url_chain, |
244 const GURL& referrer_url, | 245 const GURL& referrer_url, |
245 const std::string& mime_type, | 246 const std::string& mime_type, |
246 const std::string& original_mime_type, | 247 const std::string& original_mime_type, |
247 const base::Time& start_time, | 248 const base::Time& start_time, |
248 const base::Time& end_time, | 249 const base::Time& end_time, |
249 const std::string& etag, | 250 const std::string& etag, |
250 const std::string& last_modofied, | 251 const std::string& last_modofied, |
251 int64_t received_bytes, | 252 int64_t received_bytes, |
252 int64_t total_bytes, | 253 int64_t total_bytes, |
| 254 const std::string& hash, |
253 DownloadItem::DownloadState state, | 255 DownloadItem::DownloadState state, |
254 DownloadDangerType danger_type, | 256 DownloadDangerType danger_type, |
255 DownloadInterruptReason interrupt_reason, | 257 DownloadInterruptReason interrupt_reason, |
256 bool opened, | 258 bool opened, |
257 const net::BoundNetLog& bound_net_log) override; | 259 const net::BoundNetLog& bound_net_log) override; |
258 DownloadItemImpl* CreateActiveItem( | 260 DownloadItemImpl* CreateActiveItem( |
259 DownloadItemImplDelegate* delegate, | 261 DownloadItemImplDelegate* delegate, |
260 uint32_t download_id, | 262 uint32_t download_id, |
261 const DownloadCreateInfo& info, | 263 const DownloadCreateInfo& info, |
262 const net::BoundNetLog& bound_net_log) override; | 264 const net::BoundNetLog& bound_net_log) override; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 const std::vector<GURL>& url_chain, | 312 const std::vector<GURL>& url_chain, |
311 const GURL& referrer_url, | 313 const GURL& referrer_url, |
312 const std::string& mime_type, | 314 const std::string& mime_type, |
313 const std::string& original_mime_type, | 315 const std::string& original_mime_type, |
314 const base::Time& start_time, | 316 const base::Time& start_time, |
315 const base::Time& end_time, | 317 const base::Time& end_time, |
316 const std::string& etag, | 318 const std::string& etag, |
317 const std::string& last_modified, | 319 const std::string& last_modified, |
318 int64_t received_bytes, | 320 int64_t received_bytes, |
319 int64_t total_bytes, | 321 int64_t total_bytes, |
| 322 const std::string& hash, |
320 DownloadItem::DownloadState state, | 323 DownloadItem::DownloadState state, |
321 DownloadDangerType danger_type, | 324 DownloadDangerType danger_type, |
322 DownloadInterruptReason interrupt_reason, | 325 DownloadInterruptReason interrupt_reason, |
323 bool opened, | 326 bool opened, |
324 const net::BoundNetLog& bound_net_log) { | 327 const net::BoundNetLog& bound_net_log) { |
325 DCHECK(items_.find(download_id) == items_.end()); | 328 DCHECK(items_.find(download_id) == items_.end()); |
326 MockDownloadItemImpl* result = | 329 MockDownloadItemImpl* result = |
327 new StrictMock<MockDownloadItemImpl>(&item_delegate_); | 330 new StrictMock<MockDownloadItemImpl>(&item_delegate_); |
328 EXPECT_CALL(*result, GetId()) | 331 EXPECT_CALL(*result, GetId()) |
329 .WillRepeatedly(Return(download_id)); | 332 .WillRepeatedly(Return(download_id)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 } | 374 } |
372 | 375 |
373 class MockDownloadFileFactory | 376 class MockDownloadFileFactory |
374 : public DownloadFileFactory, | 377 : public DownloadFileFactory, |
375 public base::SupportsWeakPtr<MockDownloadFileFactory> { | 378 public base::SupportsWeakPtr<MockDownloadFileFactory> { |
376 public: | 379 public: |
377 MockDownloadFileFactory() {} | 380 MockDownloadFileFactory() {} |
378 virtual ~MockDownloadFileFactory() {} | 381 virtual ~MockDownloadFileFactory() {} |
379 | 382 |
380 // Overridden method from DownloadFileFactory | 383 // Overridden method from DownloadFileFactory |
381 MOCK_METHOD3(MockCreateFile, | 384 MOCK_METHOD2(MockCreateFile, |
382 MockDownloadFile*(const DownloadSaveInfo&, | 385 MockDownloadFile*(const DownloadSaveInfo&, ByteStreamReader*)); |
383 bool, | |
384 ByteStreamReader*)); | |
385 | 386 |
386 virtual DownloadFile* CreateFile( | 387 virtual DownloadFile* CreateFile( |
387 const DownloadSaveInfo& save_info, | 388 scoped_ptr<DownloadSaveInfo> save_info, |
388 const base::FilePath& default_download_directory, | 389 const base::FilePath& default_download_directory, |
389 const GURL& url, | |
390 const GURL& referrer_url, | |
391 bool calculate_hash, | |
392 base::File file_stream, | |
393 scoped_ptr<ByteStreamReader> byte_stream, | 390 scoped_ptr<ByteStreamReader> byte_stream, |
394 const net::BoundNetLog& bound_net_log, | 391 const net::BoundNetLog& bound_net_log, |
395 base::WeakPtr<DownloadDestinationObserver> observer) { | 392 base::WeakPtr<DownloadDestinationObserver> observer) override { |
396 return MockCreateFile(save_info, calculate_hash, byte_stream.get()); | 393 return MockCreateFile(*save_info, byte_stream.get()); |
397 } | 394 } |
398 }; | 395 }; |
399 | 396 |
400 class MockBrowserContext : public BrowserContext { | 397 class MockBrowserContext : public BrowserContext { |
401 public: | 398 public: |
402 MockBrowserContext() { | 399 MockBrowserContext() { |
403 content::BrowserContext::Initialize(this, base::FilePath()); | 400 content::BrowserContext::Initialize(this, base::FilePath()); |
404 } | 401 } |
405 ~MockBrowserContext() {} | 402 ~MockBrowserContext() {} |
406 | 403 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 | 614 |
618 EXPECT_FALSE(download_manager_->GetDownload(local_id)); | 615 EXPECT_FALSE(download_manager_->GetDownload(local_id)); |
619 | 616 |
620 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | 617 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) |
621 .WillOnce(Return()); | 618 .WillOnce(Return()); |
622 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_)) | 619 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_)) |
623 .WillOnce(RunCallback<0>(local_id)); | 620 .WillOnce(RunCallback<0>(local_id)); |
624 | 621 |
625 // Doing nothing will set the default download directory to null. | 622 // Doing nothing will set the default download directory to null. |
626 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _)); | 623 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _)); |
627 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) | |
628 .WillOnce(Return(true)); | |
629 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 624 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
630 ApplicationClientIdForFileScanning()) | 625 ApplicationClientIdForFileScanning()) |
631 .WillRepeatedly(Return("client-id")); | 626 .WillRepeatedly(Return("client-id")); |
632 MockDownloadFile* mock_file = new MockDownloadFile; | 627 MockDownloadFile* mock_file = new MockDownloadFile; |
633 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); | |
634 EXPECT_CALL(*mock_download_file_factory_.get(), | 628 EXPECT_CALL(*mock_download_file_factory_.get(), |
635 MockCreateFile(Ref(*info->save_info.get()), true, stream.get())) | 629 MockCreateFile(Ref(*info->save_info.get()), stream.get())) |
636 .WillOnce(Return(mock_file)); | 630 .WillOnce(Return(mock_file)); |
637 | 631 |
638 download_manager_->StartDownload(std::move(info), std::move(stream), | 632 download_manager_->StartDownload(std::move(info), std::move(stream), |
639 DownloadUrlParameters::OnStartedCallback()); | 633 DownloadUrlParameters::OnStartedCallback()); |
640 EXPECT_TRUE(download_manager_->GetDownload(local_id)); | 634 EXPECT_TRUE(download_manager_->GetDownload(local_id)); |
641 } | 635 } |
642 | 636 |
643 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate | 637 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate |
644 // blocks starting. | 638 // blocks starting. |
645 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { | 639 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); | 728 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); |
735 | 729 |
736 base::Callback<bool(const GURL&)> url_filter = | 730 base::Callback<bool(const GURL&)> url_filter = |
737 GetSingleURLFilter(download_urls_[0]); | 731 GetSingleURLFilter(download_urls_[0]); |
738 int remove_count = download_manager_->RemoveDownloadsByURLAndTime( | 732 int remove_count = download_manager_->RemoveDownloadsByURLAndTime( |
739 url_filter, base::Time(), base::Time::Max()); | 733 url_filter, base::Time(), base::Time::Max()); |
740 EXPECT_EQ(remove_count, 1); | 734 EXPECT_EQ(remove_count, 1); |
741 } | 735 } |
742 | 736 |
743 } // namespace content | 737 } // namespace content |
OLD | NEW |