OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ui/webui/downloads_dom_handler.h" | 5 #include "chrome/browser/ui/webui/downloads_dom_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); | 139 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); |
140 file_value->SetString("file_name", file_name); | 140 file_value->SetString("file_name", file_name); |
141 file_value->SetString("url", download_item->GetURL().spec()); | 141 file_value->SetString("url", download_item->GetURL().spec()); |
142 file_value->SetBoolean("otr", incognito); | 142 file_value->SetBoolean("otr", incognito); |
143 file_value->SetInteger("total", static_cast<int>( | 143 file_value->SetInteger("total", static_cast<int>( |
144 download_item->GetTotalBytes())); | 144 download_item->GetTotalBytes())); |
145 file_value->SetBoolean("file_externally_removed", | 145 file_value->SetBoolean("file_externally_removed", |
146 download_item->GetFileExternallyRemoved()); | 146 download_item->GetFileExternallyRemoved()); |
147 file_value->SetBoolean("retry", false); // Overridden below if needed. | 147 file_value->SetBoolean("retry", false); // Overridden below if needed. |
148 | 148 |
149 if (download_item->IsInProgress()) { | 149 switch (download_item->GetState()) { |
150 if (download_item->IsDangerous()) { | 150 case content::DownloadItem::IN_PROGRESS: |
151 file_value->SetString("state", "DANGEROUS"); | 151 if (download_item->IsDangerous()) { |
152 // These are the only danger states that the UI is equipped to handle. | 152 file_value->SetString("state", "DANGEROUS"); |
153 DCHECK(download_item->GetDangerType() == | 153 // These are the only danger states that the UI is equipped to handle. |
154 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || | 154 DCHECK(download_item->GetDangerType() == |
155 download_item->GetDangerType() == | 155 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || |
156 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || | 156 download_item->GetDangerType() == |
157 download_item->GetDangerType() == | 157 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || |
158 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || | 158 download_item->GetDangerType() == |
159 download_item->GetDangerType() == | 159 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || |
160 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT || | 160 download_item->GetDangerType() == |
161 download_item->GetDangerType() == | 161 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT || |
162 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST); | 162 download_item->GetDangerType() == |
163 const char* danger_type_value = | 163 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST); |
164 GetDangerTypeString(download_item->GetDangerType()); | 164 const char* danger_type_value = |
165 file_value->SetString("danger_type", danger_type_value); | 165 GetDangerTypeString(download_item->GetDangerType()); |
166 } else if (download_item->IsPaused()) { | 166 file_value->SetString("danger_type", danger_type_value); |
167 file_value->SetString("state", "PAUSED"); | 167 } else if (download_item->IsPaused()) { |
168 } else { | 168 file_value->SetString("state", "PAUSED"); |
169 file_value->SetString("state", "IN_PROGRESS"); | 169 } else { |
170 } | 170 file_value->SetString("state", "IN_PROGRESS"); |
171 } | |
171 | 172 |
172 file_value->SetString("progress_status_text", | 173 file_value->SetString( |
173 download_util::GetProgressStatusText(download_item)); | 174 "progress_status_text", |
175 download_util::GetProgressStatusText(download_item)); | |
174 | 176 |
175 file_value->SetInteger("percent", | 177 file_value->SetInteger( |
176 static_cast<int>(download_item->PercentComplete())); | 178 "percent", static_cast<int>(download_item->PercentComplete())); |
177 file_value->SetInteger("received", | 179 file_value->SetInteger( |
178 static_cast<int>(download_item->GetReceivedBytes())); | 180 "received", static_cast<int>(download_item->GetReceivedBytes())); |
179 } else if (download_item->IsInterrupted()) { | 181 break; |
180 file_value->SetString("state", "INTERRUPTED"); | |
181 | 182 |
182 file_value->SetString("progress_status_text", | 183 case content::DownloadItem::INTERRUPTED: |
183 download_util::GetProgressStatusText(download_item)); | 184 file_value->SetString("state", "INTERRUPTED"); |
184 | 185 |
185 file_value->SetInteger("percent", | 186 file_value->SetString( |
186 static_cast<int>(download_item->PercentComplete())); | 187 "progress_status_text", |
187 file_value->SetInteger("received", | 188 download_util::GetProgressStatusText(download_item)); |
188 static_cast<int>(download_item->GetReceivedBytes())); | 189 |
189 file_value->SetString("last_reason_text", | 190 file_value->SetInteger( |
190 download_model.GetInterruptReasonText()); | 191 "percent", static_cast<int>(download_item->PercentComplete())); |
191 if (content::DOWNLOAD_INTERRUPT_REASON_CRASH == | 192 file_value->SetInteger( |
192 download_item->GetLastReason()) | 193 "received", static_cast<int>(download_item->GetReceivedBytes())); |
194 file_value->SetString("last_reason_text", | |
195 download_model.GetInterruptReasonText()); | |
196 if (content::DOWNLOAD_INTERRUPT_REASON_CRASH == | |
197 download_item->GetLastReason()) | |
198 file_value->SetBoolean("retry", true); | |
199 break; | |
200 | |
201 case content::DownloadItem::CANCELLED: | |
202 file_value->SetString("state", "CANCELLED"); | |
193 file_value->SetBoolean("retry", true); | 203 file_value->SetBoolean("retry", true); |
194 } else if (download_item->IsCancelled()) { | 204 break; |
195 file_value->SetString("state", "CANCELLED"); | 205 |
196 file_value->SetBoolean("retry", true); | 206 case content::DownloadItem::COMPLETE: |
197 } else if (download_item->IsComplete()) { | 207 DCHECK(!download_item->IsDangerous()); |
198 DCHECK(!download_item->IsDangerous()); | 208 file_value->SetString("state", "COMPLETE"); |
199 file_value->SetString("state", "COMPLETE"); | 209 break; |
200 } else { | 210 |
201 NOTREACHED() << "state undefined"; | 211 default: |
asanka
2013/05/28 18:43:15
Nit: You could use 'case MAX_DOWNLOAD_STATE' inste
| |
212 NOTREACHED() << "state undefined"; | |
202 } | 213 } |
203 | 214 |
204 return file_value; | 215 return file_value; |
205 } | 216 } |
206 | 217 |
207 // Filters out extension downloads and downloads that don't have a filename yet. | 218 // Filters out extension downloads and downloads that don't have a filename yet. |
208 bool IsDownloadDisplayable(const content::DownloadItem& item) { | 219 bool IsDownloadDisplayable(const content::DownloadItem& item) { |
209 return (!download_crx_util::IsExtensionDownload(item) && | 220 return (!download_crx_util::IsExtensionDownload(item) && |
210 !item.IsTemporary() && | 221 !item.IsTemporary() && |
211 !item.GetFileNameToReportUser().empty() && | 222 !item.GetFileNameToReportUser().empty() && |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 content::DownloadItem* file = GetDownloadByValue(args); | 352 content::DownloadItem* file = GetDownloadByValue(args); |
342 if (file) | 353 if (file) |
343 file->OpenDownload(); | 354 file->OpenDownload(); |
344 } | 355 } |
345 | 356 |
346 void DownloadsDOMHandler::HandleDrag(const base::ListValue* args) { | 357 void DownloadsDOMHandler::HandleDrag(const base::ListValue* args) { |
347 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG); | 358 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG); |
348 content::DownloadItem* file = GetDownloadByValue(args); | 359 content::DownloadItem* file = GetDownloadByValue(args); |
349 content::WebContents* web_contents = GetWebUIWebContents(); | 360 content::WebContents* web_contents = GetWebUIWebContents(); |
350 // |web_contents| is only NULL in the test. | 361 // |web_contents| is only NULL in the test. |
351 if (!file || !web_contents || !file->IsComplete()) | 362 if (!file || !web_contents |
363 || file->GetState() != content::DownloadItem::COMPLETE) | |
asanka
2013/05/28 18:43:15
Nit: The style guide prefers wrapping after boolea
| |
352 return; | 364 return; |
353 gfx::Image* icon = g_browser_process->icon_manager()->LookupIconFromFilepath( | 365 gfx::Image* icon = g_browser_process->icon_manager()->LookupIconFromFilepath( |
354 file->GetTargetFilePath(), IconLoader::NORMAL); | 366 file->GetTargetFilePath(), IconLoader::NORMAL); |
355 gfx::NativeView view = web_contents->GetView()->GetNativeView(); | 367 gfx::NativeView view = web_contents->GetView()->GetNativeView(); |
356 { | 368 { |
357 // Enable nested tasks during DnD, while |DragDownload()| blocks. | 369 // Enable nested tasks during DnD, while |DragDownload()| blocks. |
358 base::MessageLoop::ScopedNestableTaskAllower allow( | 370 base::MessageLoop::ScopedNestableTaskAllower allow( |
359 base::MessageLoop::current()); | 371 base::MessageLoop::current()); |
360 download_util::DragDownload(file, icon, view); | 372 download_util::DragDownload(file, icon, view); |
361 } | 373 } |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
541 } | 553 } |
542 | 554 |
543 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { | 555 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { |
544 web_ui()->CallJavascriptFunction("downloadsList", downloads); | 556 web_ui()->CallJavascriptFunction("downloadsList", downloads); |
545 } | 557 } |
546 | 558 |
547 void DownloadsDOMHandler::CallDownloadUpdated( | 559 void DownloadsDOMHandler::CallDownloadUpdated( |
548 const base::ListValue& download_item) { | 560 const base::ListValue& download_item) { |
549 web_ui()->CallJavascriptFunction("downloadUpdated", download_item); | 561 web_ui()->CallJavascriptFunction("downloadUpdated", download_item); |
550 } | 562 } |
OLD | NEW |