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(base::kInvalidPlatformFileValue), |
| 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 base::PlatformFile renderer_file = base::GetFileHandleForProcess( |
| 64 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, base::PlatformFile renderer_file) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 74 if (browser_file == base::kInvalidPlatformFileValue) { |
| 75 LOG(ERROR) << "Failed to create file"; |
| 76 JobFinished(job_id, false); |
| 77 return; |
| 78 } |
| 79 |
| 80 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 81 if (iter == id_to_job_.end()) { |
| 82 NOTREACHED(); |
| 83 return; |
| 84 } |
| 85 |
| 86 Job& job = iter->second; |
| 87 job.browser_file = browser_file; |
| 88 job.renderer_file = renderer_file; |
| 89 |
| 90 TabContents* tab_contents = |
| 91 tab_util::GetTabContentsByID(job.process_id, job.routing_id); |
| 92 if (!tab_contents) { |
| 93 // The tab went away. |
| 94 JobFinished(job_id, false); |
| 95 return; |
| 96 } |
| 97 |
| 98 RenderViewHost* host = tab_contents->render_view_host(); |
| 99 host->Send(new ViewMsg_SavePageAsMHTML(host->routing_id(), job_id, |
| 100 #if defined(OS_WIN) |
| 101 renderer_file)); |
| 102 #elif defined(OS_POSIX) |
| 103 base::FileDescriptor(renderer_file, true))); |
| 104 #endif |
| 105 } |
| 106 |
| 107 void MHTMLGenerationManager::JobFinished(int job_id, bool success) { |
| 108 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 109 if (iter == id_to_job_.end()) { |
| 110 NOTREACHED(); |
| 111 return; |
| 112 } |
| 113 |
| 114 Job& job = iter->second; |
| 115 |
| 116 TabContents* tab_contents = |
| 117 tab_util::GetTabContentsByID(job.process_id, job.routing_id); |
| 118 if (tab_contents) { |
| 119 NotificationDetails details; |
| 120 details.file_path = job.file_path; |
| 121 details.success = success; |
| 122 |
| 123 NotificationService::current()->Notify( |
| 124 NotificationType::MHTML_GENERATED, |
| 125 Source<RenderViewHost>(tab_contents->render_view_host()), |
| 126 Details<NotificationDetails>(&details)); |
| 127 } |
| 128 |
| 129 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 130 NewRunnableMethod(this, &MHTMLGenerationManager::CloseFile, |
| 131 job.browser_file)); |
| 132 |
| 133 id_to_job_.erase(job_id); |
| 134 } |
| 135 |
| 136 void MHTMLGenerationManager::CloseFile(base::PlatformFile file) { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 138 base::ClosePlatformFile(file); |
| 139 } |
OLD | NEW |