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( | |
James Hawkins
2012/08/13 20:47:53
nit: Document method and parameters.
benjhayden
2012/08/13 21:49:59
Done.
| |
119 content::DownloadItem* download, | |
120 int id, | |
121 bool incognito) { | |
122 DictionaryValue* file_value = new DictionaryValue(); | |
123 | |
124 file_value->SetInteger("started", | |
James Hawkins
2012/08/13 20:47:53
nit: Start of parameter rows must align on the sam
benjhayden
2012/08/13 21:49:59
Done.
| |
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 { | |
197 NOTREACHED() << "state undefined"; | |
198 } | |
199 | |
200 return file_value; | |
201 } | |
202 | |
203 bool IsItemIncognito( | |
James Hawkins
2012/08/13 20:47:53
nit: Document method and parameters.
benjhayden
2012/08/13 21:49:59
Done.
| |
204 int32 download_id, | |
205 content::DownloadManager* manager, | |
206 content::DownloadManager* original_manager) { | |
207 // |original_manager| is only non-NULL if |manager| is incognito. | |
208 return (original_manager && | |
209 (manager->GetDownload(download_id) != NULL)); | |
210 } | |
211 | |
90 } // namespace | 212 } // namespace |
91 | 213 |
92 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) | 214 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) |
93 : search_text_(), | 215 : search_text_(), |
94 download_manager_(dlm), | 216 download_manager_(dlm), |
95 original_profile_download_manager_(NULL), | 217 original_profile_download_manager_(NULL), |
96 initialized_(false), | 218 initialized_(false), |
97 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 219 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
98 // Create our fileicon data source. | 220 // Create our fileicon data source. |
99 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); | 221 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 // something other than 'id'). | 297 // something other than 'id'). |
176 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 298 OrderedDownloads::iterator it = std::find(download_items_.begin(), |
177 download_items_.end(), | 299 download_items_.end(), |
178 download); | 300 download); |
179 if (it == download_items_.end()) | 301 if (it == download_items_.end()) |
180 return; | 302 return; |
181 | 303 |
182 const int id = static_cast<int>(it - download_items_.begin()); | 304 const int id = static_cast<int>(it - download_items_.begin()); |
183 | 305 |
184 ListValue results_value; | 306 ListValue results_value; |
185 results_value.Append(download_util::CreateDownloadItemValue(download, id)); | 307 results_value.Append(CreateDownloadItemValue(download, id, IsItemIncognito( |
308 download->GetId(), | |
309 download_manager_, | |
310 original_profile_download_manager_))); | |
186 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); | 311 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); |
187 } | 312 } |
188 | 313 |
189 void DownloadsDOMHandler::OnDownloadDestroyed( | 314 void DownloadsDOMHandler::OnDownloadDestroyed( |
190 content::DownloadItem* download) { | 315 content::DownloadItem* download) { |
191 download->RemoveObserver(this); | 316 download->RemoveObserver(this); |
192 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 317 OrderedDownloads::iterator it = std::find(download_items_.begin(), |
193 download_items_.end(), | 318 download_items_.end(), |
194 download); | 319 download); |
195 *it = NULL; | 320 *it = NULL; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
350 // DownloadsDOMHandler, private: ---------------------------------------------- | 475 // DownloadsDOMHandler, private: ---------------------------------------------- |
351 | 476 |
352 void DownloadsDOMHandler::SendCurrentDownloads() { | 477 void DownloadsDOMHandler::SendCurrentDownloads() { |
353 ListValue results_value; | 478 ListValue results_value; |
354 for (OrderedDownloads::iterator it = download_items_.begin(); | 479 for (OrderedDownloads::iterator it = download_items_.begin(); |
355 it != download_items_.end(); ++it) { | 480 it != download_items_.end(); ++it) { |
356 if (!*it) | 481 if (!*it) |
357 continue; | 482 continue; |
358 int index = static_cast<int>(it - download_items_.begin()); | 483 int index = static_cast<int>(it - download_items_.begin()); |
359 if (index <= kMaxDownloads) | 484 if (index <= kMaxDownloads) |
360 results_value.Append(download_util::CreateDownloadItemValue(*it, index)); | 485 results_value.Append(CreateDownloadItemValue(*it, index, IsItemIncognito( |
486 (*it)->GetId(), | |
487 download_manager_, | |
488 original_profile_download_manager_))); | |
361 } | 489 } |
362 | 490 |
363 web_ui()->CallJavascriptFunction("downloadsList", results_value); | 491 web_ui()->CallJavascriptFunction("downloadsList", results_value); |
364 } | 492 } |
365 | 493 |
366 void DownloadsDOMHandler::ShowDangerPrompt( | 494 void DownloadsDOMHandler::ShowDangerPrompt( |
367 content::DownloadItem* dangerous_item) { | 495 content::DownloadItem* dangerous_item) { |
368 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( | 496 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( |
369 dangerous_item, | 497 dangerous_item, |
370 TabContents::FromWebContents(web_ui()->GetWebContents()), | 498 TabContents::FromWebContents(web_ui()->GetWebContents()), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 } | 535 } |
408 | 536 |
409 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( | 537 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( |
410 const ListValue* args) { | 538 const ListValue* args) { |
411 int id; | 539 int id; |
412 if (ExtractIntegerValue(args, &id)) { | 540 if (ExtractIntegerValue(args, &id)) { |
413 return GetDownloadById(id); | 541 return GetDownloadById(id); |
414 } | 542 } |
415 return NULL; | 543 return NULL; |
416 } | 544 } |
OLD | NEW |