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 "chrome/browser/download/download_test_file_activity_observer.h" | 5 #include "chrome/browser/download/download_test_file_activity_observer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 9 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
10 #include "chrome/browser/download/download_service.h" | 10 #include "chrome/browser/download/download_service.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 explicit MockDownloadManagerDelegate(Profile* profile) | 24 explicit MockDownloadManagerDelegate(Profile* profile) |
25 : ChromeDownloadManagerDelegate(profile), | 25 : ChromeDownloadManagerDelegate(profile), |
26 file_chooser_enabled_(false), | 26 file_chooser_enabled_(false), |
27 file_chooser_displayed_(false), | 27 file_chooser_displayed_(false), |
28 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
29 if (!profile->IsOffTheRecord()) | 29 if (!profile->IsOffTheRecord()) |
30 GetDownloadIdReceiverCallback().Run( | 30 GetDownloadIdReceiverCallback().Run( |
31 content::DownloadItem::kInvalidId + 1); | 31 content::DownloadItem::kInvalidId + 1); |
32 } | 32 } |
33 | 33 |
34 virtual ~MockDownloadManagerDelegate() {} | 34 ~MockDownloadManagerDelegate() override {} |
35 | 35 |
36 void EnableFileChooser(bool enable) { | 36 void EnableFileChooser(bool enable) { |
37 file_chooser_enabled_ = enable; | 37 file_chooser_enabled_ = enable; |
38 } | 38 } |
39 | 39 |
40 bool TestAndResetDidShowFileChooser() { | 40 bool TestAndResetDidShowFileChooser() { |
41 bool did_show = file_chooser_displayed_; | 41 bool did_show = file_chooser_displayed_; |
42 file_chooser_displayed_ = false; | 42 file_chooser_displayed_ = false; |
43 return did_show; | 43 return did_show; |
44 } | 44 } |
45 | 45 |
46 base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() { | 46 base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() { |
47 return weak_ptr_factory_.GetWeakPtr(); | 47 return weak_ptr_factory_.GetWeakPtr(); |
48 } | 48 } |
49 | 49 |
50 protected: | 50 protected: |
51 | 51 void PromptUserForDownloadPath( |
52 virtual void PromptUserForDownloadPath(content::DownloadItem* item, | 52 content::DownloadItem* item, |
53 const base::FilePath& suggested_path, | 53 const base::FilePath& suggested_path, |
54 const FileSelectedCallback& | 54 const FileSelectedCallback& callback) override { |
55 callback) override { | |
56 file_chooser_displayed_ = true; | 55 file_chooser_displayed_ = true; |
57 base::MessageLoop::current()->PostTask( | 56 base::MessageLoop::current()->PostTask( |
58 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path | 57 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path |
59 : base::FilePath()))); | 58 : base::FilePath()))); |
60 } | 59 } |
61 | 60 |
62 virtual void OpenDownload(content::DownloadItem* item) override {} | 61 void OpenDownload(content::DownloadItem* item) override {} |
63 | 62 |
64 private: | 63 private: |
65 bool file_chooser_enabled_; | 64 bool file_chooser_enabled_; |
66 bool file_chooser_displayed_; | 65 bool file_chooser_displayed_; |
67 base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_; | 66 base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_; |
68 }; | 67 }; |
69 | 68 |
70 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( | 69 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( |
71 Profile* profile) { | 70 Profile* profile) { |
72 scoped_ptr<MockDownloadManagerDelegate> mock_delegate( | 71 scoped_ptr<MockDownloadManagerDelegate> mock_delegate( |
73 new MockDownloadManagerDelegate(profile)); | 72 new MockDownloadManagerDelegate(profile)); |
74 test_delegate_ = mock_delegate->GetWeakPtr(); | 73 test_delegate_ = mock_delegate->GetWeakPtr(); |
75 DownloadServiceFactory::GetForBrowserContext(profile) | 74 DownloadServiceFactory::GetForBrowserContext(profile) |
76 ->SetDownloadManagerDelegateForTesting(mock_delegate.Pass()); | 75 ->SetDownloadManagerDelegateForTesting(mock_delegate.Pass()); |
77 } | 76 } |
78 | 77 |
79 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { | 78 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { |
80 } | 79 } |
81 | 80 |
82 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { | 81 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { |
83 if (test_delegate_.get()) | 82 if (test_delegate_.get()) |
84 test_delegate_->EnableFileChooser(enable); | 83 test_delegate_->EnableFileChooser(enable); |
85 } | 84 } |
86 | 85 |
87 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { | 86 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { |
88 return test_delegate_.get() && | 87 return test_delegate_.get() && |
89 test_delegate_->TestAndResetDidShowFileChooser(); | 88 test_delegate_->TestAndResetDidShowFileChooser(); |
90 } | 89 } |
OLD | NEW |