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 // Return a JSON dictionary containing some of the attributes of |download|. |
| 119 // The JSON dictionary will also have a field "id" set to |id|, and a field |
| 120 // "otr" set to |incognito|. |
| 121 DictionaryValue* CreateDownloadItemValue( |
| 122 content::DownloadItem* download, |
| 123 int id, |
| 124 bool incognito) { |
| 125 DictionaryValue* file_value = new DictionaryValue(); |
| 126 |
| 127 file_value->SetInteger( |
| 128 "started", static_cast<int>(download->GetStartTime().ToTimeT())); |
| 129 file_value->SetString( |
| 130 "since_string", TimeFormat::RelativeDate(download->GetStartTime(), NULL)); |
| 131 file_value->SetString( |
| 132 "date_string", base::TimeFormatShortDate(download->GetStartTime())); |
| 133 file_value->SetInteger("id", id); |
| 134 |
| 135 FilePath download_path(download->GetTargetFilePath()); |
| 136 file_value->Set("file_path", base::CreateFilePathValue(download_path)); |
| 137 file_value->SetString("file_url", |
| 138 net::FilePathToFileURL(download_path).spec()); |
| 139 |
| 140 // Keep file names as LTR. |
| 141 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); |
| 142 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); |
| 143 file_value->SetString("file_name", file_name); |
| 144 file_value->SetString("url", download->GetURL().spec()); |
| 145 file_value->SetBoolean("otr", incognito); |
| 146 file_value->SetInteger("total", static_cast<int>(download->GetTotalBytes())); |
| 147 file_value->SetBoolean("file_externally_removed", |
| 148 download->GetFileExternallyRemoved()); |
| 149 |
| 150 if (download->IsInProgress()) { |
| 151 if (download->GetSafetyState() == content::DownloadItem::DANGEROUS) { |
| 152 file_value->SetString("state", "DANGEROUS"); |
| 153 // These are the only danger states we expect to see (and the UI is |
| 154 // equipped to handle): |
| 155 DCHECK(download->GetDangerType() == |
| 156 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || |
| 157 download->GetDangerType() == |
| 158 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || |
| 159 download->GetDangerType() == |
| 160 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || |
| 161 download->GetDangerType() == |
| 162 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT); |
| 163 const char* danger_type_value = |
| 164 GetDangerTypeString(download->GetDangerType()); |
| 165 file_value->SetString("danger_type", danger_type_value); |
| 166 } else if (download->IsPaused()) { |
| 167 file_value->SetString("state", "PAUSED"); |
| 168 } else { |
| 169 file_value->SetString("state", "IN_PROGRESS"); |
| 170 } |
| 171 |
| 172 file_value->SetString("progress_status_text", |
| 173 download_util::GetProgressStatusText(download)); |
| 174 |
| 175 file_value->SetInteger("percent", |
| 176 static_cast<int>(download->PercentComplete())); |
| 177 file_value->SetInteger("received", |
| 178 static_cast<int>(download->GetReceivedBytes())); |
| 179 } else if (download->IsInterrupted()) { |
| 180 file_value->SetString("state", "INTERRUPTED"); |
| 181 |
| 182 file_value->SetString("progress_status_text", |
| 183 download_util::GetProgressStatusText(download)); |
| 184 |
| 185 file_value->SetInteger("percent", |
| 186 static_cast<int>(download->PercentComplete())); |
| 187 file_value->SetInteger("received", |
| 188 static_cast<int>(download->GetReceivedBytes())); |
| 189 file_value->SetString("last_reason_text", |
| 190 BaseDownloadItemModel::InterruptReasonMessage( |
| 191 download->GetLastReason())); |
| 192 } else if (download->IsCancelled()) { |
| 193 file_value->SetString("state", "CANCELLED"); |
| 194 } else if (download->IsComplete()) { |
| 195 if (download->GetSafetyState() == content::DownloadItem::DANGEROUS) |
| 196 file_value->SetString("state", "DANGEROUS"); |
| 197 else |
| 198 file_value->SetString("state", "COMPLETE"); |
| 199 } else { |
| 200 NOTREACHED() << "state undefined"; |
| 201 } |
| 202 |
| 203 return file_value; |
| 204 } |
| 205 |
| 206 // Return true if |download_id| refers to a download that belongs to the |
| 207 // incognito download manager, if one exists. |
| 208 bool IsItemIncognito( |
| 209 int32 download_id, |
| 210 content::DownloadManager* manager, |
| 211 content::DownloadManager* original_manager) { |
| 212 // |original_manager| is only non-NULL if |manager| is incognito. |
| 213 return (original_manager && |
| 214 (manager->GetDownload(download_id) != NULL)); |
| 215 } |
| 216 |
90 } // namespace | 217 } // namespace |
91 | 218 |
92 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) | 219 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) |
93 : search_text_(), | 220 : search_text_(), |
94 download_manager_(dlm), | 221 download_manager_(dlm), |
95 original_profile_download_manager_(NULL), | 222 original_profile_download_manager_(NULL), |
96 initialized_(false), | 223 initialized_(false), |
97 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 224 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
98 // Create our fileicon data source. | 225 // Create our fileicon data source. |
99 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); | 226 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // something other than 'id'). | 302 // something other than 'id'). |
176 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 303 OrderedDownloads::iterator it = std::find(download_items_.begin(), |
177 download_items_.end(), | 304 download_items_.end(), |
178 download); | 305 download); |
179 if (it == download_items_.end()) | 306 if (it == download_items_.end()) |
180 return; | 307 return; |
181 | 308 |
182 const int id = static_cast<int>(it - download_items_.begin()); | 309 const int id = static_cast<int>(it - download_items_.begin()); |
183 | 310 |
184 ListValue results_value; | 311 ListValue results_value; |
185 results_value.Append(download_util::CreateDownloadItemValue(download, id)); | 312 results_value.Append(CreateDownloadItemValue(download, id, IsItemIncognito( |
| 313 download->GetId(), |
| 314 download_manager_, |
| 315 original_profile_download_manager_))); |
186 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); | 316 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); |
187 } | 317 } |
188 | 318 |
189 void DownloadsDOMHandler::OnDownloadDestroyed( | 319 void DownloadsDOMHandler::OnDownloadDestroyed( |
190 content::DownloadItem* download) { | 320 content::DownloadItem* download) { |
191 download->RemoveObserver(this); | 321 download->RemoveObserver(this); |
192 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 322 OrderedDownloads::iterator it = std::find(download_items_.begin(), |
193 download_items_.end(), | 323 download_items_.end(), |
194 download); | 324 download); |
195 if (it != download_items_.end()) | 325 if (it != download_items_.end()) |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 // DownloadsDOMHandler, private: ---------------------------------------------- | 481 // DownloadsDOMHandler, private: ---------------------------------------------- |
352 | 482 |
353 void DownloadsDOMHandler::SendCurrentDownloads() { | 483 void DownloadsDOMHandler::SendCurrentDownloads() { |
354 ListValue results_value; | 484 ListValue results_value; |
355 for (OrderedDownloads::iterator it = download_items_.begin(); | 485 for (OrderedDownloads::iterator it = download_items_.begin(); |
356 it != download_items_.end(); ++it) { | 486 it != download_items_.end(); ++it) { |
357 if (!*it) | 487 if (!*it) |
358 continue; | 488 continue; |
359 int index = static_cast<int>(it - download_items_.begin()); | 489 int index = static_cast<int>(it - download_items_.begin()); |
360 if (index <= kMaxDownloads) | 490 if (index <= kMaxDownloads) |
361 results_value.Append(download_util::CreateDownloadItemValue(*it, index)); | 491 results_value.Append(CreateDownloadItemValue(*it, index, IsItemIncognito( |
| 492 (*it)->GetId(), |
| 493 download_manager_, |
| 494 original_profile_download_manager_))); |
362 } | 495 } |
363 | 496 |
364 web_ui()->CallJavascriptFunction("downloadsList", results_value); | 497 web_ui()->CallJavascriptFunction("downloadsList", results_value); |
365 } | 498 } |
366 | 499 |
367 void DownloadsDOMHandler::ShowDangerPrompt( | 500 void DownloadsDOMHandler::ShowDangerPrompt( |
368 content::DownloadItem* dangerous_item) { | 501 content::DownloadItem* dangerous_item) { |
369 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( | 502 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( |
370 dangerous_item, | 503 dangerous_item, |
371 TabContents::FromWebContents(web_ui()->GetWebContents()), | 504 TabContents::FromWebContents(web_ui()->GetWebContents()), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 } | 541 } |
409 | 542 |
410 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( | 543 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( |
411 const ListValue* args) { | 544 const ListValue* args) { |
412 int id; | 545 int id; |
413 if (ExtractIntegerValue(args, &id)) { | 546 if (ExtractIntegerValue(args, &id)) { |
414 return GetDownloadById(id); | 547 return GetDownloadById(id); |
415 } | 548 } |
416 return NULL; | 549 return NULL; |
417 } | 550 } |
OLD | NEW |