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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "content/browser/download/save_file_manager.h" | 7 #include "content/browser/download/save_file_manager.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/stl_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "content/browser/child_process_security_policy_impl.h" | 15 #include "content/browser/child_process_security_policy_impl.h" |
16 #include "content/browser/download/save_file.h" | 16 #include "content/browser/download/save_file.h" |
17 #include "content/browser/download/save_file_resource_handler.h" | 17 #include "content/browser/download/save_file_resource_handler.h" |
18 #include "content/browser/download/save_package.h" | 18 #include "content/browser/download/save_package.h" |
19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
20 #include "content/browser/renderer_host/render_view_host_impl.h" | 20 #include "content/browser/renderer_host/render_view_host_impl.h" |
21 #include "content/browser/web_contents/web_contents_impl.h" | 21 #include "content/browser/web_contents/web_contents_impl.h" |
22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // timers) that live on the saving thread (file thread). | 59 // timers) that live on the saving thread (file thread). |
60 void SaveFileManager::Shutdown() { | 60 void SaveFileManager::Shutdown() { |
61 BrowserThread::PostTask( | 61 BrowserThread::PostTask( |
62 BrowserThread::FILE, FROM_HERE, | 62 BrowserThread::FILE, FROM_HERE, |
63 base::Bind(&SaveFileManager::OnShutdown, this)); | 63 base::Bind(&SaveFileManager::OnShutdown, this)); |
64 } | 64 } |
65 | 65 |
66 // Stop file thread operations. | 66 // Stop file thread operations. |
67 void SaveFileManager::OnShutdown() { | 67 void SaveFileManager::OnShutdown() { |
68 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 68 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
69 base::STLDeleteValues(&save_file_map_); | 69 save_file_map_.clear(); |
70 } | 70 } |
71 | 71 |
72 SaveFile* SaveFileManager::LookupSaveFile(SaveItemId save_item_id) { | 72 SaveFile* SaveFileManager::LookupSaveFile(SaveItemId save_item_id) { |
73 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 73 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
74 SaveFileMap::iterator it = save_file_map_.find(save_item_id); | 74 auto it = save_file_map_.find(save_item_id); |
75 return it == save_file_map_.end() ? nullptr : it->second; | 75 return it == save_file_map_.end() ? nullptr : it->second.get(); |
76 } | 76 } |
77 | 77 |
78 // Look up a SavePackage according to a save id. | 78 // Look up a SavePackage according to a save id. |
79 SavePackage* SaveFileManager::LookupPackage(SaveItemId save_item_id) { | 79 SavePackage* SaveFileManager::LookupPackage(SaveItemId save_item_id) { |
80 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 80 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
81 SavePackageMap::iterator it = packages_.find(save_item_id); | 81 auto it = packages_.find(save_item_id); |
82 if (it != packages_.end()) | 82 if (it != packages_.end()) |
83 return it->second; | 83 return it->second; |
84 return nullptr; | 84 return nullptr; |
85 } | 85 } |
86 | 86 |
87 // Call from SavePackage for starting a saving job | 87 // Call from SavePackage for starting a saving job |
88 void SaveFileManager::SaveURL(SaveItemId save_item_id, | 88 void SaveFileManager::SaveURL(SaveItemId save_item_id, |
89 const GURL& url, | 89 const GURL& url, |
90 const Referrer& referrer, | 90 const Referrer& referrer, |
91 int render_process_host_id, | 91 int render_process_host_id, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 // Utility function for look up table maintenance, called on the UI thread. | 127 // Utility function for look up table maintenance, called on the UI thread. |
128 // A manager may have multiple save page job (SavePackage) in progress, | 128 // A manager may have multiple save page job (SavePackage) in progress, |
129 // so we just look up the save id and remove it from the tracking table. | 129 // so we just look up the save id and remove it from the tracking table. |
130 void SaveFileManager::RemoveSaveFile(SaveItemId save_item_id, | 130 void SaveFileManager::RemoveSaveFile(SaveItemId save_item_id, |
131 SavePackage* save_package) { | 131 SavePackage* save_package) { |
132 DCHECK(save_package); | 132 DCHECK(save_package); |
133 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
134 // A save page job (SavePackage) can only have one manager, | 134 // A save page job (SavePackage) can only have one manager, |
135 // so remove it if it exists. | 135 // so remove it if it exists. |
136 SavePackageMap::iterator it = packages_.find(save_item_id); | 136 auto it = packages_.find(save_item_id); |
137 if (it != packages_.end()) | 137 if (it != packages_.end()) |
138 packages_.erase(it); | 138 packages_.erase(it); |
139 } | 139 } |
140 | 140 |
141 // Static | 141 // Static |
142 SavePackage* SaveFileManager::GetSavePackageFromRenderIds( | 142 SavePackage* SaveFileManager::GetSavePackageFromRenderIds( |
143 int render_process_id, | 143 int render_process_id, |
144 int render_frame_routing_id) { | 144 int render_frame_routing_id) { |
145 RenderFrameHost* render_frame_host = | 145 RenderFrameHost* render_frame_host = |
146 RenderFrameHost::FromID(render_process_id, render_frame_routing_id); | 146 RenderFrameHost::FromID(render_process_id, render_frame_routing_id); |
(...skipping 28 matching lines...) Expand all Loading... |
175 | 175 |
176 // Notifications sent from the IO thread and run on the file thread: | 176 // Notifications sent from the IO thread and run on the file thread: |
177 | 177 |
178 // The IO thread created |info|, but the file thread (this method) uses it | 178 // The IO thread created |info|, but the file thread (this method) uses it |
179 // to create a SaveFile which will hold and finally destroy |info|. It will | 179 // to create a SaveFile which will hold and finally destroy |info|. It will |
180 // then passes |info| to the UI thread for reporting saving status. | 180 // then passes |info| to the UI thread for reporting saving status. |
181 void SaveFileManager::StartSave(SaveFileCreateInfo* info) { | 181 void SaveFileManager::StartSave(SaveFileCreateInfo* info) { |
182 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 182 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
183 DCHECK(info); | 183 DCHECK(info); |
184 // No need to calculate hash. | 184 // No need to calculate hash. |
185 SaveFile* save_file = new SaveFile(info, false); | 185 std::unique_ptr<SaveFile> save_file = base::MakeUnique<SaveFile>(info, false); |
186 | 186 |
187 // TODO(phajdan.jr): We should check the return value and handle errors here. | 187 // TODO(phajdan.jr): We should check the return value and handle errors here. |
188 save_file->Initialize(); | 188 save_file->Initialize(); |
| 189 info->path = save_file->FullPath(); |
189 | 190 |
190 DCHECK(!LookupSaveFile(info->save_item_id)); | 191 DCHECK(!LookupSaveFile(info->save_item_id)); |
191 save_file_map_[info->save_item_id] = save_file; | 192 save_file_map_[info->save_item_id] = std::move(save_file); |
192 info->path = save_file->FullPath(); | |
193 | 193 |
194 BrowserThread::PostTask( | 194 BrowserThread::PostTask( |
195 BrowserThread::UI, FROM_HERE, | 195 BrowserThread::UI, FROM_HERE, |
196 base::Bind(&SaveFileManager::OnStartSave, this, *info)); | 196 base::Bind(&SaveFileManager::OnStartSave, this, *info)); |
197 } | 197 } |
198 | 198 |
199 // We do forward an update to the UI thread here, since we do not use timer to | 199 // We do forward an update to the UI thread here, since we do not use timer to |
200 // update the UI. If the user has canceled the saving action (in the UI | 200 // update the UI. If the user has canceled the saving action (in the UI |
201 // thread). We may receive a few more updates before the IO thread gets the | 201 // thread). We may receive a few more updates before the IO thread gets the |
202 // cancel message. We just delete the data since the SaveFile has been deleted. | 202 // cancel message. We just delete the data since the SaveFile has been deleted. |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 | 355 |
356 // Notifications sent from the UI thread and run on the file thread. | 356 // Notifications sent from the UI thread and run on the file thread. |
357 | 357 |
358 // This method will be sent via a user action, or shutdown on the UI thread, | 358 // This method will be sent via a user action, or shutdown on the UI thread, |
359 // and run on the file thread. We don't post a message back for cancels, | 359 // and run on the file thread. We don't post a message back for cancels, |
360 // but we do forward the cancel to the IO thread. Since this message has been | 360 // but we do forward the cancel to the IO thread. Since this message has been |
361 // sent from the UI thread, the saving job may have already completed and | 361 // sent from the UI thread, the saving job may have already completed and |
362 // won't exist in our map. | 362 // won't exist in our map. |
363 void SaveFileManager::CancelSave(SaveItemId save_item_id) { | 363 void SaveFileManager::CancelSave(SaveItemId save_item_id) { |
364 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 364 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
365 SaveFileMap::iterator it = save_file_map_.find(save_item_id); | 365 auto it = save_file_map_.find(save_item_id); |
366 if (it != save_file_map_.end()) { | 366 if (it != save_file_map_.end()) { |
367 SaveFile* save_file = it->second; | 367 std::unique_ptr<SaveFile> save_file = std::move(it->second); |
368 | 368 |
369 if (!save_file->InProgress()) { | 369 if (!save_file->InProgress()) { |
370 // We've won a race with the UI thread--we finished the file before | 370 // We've won a race with the UI thread--we finished the file before |
371 // the UI thread cancelled it on us. Unfortunately, in this situation | 371 // the UI thread cancelled it on us. Unfortunately, in this situation |
372 // the cancel wins, so we need to delete the now detached file. | 372 // the cancel wins, so we need to delete the now detached file. |
373 base::DeleteFile(save_file->FullPath(), false); | 373 base::DeleteFile(save_file->FullPath(), false); |
374 } else if (save_file->save_source() == | 374 } else if (save_file->save_source() == |
375 SaveFileCreateInfo::SAVE_FILE_FROM_NET) { | 375 SaveFileCreateInfo::SAVE_FILE_FROM_NET) { |
376 // If the data comes from the net IO thread and hasn't completed | 376 // If the data comes from the net IO thread and hasn't completed |
377 // yet, then forward the cancel message to IO thread & cancel the | 377 // yet, then forward the cancel message to IO thread & cancel the |
378 // save locally. If the data doesn't come from the IO thread, | 378 // save locally. If the data doesn't come from the IO thread, |
379 // we can ignore the message. | 379 // we can ignore the message. |
380 BrowserThread::PostTask( | 380 BrowserThread::PostTask( |
381 BrowserThread::IO, FROM_HERE, | 381 BrowserThread::IO, FROM_HERE, |
382 base::Bind(&SaveFileManager::ExecuteCancelSaveRequest, this, | 382 base::Bind(&SaveFileManager::ExecuteCancelSaveRequest, this, |
383 save_file->render_process_id(), save_file->request_id())); | 383 save_file->render_process_id(), save_file->request_id())); |
384 } | 384 } |
385 | 385 |
386 // Whatever the save file is complete or not, just delete it. This | 386 // Whatever the save file is complete or not, just delete it. This |
387 // will delete the underlying file if InProgress() is true. | 387 // will delete the underlying file if InProgress() is true. |
388 save_file_map_.erase(it); | 388 save_file_map_.erase(it); |
389 delete save_file; | |
390 } | 389 } |
391 } | 390 } |
392 | 391 |
393 void SaveFileManager::OnDeleteDirectoryOrFile(const base::FilePath& full_path, | 392 void SaveFileManager::OnDeleteDirectoryOrFile(const base::FilePath& full_path, |
394 bool is_dir) { | 393 bool is_dir) { |
395 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 394 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
396 DCHECK(!full_path.empty()); | 395 DCHECK(!full_path.empty()); |
397 | 396 |
398 base::DeleteFile(full_path, is_dir); | 397 base::DeleteFile(full_path, is_dir); |
399 } | 398 } |
400 | 399 |
401 void SaveFileManager::RenameAllFiles(const FinalNamesMap& final_names, | 400 void SaveFileManager::RenameAllFiles(const FinalNamesMap& final_names, |
402 const base::FilePath& resource_dir, | 401 const base::FilePath& resource_dir, |
403 int render_process_id, | 402 int render_process_id, |
404 int render_frame_routing_id, | 403 int render_frame_routing_id, |
405 SavePackageId save_package_id) { | 404 SavePackageId save_package_id) { |
406 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 405 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
407 | 406 |
408 if (!resource_dir.empty() && !base::PathExists(resource_dir)) | 407 if (!resource_dir.empty() && !base::PathExists(resource_dir)) |
409 base::CreateDirectory(resource_dir); | 408 base::CreateDirectory(resource_dir); |
410 | 409 |
411 for (const auto& i : final_names) { | 410 for (const auto& i : final_names) { |
412 SaveItemId save_item_id = i.first; | 411 SaveItemId save_item_id = i.first; |
413 const base::FilePath& final_name = i.second; | 412 const base::FilePath& final_name = i.second; |
414 | 413 |
415 SaveFileMap::iterator it = save_file_map_.find(save_item_id); | 414 auto it = save_file_map_.find(save_item_id); |
416 if (it != save_file_map_.end()) { | 415 if (it != save_file_map_.end()) { |
417 SaveFile* save_file = it->second; | 416 SaveFile* save_file = it->second.get(); |
418 DCHECK(!save_file->InProgress()); | 417 DCHECK(!save_file->InProgress()); |
419 save_file->Rename(final_name); | 418 save_file->Rename(final_name); |
420 delete save_file; | |
421 save_file_map_.erase(it); | 419 save_file_map_.erase(it); |
422 } | 420 } |
423 } | 421 } |
424 | 422 |
425 BrowserThread::PostTask( | 423 BrowserThread::PostTask( |
426 BrowserThread::UI, FROM_HERE, | 424 BrowserThread::UI, FROM_HERE, |
427 base::Bind(&SaveFileManager::OnFinishSavePageJob, this, render_process_id, | 425 base::Bind(&SaveFileManager::OnFinishSavePageJob, this, render_process_id, |
428 render_frame_routing_id, save_package_id)); | 426 render_frame_routing_id, save_package_id)); |
429 } | 427 } |
430 | 428 |
431 void SaveFileManager::OnFinishSavePageJob(int render_process_id, | 429 void SaveFileManager::OnFinishSavePageJob(int render_process_id, |
432 int render_frame_routing_id, | 430 int render_frame_routing_id, |
433 SavePackageId save_package_id) { | 431 SavePackageId save_package_id) { |
434 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 432 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
435 | 433 |
436 SavePackage* save_package = | 434 SavePackage* save_package = |
437 GetSavePackageFromRenderIds(render_process_id, render_frame_routing_id); | 435 GetSavePackageFromRenderIds(render_process_id, render_frame_routing_id); |
438 | 436 |
439 if (save_package && save_package->id() == save_package_id) | 437 if (save_package && save_package->id() == save_package_id) |
440 save_package->Finish(); | 438 save_package->Finish(); |
441 } | 439 } |
442 | 440 |
443 void SaveFileManager::RemoveSavedFileFromFileMap( | 441 void SaveFileManager::RemoveSavedFileFromFileMap( |
444 const std::vector<SaveItemId>& save_item_ids) { | 442 const std::vector<SaveItemId>& save_item_ids) { |
445 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 443 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
446 | 444 |
447 for (const SaveItemId save_item_id : save_item_ids) { | 445 for (const SaveItemId save_item_id : save_item_ids) { |
448 SaveFileMap::iterator it = save_file_map_.find(save_item_id); | 446 auto it = save_file_map_.find(save_item_id); |
449 if (it != save_file_map_.end()) { | 447 if (it != save_file_map_.end()) { |
450 SaveFile* save_file = it->second; | 448 SaveFile* save_file = it->second.get(); |
451 DCHECK(!save_file->InProgress()); | 449 DCHECK(!save_file->InProgress()); |
452 base::DeleteFile(save_file->FullPath(), false); | 450 base::DeleteFile(save_file->FullPath(), false); |
453 delete save_file; | |
454 save_file_map_.erase(it); | 451 save_file_map_.erase(it); |
455 } | 452 } |
456 } | 453 } |
457 } | 454 } |
458 | 455 |
459 } // namespace content | 456 } // namespace content |
OLD | NEW |