Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(674)

Side by Side Diff: chrome/browser/download/mhtml_generation_manager.cc

Issue 7044095: Hooking MHTML generation to the browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Style fixes Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "content/browser/renderer_host/render_process_host.h"
10 #include "content/browser/renderer_host/render_view_host.h"
11 #include "content/browser/tab_contents/tab_contents.h"
12 #include "content/common/notification_service.h"
13 #include "content/common/page_transition_types.h"
14 #include "content/common/view_messages.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& filePath,
53 base::ProcessHandle renderer_process) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
55 base::PlatformFile browser_file = base::CreatePlatformFile(filePath,
56 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
57 NULL, NULL);
58
59 base::PlatformFile renderer_file = base::GetFileHandleForProcess(
60 renderer_process, browser_file, false);
61
62 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
63 NewRunnableMethod(this, &MHTMLGenerationManager::FileCreated,
64 job_id, browser_file, renderer_file));
65 }
66
67 void MHTMLGenerationManager::FileCreated(int job_id,
68 base::PlatformFile browser_file, base::PlatformFile renderer_file) {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
70 if (browser_file == base::kInvalidPlatformFileValue) {
71 LOG(ERROR) << "Failed to create file";
Randy Smith (Not in Mondays) 2011/06/10 18:45:38 I don't think this error's likely enough that you
Jay Civelli 2011/06/10 22:38:22 Good point, now notifying of the failure,
72 return;
73 }
74
75 IDToJobMap::iterator iter = id_to_job_.find(job_id);
76 if (iter == id_to_job_.end()) {
77 NOTREACHED();
78 return;
79 }
80
81 Job& job = iter->second;
82 job.browser_file = browser_file;
83 job.renderer_file = renderer_file;
84
85 TabContents* tab_contents =
86 tab_util::GetTabContentsByID(job.process_id, job.routing_id);
87 if (!tab_contents) {
88 // The tab went away.
89 JobFinished(job_id, false);
90 return;
91 }
92
93 RenderViewHost* host = tab_contents->render_view_host();
94 host->Send(new ViewMsg_SavePageAsMHTML(host->routing_id(), job_id,
95 #if defined(OS_WIN)
96 renderer_file));
97 #elif defined(OS_POSIX)
98 base::FileDescriptor(renderer_file, true)));
99 #endif
100 }
101
102 void MHTMLGenerationManager::JobFinished(int job_id, bool success) {
103 IDToJobMap::iterator iter = id_to_job_.find(job_id);
104 if (iter == id_to_job_.end()) {
105 NOTREACHED();
106 return;
107 }
108
109 Job& job = iter->second;
110
111 TabContents* tab_contents =
112 tab_util::GetTabContentsByID(job.process_id, job.routing_id);
113 if (tab_contents) {
114 NotificationDetails details;
115 details.file_path = job.file_path;
116 details.success = success;
117
118 NotificationService::current()->Notify(
119 NotificationType::MHTML_GENERATED,
120 Source<RenderViewHost>(tab_contents->render_view_host()),
121 Details<NotificationDetails>(&details));
122 }
123
124 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
125 NewRunnableMethod(this, &MHTMLGenerationManager::CloseFile,
126 job.browser_file));
127
128 id_to_job_.erase(job_id);
129 }
130
131 void MHTMLGenerationManager::CloseFile(base::PlatformFile file) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
133 base::ClosePlatformFile(file);
134 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698