OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/download/mhtml_generation_manager.h" |
| 6 |
| 7 #include "base/platform_file.h" |
| 8 #include "chrome/browser/tab_contents/tab_util.h" |
| 9 #include "chrome/common/render_messages.h" |
| 10 #include "content/browser/renderer_host/render_process_host.h" |
| 11 #include "content/browser/renderer_host/render_view_host.h" |
| 12 #include "content/browser/tab_contents/tab_contents.h" |
| 13 #include "content/common/notification_service.h" |
| 14 #include "content/common/page_transition_types.h" |
| 15 |
| 16 MHTMLGenerationManager::Job::Job() |
| 17 : browser_file(base::kInvalidPlatformFileValue), |
| 18 renderer_file(IPC::InvalidPlatformFileForTransit()), |
| 19 process_id(-1), |
| 20 routing_id(-1) { |
| 21 } |
| 22 |
| 23 MHTMLGenerationManager::MHTMLGenerationManager() { |
| 24 } |
| 25 |
| 26 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 27 } |
| 28 |
| 29 void MHTMLGenerationManager::GenerateMHTML(TabContents* tab_contents, |
| 30 const FilePath& file) { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 static int id_counter = 0; |
| 33 |
| 34 int job_id = id_counter++; |
| 35 Job job; |
| 36 job.file_path = file; |
| 37 job.process_id = tab_contents->GetRenderProcessHost()->id(); |
| 38 job.routing_id = tab_contents->render_view_host()->routing_id(); |
| 39 id_to_job_[job_id] = job; |
| 40 |
| 41 base::ProcessHandle renderer_process = |
| 42 tab_contents->GetRenderProcessHost()->GetHandle(); |
| 43 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 44 NewRunnableMethod(this, &MHTMLGenerationManager::CreateFile, |
| 45 job_id, file, renderer_process)); |
| 46 } |
| 47 |
| 48 void MHTMLGenerationManager::MHTMLGenerated(int job_id, bool success) { |
| 49 JobFinished(job_id, success); |
| 50 } |
| 51 |
| 52 void MHTMLGenerationManager::CreateFile(int job_id, const FilePath& file_path, |
| 53 base::ProcessHandle renderer_process) { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 55 base::PlatformFile browser_file = base::CreatePlatformFile(file_path, |
| 56 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 57 NULL, NULL); |
| 58 if (browser_file == base::kInvalidPlatformFileValue) { |
| 59 LOG(ERROR) << "Failed to create file to save MHTML at: " << |
| 60 file_path.value(); |
| 61 } |
| 62 |
| 63 IPC::PlatformFileForTransit renderer_file = |
| 64 IPC::GetFileHandleForProcess(browser_file, renderer_process, false); |
| 65 |
| 66 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 67 NewRunnableMethod(this, &MHTMLGenerationManager::FileCreated, |
| 68 job_id, browser_file, renderer_file)); |
| 69 } |
| 70 |
| 71 void MHTMLGenerationManager::FileCreated(int job_id, |
| 72 base::PlatformFile browser_file, |
| 73 IPC::PlatformFileForTransit renderer_file) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 if (browser_file == base::kInvalidPlatformFileValue) { |
| 76 LOG(ERROR) << "Failed to create file"; |
| 77 JobFinished(job_id, false); |
| 78 return; |
| 79 } |
| 80 |
| 81 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 82 if (iter == id_to_job_.end()) { |
| 83 NOTREACHED(); |
| 84 return; |
| 85 } |
| 86 |
| 87 Job& job = iter->second; |
| 88 job.browser_file = browser_file; |
| 89 job.renderer_file = renderer_file; |
| 90 |
| 91 TabContents* tab_contents = |
| 92 tab_util::GetTabContentsByID(job.process_id, job.routing_id); |
| 93 if (!tab_contents) { |
| 94 // The tab went away. |
| 95 JobFinished(job_id, false); |
| 96 return; |
| 97 } |
| 98 |
| 99 RenderViewHost* host = tab_contents->render_view_host(); |
| 100 host->Send(new ViewMsg_SavePageAsMHTML(host->routing_id(), job_id, |
| 101 renderer_file)); |
| 102 } |
| 103 |
| 104 void MHTMLGenerationManager::JobFinished(int job_id, bool success) { |
| 105 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 106 if (iter == id_to_job_.end()) { |
| 107 NOTREACHED(); |
| 108 return; |
| 109 } |
| 110 |
| 111 Job& job = iter->second; |
| 112 |
| 113 TabContents* tab_contents = |
| 114 tab_util::GetTabContentsByID(job.process_id, job.routing_id); |
| 115 if (tab_contents) { |
| 116 NotificationDetails details; |
| 117 details.file_path = job.file_path; |
| 118 details.success = success; |
| 119 |
| 120 NotificationService::current()->Notify( |
| 121 NotificationType::MHTML_GENERATED, |
| 122 Source<RenderViewHost>(tab_contents->render_view_host()), |
| 123 Details<NotificationDetails>(&details)); |
| 124 } |
| 125 |
| 126 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 127 NewRunnableMethod(this, &MHTMLGenerationManager::CloseFile, |
| 128 job.browser_file)); |
| 129 |
| 130 id_to_job_.erase(job_id); |
| 131 } |
| 132 |
| 133 void MHTMLGenerationManager::CloseFile(base::PlatformFile file) { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 135 base::ClosePlatformFile(file); |
| 136 } |
OLD | NEW |