| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/download/mhtml_generation_manager.h" | 5 #include "content/browser/download/mhtml_generation_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/platform_file.h" | 8 #include "base/platform_file.h" |
| 9 #include "content/browser/renderer_host/render_process_host_impl.h" | 9 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 10 #include "content/browser/renderer_host/render_view_host.h" | 10 #include "content/browser/renderer_host/render_view_host.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 13 #include "content/common/view_messages.h" | 14 #include "content/common/view_messages.h" |
| 14 #include "content/public/browser/notification_types.h" | 15 #include "content/public/browser/notification_types.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 using content::WebContents; | 18 using content::WebContents; |
| 18 | 19 |
| 19 MHTMLGenerationManager::Job::Job() | 20 MHTMLGenerationManager::Job::Job() |
| 20 : browser_file(base::kInvalidPlatformFileValue), | 21 : browser_file(base::kInvalidPlatformFileValue), |
| 21 renderer_file(IPC::InvalidPlatformFileForTransit()), | 22 renderer_file(IPC::InvalidPlatformFileForTransit()), |
| 22 process_id(-1), | 23 process_id(-1), |
| 23 routing_id(-1) { | 24 routing_id(-1) { |
| 24 } | 25 } |
| 25 | 26 |
| 26 MHTMLGenerationManager::Job::~Job() { | 27 MHTMLGenerationManager::Job::~Job() { |
| 27 } | 28 } |
| 28 | 29 |
| 30 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { |
| 31 return Singleton<MHTMLGenerationManager>::get(); |
| 32 } |
| 33 |
| 29 MHTMLGenerationManager::MHTMLGenerationManager() { | 34 MHTMLGenerationManager::MHTMLGenerationManager() { |
| 30 } | 35 } |
| 31 | 36 |
| 32 MHTMLGenerationManager::~MHTMLGenerationManager() { | 37 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 33 } | 38 } |
| 34 | 39 |
| 35 void MHTMLGenerationManager::GenerateMHTML(WebContents* web_contents, | 40 void MHTMLGenerationManager::GenerateMHTML( |
| 41 WebContents* web_contents, |
| 36 const FilePath& file, | 42 const FilePath& file, |
| 37 const GenerateMHTMLCallback& callback) { | 43 const GenerateMHTMLCallback& callback) { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 39 static int id_counter = 0; | 45 static int id_counter = 0; |
| 40 | 46 |
| 41 int job_id = id_counter++; | 47 int job_id = id_counter++; |
| 42 Job job; | 48 Job job; |
| 43 job.file_path = file; | 49 job.file_path = file; |
| 44 job.process_id = web_contents->GetRenderProcessHost()->GetID(); | 50 job.process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 45 job.routing_id = web_contents->GetRenderViewHost()->routing_id(); | 51 job.routing_id = web_contents->GetRenderViewHost()->routing_id(); |
| 46 job.callback = callback; | 52 job.callback = callback; |
| 47 id_to_job_[job_id] = job; | 53 id_to_job_[job_id] = job; |
| 48 | 54 |
| 49 base::ProcessHandle renderer_process = | 55 base::ProcessHandle renderer_process = |
| 50 web_contents->GetRenderProcessHost()->GetHandle(); | 56 web_contents->GetRenderProcessHost()->GetHandle(); |
| 51 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 57 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 52 base::Bind(&MHTMLGenerationManager::CreateFile, this, | 58 base::Bind(&MHTMLGenerationManager::CreateFile, base::Unretained(this), |
| 53 job_id, file, renderer_process)); | 59 job_id, file, renderer_process)); |
| 54 } | 60 } |
| 55 | 61 |
| 56 void MHTMLGenerationManager::MHTMLGenerated(int job_id, int64 mhtml_data_size) { | 62 void MHTMLGenerationManager::MHTMLGenerated(int job_id, int64 mhtml_data_size) { |
| 57 JobFinished(job_id, mhtml_data_size); | 63 JobFinished(job_id, mhtml_data_size); |
| 58 } | 64 } |
| 59 | 65 |
| 60 void MHTMLGenerationManager::CreateFile(int job_id, const FilePath& file_path, | 66 void MHTMLGenerationManager::CreateFile( |
| 67 int job_id, const FilePath& file_path, |
| 61 base::ProcessHandle renderer_process) { | 68 base::ProcessHandle renderer_process) { |
| 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 63 base::PlatformFile browser_file = base::CreatePlatformFile(file_path, | 70 base::PlatformFile browser_file = base::CreatePlatformFile(file_path, |
| 64 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, | 71 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 65 NULL, NULL); | 72 NULL, NULL); |
| 66 if (browser_file == base::kInvalidPlatformFileValue) { | 73 if (browser_file == base::kInvalidPlatformFileValue) { |
| 67 LOG(ERROR) << "Failed to create file to save MHTML at: " << | 74 LOG(ERROR) << "Failed to create file to save MHTML at: " << |
| 68 file_path.value(); | 75 file_path.value(); |
| 69 } | 76 } |
| 70 | 77 |
| 71 IPC::PlatformFileForTransit renderer_file = | 78 IPC::PlatformFileForTransit renderer_file = |
| 72 IPC::GetFileHandleForProcess(browser_file, renderer_process, false); | 79 IPC::GetFileHandleForProcess(browser_file, renderer_process, false); |
| 73 | 80 |
| 74 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 81 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 75 base::Bind(&MHTMLGenerationManager::FileCreated, this, | 82 base::Bind(&MHTMLGenerationManager::FileCreated, base::Unretained(this), |
| 76 job_id, browser_file, renderer_file)); | 83 job_id, browser_file, renderer_file)); |
| 77 } | 84 } |
| 78 | 85 |
| 79 void MHTMLGenerationManager::FileCreated(int job_id, | 86 void MHTMLGenerationManager::FileCreated(int job_id, |
| 80 base::PlatformFile browser_file, | 87 base::PlatformFile browser_file, |
| 81 IPC::PlatformFileForTransit renderer_file) { | 88 IPC::PlatformFileForTransit renderer_file) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 83 if (browser_file == base::kInvalidPlatformFileValue) { | 90 if (browser_file == base::kInvalidPlatformFileValue) { |
| 84 LOG(ERROR) << "Failed to create file"; | 91 LOG(ERROR) << "Failed to create file"; |
| 85 JobFinished(job_id, -1); | 92 JobFinished(job_id, -1); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 111 IDToJobMap::iterator iter = id_to_job_.find(job_id); | 118 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 112 if (iter == id_to_job_.end()) { | 119 if (iter == id_to_job_.end()) { |
| 113 NOTREACHED(); | 120 NOTREACHED(); |
| 114 return; | 121 return; |
| 115 } | 122 } |
| 116 | 123 |
| 117 Job& job = iter->second; | 124 Job& job = iter->second; |
| 118 job.callback.Run(job.file_path, file_size); | 125 job.callback.Run(job.file_path, file_size); |
| 119 | 126 |
| 120 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 127 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 121 base::Bind(&MHTMLGenerationManager::CloseFile, this, job.browser_file)); | 128 base::Bind(&MHTMLGenerationManager::CloseFile, base::Unretained(this), |
| 129 job.browser_file)); |
| 122 | 130 |
| 123 id_to_job_.erase(job_id); | 131 id_to_job_.erase(job_id); |
| 124 } | 132 } |
| 125 | 133 |
| 126 void MHTMLGenerationManager::CloseFile(base::PlatformFile file) { | 134 void MHTMLGenerationManager::CloseFile(base::PlatformFile file) { |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 128 base::ClosePlatformFile(file); | 136 base::ClosePlatformFile(file); |
| 129 } | 137 } |
| OLD | NEW |