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 "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/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/browser/renderer_host/render_view_host_impl.h" | 10 #include "content/browser/renderer_host/render_view_host_impl.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 MHTMLGenerationManager::MHTMLGenerationManager() { | 91 MHTMLGenerationManager::MHTMLGenerationManager() { |
92 } | 92 } |
93 | 93 |
94 MHTMLGenerationManager::~MHTMLGenerationManager() { | 94 MHTMLGenerationManager::~MHTMLGenerationManager() { |
95 STLDeleteValues(&id_to_job_); | 95 STLDeleteValues(&id_to_job_); |
96 } | 96 } |
97 | 97 |
98 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 98 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
99 const base::FilePath& file, | 99 const base::FilePath& file, |
100 const GenerateMHTMLCallback& callback) { | 100 const GenerateMHTMLCallback& callback) { |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 101 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
102 | 102 |
103 int job_id = NewJob(web_contents, callback); | 103 int job_id = NewJob(web_contents, callback); |
104 | 104 |
105 base::ProcessHandle renderer_process = | 105 base::ProcessHandle renderer_process = |
106 web_contents->GetRenderProcessHost()->GetHandle(); | 106 web_contents->GetRenderProcessHost()->GetHandle(); |
107 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 107 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
108 base::Bind(&MHTMLGenerationManager::CreateFile, base::Unretained(this), | 108 base::Bind(&MHTMLGenerationManager::CreateFile, base::Unretained(this), |
109 job_id, file, renderer_process)); | 109 job_id, file, renderer_process)); |
110 } | 110 } |
111 | 111 |
112 void MHTMLGenerationManager::StreamMHTML( | 112 void MHTMLGenerationManager::StreamMHTML( |
113 WebContents* web_contents, | 113 WebContents* web_contents, |
114 base::File browser_file, | 114 base::File browser_file, |
115 const GenerateMHTMLCallback& callback) { | 115 const GenerateMHTMLCallback& callback) { |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
117 | 117 |
118 int job_id = NewJob(web_contents, callback); | 118 int job_id = NewJob(web_contents, callback); |
119 | 119 |
120 base::ProcessHandle renderer_process = | 120 base::ProcessHandle renderer_process = |
121 web_contents->GetRenderProcessHost()->GetHandle(); | 121 web_contents->GetRenderProcessHost()->GetHandle(); |
122 IPC::PlatformFileForTransit renderer_file = | 122 IPC::PlatformFileForTransit renderer_file = |
123 IPC::GetFileHandleForProcess(browser_file.GetPlatformFile(), | 123 IPC::GetFileHandleForProcess(browser_file.GetPlatformFile(), |
124 renderer_process, false); | 124 renderer_process, false); |
125 | 125 |
126 FileAvailable(job_id, browser_file.Pass(), renderer_file); | 126 FileAvailable(job_id, browser_file.Pass(), renderer_file); |
127 } | 127 } |
128 | 128 |
129 | 129 |
130 void MHTMLGenerationManager::MHTMLGenerated(int job_id, int64 mhtml_data_size) { | 130 void MHTMLGenerationManager::MHTMLGenerated(int job_id, int64 mhtml_data_size) { |
131 JobFinished(job_id, mhtml_data_size); | 131 JobFinished(job_id, mhtml_data_size); |
132 } | 132 } |
133 | 133 |
134 void MHTMLGenerationManager::CreateFile( | 134 void MHTMLGenerationManager::CreateFile( |
135 int job_id, const base::FilePath& file_path, | 135 int job_id, const base::FilePath& file_path, |
136 base::ProcessHandle renderer_process) { | 136 base::ProcessHandle renderer_process) { |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 137 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
138 base::File browser_file( | 138 base::File browser_file( |
139 file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 139 file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
140 if (!browser_file.IsValid()) { | 140 if (!browser_file.IsValid()) { |
141 LOG(ERROR) << "Failed to create file to save MHTML at: " << | 141 LOG(ERROR) << "Failed to create file to save MHTML at: " << |
142 file_path.value(); | 142 file_path.value(); |
143 } | 143 } |
144 | 144 |
145 IPC::PlatformFileForTransit renderer_file = | 145 IPC::PlatformFileForTransit renderer_file = |
146 IPC::GetFileHandleForProcess(browser_file.GetPlatformFile(), | 146 IPC::GetFileHandleForProcess(browser_file.GetPlatformFile(), |
147 renderer_process, false); | 147 renderer_process, false); |
148 | 148 |
149 BrowserThread::PostTask( | 149 BrowserThread::PostTask( |
150 BrowserThread::UI, | 150 BrowserThread::UI, |
151 FROM_HERE, | 151 FROM_HERE, |
152 base::Bind(&MHTMLGenerationManager::FileAvailable, | 152 base::Bind(&MHTMLGenerationManager::FileAvailable, |
153 base::Unretained(this), | 153 base::Unretained(this), |
154 job_id, | 154 job_id, |
155 base::Passed(&browser_file), | 155 base::Passed(&browser_file), |
156 renderer_file)); | 156 renderer_file)); |
157 } | 157 } |
158 | 158 |
159 void MHTMLGenerationManager::FileAvailable( | 159 void MHTMLGenerationManager::FileAvailable( |
160 int job_id, | 160 int job_id, |
161 base::File browser_file, | 161 base::File browser_file, |
162 IPC::PlatformFileForTransit renderer_file) { | 162 IPC::PlatformFileForTransit renderer_file) { |
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
164 if (!browser_file.IsValid()) { | 164 if (!browser_file.IsValid()) { |
165 LOG(ERROR) << "Failed to create file"; | 165 LOG(ERROR) << "Failed to create file"; |
166 JobFinished(job_id, -1); | 166 JobFinished(job_id, -1); |
167 return; | 167 return; |
168 } | 168 } |
169 | 169 |
170 IDToJobMap::iterator iter = id_to_job_.find(job_id); | 170 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
171 if (iter == id_to_job_.end()) { | 171 if (iter == id_to_job_.end()) { |
172 NOTREACHED(); | 172 NOTREACHED(); |
173 return; | 173 return; |
174 } | 174 } |
175 | 175 |
176 Job* job = iter->second; | 176 Job* job = iter->second; |
177 job->set_browser_file(browser_file.Pass()); | 177 job->set_browser_file(browser_file.Pass()); |
178 | 178 |
179 RenderViewHost* rvh = RenderViewHost::FromID( | 179 RenderViewHost* rvh = RenderViewHost::FromID( |
180 job->process_id(), job->routing_id()); | 180 job->process_id(), job->routing_id()); |
181 if (!rvh) { | 181 if (!rvh) { |
182 // The contents went away. | 182 // The contents went away. |
183 JobFinished(job_id, -1); | 183 JobFinished(job_id, -1); |
184 return; | 184 return; |
185 } | 185 } |
186 | 186 |
187 rvh->Send(new ViewMsg_SavePageAsMHTML(rvh->GetRoutingID(), job_id, | 187 rvh->Send(new ViewMsg_SavePageAsMHTML(rvh->GetRoutingID(), job_id, |
188 renderer_file)); | 188 renderer_file)); |
189 } | 189 } |
190 | 190 |
191 void MHTMLGenerationManager::JobFinished(int job_id, int64 file_size) { | 191 void MHTMLGenerationManager::JobFinished(int job_id, int64 file_size) { |
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 192 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
193 IDToJobMap::iterator iter = id_to_job_.find(job_id); | 193 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
194 if (iter == id_to_job_.end()) { | 194 if (iter == id_to_job_.end()) { |
195 NOTREACHED(); | 195 NOTREACHED(); |
196 return; | 196 return; |
197 } | 197 } |
198 | 198 |
199 Job* job = iter->second; | 199 Job* job = iter->second; |
200 job->callback().Run(file_size); | 200 job->callback().Run(file_size); |
201 | 201 |
202 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 202 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
203 base::Bind(&MHTMLGenerationManager::CloseFile, base::Unretained(this), | 203 base::Bind(&MHTMLGenerationManager::CloseFile, base::Unretained(this), |
204 base::Passed(job->browser_file()))); | 204 base::Passed(job->browser_file()))); |
205 | 205 |
206 id_to_job_.erase(job_id); | 206 id_to_job_.erase(job_id); |
207 delete job; | 207 delete job; |
208 } | 208 } |
209 | 209 |
210 void MHTMLGenerationManager::CloseFile(base::File file) { | 210 void MHTMLGenerationManager::CloseFile(base::File file) { |
211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 211 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
212 file.Close(); | 212 file.Close(); |
213 } | 213 } |
214 | 214 |
215 int MHTMLGenerationManager::NewJob(WebContents* web_contents, | 215 int MHTMLGenerationManager::NewJob(WebContents* web_contents, |
216 const GenerateMHTMLCallback& callback) { | 216 const GenerateMHTMLCallback& callback) { |
217 static int id_counter = 0; | 217 static int id_counter = 0; |
218 int job_id = id_counter++; | 218 int job_id = id_counter++; |
219 Job* job = new Job(); | 219 Job* job = new Job(); |
220 id_to_job_[job_id] = job; | 220 id_to_job_[job_id] = job; |
221 job->SetWebContents(web_contents); | 221 job->SetWebContents(web_contents); |
222 job->set_callback(callback); | 222 job->set_callback(callback); |
223 return job_id; | 223 return job_id; |
224 } | 224 } |
225 | 225 |
226 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 226 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 227 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
228 for (IDToJobMap::iterator it = id_to_job_.begin(); it != id_to_job_.end(); | 228 for (IDToJobMap::iterator it = id_to_job_.begin(); it != id_to_job_.end(); |
229 ++it) { | 229 ++it) { |
230 if (it->second == job) { | 230 if (it->second == job) { |
231 JobFinished(it->first, -1); | 231 JobFinished(it->first, -1); |
232 return; | 232 return; |
233 } | 233 } |
234 } | 234 } |
235 NOTREACHED(); | 235 NOTREACHED(); |
236 } | 236 } |
237 | 237 |
238 } // namespace content | 238 } // namespace content |
OLD | NEW |