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/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/download/download_history.h" | |
8 #include "chrome/browser/download/download_item_model.h" | 10 #include "chrome/browser/download/download_item_model.h" |
11 #include "chrome/browser/download/download_service.h" | |
12 #include "chrome/browser/download/download_service_factory.h" | |
9 #include "chrome/browser/ui/browser_finder.h" | 13 #include "chrome/browser/ui/browser_finder.h" |
10 #include "chrome/browser/ui/browser_tabstrip.h" | 14 #include "chrome/browser/ui/browser_tabstrip.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" |
(...skipping 11 matching lines...) Expand all Loading... | |
30 DefaultUIControllerDelegateAndroid() {} | 34 DefaultUIControllerDelegateAndroid() {} |
31 virtual ~DefaultUIControllerDelegateAndroid() {} | 35 virtual ~DefaultUIControllerDelegateAndroid() {} |
32 | 36 |
33 private: | 37 private: |
34 // DownloadUIController::Delegate | 38 // DownloadUIController::Delegate |
35 virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE; | 39 virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE; |
36 }; | 40 }; |
37 | 41 |
38 void DefaultUIControllerDelegateAndroid::NotifyDownloadStarting( | 42 void DefaultUIControllerDelegateAndroid::NotifyDownloadStarting( |
39 content::DownloadItem* item) { | 43 content::DownloadItem* item) { |
44 // The Android DownloadController is only interested in IN_PROGRESS downloads. | |
45 // Ones which are INTERRUPTED etc. can't be handed over to the Android | |
46 // DownloadManager. | |
47 if (item->GetState() != content::DownloadItem::IN_PROGRESS) | |
48 return; | |
49 | |
40 // GET downloads without authentication are delegated to the Android | 50 // GET downloads without authentication are delegated to the Android |
41 // DownloadManager. Chrome is responsible for the rest. See | 51 // DownloadManager. Chrome is responsible for the rest. See |
42 // InterceptDownloadResourceThrottle::ProcessDownloadRequest(). | 52 // InterceptDownloadResourceThrottle::ProcessDownloadRequest(). |
43 content::DownloadControllerAndroid::Get()->OnDownloadStarted(item); | 53 content::DownloadControllerAndroid::Get()->OnDownloadStarted(item); |
44 } | 54 } |
45 | 55 |
46 #else // OS_ANDROID | 56 #else // OS_ANDROID |
47 | 57 |
48 class DefaultUIControllerDelegate : public DownloadUIController::Delegate { | 58 class DefaultUIControllerDelegate : public DownloadUIController::Delegate { |
49 public: | 59 public: |
(...skipping 21 matching lines...) Expand all Loading... | |
71 browser = chrome::FindLastActiveWithProfile(profile_, | 81 browser = chrome::FindLastActiveWithProfile(profile_, |
72 chrome::GetActiveDesktop()); | 82 chrome::GetActiveDesktop()); |
73 } | 83 } |
74 | 84 |
75 if (browser) | 85 if (browser) |
76 browser->ShowDownload(item); | 86 browser->ShowDownload(item); |
77 } | 87 } |
78 | 88 |
79 #endif // !OS_ANDROID | 89 #endif // !OS_ANDROID |
80 | 90 |
91 // Predicate that returns true if the given download was not restored from | |
92 // history. Used to construct a DownloadFilter based on a DownloadHistory. | |
93 bool IsNewDownload(DownloadHistory* download_history, | |
94 const content::DownloadItem* download_item) { | |
95 // Incognito profiles won't have a DownloadHistory. All downloads on such | |
96 // profiles are considered new. | |
97 return !download_history || | |
98 !download_history->WasRestoredFromHistory(download_item); | |
99 } | |
100 | |
81 } // namespace | 101 } // namespace |
82 | 102 |
83 DownloadUIController::Delegate::~Delegate() { | 103 DownloadUIController::Delegate::~Delegate() { |
84 } | 104 } |
85 | 105 |
86 DownloadUIController::DownloadUIController(content::DownloadManager* manager, | 106 DownloadUIController::DownloadFilter |
87 scoped_ptr<Delegate> delegate) | 107 DownloadUIController::NewDownloadFilterFromDownloadHistory( |
108 DownloadHistory* download_history) { | |
109 return base::Bind(&IsNewDownload, download_history); | |
110 } | |
111 | |
112 DownloadUIController::DownloadUIController( | |
113 content::DownloadManager* manager, | |
114 const DownloadFilter& new_download_filter, | |
115 scoped_ptr<Delegate> delegate) | |
88 : download_notifier_(manager, this), | 116 : download_notifier_(manager, this), |
117 new_download_filter_(new_download_filter), | |
89 delegate_(delegate.Pass()) { | 118 delegate_(delegate.Pass()) { |
119 DCHECK(!new_download_filter_.is_null()); | |
90 if (!delegate_) { | 120 if (!delegate_) { |
91 #if defined(OS_ANDROID) | 121 #if defined(OS_ANDROID) |
92 delegate_.reset(new DefaultUIControllerDelegateAndroid()); | 122 delegate_.reset(new DefaultUIControllerDelegateAndroid()); |
93 #else | 123 #else |
94 // The delegate should not be invoked after the profile has gone away. This | 124 // The delegate should not be invoked after the profile has gone away. This |
95 // should be the case since DownloadUIController is owned by | 125 // should be the case since DownloadUIController is owned by |
96 // DownloadService, which in turn is a profile keyed service. | 126 // DownloadService, which in turn is a profile keyed service. |
97 delegate_.reset(new DefaultUIControllerDelegate( | 127 delegate_.reset(new DefaultUIControllerDelegate( |
98 Profile::FromBrowserContext(manager->GetBrowserContext()))); | 128 Profile::FromBrowserContext(manager->GetBrowserContext()))); |
99 #endif | 129 #endif |
100 } | 130 } |
101 } | 131 } |
102 | 132 |
103 DownloadUIController::~DownloadUIController() { | 133 DownloadUIController::~DownloadUIController() { |
104 } | 134 } |
105 | 135 |
106 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager, | 136 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager, |
107 content::DownloadItem* item) { | 137 content::DownloadItem* item) { |
108 // If this isn't a new download, there's nothing to do. | 138 // If this isn't a new download, there's nothing to do. |
109 if (item->GetState() != content::DownloadItem::IN_PROGRESS) | 139 if (!new_download_filter_.Run(item)) |
Randy Smith (Not in Mondays)
2014/04/10 18:03:23
There's something basic I'm not understanding here
asanka
2014/04/17 21:16:10
This code was refactored somewhat, but the questio
| |
110 return; | 140 return; |
111 | 141 |
112 DownloadItemModel(item).SetShouldNotifyUI(true); | 142 DownloadItemModel(item).SetShouldNotifyUI(true); |
113 // SavePackage downloads are created in a state where they can be shown in the | 143 // SavePackage downloads are created in a state where they can be shown in the |
114 // browser. Call OnDownloadUpdated() once to notify the UI immediately. | 144 // browser. Call OnDownloadUpdated() once to notify the UI immediately. |
115 OnDownloadUpdated(manager, item); | 145 OnDownloadUpdated(manager, item); |
116 } | 146 } |
117 | 147 |
118 void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager, | 148 void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager, |
119 content::DownloadItem* item) { | 149 content::DownloadItem* item) { |
120 // Ignore if we've already notified the UI about |item| or if it isn't a new | 150 // Ignore if we've already notified the UI about |item| or if it isn't a new |
121 // download. | 151 // download. |
122 if (!DownloadItemModel(item).ShouldNotifyUI()) | 152 if (!DownloadItemModel(item).ShouldNotifyUI()) |
123 return; | 153 return; |
124 | 154 |
125 // Wait until the target path is determined. | 155 // Wait until the target path is determined. |
126 if (item->GetTargetFilePath().empty()) | 156 if (item->GetTargetFilePath().empty()) |
127 return; | 157 return; |
128 | 158 |
129 // Can't be complete. That would imply that we didn't receive an | |
130 // OnDownloadUpdated() after the target was determined. | |
131 DCHECK_NE(content::DownloadItem::COMPLETE, item->GetState()); | |
132 | |
133 DownloadItemModel(item).SetShouldNotifyUI(false); | 159 DownloadItemModel(item).SetShouldNotifyUI(false); |
134 delegate_->NotifyDownloadStarting(item); | 160 delegate_->NotifyDownloadStarting(item); |
135 } | 161 } |
OLD | NEW |