Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: chrome/browser/download/download_test_file_activity_observer.cc

Issue 12850002: Move download filename determintion into a separate class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.h" 8 #include "base/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_file_picker.h"
10 #include "chrome/browser/download/download_service.h" 11 #include "chrome/browser/download/download_service.h"
11 #include "chrome/browser/download/download_service_factory.h" 12 #include "chrome/browser/download/download_service_factory.h"
12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
13 14
14 namespace content { 15 namespace content {
15 class DownloadItem; 16 class DownloadItem;
16 } 17 }
17 18
18 // Test ChromeDownloadManagerDelegate that controls whether how file chooser 19 class DownloadTestFileActivityObserver::TestDownloadFilePickerFactory
19 // dialogs are handled, and how files are opend. 20 : public DownloadFilePickerFactory {
20 // By default, file chooser dialogs are disabled.
21 class DownloadTestFileActivityObserver::MockDownloadManagerDelegate
22 : public ChromeDownloadManagerDelegate {
23 public: 21 public:
24 explicit MockDownloadManagerDelegate(Profile* profile) 22 TestDownloadFilePickerFactory()
25 : ChromeDownloadManagerDelegate(profile), 23 : file_chooser_enabled_(false),
26 file_chooser_enabled_(false), 24 file_chooser_displayed_(false),
27 file_chooser_displayed_(false) {} 25 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
26 }
27
28 // DownloadFilePicker
29 virtual void Create(
30 content::DownloadItem* download,
31 const base::FilePath& suggested_path,
32 const DownloadFilePicker::FileSelectedCallback& callback) OVERRIDE {
33 base::FilePath target_path =
34 file_chooser_enabled_ ? suggested_path : base::FilePath();
35 file_chooser_displayed_ = true;
36 MessageLoop::current()->PostTask(
37 FROM_HERE, base::Bind(callback, target_path, target_path));
38 }
28 39
29 void EnableFileChooser(bool enable) { 40 void EnableFileChooser(bool enable) {
30 file_chooser_enabled_ = enable; 41 file_chooser_enabled_ = enable;
31 } 42 }
32 43
33 bool TestAndResetDidShowFileChooser() { 44 bool TestAndResetDidShowFileChooser() {
34 bool did_show = file_chooser_displayed_; 45 bool did_show = file_chooser_displayed_;
35 file_chooser_displayed_ = false; 46 file_chooser_displayed_ = false;
36 return did_show; 47 return did_show;
37 } 48 }
38 49
39 protected: 50 base::WeakPtr<TestDownloadFilePickerFactory> GetWeakPtr() {
40 51 return weak_ptr_factory_.GetWeakPtr();
41 virtual void ChooseDownloadPath(content::DownloadItem* item,
42 const base::FilePath& suggested_path,
43 const FileSelectedCallback&
44 callback) OVERRIDE {
45 file_chooser_displayed_ = true;
46 MessageLoop::current()->PostTask(
47 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path
48 : base::FilePath())));
49 } 52 }
50 53
51 virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {}
52
53 private: 54 private:
54 virtual ~MockDownloadManagerDelegate() {}
55
56 bool file_chooser_enabled_; 55 bool file_chooser_enabled_;
57 bool file_chooser_displayed_; 56 bool file_chooser_displayed_;
57 base::WeakPtrFactory<TestDownloadFilePickerFactory> weak_ptr_factory_;
58 }; 58 };
59 59
60 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( 60 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
61 Profile* profile) { 61 Profile* profile) {
62 test_delegate_ = new MockDownloadManagerDelegate(profile); 62 scoped_ptr<TestDownloadFilePickerFactory> file_picker_factory(
63 DownloadServiceFactory::GetForProfile(profile)-> 63 new TestDownloadFilePickerFactory);
64 SetDownloadManagerDelegateForTesting(test_delegate_.get()); 64 test_file_picker_factory_ = file_picker_factory->GetWeakPtr();
65
66 DownloadService* service = DownloadServiceFactory::GetForProfile(profile);
67 service->GetDownloadManagerDelegate()->SetFilePickerFactoryForTesting(
68 file_picker_factory.PassAs<DownloadFilePickerFactory>());
65 } 69 }
66 70
67 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { 71 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
68 } 72 }
69 73
70 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { 74 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) {
71 test_delegate_->EnableFileChooser(enable); 75 if (test_file_picker_factory_.get())
76 test_file_picker_factory_->EnableFileChooser(enable);
72 } 77 }
73 78
74 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { 79 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
75 return test_delegate_->TestAndResetDidShowFileChooser(); 80 return test_file_picker_factory_.get() &&
81 test_file_picker_factory_->TestAndResetDidShowFileChooser();
76 } 82 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698