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

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

Issue 11640007: Make the UI an observer of downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use DownloadItemModel. Address Nits. Created 8 years 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
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/download/download_ui_controller.h"
6
7 #include "base/stl_util.h"
8 #include "chrome/browser/download/download_item_model.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/browser_tabstrip.h"
11 #include "content/public/browser/download_item.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_delegate.h"
14
15 #if defined(OS_ANDROID)
16 #include "content/public/browser/android/download_controller_android.h"
17 #else
18 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
19 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper_delegate. h"
20 #endif
21
22 DownloadUIController::DownloadUIController(Profile* profile,
23 content::DownloadManager* manager)
24 : ALLOW_THIS_IN_INITIALIZER_LIST(notifier_(manager, this)),
25 profile_(profile) {
26 }
27
28 DownloadUIController::~DownloadUIController() {
29 }
30
31 void DownloadUIController::OnDownloadCreated(content::DownloadManager* manager,
32 content::DownloadItem* item) {
33 // If this isn't a new download, there's nothing to do.
34 if (!item->IsInProgress())
35 return;
36
37 DownloadItemModel(item).SetShouldNotifyUI(true);
38 // SavePackage downloads are created in a state where they can be shown
39 // immediately in the browser.
40 OnDownloadUpdated(manager, item);
41 }
42
43 void DownloadUIController::OnDownloadUpdated(content::DownloadManager* manager,
44 content::DownloadItem* item) {
45
46 // Ignore if we've already notified the UI about |item| or if it isn't a new
47 // download.
48 if (!DownloadItemModel(item).ShouldNotifyUI())
49 return;
50
51 // Wait until the target path is determined.
52 if (item->GetTargetFilePath().empty())
53 return;
54
55 // For a download that is in-progress, wait until the intermediate rename is
56 // complete.
Randy Smith (Not in Mondays) 2012/12/20 23:09:25 What's the goal here? And isn't it true that the
asanka 2012/12/20 23:56:19 I suppose the TODO wasn't a giveaway.
asanka 2013/01/10 22:12:14 https://codereview.chromium.org/11801022/. I'm cha
57 // TODO(asanka): Get rid of remaining users of GetFullPath() in ui/.
58 if (item->IsInProgress() && item->GetFullPath().empty())
59 return;
60
61 // At this point, we expect that the download is either interrupted, or
62 // in-progress and the path is known.
Randy Smith (Not in Mondays) 2012/12/20 23:09:25 What happens if the download is interrupted before
asanka 2012/12/20 23:56:19 Target name determination happens in chrome/. If t
63
64 // Can't be complete. That would imply that we didn't receive an
65 // OnDownloadUpdated() after the intermediate rename was completed.
66 DCHECK(!item->IsComplete());
67
68 DownloadItemModel(item).SetShouldNotifyUI(false);
69 NotifyDownloadStarting(item);
70 }
71
72 void DownloadUIController::NotifyDownloadStarting(content::DownloadItem* item) {
73 #if defined(OS_ANDROID)
74 // GET downloads are delegated to the Android DownloadManager. Chrome is only
75 // responsible for POST downloads. See
76 // ChromeWebContentsDelegateAndroid::CanDownload().
77 content::DownloadControllerAndroid::Get()->OnPostDownloadStarted(item);
78 #else
79 content::WebContents* web_contents = item->GetWebContents();
80
81 // If the tab requesting the download is a constrained popup that is not
82 // shown, treat the request as if it came from the parent.
83 if (web_contents != NULL) {
84 BlockedContentTabHelper* blocked_content_tab_helper =
85 BlockedContentTabHelper::FromWebContents(web_contents);
86 if (blocked_content_tab_helper &&
87 blocked_content_tab_helper->delegate()) {
88 content::WebContents* constraining_web_contents =
89 blocked_content_tab_helper->delegate()->
90 GetConstrainingWebContents(web_contents);
91 if (constraining_web_contents)
92 web_contents = constraining_web_contents;
93 }
94 }
95
96 Browser* browser =
97 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL;
98
99 // As a last resort, use the last active browser for this profile. Not ideal,
100 // but better than not showing the download at all.
101 if (browser == NULL) {
102 browser = chrome::FindLastActiveWithProfile(profile_,
103 chrome::GetActiveDesktop());
104 }
105
106 if (browser)
107 browser->ShowDownload(item);
108 #endif
109 }
110
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698