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