OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_ui_controller.h" | 5 #include "chrome/browser/download/download_ui_controller.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" |
8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
9 #include "chrome/browser/download/download_item_model.h" | 10 #include "chrome/browser/download/download_item_model.h" |
| 11 #include "chrome/browser/download/notification/download_notification_manager.h" |
| 12 #include "chrome/browser/ui/browser_finder.h" |
10 #include "chrome/browser/ui/browser_tabstrip.h" | 13 #include "chrome/browser/ui/browser_tabstrip.h" |
| 14 #include "chrome/common/chrome_switches.h" |
11 #include "content/public/browser/download_item.h" | 15 #include "content/public/browser/download_item.h" |
12 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
13 #include "content/public/browser/web_contents_delegate.h" | 17 #include "content/public/browser/web_contents_delegate.h" |
14 | 18 |
15 #if defined(OS_ANDROID) | 19 #if defined(OS_ANDROID) |
16 #include "content/public/browser/android/download_controller_android.h" | 20 #include "content/public/browser/android/download_controller_android.h" |
17 #else | 21 #else |
18 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/ui/browser_finder.h" | 23 #include "chrome/browser/ui/browser_finder.h" |
20 #include "chrome/browser/ui/host_desktop.h" | 24 #include "chrome/browser/ui/host_desktop.h" |
21 #endif | 25 #endif |
22 | 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 // DefaultUIControllerDelegate{Android,} is used when a DownloadUIController is | 29 // DownloadShelfUIControllerDelegate{Android,} is used when a |
| 30 // DownloadUIController is |
26 // constructed without specifying an explicit Delegate. | 31 // constructed without specifying an explicit Delegate. |
27 #if defined(OS_ANDROID) | 32 #if defined(OS_ANDROID) |
28 | 33 |
29 class DefaultUIControllerDelegateAndroid | 34 class AndroidUIControllerDelegate : public DownloadUIController::Delegate { |
30 : public DownloadUIController::Delegate { | |
31 public: | 35 public: |
32 DefaultUIControllerDelegateAndroid() {} | 36 AndroidUIControllerDelegate() {} |
33 ~DefaultUIControllerDelegateAndroid() override {} | 37 ~AndroidUIControllerDelegate() override {} |
34 | 38 |
35 private: | 39 private: |
36 // DownloadUIController::Delegate | 40 // DownloadUIController::Delegate |
37 void OnNewDownloadReady(content::DownloadItem* item) override; | 41 void OnNewDownloadReady(content::DownloadItem* item) override; |
38 }; | 42 }; |
39 | 43 |
40 void DefaultUIControllerDelegateAndroid::OnNewDownloadReady( | 44 void AndroidUIControllerDelegate::OnNewDownloadReady( |
41 content::DownloadItem* item) { | 45 content::DownloadItem* item) { |
42 // The Android DownloadController is only interested in IN_PROGRESS downloads. | 46 // The Android DownloadController is only interested in IN_PROGRESS downloads. |
43 // Ones which are INTERRUPTED etc. can't be handed over to the Android | 47 // Ones which are INTERRUPTED etc. can't be handed over to the Android |
44 // DownloadManager. | 48 // DownloadManager. |
45 if (item->GetState() != content::DownloadItem::IN_PROGRESS) | 49 if (item->GetState() != content::DownloadItem::IN_PROGRESS) |
46 return; | 50 return; |
47 | 51 |
48 // GET downloads without authentication are delegated to the Android | 52 // GET downloads without authentication are delegated to the Android |
49 // DownloadManager. Chrome is responsible for the rest. See | 53 // DownloadManager. Chrome is responsible for the rest. See |
50 // InterceptDownloadResourceThrottle::ProcessDownloadRequest(). | 54 // InterceptDownloadResourceThrottle::ProcessDownloadRequest(). |
51 content::DownloadControllerAndroid::Get()->OnDownloadStarted(item); | 55 content::DownloadControllerAndroid::Get()->OnDownloadStarted(item); |
52 } | 56 } |
53 | 57 |
54 #else // OS_ANDROID | 58 #else // OS_ANDROID |
55 | 59 |
56 class DefaultUIControllerDelegate : public DownloadUIController::Delegate { | 60 class DownloadShelfUIControllerDelegate |
| 61 : public DownloadUIController::Delegate { |
57 public: | 62 public: |
58 // |profile| is required to outlive DefaultUIControllerDelegate. | 63 // |profile| is required to outlive DownloadShelfUIControllerDelegate. |
59 explicit DefaultUIControllerDelegate(Profile* profile) | 64 explicit DownloadShelfUIControllerDelegate(Profile* profile) |
60 : profile_(profile) {} | 65 : profile_(profile) {} |
61 ~DefaultUIControllerDelegate() override {} | 66 ~DownloadShelfUIControllerDelegate() override {} |
62 | 67 |
63 private: | 68 private: |
64 // DownloadUIController::Delegate | 69 // DownloadUIController::Delegate |
65 void OnNewDownloadReady(content::DownloadItem* item) override; | 70 void OnNewDownloadReady(content::DownloadItem* item) override; |
66 | 71 |
67 Profile* profile_; | 72 Profile* profile_; |
68 }; | 73 }; |
69 | 74 |
70 void DefaultUIControllerDelegate::OnNewDownloadReady( | 75 void DownloadShelfUIControllerDelegate::OnNewDownloadReady( |
71 content::DownloadItem* item) { | 76 content::DownloadItem* item) { |
72 content::WebContents* web_contents = item->GetWebContents(); | 77 content::WebContents* web_contents = item->GetWebContents(); |
73 Browser* browser = | 78 Browser* browser = |
74 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL; | 79 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL; |
75 | 80 |
76 // As a last resort, use the last active browser for this profile. Not ideal, | 81 // As a last resort, use the last active browser for this profile. Not ideal, |
77 // but better than not showing the download at all. | 82 // but better than not showing the download at all. |
78 if (browser == NULL) { | 83 if (browser == NULL) { |
79 browser = chrome::FindLastActiveWithProfile(profile_, | 84 browser = chrome::FindLastActiveWithProfile(profile_, |
80 chrome::GetActiveDesktop()); | 85 chrome::GetActiveDesktop()); |
81 } | 86 } |
82 | 87 |
83 if (browser) | 88 if (browser) |
84 browser->ShowDownload(item); | 89 browser->ShowDownload(item); |
85 } | 90 } |
86 | 91 |
87 #endif // !OS_ANDROID | 92 #endif // !OS_ANDROID |
88 | 93 |
89 } // namespace | 94 } // namespace |
90 | 95 |
91 DownloadUIController::Delegate::~Delegate() { | 96 DownloadUIController::Delegate::~Delegate() { |
92 } | 97 } |
93 | 98 |
94 DownloadUIController::DownloadUIController(content::DownloadManager* manager, | 99 DownloadUIController::DownloadUIController(content::DownloadManager* manager, |
95 scoped_ptr<Delegate> delegate) | 100 scoped_ptr<Delegate> delegate) |
96 : download_notifier_(manager, this), | 101 : download_notifier_(manager, this), |
97 delegate_(delegate.Pass()) { | 102 delegate_(delegate.Pass()) { |
98 if (!delegate_) { | 103 if (!delegate_) { |
99 #if defined(OS_ANDROID) | 104 #if defined(OS_ANDROID) |
100 delegate_.reset(new DefaultUIControllerDelegateAndroid()); | 105 delegate_.reset(new AndroidUIControllerDelegate()); |
101 #else | 106 #else |
102 // The delegate should not be invoked after the profile has gone away. This | 107 // The delegate should not be invoked after the profile has gone away. This |
103 // should be the case since DownloadUIController is owned by | 108 // should be the case since DownloadUIController is owned by |
104 // DownloadService, which in turn is a profile keyed service. | 109 // DownloadService, which in turn is a profile keyed service. |
105 delegate_.reset(new DefaultUIControllerDelegate( | 110 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
106 Profile::FromBrowserContext(manager->GetBrowserContext()))); | 111 switches::kEnableDownloadNotification)) { |
| 112 delegate_.reset(new DownloadNotificationManager( |
| 113 Profile::FromBrowserContext(manager->GetBrowserContext()))); |
| 114 } else { |
| 115 delegate_.reset(new DownloadShelfUIControllerDelegate( |
| 116 Profile::FromBrowserContext(manager->GetBrowserContext()))); |
| 117 } |
107 #endif | 118 #endif |
108 } | 119 } |
109 } | 120 } |
110 | 121 |
111 DownloadUIController::~DownloadUIController() { | 122 DownloadUIController::~DownloadUIController() { |
112 } | 123 } |
113 | 124 |
114 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager, | 125 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager, |
115 content::DownloadItem* item) { | 126 content::DownloadItem* item) { |
116 // SavePackage downloads are created in a state where they can be shown in the | 127 // SavePackage downloads are created in a state where they can be shown in the |
(...skipping 10 matching lines...) Expand all Loading... |
127 if (item_model.WasUINotified() || !item_model.ShouldNotifyUI()) | 138 if (item_model.WasUINotified() || !item_model.ShouldNotifyUI()) |
128 return; | 139 return; |
129 | 140 |
130 // Wait until the target path is determined. | 141 // Wait until the target path is determined. |
131 if (item->GetTargetFilePath().empty()) | 142 if (item->GetTargetFilePath().empty()) |
132 return; | 143 return; |
133 | 144 |
134 DownloadItemModel(item).SetWasUINotified(true); | 145 DownloadItemModel(item).SetWasUINotified(true); |
135 delegate_->OnNewDownloadReady(item); | 146 delegate_->OnNewDownloadReady(item); |
136 } | 147 } |
OLD | NEW |