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" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/i18n/rtl.h" |
| 14 #include "base/i18n/time_formatting.h" |
13 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
14 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
15 #include "base/string_piece.h" | 17 #include "base/string_piece.h" |
16 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
17 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "base/value_conversions.h" |
18 #include "base/values.h" | 21 #include "base/values.h" |
19 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/download/download_crx_util.h" | 23 #include "chrome/browser/download/download_crx_util.h" |
21 #include "chrome/browser/download/download_danger_prompt.h" | 24 #include "chrome/browser/download/download_danger_prompt.h" |
22 #include "chrome/browser/download/download_history.h" | 25 #include "chrome/browser/download/download_history.h" |
| 26 #include "chrome/browser/download/download_item_model.h" |
23 #include "chrome/browser/download/download_prefs.h" | 27 #include "chrome/browser/download/download_prefs.h" |
24 #include "chrome/browser/download/download_service.h" | 28 #include "chrome/browser/download/download_service.h" |
25 #include "chrome/browser/download/download_service_factory.h" | 29 #include "chrome/browser/download/download_service_factory.h" |
26 #include "chrome/browser/download/download_util.h" | 30 #include "chrome/browser/download/download_util.h" |
27 #include "chrome/browser/platform_util.h" | 31 #include "chrome/browser/platform_util.h" |
28 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
29 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 33 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
30 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 34 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
31 #include "chrome/browser/ui/webui/fileicon_source.h" | 35 #include "chrome/browser/ui/webui/fileicon_source.h" |
| 36 #include "chrome/common/time_format.h" |
32 #include "chrome/common/url_constants.h" | 37 #include "chrome/common/url_constants.h" |
33 #include "content/public/browser/download_item.h" | 38 #include "content/public/browser/download_item.h" |
34 #include "content/public/browser/user_metrics.h" | 39 #include "content/public/browser/user_metrics.h" |
35 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
36 #include "content/public/browser/web_ui.h" | 41 #include "content/public/browser/web_ui.h" |
37 #include "grit/generated_resources.h" | 42 #include "grit/generated_resources.h" |
| 43 #include "net/base/net_util.h" |
38 #include "ui/gfx/image/image.h" | 44 #include "ui/gfx/image/image.h" |
39 | 45 |
40 #if !defined(OS_MACOSX) | 46 #if !defined(OS_MACOSX) |
41 #include "content/public/browser/browser_thread.h" | 47 #include "content/public/browser/browser_thread.h" |
42 #endif | 48 #endif |
43 | 49 |
44 #if defined(OS_CHROMEOS) | 50 #if defined(OS_CHROMEOS) |
45 #include "chrome/browser/chromeos/extensions/file_manager_util.h" | 51 #include "chrome/browser/chromeos/extensions/file_manager_util.h" |
46 #endif | 52 #endif |
47 | 53 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 DOWNLOADS_DOM_EVENT_OPEN_FOLDER = 10, | 86 DOWNLOADS_DOM_EVENT_OPEN_FOLDER = 10, |
81 DOWNLOADS_DOM_EVENT_MAX | 87 DOWNLOADS_DOM_EVENT_MAX |
82 }; | 88 }; |
83 | 89 |
84 void CountDownloadsDOMEvents(DownloadsDOMEvent event) { | 90 void CountDownloadsDOMEvents(DownloadsDOMEvent event) { |
85 UMA_HISTOGRAM_ENUMERATION("Download.DOMEvent", | 91 UMA_HISTOGRAM_ENUMERATION("Download.DOMEvent", |
86 event, | 92 event, |
87 DOWNLOADS_DOM_EVENT_MAX); | 93 DOWNLOADS_DOM_EVENT_MAX); |
88 } | 94 } |
89 | 95 |
| 96 // Returns a string constant to be used as the |danger_type| value in |
| 97 // CreateDownloadItemValue(). We only return strings for DANGEROUS_FILE, |
| 98 // DANGEROUS_URL, DANGEROUS_CONTENT, and UNCOMMON_CONTENT because the |
| 99 // |danger_type| value is only defined if the value of |state| is |DANGEROUS|. |
| 100 const char* GetDangerTypeString(content::DownloadDangerType danger_type) { |
| 101 switch (danger_type) { |
| 102 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: |
| 103 return "DANGEROUS_FILE"; |
| 104 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: |
| 105 return "DANGEROUS_URL"; |
| 106 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: |
| 107 return "DANGEROUS_CONTENT"; |
| 108 case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: |
| 109 return "UNCOMMON_CONTENT"; |
| 110 default: |
| 111 // We shouldn't be returning a danger type string if it is |
| 112 // NOT_DANGEROUS or MAYBE_DANGEROUS_CONTENT. |
| 113 NOTREACHED(); |
| 114 return ""; |
| 115 } |
| 116 } |
| 117 |
| 118 DictionaryValue* CreateDownloadItemValue( |
| 119 content::DownloadItem* download, |
| 120 int id, |
| 121 bool incognito) { |
| 122 DictionaryValue* file_value = new DictionaryValue(); |
| 123 |
| 124 file_value->SetInteger("started", |
| 125 static_cast<int>(download->GetStartTime().ToTimeT())); |
| 126 file_value->SetString("since_string", |
| 127 TimeFormat::RelativeDate(download->GetStartTime(), NULL)); |
| 128 file_value->SetString("date_string", |
| 129 base::TimeFormatShortDate(download->GetStartTime())); |
| 130 file_value->SetInteger("id", id); |
| 131 |
| 132 FilePath download_path(download->GetTargetFilePath()); |
| 133 file_value->Set("file_path", base::CreateFilePathValue(download_path)); |
| 134 file_value->SetString("file_url", |
| 135 net::FilePathToFileURL(download_path).spec()); |
| 136 |
| 137 // Keep file names as LTR. |
| 138 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); |
| 139 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); |
| 140 file_value->SetString("file_name", file_name); |
| 141 file_value->SetString("url", download->GetURL().spec()); |
| 142 file_value->SetBoolean("otr", incognito); |
| 143 file_value->SetInteger("total", static_cast<int>(download->GetTotalBytes())); |
| 144 file_value->SetBoolean("file_externally_removed", |
| 145 download->GetFileExternallyRemoved()); |
| 146 |
| 147 if (download->IsInProgress()) { |
| 148 if (download->GetSafetyState() == content::DownloadItem::DANGEROUS) { |
| 149 file_value->SetString("state", "DANGEROUS"); |
| 150 // These are the only danger states we expect to see (and the UI is |
| 151 // equipped to handle): |
| 152 DCHECK(download->GetDangerType() == |
| 153 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || |
| 154 download->GetDangerType() == |
| 155 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || |
| 156 download->GetDangerType() == |
| 157 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || |
| 158 download->GetDangerType() == |
| 159 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT); |
| 160 const char* danger_type_value = |
| 161 GetDangerTypeString(download->GetDangerType()); |
| 162 file_value->SetString("danger_type", danger_type_value); |
| 163 } else if (download->IsPaused()) { |
| 164 file_value->SetString("state", "PAUSED"); |
| 165 } else { |
| 166 file_value->SetString("state", "IN_PROGRESS"); |
| 167 } |
| 168 |
| 169 file_value->SetString("progress_status_text", |
| 170 download_util::GetProgressStatusText(download)); |
| 171 |
| 172 file_value->SetInteger("percent", |
| 173 static_cast<int>(download->PercentComplete())); |
| 174 file_value->SetInteger("received", |
| 175 static_cast<int>(download->GetReceivedBytes())); |
| 176 } else if (download->IsInterrupted()) { |
| 177 file_value->SetString("state", "INTERRUPTED"); |
| 178 |
| 179 file_value->SetString("progress_status_text", |
| 180 download_util::GetProgressStatusText(download)); |
| 181 |
| 182 file_value->SetInteger("percent", |
| 183 static_cast<int>(download->PercentComplete())); |
| 184 file_value->SetInteger("received", |
| 185 static_cast<int>(download->GetReceivedBytes())); |
| 186 file_value->SetString("last_reason_text", |
| 187 BaseDownloadItemModel::InterruptReasonMessage( |
| 188 download->GetLastReason())); |
| 189 } else if (download->IsCancelled()) { |
| 190 file_value->SetString("state", "CANCELLED"); |
| 191 } else if (download->IsComplete()) { |
| 192 if (download->GetSafetyState() == content::DownloadItem::DANGEROUS) |
| 193 file_value->SetString("state", "DANGEROUS"); |
| 194 else |
| 195 file_value->SetString("state", "COMPLETE"); |
| 196 } else if (download->GetState() == content::DownloadItem::REMOVING) { |
| 197 file_value->SetString("state", "REMOVING"); |
| 198 } else { |
| 199 NOTREACHED() << "state undefined"; |
| 200 } |
| 201 |
| 202 return file_value; |
| 203 } |
| 204 |
| 205 bool IsItemIncognito( |
| 206 int32 download_id, |
| 207 content::DownloadManager* manager, |
| 208 content::DownloadManager* original_manager) { |
| 209 // |original_manager| is only non-NULL if |manager| is incognito. |
| 210 return (original_manager && |
| 211 (manager->GetDownload(download_id) != NULL)); |
| 212 } |
| 213 |
90 } // namespace | 214 } // namespace |
91 | 215 |
92 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) | 216 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) |
93 : search_text_(), | 217 : search_text_(), |
94 download_manager_(dlm), | 218 download_manager_(dlm), |
95 original_profile_download_manager_(NULL), | 219 original_profile_download_manager_(NULL), |
96 initialized_(false), | 220 initialized_(false), |
97 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 221 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
98 // Create our fileicon data source. | 222 // Create our fileicon data source. |
99 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); | 223 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 (*it)->RemoveObserver(this); | 307 (*it)->RemoveObserver(this); |
184 *it = NULL; | 308 *it = NULL; |
185 // A later ModelChanged() notification will change the WebUI's | 309 // A later ModelChanged() notification will change the WebUI's |
186 // view of the downloads list. | 310 // view of the downloads list. |
187 return; | 311 return; |
188 } | 312 } |
189 | 313 |
190 const int id = static_cast<int>(it - download_items_.begin()); | 314 const int id = static_cast<int>(it - download_items_.begin()); |
191 | 315 |
192 ListValue results_value; | 316 ListValue results_value; |
193 results_value.Append(download_util::CreateDownloadItemValue(download, id)); | 317 results_value.Append(CreateDownloadItemValue(download, id, IsItemIncognito( |
| 318 download->GetId(), |
| 319 download_manager_, |
| 320 original_profile_download_manager_))); |
194 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); | 321 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); |
195 } | 322 } |
196 | 323 |
197 // A download has started or been deleted. Query our DownloadManager for the | 324 // A download has started or been deleted. Query our DownloadManager for the |
198 // current set of downloads. | 325 // current set of downloads. |
199 void DownloadsDOMHandler::ModelChanged(content::DownloadManager* manager) { | 326 void DownloadsDOMHandler::ModelChanged(content::DownloadManager* manager) { |
200 DCHECK(manager == download_manager_ || | 327 DCHECK(manager == download_manager_ || |
201 manager == original_profile_download_manager_); | 328 manager == original_profile_download_manager_); |
202 | 329 |
203 ClearDownloadItems(); | 330 ClearDownloadItems(); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 // DownloadsDOMHandler, private: ---------------------------------------------- | 474 // DownloadsDOMHandler, private: ---------------------------------------------- |
348 | 475 |
349 void DownloadsDOMHandler::SendCurrentDownloads() { | 476 void DownloadsDOMHandler::SendCurrentDownloads() { |
350 ListValue results_value; | 477 ListValue results_value; |
351 for (OrderedDownloads::iterator it = download_items_.begin(); | 478 for (OrderedDownloads::iterator it = download_items_.begin(); |
352 it != download_items_.end(); ++it) { | 479 it != download_items_.end(); ++it) { |
353 if (!*it) | 480 if (!*it) |
354 continue; | 481 continue; |
355 int index = static_cast<int>(it - download_items_.begin()); | 482 int index = static_cast<int>(it - download_items_.begin()); |
356 if (index <= kMaxDownloads) | 483 if (index <= kMaxDownloads) |
357 results_value.Append(download_util::CreateDownloadItemValue(*it, index)); | 484 results_value.Append(CreateDownloadItemValue(*it, index, IsItemIncognito( |
| 485 (*it)->GetId(), |
| 486 download_manager_, |
| 487 original_profile_download_manager_))); |
358 } | 488 } |
359 | 489 |
360 web_ui()->CallJavascriptFunction("downloadsList", results_value); | 490 web_ui()->CallJavascriptFunction("downloadsList", results_value); |
361 } | 491 } |
362 | 492 |
363 void DownloadsDOMHandler::ShowDangerPrompt( | 493 void DownloadsDOMHandler::ShowDangerPrompt( |
364 content::DownloadItem* dangerous_item) { | 494 content::DownloadItem* dangerous_item) { |
365 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( | 495 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( |
366 dangerous_item, | 496 dangerous_item, |
367 TabContents::FromWebContents(web_ui()->GetWebContents()), | 497 TabContents::FromWebContents(web_ui()->GetWebContents()), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 } | 534 } |
405 | 535 |
406 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( | 536 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( |
407 const ListValue* args) { | 537 const ListValue* args) { |
408 int id; | 538 int id; |
409 if (ExtractIntegerValue(args, &id)) { | 539 if (ExtractIntegerValue(args, &id)) { |
410 return GetDownloadById(id); | 540 return GetDownloadById(id); |
411 } | 541 } |
412 return NULL; | 542 return NULL; |
413 } | 543 } |
OLD | NEW |