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