- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / download / download_test_file_activity_observer.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/download/download_test_file_activity_observer.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/browser/download/chrome_download_manager_delegate.h"
10 #include "chrome/browser/download/download_service.h"
11 #include "chrome/browser/download/download_service_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13
14 namespace content {
15 class DownloadItem;
16 }
17
18 // Test ChromeDownloadManagerDelegate that controls whether how file chooser
19 // dialogs are handled, and how files are opend.
20 // By default, file chooser dialogs are disabled.
21 class DownloadTestFileActivityObserver::MockDownloadManagerDelegate
22     : public ChromeDownloadManagerDelegate {
23  public:
24   explicit MockDownloadManagerDelegate(Profile* profile)
25       : ChromeDownloadManagerDelegate(profile),
26         file_chooser_enabled_(false),
27         file_chooser_displayed_(false) {
28     if (!profile->IsOffTheRecord())
29       SetNextId(content::DownloadItem::kInvalidId + 1);
30   }
31
32   void EnableFileChooser(bool enable) {
33     file_chooser_enabled_ = enable;
34   }
35
36   bool TestAndResetDidShowFileChooser() {
37     bool did_show = file_chooser_displayed_;
38     file_chooser_displayed_ = false;
39     return did_show;
40   }
41
42  protected:
43
44   virtual void PromptUserForDownloadPath(content::DownloadItem* item,
45                                          const base::FilePath& suggested_path,
46                                          const FileSelectedCallback&
47                                              callback) OVERRIDE {
48     file_chooser_displayed_ = true;
49     base::MessageLoop::current()->PostTask(
50         FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path
51                                          : base::FilePath())));
52   }
53
54   virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {}
55
56  private:
57   virtual ~MockDownloadManagerDelegate() {}
58
59   bool file_chooser_enabled_;
60   bool file_chooser_displayed_;
61 };
62
63 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
64     Profile* profile) {
65   test_delegate_ = new MockDownloadManagerDelegate(profile);
66   DownloadServiceFactory::GetForBrowserContext(profile)->
67       SetDownloadManagerDelegateForTesting(test_delegate_.get());
68 }
69
70 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
71 }
72
73 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) {
74   test_delegate_->EnableFileChooser(enable);
75 }
76
77 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
78   return test_delegate_->TestAndResetDidShowFileChooser();
79 }