OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_request_manager.h" | 5 #include "chrome/browser/download/download_request_manager.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/thread.h" | 8 #include "base/thread.h" |
9 #include "chrome/browser/download/download_request_dialog_delegate.h" | 9 #include "chrome/browser/download/download_request_infobar_delegate.h" |
10 #include "chrome/browser/tab_contents/navigation_controller.h" | 10 #include "chrome/browser/tab_contents/navigation_controller.h" |
11 #include "chrome/browser/tab_contents/navigation_entry.h" | 11 #include "chrome/browser/tab_contents/navigation_entry.h" |
12 #include "chrome/browser/tab_contents/tab_contents_delegate.h" | 12 #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
13 #include "chrome/browser/tab_contents/tab_util.h" | 13 #include "chrome/browser/tab_contents/tab_util.h" |
14 #include "chrome/browser/tab_contents/tab_contents.h" | 14 #include "chrome/browser/tab_contents/tab_contents.h" |
15 #include "chrome/common/notification_service.h" | 15 #include "chrome/common/notification_service.h" |
16 | 16 |
17 // TabDownloadState ------------------------------------------------------------ | 17 // TabDownloadState ------------------------------------------------------------ |
18 | 18 |
19 DownloadRequestManager::TabDownloadState::TabDownloadState( | 19 DownloadRequestManager::TabDownloadState::TabDownloadState( |
20 DownloadRequestManager* host, | 20 DownloadRequestManager* host, |
21 NavigationController* controller, | 21 NavigationController* controller, |
22 NavigationController* originating_controller) | 22 NavigationController* originating_controller) |
23 : host_(host), | 23 : host_(host), |
24 controller_(controller), | 24 controller_(controller), |
25 status_(DownloadRequestManager::ALLOW_ONE_DOWNLOAD), | 25 status_(DownloadRequestManager::ALLOW_ONE_DOWNLOAD), |
26 dialog_delegate_(NULL) { | 26 infobar_(NULL) { |
27 Source<NavigationController> notification_source(controller); | 27 Source<NavigationController> notification_source(controller); |
28 registrar_.Add(this, NotificationType::NAV_ENTRY_PENDING, | 28 registrar_.Add(this, NotificationType::NAV_ENTRY_PENDING, |
29 notification_source); | 29 notification_source); |
30 registrar_.Add(this, NotificationType::TAB_CLOSED, notification_source); | 30 registrar_.Add(this, NotificationType::TAB_CLOSED, notification_source); |
31 | 31 |
32 NavigationEntry* active_entry = originating_controller ? | 32 NavigationEntry* active_entry = originating_controller ? |
33 originating_controller->GetActiveEntry() : controller->GetActiveEntry(); | 33 originating_controller->GetActiveEntry() : controller->GetActiveEntry(); |
34 if (active_entry) | 34 if (active_entry) |
35 initial_page_host_ = active_entry->url().host(); | 35 initial_page_host_ = active_entry->url().host(); |
36 } | 36 } |
37 | 37 |
38 DownloadRequestManager::TabDownloadState::~TabDownloadState() { | 38 DownloadRequestManager::TabDownloadState::~TabDownloadState() { |
39 // We should only be destroyed after the callbacks have been notified. | 39 // We should only be destroyed after the callbacks have been notified. |
40 DCHECK(callbacks_.empty()); | 40 DCHECK(callbacks_.empty()); |
41 | 41 |
42 // And we should have closed the message box. | 42 // And we should have closed the infobar. |
43 DCHECK(!dialog_delegate_); | 43 DCHECK(!infobar_); |
44 } | 44 } |
45 | 45 |
46 void DownloadRequestManager::TabDownloadState::OnUserGesture() { | 46 void DownloadRequestManager::TabDownloadState::OnUserGesture() { |
47 if (is_showing_prompt()) { | 47 if (is_showing_prompt()) { |
48 // Don't change the state if the user clicks on the page some where. | 48 // Don't change the state if the user clicks on the page some where. |
49 return; | 49 return; |
50 } | 50 } |
51 | 51 |
52 if (status_ != DownloadRequestManager::ALLOW_ALL_DOWNLOADS && | 52 if (status_ != DownloadRequestManager::ALLOW_ALL_DOWNLOADS && |
53 status_ != DownloadRequestManager::DOWNLOADS_NOT_ALLOWED) { | 53 status_ != DownloadRequestManager::DOWNLOADS_NOT_ALLOWED) { |
54 // Revert to default status. | 54 // Revert to default status. |
55 host_->Remove(this); | 55 host_->Remove(this); |
56 // WARNING: We've been deleted. | 56 // WARNING: We've been deleted. |
57 return; | 57 return; |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 void DownloadRequestManager::TabDownloadState::PromptUserForDownload( | 61 void DownloadRequestManager::TabDownloadState::PromptUserForDownload( |
62 TabContents* tab, | 62 TabContents* tab, |
63 DownloadRequestManager::Callback* callback) { | 63 DownloadRequestManager::Callback* callback) { |
64 callbacks_.push_back(callback); | 64 callbacks_.push_back(callback); |
65 | 65 |
66 if (is_showing_prompt()) | 66 if (is_showing_prompt()) |
67 return; // Already showing prompt. | 67 return; // Already showing prompt. |
68 | 68 |
69 if (DownloadRequestManager::delegate_) { | 69 if (DownloadRequestManager::delegate_) { |
70 NotifyCallbacks(DownloadRequestManager::delegate_->ShouldAllowDownload()); | 70 NotifyCallbacks(DownloadRequestManager::delegate_->ShouldAllowDownload()); |
71 } else { | 71 } else { |
72 dialog_delegate_ = DownloadRequestDialogDelegate::Create(tab, this); | 72 infobar_ = new DownloadRequestInfoBarDelegate(tab, this); |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 void DownloadRequestManager::TabDownloadState::Cancel() { | 76 void DownloadRequestManager::TabDownloadState::Cancel() { |
77 NotifyCallbacks(false); | 77 NotifyCallbacks(false); |
78 } | 78 } |
79 | 79 |
80 void DownloadRequestManager::TabDownloadState::Accept() { | 80 void DownloadRequestManager::TabDownloadState::Accept() { |
81 NotifyCallbacks(true); | 81 NotifyCallbacks(true); |
82 } | 82 } |
(...skipping 19 matching lines...) Expand all Loading... |
102 // 100% right, so we don't deal with it. | 102 // 100% right, so we don't deal with it. |
103 NavigationEntry* entry = controller_->pending_entry(); | 103 NavigationEntry* entry = controller_->pending_entry(); |
104 if (!entry) | 104 if (!entry) |
105 return; | 105 return; |
106 | 106 |
107 if (PageTransition::IsRedirect(entry->transition_type())) { | 107 if (PageTransition::IsRedirect(entry->transition_type())) { |
108 // Redirects don't count. | 108 // Redirects don't count. |
109 return; | 109 return; |
110 } | 110 } |
111 | 111 |
112 if (is_showing_prompt()) { | 112 if (status_ == DownloadRequestManager::ALLOW_ALL_DOWNLOADS || |
113 // We're prompting the user and they navigated away. Close the popup and | 113 status_ == DownloadRequestManager::DOWNLOADS_NOT_ALLOWED) { |
114 // cancel the downloads. | |
115 dialog_delegate_->CloseWindow(); | |
116 // After switch we'll notify callbacks and get deleted. | |
117 } else if (status_ == DownloadRequestManager::ALLOW_ALL_DOWNLOADS || | |
118 status_ == DownloadRequestManager::DOWNLOADS_NOT_ALLOWED) { | |
119 // User has either allowed all downloads or canceled all downloads. Only | 114 // User has either allowed all downloads or canceled all downloads. Only |
120 // reset the download state if the user is navigating to a different | 115 // reset the download state if the user is navigating to a different |
121 // host (or host is empty). | 116 // host (or host is empty). |
122 if (!initial_page_host_.empty() && !entry->url().host().empty() && | 117 if (!initial_page_host_.empty() && !entry->url().host().empty() && |
123 entry->url().host() == initial_page_host_) { | 118 entry->url().host() == initial_page_host_) { |
124 return; | 119 return; |
125 } | 120 } |
126 } // else case: we're not prompting user and user hasn't allowed or | 121 } |
127 // disallowed downloads, break so that we get deleted after switch. | |
128 break; | 122 break; |
129 } | 123 } |
130 | 124 |
131 case NotificationType::TAB_CLOSED: | 125 case NotificationType::TAB_CLOSED: |
132 // Tab closed, no need to handle closing the dialog as it's owned by the | 126 // Tab closed, no need to handle closing the dialog as it's owned by the |
133 // TabContents, break so that we get deleted after switch. | 127 // TabContents, break so that we get deleted after switch. |
134 break; | 128 break; |
135 | 129 |
136 default: | 130 default: |
137 NOTREACHED(); | 131 NOTREACHED(); |
138 } | 132 } |
139 | 133 |
140 NotifyCallbacks(false); | 134 NotifyCallbacks(false); |
141 host_->Remove(this); | 135 host_->Remove(this); |
142 } | 136 } |
143 | 137 |
144 void DownloadRequestManager::TabDownloadState::NotifyCallbacks(bool allow) { | 138 void DownloadRequestManager::TabDownloadState::NotifyCallbacks(bool allow) { |
145 if (dialog_delegate_) { | 139 if (infobar_) { |
146 // Reset the delegate so we don't get notified again. | 140 // Reset the delegate so we don't get notified again. |
147 dialog_delegate_->set_host(NULL); | 141 infobar_->set_host(NULL); |
148 dialog_delegate_ = NULL; | 142 infobar_ = NULL; |
149 } | 143 } |
150 status_ = allow ? | 144 status_ = allow ? |
151 DownloadRequestManager::ALLOW_ALL_DOWNLOADS : | 145 DownloadRequestManager::ALLOW_ALL_DOWNLOADS : |
152 DownloadRequestManager::DOWNLOADS_NOT_ALLOWED; | 146 DownloadRequestManager::DOWNLOADS_NOT_ALLOWED; |
153 std::vector<DownloadRequestManager::Callback*> callbacks; | 147 std::vector<DownloadRequestManager::Callback*> callbacks; |
154 callbacks.swap(callbacks_); | 148 callbacks.swap(callbacks_); |
155 for (size_t i = 0; i < callbacks.size(); ++i) | 149 for (size_t i = 0; i < callbacks.size(); ++i) |
156 host_->ScheduleNotification(callbacks[i], allow); | 150 host_->ScheduleNotification(callbacks[i], allow); |
157 } | 151 } |
158 | 152 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 285 |
292 void DownloadRequestManager::Remove(TabDownloadState* state) { | 286 void DownloadRequestManager::Remove(TabDownloadState* state) { |
293 DCHECK(state_map_.find(state->controller()) != state_map_.end()); | 287 DCHECK(state_map_.find(state->controller()) != state_map_.end()); |
294 state_map_.erase(state->controller()); | 288 state_map_.erase(state->controller()); |
295 delete state; | 289 delete state; |
296 } | 290 } |
297 | 291 |
298 // static | 292 // static |
299 DownloadRequestManager::TestingDelegate* DownloadRequestManager::delegate_ = | 293 DownloadRequestManager::TestingDelegate* DownloadRequestManager::delegate_ = |
300 NULL; | 294 NULL; |
OLD | NEW |