Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: chrome/browser/download/download_ui_controller.cc

Issue 230103002: [Downloads] Ask DownloadHistory if a download was from history. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move responsibility of determining whether to show a download in the UI to DownloadItemModel Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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) {
71
Randy Smith (Not in Mondays) 2014/04/21 18:21:22 nit: Did you intentionally put the newline in?
asanka 2014/04/23 05:51:09 Nope. Removed.
64 content::WebContents* web_contents = item->GetWebContents(); 72 content::WebContents* web_contents = item->GetWebContents();
65 Browser* browser = 73 Browser* browser =
66 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL; 74 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL;
67 75
68 // As a last resort, use the last active browser for this profile. Not ideal, 76 // As a last resort, use the last active browser for this profile. Not ideal,
69 // but better than not showing the download at all. 77 // but better than not showing the download at all.
70 if (browser == NULL) { 78 if (browser == NULL) {
71 browser = chrome::FindLastActiveWithProfile(profile_, 79 browser = chrome::FindLastActiveWithProfile(profile_,
72 chrome::GetActiveDesktop()); 80 chrome::GetActiveDesktop());
73 } 81 }
(...skipping 24 matching lines...) Expand all
98 Profile::FromBrowserContext(manager->GetBrowserContext()))); 106 Profile::FromBrowserContext(manager->GetBrowserContext())));
99 #endif 107 #endif
100 } 108 }
101 } 109 }
102 110
103 DownloadUIController::~DownloadUIController() { 111 DownloadUIController::~DownloadUIController() {
104 } 112 }
105 113
106 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager, 114 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager,
107 content::DownloadItem* item) { 115 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 116 // SavePackage downloads are created in a state where they can be shown in the
114 // browser. Call OnDownloadUpdated() once to notify the UI immediately. 117 // browser. Call OnDownloadUpdated() once to notify the UI immediately.
115 OnDownloadUpdated(manager, item); 118 OnDownloadUpdated(manager, item);
116 } 119 }
117 120
118 void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager, 121 void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager,
119 content::DownloadItem* item) { 122 content::DownloadItem* item) {
123 DownloadItemModel item_model(item);
124
120 // Ignore if we've already notified the UI about |item| or if it isn't a new 125 // Ignore if we've already notified the UI about |item| or if it isn't a new
121 // download. 126 // download.
122 if (!DownloadItemModel(item).ShouldNotifyUI()) 127 if (item_model.WasUINotified() || !item_model.ShouldNotifyUI())
123 return; 128 return;
124 129
125 // Wait until the target path is determined. 130 // Wait until the target path is determined.
126 if (item->GetTargetFilePath().empty()) 131 if (item->GetTargetFilePath().empty())
127 return; 132 return;
128 133
129 // Can't be complete. That would imply that we didn't receive an 134 DownloadItemModel(item).SetWasUINotified(true);
130 // OnDownloadUpdated() after the target was determined. 135 delegate_->OnNewDownloadReady(item);
131 DCHECK_NE(content::DownloadItem::COMPLETE, item->GetState());
132
133 DownloadItemModel(item).SetShouldNotifyUI(false);
134 delegate_->NotifyDownloadStarting(item);
135 } 136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698