Chromium Code Reviews| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 #endif | 52 #endif |
| 53 | 53 |
| 54 using content::BrowserContext; | 54 using content::BrowserContext; |
| 55 using content::BrowserThread; | 55 using content::BrowserThread; |
| 56 using content::UserMetricsAction; | 56 using content::UserMetricsAction; |
| 57 | 57 |
| 58 namespace { | 58 namespace { |
| 59 | 59 |
| 60 // Maximum number of downloads to show. TODO(glen): Remove this and instead | 60 // Maximum number of downloads to show. TODO(glen): Remove this and instead |
| 61 // stuff the downloads down the pipe slowly. | 61 // stuff the downloads down the pipe slowly. |
| 62 static const int kMaxDownloads = 150; | 62 static const size_t kMaxDownloads = 150; |
| 63 | 63 |
| 64 // Sort DownloadItems into descending order by their start time. | 64 // Sort DownloadItems into descending order by their start time. |
| 65 class DownloadItemSorter : public std::binary_function<content::DownloadItem*, | 65 class DownloadItemSorter : public std::binary_function<content::DownloadItem*, |
| 66 content::DownloadItem*, | 66 content::DownloadItem*, |
| 67 bool> { | 67 bool> { |
| 68 public: | 68 public: |
| 69 bool operator()(const content::DownloadItem* lhs, | 69 bool operator()(const content::DownloadItem* lhs, |
| 70 const content::DownloadItem* rhs) { | 70 const content::DownloadItem* rhs) { |
| 71 return lhs->GetStartTime() > rhs->GetStartTime(); | 71 return lhs->GetStartTime() > rhs->GetStartTime(); |
| 72 } | 72 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 // NOT_DANGEROUS or MAYBE_DANGEROUS_CONTENT. | 112 // NOT_DANGEROUS or MAYBE_DANGEROUS_CONTENT. |
| 113 NOTREACHED(); | 113 NOTREACHED(); |
| 114 return ""; | 114 return ""; |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 // Return a JSON dictionary containing some of the attributes of |download|. | 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 | 119 // The JSON dictionary will also have a field "id" set to |id|, and a field |
| 120 // "otr" set to |incognito|. | 120 // "otr" set to |incognito|. |
| 121 DictionaryValue* CreateDownloadItemValue( | 121 DictionaryValue* CreateDownloadItemValue( |
| 122 content::DownloadItem* download, | 122 content::DownloadItem* download_item, |
| 123 int id, | |
| 124 bool incognito) { | 123 bool incognito) { |
| 125 DictionaryValue* file_value = new DictionaryValue(); | 124 DictionaryValue* file_value = new DictionaryValue(); |
| 126 | 125 |
| 127 file_value->SetInteger( | 126 file_value->SetInteger( |
| 128 "started", static_cast<int>(download->GetStartTime().ToTimeT())); | 127 "started", static_cast<int>(download_item->GetStartTime().ToTimeT())); |
| 129 file_value->SetString( | 128 file_value->SetString( |
| 130 "since_string", TimeFormat::RelativeDate(download->GetStartTime(), NULL)); | 129 "since_string", TimeFormat::RelativeDate( |
| 130 download_item->GetStartTime(), NULL)); | |
| 131 file_value->SetString( | 131 file_value->SetString( |
| 132 "date_string", base::TimeFormatShortDate(download->GetStartTime())); | 132 "date_string", base::TimeFormatShortDate(download_item->GetStartTime())); |
| 133 file_value->SetInteger("id", id); | 133 file_value->SetInteger("id", download_item->GetId()); |
| 134 | 134 |
| 135 FilePath download_path(download->GetTargetFilePath()); | 135 FilePath download_path(download_item->GetTargetFilePath()); |
| 136 file_value->Set("file_path", base::CreateFilePathValue(download_path)); | 136 file_value->Set("file_path", base::CreateFilePathValue(download_path)); |
| 137 file_value->SetString("file_url", | 137 file_value->SetString("file_url", |
| 138 net::FilePathToFileURL(download_path).spec()); | 138 net::FilePathToFileURL(download_path).spec()); |
| 139 | 139 |
| 140 // Keep file names as LTR. | 140 // Keep file names as LTR. |
| 141 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); | 141 string16 file_name = |
| 142 download_item->GetFileNameToReportUser().LossyDisplayName(); | |
| 142 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); | 143 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); |
| 143 file_value->SetString("file_name", file_name); | 144 file_value->SetString("file_name", file_name); |
| 144 file_value->SetString("url", download->GetURL().spec()); | 145 file_value->SetString("url", download_item->GetURL().spec()); |
| 145 file_value->SetBoolean("otr", incognito); | 146 file_value->SetBoolean("otr", incognito); |
| 146 file_value->SetInteger("total", static_cast<int>(download->GetTotalBytes())); | 147 file_value->SetInteger("total", static_cast<int>( |
| 148 download_item->GetTotalBytes())); | |
| 147 file_value->SetBoolean("file_externally_removed", | 149 file_value->SetBoolean("file_externally_removed", |
| 148 download->GetFileExternallyRemoved()); | 150 download_item->GetFileExternallyRemoved()); |
| 149 | 151 |
| 150 if (download->IsInProgress()) { | 152 if (download_item->IsInProgress()) { |
| 151 if (download->GetSafetyState() == content::DownloadItem::DANGEROUS) { | 153 if (download_item->GetSafetyState() == content::DownloadItem::DANGEROUS) { |
| 152 file_value->SetString("state", "DANGEROUS"); | 154 file_value->SetString("state", "DANGEROUS"); |
| 153 // These are the only danger states we expect to see (and the UI is | 155 // These are the only danger states we expect to see (and the UI is |
| 154 // equipped to handle): | 156 // equipped to handle): |
| 155 DCHECK(download->GetDangerType() == | 157 DCHECK(download_item->GetDangerType() == |
| 156 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || | 158 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || |
| 157 download->GetDangerType() == | 159 download_item->GetDangerType() == |
| 158 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || | 160 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || |
| 159 download->GetDangerType() == | 161 download_item->GetDangerType() == |
| 160 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || | 162 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || |
| 161 download->GetDangerType() == | 163 download_item->GetDangerType() == |
| 162 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT); | 164 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT); |
| 163 const char* danger_type_value = | 165 const char* danger_type_value = |
| 164 GetDangerTypeString(download->GetDangerType()); | 166 GetDangerTypeString(download_item->GetDangerType()); |
| 165 file_value->SetString("danger_type", danger_type_value); | 167 file_value->SetString("danger_type", danger_type_value); |
| 166 } else if (download->IsPaused()) { | 168 } else if (download_item->IsPaused()) { |
| 167 file_value->SetString("state", "PAUSED"); | 169 file_value->SetString("state", "PAUSED"); |
| 168 } else { | 170 } else { |
| 169 file_value->SetString("state", "IN_PROGRESS"); | 171 file_value->SetString("state", "IN_PROGRESS"); |
| 170 } | 172 } |
| 171 | 173 |
| 172 file_value->SetString("progress_status_text", | 174 file_value->SetString("progress_status_text", |
| 173 download_util::GetProgressStatusText(download)); | 175 download_util::GetProgressStatusText(download_item)); |
| 174 | 176 |
| 175 file_value->SetInteger("percent", | 177 file_value->SetInteger("percent", |
| 176 static_cast<int>(download->PercentComplete())); | 178 static_cast<int>(download_item->PercentComplete())); |
| 177 file_value->SetInteger("received", | 179 file_value->SetInteger("received", |
| 178 static_cast<int>(download->GetReceivedBytes())); | 180 static_cast<int>(download_item->GetReceivedBytes())); |
| 179 } else if (download->IsInterrupted()) { | 181 } else if (download_item->IsInterrupted()) { |
| 180 file_value->SetString("state", "INTERRUPTED"); | 182 file_value->SetString("state", "INTERRUPTED"); |
| 181 | 183 |
| 182 file_value->SetString("progress_status_text", | 184 file_value->SetString("progress_status_text", |
| 183 download_util::GetProgressStatusText(download)); | 185 download_util::GetProgressStatusText(download_item)); |
| 184 | 186 |
| 185 file_value->SetInteger("percent", | 187 file_value->SetInteger("percent", |
| 186 static_cast<int>(download->PercentComplete())); | 188 static_cast<int>(download_item->PercentComplete())); |
| 187 file_value->SetInteger("received", | 189 file_value->SetInteger("received", |
| 188 static_cast<int>(download->GetReceivedBytes())); | 190 static_cast<int>(download_item->GetReceivedBytes())); |
| 189 file_value->SetString("last_reason_text", | 191 file_value->SetString("last_reason_text", |
| 190 BaseDownloadItemModel::InterruptReasonMessage( | 192 BaseDownloadItemModel::InterruptReasonMessage( |
| 191 download->GetLastReason())); | 193 download_item->GetLastReason())); |
| 192 } else if (download->IsCancelled()) { | 194 } else if (download_item->IsCancelled()) { |
| 193 file_value->SetString("state", "CANCELLED"); | 195 file_value->SetString("state", "CANCELLED"); |
| 194 } else if (download->IsComplete()) { | 196 } else if (download_item->IsComplete()) { |
| 195 if (download->GetSafetyState() == content::DownloadItem::DANGEROUS) | 197 if (download_item->GetSafetyState() == content::DownloadItem::DANGEROUS) |
| 196 file_value->SetString("state", "DANGEROUS"); | 198 file_value->SetString("state", "DANGEROUS"); |
| 197 else | 199 else |
| 198 file_value->SetString("state", "COMPLETE"); | 200 file_value->SetString("state", "COMPLETE"); |
| 199 } else { | 201 } else { |
| 200 NOTREACHED() << "state undefined"; | 202 NOTREACHED() << "state undefined"; |
| 201 } | 203 } |
| 202 | 204 |
| 203 return file_value; | 205 return file_value; |
| 204 } | 206 } |
| 205 | 207 |
| 206 // Return true if |download_id| refers to a download that belongs to the | 208 // Return true if |download_id| refers to a download that belongs to the |
| 207 // incognito download manager, if one exists. | 209 // incognito download manager, if one exists. |
| 208 bool IsItemIncognito( | 210 bool IsItemIncognito( |
| 209 int32 download_id, | 211 int32 download_id, |
| 210 content::DownloadManager* manager, | 212 content::DownloadManager* manager, |
| 211 content::DownloadManager* original_manager) { | 213 content::DownloadManager* original_manager) { |
| 212 // |original_manager| is only non-NULL if |manager| is incognito. | 214 // |original_manager| is only non-NULL if |manager| is incognito. |
| 213 return (original_manager && | 215 return (original_manager && |
| 214 (manager->GetDownload(download_id) != NULL)); | 216 (manager->GetDownload(download_id) != NULL)); |
| 215 } | 217 } |
| 216 | 218 |
| 219 // Filter out extension downloads and downloads that don't have a filename yet. | |
| 220 bool IsDownloadDisplayable(const content::DownloadItem& item) { | |
| 221 return (!download_crx_util::IsExtensionDownload(item) && | |
| 222 !item.GetFileNameToReportUser().empty() && | |
| 223 !item.GetTargetFilePath().empty()); | |
| 224 } | |
| 225 | |
| 217 } // namespace | 226 } // namespace |
| 218 | 227 |
| 219 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) | 228 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) |
| 220 : search_text_(), | 229 : search_text_(), |
| 221 download_manager_(dlm), | 230 download_manager_(dlm), |
| 222 original_profile_download_manager_(NULL), | 231 original_profile_download_manager_(NULL), |
| 223 initialized_(false), | 232 update_scheduled_(false), |
| 224 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 233 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 225 // Create our fileicon data source. | 234 // Create our fileicon data source. |
| 226 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); | 235 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); |
| 227 ChromeURLDataManager::AddDataSource(profile, new FileIconSource()); | 236 ChromeURLDataManager::AddDataSource(profile, new FileIconSource()); |
| 228 | 237 |
| 229 // Figure out our parent DownloadManager, if any. | 238 // Observe the DownloadManagers. |
| 230 Profile* original_profile = profile->GetOriginalProfile(); | 239 download_manager_->AddObserver(this); |
| 231 if (original_profile != profile) { | 240 if (profile->IsOffTheRecord()) { |
| 232 original_profile_download_manager_ = | 241 original_profile_download_manager_ = |
| 233 BrowserContext::GetDownloadManager(original_profile); | 242 BrowserContext::GetDownloadManager(profile->GetOriginalProfile()); |
| 243 original_profile_download_manager_->AddObserver(this); | |
| 244 } | |
| 245 | |
| 246 // Observe all the DownloadItems. | |
| 247 content::DownloadManager::DownloadVector downloads; | |
| 248 SearchDownloads(&downloads); | |
| 249 for (content::DownloadManager::DownloadVector::const_iterator | |
| 250 iter = downloads.begin(); | |
| 251 iter != downloads.end(); ++iter) { | |
| 252 (*iter)->AddObserver(this); | |
| 253 observing_items_.insert(*iter); | |
| 234 } | 254 } |
| 235 } | 255 } |
| 236 | 256 |
| 237 DownloadsDOMHandler::~DownloadsDOMHandler() { | 257 DownloadsDOMHandler::~DownloadsDOMHandler() { |
| 238 ClearDownloadItems(); | 258 for (DownloadSet::const_iterator it = observing_items_.begin(); |
| 259 it != observing_items_.end(); ++it) { | |
| 260 (*it)->RemoveObserver(this); | |
| 261 } | |
| 262 observing_items_.clear(); | |
| 239 download_manager_->RemoveObserver(this); | 263 download_manager_->RemoveObserver(this); |
| 240 if (original_profile_download_manager_) | 264 if (original_profile_download_manager_) { |
| 241 original_profile_download_manager_->RemoveObserver(this); | 265 original_profile_download_manager_->RemoveObserver(this); |
| 266 } | |
| 242 } | 267 } |
| 243 | 268 |
| 244 // DownloadsDOMHandler, public: ----------------------------------------------- | 269 // DownloadsDOMHandler, public: ----------------------------------------------- |
| 245 | 270 |
| 246 void DownloadsDOMHandler::OnPageLoaded(const base::ListValue* args) { | 271 void DownloadsDOMHandler::OnPageLoaded(const base::ListValue* args) { |
| 247 if (initialized_) | 272 SendCurrentDownloads(); |
| 248 return; | |
| 249 initialized_ = true; | |
| 250 | |
| 251 download_manager_->AddObserver(this); | |
| 252 if (original_profile_download_manager_) | |
| 253 original_profile_download_manager_->AddObserver(this); | |
| 254 } | 273 } |
| 255 | 274 |
| 256 void DownloadsDOMHandler::RegisterMessages() { | 275 void DownloadsDOMHandler::RegisterMessages() { |
| 257 web_ui()->RegisterMessageCallback("onPageLoaded", | 276 web_ui()->RegisterMessageCallback("onPageLoaded", |
| 258 base::Bind(&DownloadsDOMHandler::OnPageLoaded, | 277 base::Bind(&DownloadsDOMHandler::OnPageLoaded, |
| 259 base::Unretained(this))); | 278 weak_ptr_factory_.GetWeakPtr())); |
| 260 web_ui()->RegisterMessageCallback("getDownloads", | 279 web_ui()->RegisterMessageCallback("getDownloads", |
| 261 base::Bind(&DownloadsDOMHandler::HandleGetDownloads, | 280 base::Bind(&DownloadsDOMHandler::HandleGetDownloads, |
| 262 base::Unretained(this))); | 281 weak_ptr_factory_.GetWeakPtr())); |
| 263 web_ui()->RegisterMessageCallback("openFile", | 282 web_ui()->RegisterMessageCallback("openFile", |
| 264 base::Bind(&DownloadsDOMHandler::HandleOpenFile, | 283 base::Bind(&DownloadsDOMHandler::HandleOpenFile, |
| 265 base::Unretained(this))); | 284 weak_ptr_factory_.GetWeakPtr())); |
| 266 web_ui()->RegisterMessageCallback("drag", | 285 web_ui()->RegisterMessageCallback("drag", |
| 267 base::Bind(&DownloadsDOMHandler::HandleDrag, | 286 base::Bind(&DownloadsDOMHandler::HandleDrag, |
| 268 base::Unretained(this))); | 287 weak_ptr_factory_.GetWeakPtr())); |
| 269 web_ui()->RegisterMessageCallback("saveDangerous", | 288 web_ui()->RegisterMessageCallback("saveDangerous", |
| 270 base::Bind(&DownloadsDOMHandler::HandleSaveDangerous, | 289 base::Bind(&DownloadsDOMHandler::HandleSaveDangerous, |
| 271 base::Unretained(this))); | 290 weak_ptr_factory_.GetWeakPtr())); |
| 272 web_ui()->RegisterMessageCallback("discardDangerous", | 291 web_ui()->RegisterMessageCallback("discardDangerous", |
| 273 base::Bind(&DownloadsDOMHandler::HandleDiscardDangerous, | 292 base::Bind(&DownloadsDOMHandler::HandleDiscardDangerous, |
| 274 base::Unretained(this))); | 293 weak_ptr_factory_.GetWeakPtr())); |
| 275 web_ui()->RegisterMessageCallback("show", | 294 web_ui()->RegisterMessageCallback("show", |
| 276 base::Bind(&DownloadsDOMHandler::HandleShow, | 295 base::Bind(&DownloadsDOMHandler::HandleShow, |
| 277 base::Unretained(this))); | 296 weak_ptr_factory_.GetWeakPtr())); |
| 278 web_ui()->RegisterMessageCallback("togglepause", | 297 web_ui()->RegisterMessageCallback("togglepause", |
| 279 base::Bind(&DownloadsDOMHandler::HandlePause, | 298 base::Bind(&DownloadsDOMHandler::HandlePause, |
| 280 base::Unretained(this))); | 299 weak_ptr_factory_.GetWeakPtr())); |
| 281 web_ui()->RegisterMessageCallback("resume", | 300 web_ui()->RegisterMessageCallback("resume", |
| 282 base::Bind(&DownloadsDOMHandler::HandlePause, | 301 base::Bind(&DownloadsDOMHandler::HandlePause, |
| 283 base::Unretained(this))); | 302 weak_ptr_factory_.GetWeakPtr())); |
| 284 web_ui()->RegisterMessageCallback("remove", | 303 web_ui()->RegisterMessageCallback("remove", |
| 285 base::Bind(&DownloadsDOMHandler::HandleRemove, | 304 base::Bind(&DownloadsDOMHandler::HandleRemove, |
| 286 base::Unretained(this))); | 305 weak_ptr_factory_.GetWeakPtr())); |
| 287 web_ui()->RegisterMessageCallback("cancel", | 306 web_ui()->RegisterMessageCallback("cancel", |
| 288 base::Bind(&DownloadsDOMHandler::HandleCancel, | 307 base::Bind(&DownloadsDOMHandler::HandleCancel, |
| 289 base::Unretained(this))); | 308 weak_ptr_factory_.GetWeakPtr())); |
| 290 web_ui()->RegisterMessageCallback("clearAll", | 309 web_ui()->RegisterMessageCallback("clearAll", |
| 291 base::Bind(&DownloadsDOMHandler::HandleClearAll, | 310 base::Bind(&DownloadsDOMHandler::HandleClearAll, |
| 292 base::Unretained(this))); | 311 weak_ptr_factory_.GetWeakPtr())); |
| 293 web_ui()->RegisterMessageCallback("openDownloadsFolder", | 312 web_ui()->RegisterMessageCallback("openDownloadsFolder", |
| 294 base::Bind(&DownloadsDOMHandler::HandleOpenDownloadsFolder, | 313 base::Bind(&DownloadsDOMHandler::HandleOpenDownloadsFolder, |
| 295 base::Unretained(this))); | 314 weak_ptr_factory_.GetWeakPtr())); |
| 296 } | 315 } |
| 297 | 316 |
| 298 void DownloadsDOMHandler::OnDownloadUpdated(content::DownloadItem* download) { | 317 void DownloadsDOMHandler::OnDownloadCreated( |
| 299 // Get the id for the download. Our downloads are sorted latest to first, | 318 content::DownloadManager* manager, content::DownloadItem* download_item) { |
| 300 // and the id is the index into that list. We should be careful of sync | 319 // DownloadsDOMHandler observes all items, and only chooses which downloads to |
| 301 // errors between the UI and the download_items_ list (we may wish to use | 320 // display in SendCurrentDownloads() and OnDownloadUpdated() using |
| 302 // something other than 'id'). | 321 // IsDownloadDisplayable(). |
| 303 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 322 download_item->AddObserver(this); |
| 304 download_items_.end(), | 323 observing_items_.insert(download_item); |
| 305 download); | 324 if (IsDownloadDisplayable(*download_item) && |
|
asanka
2012/08/17 21:19:49
OnDownloadCreated() is also called for temporary d
benjhayden
2012/08/17 21:36:30
Done.
| |
| 306 if (it == download_items_.end()) | 325 !update_scheduled_) { |
| 307 return; | 326 // Don't call SendCurrentDownloads() every time anything changes. Batch them |
| 327 // together instead. We may handle hundreds of OnDownloadCreated() calls in | |
| 328 // a single UI message loop iteration when the history is loaded. | |
| 329 update_scheduled_ = true; | |
| 330 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 331 base::Bind(&DownloadsDOMHandler::SendCurrentDownloads, | |
| 332 weak_ptr_factory_.GetWeakPtr())); | |
| 333 } | |
| 334 } | |
| 308 | 335 |
| 309 const int id = static_cast<int>(it - download_items_.begin()); | 336 void DownloadsDOMHandler::OnDownloadUpdated( |
| 310 | 337 content::DownloadItem* download_item) { |
| 311 ListValue results_value; | 338 if (IsDownloadDisplayable(*download_item)) { |
| 312 results_value.Append(CreateDownloadItemValue(download, id, IsItemIncognito( | 339 base::ListValue results_value; |
| 313 download->GetId(), | 340 results_value.Append(CreateDownloadItemValue(download_item, IsItemIncognito( |
| 314 download_manager_, | 341 download_item->GetId(), |
| 315 original_profile_download_manager_))); | 342 download_manager_, |
| 316 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); | 343 original_profile_download_manager_))); |
| 344 CallDownloadUpdated(results_value); | |
| 345 } | |
| 317 } | 346 } |
| 318 | 347 |
| 319 void DownloadsDOMHandler::OnDownloadDestroyed( | 348 void DownloadsDOMHandler::OnDownloadDestroyed( |
| 320 content::DownloadItem* download) { | 349 content::DownloadItem* download_item) { |
| 321 download->RemoveObserver(this); | 350 download_item->RemoveObserver(this); |
| 322 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 351 observing_items_.erase(download_item); |
| 323 download_items_.end(), | 352 // Don't call SendCurrentDownloads() every time anything changes. Batch them |
| 324 download); | 353 // together instead. We may handle hundreds of OnDownloadDestroyed() calls in |
| 325 if (it != download_items_.end()) | 354 // a single UI message loop iteration when the user Clears All downloads. |
| 326 *it = NULL; | 355 if (!update_scheduled_) { |
| 327 // A later ModelChanged() notification will change the WebUI's | 356 update_scheduled_ = true; |
| 328 // view of the downloads list. | 357 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 329 } | 358 base::Bind(&DownloadsDOMHandler::SendCurrentDownloads, |
| 330 | 359 weak_ptr_factory_.GetWeakPtr())); |
| 331 // A download has started or been deleted. Query our DownloadManager for the | |
| 332 // current set of downloads. | |
| 333 void DownloadsDOMHandler::ModelChanged(content::DownloadManager* manager) { | |
| 334 DCHECK(manager == download_manager_ || | |
| 335 manager == original_profile_download_manager_); | |
| 336 | |
| 337 ClearDownloadItems(); | |
| 338 download_manager_->SearchDownloads(WideToUTF16(search_text_), | |
| 339 &download_items_); | |
| 340 // If we have a parent DownloadManager, let it add its downloads to the | |
| 341 // results. | |
| 342 if (original_profile_download_manager_) { | |
| 343 original_profile_download_manager_->SearchDownloads( | |
| 344 WideToUTF16(search_text_), &download_items_); | |
| 345 } | 360 } |
| 346 | |
| 347 sort(download_items_.begin(), download_items_.end(), DownloadItemSorter()); | |
| 348 | |
| 349 // Remove any extension downloads. | |
| 350 for (size_t i = 0; i < download_items_.size();) { | |
| 351 if (download_crx_util::IsExtensionDownload(*download_items_[i])) | |
| 352 download_items_.erase(download_items_.begin() + i); | |
| 353 else | |
| 354 i++; | |
| 355 } | |
| 356 | |
| 357 // Add ourself to all download items as an observer. | |
| 358 for (OrderedDownloads::iterator it = download_items_.begin(); | |
| 359 it != download_items_.end(); ++it) { | |
| 360 if (static_cast<int>(it - download_items_.begin()) > kMaxDownloads) | |
| 361 break; | |
| 362 | |
| 363 // We should never see anything that isn't already in the history. | |
| 364 DCHECK(*it); | |
| 365 DCHECK((*it)->IsPersisted()); | |
| 366 | |
| 367 (*it)->AddObserver(this); | |
| 368 } | |
| 369 | |
| 370 SendCurrentDownloads(); | |
| 371 } | 361 } |
| 372 | 362 |
| 373 void DownloadsDOMHandler::ManagerGoingDown(content::DownloadManager* manager) { | 363 void DownloadsDOMHandler::ManagerGoingDown(content::DownloadManager* manager) { |
| 374 // This should never happen. The lifetime of the DownloadsDOMHandler | 364 // This should never happen. The lifetime of the DownloadsDOMHandler |
| 375 // is tied to the tab in which downloads.html is displayed, which cannot | 365 // is tied to the tab in which downloads.html is displayed, which cannot |
| 376 // outlive the Browser that contains it, which cannot outlive the Profile | 366 // outlive the Browser that contains it, which cannot outlive the Profile |
| 377 // it is associated with. If that profile is an incognito profile, | 367 // it is associated with. If that profile is an incognito profile, |
| 378 // it cannot outlive its original profile. Thus this class should be | 368 // it cannot outlive its original profile. Thus this class should be |
| 379 // destroyed before a ManagerGoingDown() notification occurs. | 369 // destroyed before a ManagerGoingDown() notification occurs. |
| 380 NOTREACHED(); | 370 NOTREACHED(); |
| 381 } | 371 } |
| 382 | 372 |
| 383 void DownloadsDOMHandler::HandleGetDownloads(const ListValue* args) { | 373 void DownloadsDOMHandler::HandleGetDownloads(const base::ListValue* args) { |
| 384 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_GET_DOWNLOADS); | 374 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_GET_DOWNLOADS); |
| 385 std::wstring new_search = UTF16ToWideHack(ExtractStringValue(args)); | 375 search_text_ = ExtractStringValue(args); |
| 386 if (search_text_.compare(new_search) != 0) { | 376 SendCurrentDownloads(); |
| 387 search_text_ = new_search; | |
| 388 ModelChanged(download_manager_); | |
| 389 } else { | |
| 390 SendCurrentDownloads(); | |
| 391 } | |
| 392 | |
| 393 download_manager_->CheckForHistoryFilesRemoval(); | 377 download_manager_->CheckForHistoryFilesRemoval(); |
| 394 } | 378 } |
| 395 | 379 |
| 396 void DownloadsDOMHandler::HandleOpenFile(const ListValue* args) { | 380 void DownloadsDOMHandler::HandleOpenFile(const base::ListValue* args) { |
| 397 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FILE); | 381 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FILE); |
| 398 content::DownloadItem* file = GetDownloadByValue(args); | 382 content::DownloadItem* file = GetDownloadByValue(args); |
| 399 if (file) | 383 if (file) |
| 400 file->OpenDownload(); | 384 file->OpenDownload(); |
| 401 } | 385 } |
| 402 | 386 |
| 403 void DownloadsDOMHandler::HandleDrag(const ListValue* args) { | 387 void DownloadsDOMHandler::HandleDrag(const base::ListValue* args) { |
| 404 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG); | 388 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG); |
| 405 content::DownloadItem* file = GetDownloadByValue(args); | 389 content::DownloadItem* file = GetDownloadByValue(args); |
| 406 if (file) { | 390 content::WebContents* web_contents = GetWebUIWebContents(); |
| 407 IconManager* im = g_browser_process->icon_manager(); | 391 if (!file || !web_contents) |
| 408 gfx::Image* icon = im->LookupIcon(file->GetUserVerifiedFilePath(), | 392 return; |
| 409 IconLoader::NORMAL); | 393 gfx::Image* icon = g_browser_process->icon_manager()->LookupIcon( |
| 410 gfx::NativeView view = web_ui()->GetWebContents()->GetNativeView(); | 394 file->GetUserVerifiedFilePath(), IconLoader::NORMAL); |
| 411 { | 395 gfx::NativeView view = web_contents->GetNativeView(); |
| 412 // Enable nested tasks during DnD, while |DragDownload()| blocks. | 396 { |
| 413 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 397 // Enable nested tasks during DnD, while |DragDownload()| blocks. |
| 414 download_util::DragDownload(file, icon, view); | 398 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 415 } | 399 download_util::DragDownload(file, icon, view); |
| 416 } | 400 } |
| 417 } | 401 } |
| 418 | 402 |
| 419 void DownloadsDOMHandler::HandleSaveDangerous(const ListValue* args) { | 403 void DownloadsDOMHandler::HandleSaveDangerous(const base::ListValue* args) { |
| 420 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); | 404 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); |
| 421 content::DownloadItem* file = GetDownloadByValue(args); | 405 content::DownloadItem* file = GetDownloadByValue(args); |
| 422 if (file) | 406 if (file) |
| 423 ShowDangerPrompt(file); | 407 ShowDangerPrompt(file); |
| 424 // TODO(benjhayden): else ModelChanged()? Downloads might be able to disappear | |
| 425 // out from under us, so update our idea of the downloads as soon as possible. | |
| 426 } | 408 } |
| 427 | 409 |
| 428 void DownloadsDOMHandler::HandleDiscardDangerous(const ListValue* args) { | 410 void DownloadsDOMHandler::HandleDiscardDangerous(const base::ListValue* args) { |
| 429 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS); | 411 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS); |
| 430 content::DownloadItem* file = GetDownloadByValue(args); | 412 content::DownloadItem* file = GetDownloadByValue(args); |
| 431 if (file) | 413 if (file) |
| 432 file->Delete(content::DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 414 file->Delete(content::DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 433 } | 415 } |
| 434 | 416 |
| 435 void DownloadsDOMHandler::HandleShow(const ListValue* args) { | 417 void DownloadsDOMHandler::HandleShow(const base::ListValue* args) { |
| 436 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SHOW); | 418 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SHOW); |
| 437 content::DownloadItem* file = GetDownloadByValue(args); | 419 content::DownloadItem* file = GetDownloadByValue(args); |
| 438 if (file) | 420 if (file) |
| 439 file->ShowDownloadInShell(); | 421 file->ShowDownloadInShell(); |
| 440 } | 422 } |
| 441 | 423 |
| 442 void DownloadsDOMHandler::HandlePause(const ListValue* args) { | 424 void DownloadsDOMHandler::HandlePause(const base::ListValue* args) { |
| 443 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); | 425 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); |
| 444 content::DownloadItem* file = GetDownloadByValue(args); | 426 content::DownloadItem* file = GetDownloadByValue(args); |
| 445 if (file) | 427 if (file) |
| 446 file->TogglePause(); | 428 file->TogglePause(); |
| 447 } | 429 } |
| 448 | 430 |
| 449 void DownloadsDOMHandler::HandleRemove(const ListValue* args) { | 431 void DownloadsDOMHandler::HandleRemove(const base::ListValue* args) { |
| 450 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); | 432 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); |
| 451 content::DownloadItem* file = GetDownloadByValue(args); | 433 content::DownloadItem* file = GetDownloadByValue(args); |
| 452 if (file) { | 434 if (file) { |
| 453 DCHECK(file->IsPersisted()); | 435 DCHECK(file->IsPersisted()); |
| 454 file->Remove(); | 436 file->Remove(); |
| 455 } | 437 } |
| 456 } | 438 } |
| 457 | 439 |
| 458 void DownloadsDOMHandler::HandleCancel(const ListValue* args) { | 440 void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) { |
| 459 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); | 441 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); |
| 460 content::DownloadItem* file = GetDownloadByValue(args); | 442 content::DownloadItem* file = GetDownloadByValue(args); |
| 461 if (file) | 443 if (file) |
| 462 file->Cancel(true); | 444 file->Cancel(true); |
| 463 } | 445 } |
| 464 | 446 |
| 465 void DownloadsDOMHandler::HandleClearAll(const ListValue* args) { | 447 void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) { |
| 466 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL); | 448 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL); |
| 467 download_manager_->RemoveAllDownloads(); | 449 download_manager_->RemoveAllDownloads(); |
| 468 | 450 |
| 469 // If this is an incognito downloader, clear All should clear main download | 451 // If this is an incognito downloader, clear All should clear main download |
| 470 // manager as well. | 452 // manager as well. |
| 471 if (original_profile_download_manager_) | 453 if (original_profile_download_manager_) |
| 472 original_profile_download_manager_->RemoveAllDownloads(); | 454 original_profile_download_manager_->RemoveAllDownloads(); |
| 473 } | 455 } |
| 474 | 456 |
| 475 void DownloadsDOMHandler::HandleOpenDownloadsFolder(const ListValue* args) { | 457 void DownloadsDOMHandler::HandleOpenDownloadsFolder( |
| 458 const base::ListValue* args) { | |
| 476 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER); | 459 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER); |
| 477 platform_util::OpenItem( | 460 platform_util::OpenItem( |
| 478 DownloadPrefs::FromDownloadManager(download_manager_)->DownloadPath()); | 461 DownloadPrefs::FromDownloadManager(download_manager_)->DownloadPath()); |
| 479 } | 462 } |
| 480 | 463 |
| 481 // DownloadsDOMHandler, private: ---------------------------------------------- | 464 // DownloadsDOMHandler, private: ---------------------------------------------- |
| 482 | 465 |
| 483 void DownloadsDOMHandler::SendCurrentDownloads() { | 466 void DownloadsDOMHandler::SendCurrentDownloads() { |
| 484 ListValue results_value; | 467 update_scheduled_ = false; |
| 485 for (OrderedDownloads::iterator it = download_items_.begin(); | 468 content::DownloadManager::DownloadVector downloads; |
| 486 it != download_items_.end(); ++it) { | 469 SearchDownloads(&downloads); |
| 487 if (!*it) | 470 sort(downloads.begin(), downloads.end(), DownloadItemSorter()); |
| 488 continue; | 471 base::ListValue results_value; |
| 489 int index = static_cast<int>(it - download_items_.begin()); | 472 for (content::DownloadManager::DownloadVector::const_iterator |
| 490 if (index <= kMaxDownloads) | 473 iter = downloads.begin(); |
| 491 results_value.Append(CreateDownloadItemValue(*it, index, IsItemIncognito( | 474 iter != downloads.end(); ++iter) { |
| 492 (*it)->GetId(), | 475 if (IsDownloadDisplayable(**iter)) { |
| 476 results_value.Append(CreateDownloadItemValue(*iter, IsItemIncognito( | |
| 477 (*iter)->GetId(), | |
| 493 download_manager_, | 478 download_manager_, |
| 494 original_profile_download_manager_))); | 479 original_profile_download_manager_))); |
| 480 } | |
| 481 if (results_value.GetSize() == kMaxDownloads) | |
| 482 break; | |
| 495 } | 483 } |
| 484 CallDownloadsList(results_value); | |
| 485 } | |
| 496 | 486 |
| 497 web_ui()->CallJavascriptFunction("downloadsList", results_value); | 487 void DownloadsDOMHandler::SearchDownloads( |
| 488 content::DownloadManager::DownloadVector* downloads) { | |
| 489 download_manager_->SearchDownloads(search_text_, downloads); | |
| 490 if (original_profile_download_manager_) { | |
| 491 original_profile_download_manager_->SearchDownloads( | |
| 492 search_text_, downloads); | |
| 493 } | |
| 498 } | 494 } |
| 499 | 495 |
| 500 void DownloadsDOMHandler::ShowDangerPrompt( | 496 void DownloadsDOMHandler::ShowDangerPrompt( |
| 501 content::DownloadItem* dangerous_item) { | 497 content::DownloadItem* dangerous_item) { |
| 502 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( | 498 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( |
| 503 dangerous_item, | 499 dangerous_item, |
| 504 TabContents::FromWebContents(web_ui()->GetWebContents()), | 500 TabContents::FromWebContents(GetWebUIWebContents()), |
| 505 base::Bind(&DownloadsDOMHandler::DangerPromptAccepted, | 501 base::Bind(&DownloadsDOMHandler::DangerPromptAccepted, |
| 506 weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()), | 502 weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()), |
| 507 base::Closure()); | 503 base::Closure()); |
| 508 // danger_prompt will delete itself. | 504 // danger_prompt will delete itself. |
| 509 DCHECK(danger_prompt); | 505 DCHECK(danger_prompt); |
| 510 } | 506 } |
| 511 | 507 |
| 512 void DownloadsDOMHandler::DangerPromptAccepted(int download_id) { | 508 void DownloadsDOMHandler::DangerPromptAccepted(int download_id) { |
| 513 content::DownloadItem* item = download_manager_->GetActiveDownloadItem( | 509 content::DownloadItem* item = download_manager_->GetActiveDownloadItem( |
| 514 download_id); | 510 download_id); |
| 515 if (!item) | 511 if (!item) |
| 516 return; | 512 return; |
| 517 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); | 513 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); |
| 518 item->DangerousDownloadValidated(); | 514 item->DangerousDownloadValidated(); |
| 519 } | 515 } |
| 520 | 516 |
| 521 void DownloadsDOMHandler::ClearDownloadItems() { | 517 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( |
| 522 // Clear out old state and remove self as observer for each download. | 518 const base::ListValue* args) { |
| 523 for (OrderedDownloads::iterator it = download_items_.begin(); | 519 int id = -1; |
| 524 it != download_items_.end(); ++it) { | 520 if (!ExtractIntegerValue(args, &id)) |
| 525 if (!*it) | 521 return NULL; |
| 526 continue; | 522 content::DownloadItem* download_item = download_manager_->GetDownload(id); |
| 527 (*it)->RemoveObserver(this); | 523 if (download_item == NULL) { |
| 524 download_item = original_profile_download_manager_->GetDownload(id); | |
| 528 } | 525 } |
| 529 download_items_.clear(); | 526 return download_item; |
| 530 } | 527 } |
| 531 | 528 |
| 532 content::DownloadItem* DownloadsDOMHandler::GetDownloadById(int id) { | 529 content::WebContents* DownloadsDOMHandler::GetWebUIWebContents() { |
| 533 for (OrderedDownloads::iterator it = download_items_.begin(); | 530 return web_ui()->GetWebContents(); |
| 534 it != download_items_.end(); ++it) { | |
| 535 if (static_cast<int>(it - download_items_.begin() == id)) { | |
| 536 return (*it); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 return NULL; | |
| 541 } | 531 } |
| 542 | 532 |
| 543 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( | 533 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { |
| 544 const ListValue* args) { | 534 web_ui()->CallJavascriptFunction("downloadsList", downloads); |
| 545 int id; | |
| 546 if (ExtractIntegerValue(args, &id)) { | |
| 547 return GetDownloadById(id); | |
| 548 } | |
| 549 return NULL; | |
| 550 } | 535 } |
| 536 | |
| 537 void DownloadsDOMHandler::CallDownloadUpdated( | |
| 538 const base::ListValue& download_item) { | |
| 539 web_ui()->CallJavascriptFunction("downloadUpdated", download_item); | |
| 540 } | |
| OLD | NEW |