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" |
8 #include "chrome/browser/download/download_item_model.h" | 9 #include "chrome/browser/download/download_item_model.h" |
9 #include "chrome/browser/ui/browser_finder.h" | 10 #include "chrome/browser/ui/browser_finder.h" |
10 #include "chrome/browser/ui/browser_tabstrip.h" | 11 #include "chrome/browser/ui/browser_tabstrip.h" |
11 #include "content/public/browser/download_item.h" | 12 #include "content/public/browser/download_item.h" |
12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
13 #include "content/public/browser/web_contents_delegate.h" | 14 #include "content/public/browser/web_contents_delegate.h" |
14 | 15 |
15 #if defined(OS_ANDROID) | 16 #if defined(OS_ANDROID) |
16 #include "content/public/browser/android/download_controller_android.h" | 17 #include "content/public/browser/android/download_controller_android.h" |
17 #else | 18 #else |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #endif | 20 #endif |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // DefaultUIControllerDelegate{Android,} is used when a DownloadUIController is | 24 // DefaultUIControllerDelegate{Android,} is used when a DownloadUIController is |
24 // constructed without specifying an explicit Delegate. | 25 // constructed without specifying an explicit Delegate. |
25 #if defined(OS_ANDROID) | 26 #if defined(OS_ANDROID) |
26 | 27 |
27 class DefaultUIControllerDelegateAndroid | 28 class DefaultUIControllerDelegateAndroid |
28 : public DownloadUIController::Delegate { | 29 : public DownloadUIController::Delegate { |
29 public: | 30 public: |
30 DefaultUIControllerDelegateAndroid() {} | 31 DefaultUIControllerDelegateAndroid() {} |
31 virtual ~DefaultUIControllerDelegateAndroid() {} | 32 virtual ~DefaultUIControllerDelegateAndroid() {} |
32 | 33 |
33 private: | 34 private: |
34 // DownloadUIController::Delegate | 35 // DownloadUIController::Delegate |
35 virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE; | 36 virtual void OnNewDownloadReady(content::DownloadItem* item) OVERRIDE; |
36 }; | 37 }; |
37 | 38 |
38 void DefaultUIControllerDelegateAndroid::NotifyDownloadStarting( | 39 void DefaultUIControllerDelegateAndroid::OnNewDownloadReady( |
39 content::DownloadItem* item) { | 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 |
40 // GET downloads without authentication are delegated to the Android | 47 // GET downloads without authentication are delegated to the Android |
41 // DownloadManager. Chrome is responsible for the rest. See | 48 // DownloadManager. Chrome is responsible for the rest. See |
42 // InterceptDownloadResourceThrottle::ProcessDownloadRequest(). | 49 // InterceptDownloadResourceThrottle::ProcessDownloadRequest(). |
43 content::DownloadControllerAndroid::Get()->OnDownloadStarted(item); | 50 content::DownloadControllerAndroid::Get()->OnDownloadStarted(item); |
44 } | 51 } |
45 | 52 |
46 #else // OS_ANDROID | 53 #else // OS_ANDROID |
47 | 54 |
48 class DefaultUIControllerDelegate : public DownloadUIController::Delegate { | 55 class DefaultUIControllerDelegate : public DownloadUIController::Delegate { |
49 public: | 56 public: |
50 // |profile| is required to outlive DefaultUIControllerDelegate. | 57 // |profile| is required to outlive DefaultUIControllerDelegate. |
51 explicit DefaultUIControllerDelegate(Profile* profile) | 58 explicit DefaultUIControllerDelegate(Profile* profile) |
52 : profile_(profile) {} | 59 : profile_(profile) {} |
53 virtual ~DefaultUIControllerDelegate() {} | 60 virtual ~DefaultUIControllerDelegate() {} |
54 | 61 |
55 private: | 62 private: |
56 // DownloadUIController::Delegate | 63 // DownloadUIController::Delegate |
57 virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE; | 64 virtual void OnNewDownloadReady(content::DownloadItem* item) OVERRIDE; |
58 | 65 |
59 Profile* profile_; | 66 Profile* profile_; |
60 }; | 67 }; |
61 | 68 |
62 void DefaultUIControllerDelegate::NotifyDownloadStarting( | 69 void DefaultUIControllerDelegate::OnNewDownloadReady( |
63 content::DownloadItem* item) { | 70 content::DownloadItem* item) { |
64 content::WebContents* web_contents = item->GetWebContents(); | 71 content::WebContents* web_contents = item->GetWebContents(); |
65 Browser* browser = | 72 Browser* browser = |
66 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL; | 73 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL; |
67 | 74 |
68 // As a last resort, use the last active browser for this profile. Not ideal, | 75 // As a last resort, use the last active browser for this profile. Not ideal, |
69 // but better than not showing the download at all. | 76 // but better than not showing the download at all. |
70 if (browser == NULL) { | 77 if (browser == NULL) { |
71 browser = chrome::FindLastActiveWithProfile(profile_, | 78 browser = chrome::FindLastActiveWithProfile(profile_, |
72 chrome::GetActiveDesktop()); | 79 chrome::GetActiveDesktop()); |
(...skipping 25 matching lines...) Expand all Loading... |
98 Profile::FromBrowserContext(manager->GetBrowserContext()))); | 105 Profile::FromBrowserContext(manager->GetBrowserContext()))); |
99 #endif | 106 #endif |
100 } | 107 } |
101 } | 108 } |
102 | 109 |
103 DownloadUIController::~DownloadUIController() { | 110 DownloadUIController::~DownloadUIController() { |
104 } | 111 } |
105 | 112 |
106 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager, | 113 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager, |
107 content::DownloadItem* item) { | 114 content::DownloadItem* item) { |
108 // If this isn't a new download, there's nothing to do. | |
109 if (item->GetState() != content::DownloadItem::IN_PROGRESS) | |
110 return; | |
111 | |
112 DownloadItemModel(item).SetShouldNotifyUI(true); | |
113 // SavePackage downloads are created in a state where they can be shown in the | 115 // SavePackage downloads are created in a state where they can be shown in the |
114 // browser. Call OnDownloadUpdated() once to notify the UI immediately. | 116 // browser. Call OnDownloadUpdated() once to notify the UI immediately. |
115 OnDownloadUpdated(manager, item); | 117 OnDownloadUpdated(manager, item); |
116 } | 118 } |
117 | 119 |
118 void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager, | 120 void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager, |
119 content::DownloadItem* item) { | 121 content::DownloadItem* item) { |
| 122 DownloadItemModel item_model(item); |
| 123 |
120 // Ignore if we've already notified the UI about |item| or if it isn't a new | 124 // Ignore if we've already notified the UI about |item| or if it isn't a new |
121 // download. | 125 // download. |
122 if (!DownloadItemModel(item).ShouldNotifyUI()) | 126 if (item_model.WasUINotified() || !item_model.ShouldNotifyUI()) |
123 return; | 127 return; |
124 | 128 |
125 // Wait until the target path is determined. | 129 // Wait until the target path is determined. |
126 if (item->GetTargetFilePath().empty()) | 130 if (item->GetTargetFilePath().empty()) |
127 return; | 131 return; |
128 | 132 |
129 // Can't be complete. That would imply that we didn't receive an | 133 DownloadItemModel(item).SetWasUINotified(true); |
130 // OnDownloadUpdated() after the target was determined. | 134 delegate_->OnNewDownloadReady(item); |
131 DCHECK_NE(content::DownloadItem::COMPLETE, item->GetState()); | |
132 | |
133 DownloadItemModel(item).SetShouldNotifyUI(false); | |
134 delegate_->NotifyDownloadStarting(item); | |
135 } | 135 } |
OLD | NEW |