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 <map> | 7 #include <map> |
8 #include <queue> | 8 #include <queue> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/guid.h" | 12 #include "base/guid.h" |
13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
14 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "content/browser/frame_host/frame_tree_node.h" | 18 #include "content/browser/frame_host/frame_tree_node.h" |
19 #include "content/common/frame_messages.h" | 19 #include "content/common/frame_messages.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/render_process_host_observer.h" | 23 #include "content/public/browser/render_process_host_observer.h" |
24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
25 #include "url/gurl.h" | |
25 | 26 |
26 namespace content { | 27 namespace content { |
27 | 28 |
28 // The class and all of its members live on the UI thread. Only static methods | 29 // The class and all of its members live on the UI thread. Only static methods |
29 // are executed on other threads. | 30 // are executed on other threads. |
30 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { | 31 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { |
31 public: | 32 public: |
32 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback); | 33 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback); |
33 ~Job() override; | 34 ~Job() override; |
34 | 35 |
35 void set_browser_file(base::File file) { browser_file_ = file.Pass(); } | 36 void set_browser_file(base::File file) { browser_file_ = file.Pass(); } |
36 | 37 |
37 GenerateMHTMLCallback callback() const { return callback_; } | 38 GenerateMHTMLCallback callback() const { return callback_; } |
38 | 39 |
40 // Handler for FrameHostMsg_SerializeAsMHTMLResponse (a notification from the | |
41 // renderer that the MHTML generation for previous frame has finished). | |
42 // Returns |true| upon success; |false| otherwise. | |
43 bool OnSerializeAsMHTMLResponse( | |
44 const std::set<std::string>& digests_of_uris_of_serialized_resources); | |
45 | |
39 // Sends IPC to the renderer, asking for MHTML generation of the next frame. | 46 // Sends IPC to the renderer, asking for MHTML generation of the next frame. |
40 // | 47 // |
41 // Returns true if the message was sent successfully; false otherwise. | 48 // Returns true if the message was sent successfully; false otherwise. |
42 bool SendToNextRenderFrame(); | 49 bool SendToNextRenderFrame(); |
43 | 50 |
44 // Indicates if more calls to SendToNextRenderFrame are needed. | 51 // Indicates if more calls to SendToNextRenderFrame are needed. |
45 bool HasMoreFramesToProcess() const { | 52 bool HasMoreFramesToProcess() const { |
46 return !pending_frame_tree_node_ids_.empty(); | 53 return !pending_frame_tree_node_ids_.empty(); |
47 } | 54 } |
48 | 55 |
(...skipping 28 matching lines...) Expand all Loading... | |
77 // The IDs of frames we still need to process. | 84 // The IDs of frames we still need to process. |
78 std::queue<int> pending_frame_tree_node_ids_; | 85 std::queue<int> pending_frame_tree_node_ids_; |
79 | 86 |
80 // Map from frames into content ids (see WebPageSerializer::generateMHTMLParts | 87 // Map from frames into content ids (see WebPageSerializer::generateMHTMLParts |
81 // for more details about what "content ids" are and how they are used). | 88 // for more details about what "content ids" are and how they are used). |
82 std::map<int, std::string> frame_tree_node_to_content_id_; | 89 std::map<int, std::string> frame_tree_node_to_content_id_; |
83 | 90 |
84 // MIME multipart boundary to use in the MHTML doc. | 91 // MIME multipart boundary to use in the MHTML doc. |
85 std::string mhtml_boundary_marker_; | 92 std::string mhtml_boundary_marker_; |
86 | 93 |
94 // Digests of URIs of already generated MHTML parts. | |
95 std::set<std::string> digests_of_already_serialized_uris_; | |
96 std::string salt_; | |
97 | |
87 // The callback to call once generation is complete. | 98 // The callback to call once generation is complete. |
88 GenerateMHTMLCallback callback_; | 99 GenerateMHTMLCallback callback_; |
89 | 100 |
90 // RAII helper for registering this Job as a RenderProcessHost observer. | 101 // RAII helper for registering this Job as a RenderProcessHost observer. |
91 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 102 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
92 observed_renderer_process_host_; | 103 observed_renderer_process_host_; |
93 | 104 |
94 DISALLOW_COPY_AND_ASSIGN(Job); | 105 DISALLOW_COPY_AND_ASSIGN(Job); |
95 }; | 106 }; |
96 | 107 |
97 MHTMLGenerationManager::Job::Job(int job_id, | 108 MHTMLGenerationManager::Job::Job(int job_id, |
98 WebContents* web_contents, | 109 WebContents* web_contents, |
99 GenerateMHTMLCallback callback) | 110 GenerateMHTMLCallback callback) |
100 : job_id_(job_id), | 111 : job_id_(job_id), |
101 mhtml_boundary_marker_(GenerateMHTMLBoundaryMarker()), | 112 mhtml_boundary_marker_(GenerateMHTMLBoundaryMarker()), |
113 salt_(base::GenerateGUID()), | |
102 callback_(callback), | 114 callback_(callback), |
103 observed_renderer_process_host_(this) { | 115 observed_renderer_process_host_(this) { |
104 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
105 web_contents->ForEachFrame(base::Bind( | 117 web_contents->ForEachFrame(base::Bind( |
106 &MHTMLGenerationManager::Job::AddFrame, | 118 &MHTMLGenerationManager::Job::AddFrame, |
107 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 119 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
108 | 120 |
109 // Main frame needs to be processed first. | 121 // Main frame needs to be processed first. |
110 DCHECK(!pending_frame_tree_node_ids_.empty()); | 122 DCHECK(!pending_frame_tree_node_ids_.empty()); |
111 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) | 123 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) |
(...skipping 23 matching lines...) Expand all Loading... | |
135 | 147 |
136 result[routing_id] = content_id; | 148 result[routing_id] = content_id; |
137 } | 149 } |
138 return result; | 150 return result; |
139 } | 151 } |
140 | 152 |
141 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { | 153 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { |
142 DCHECK(browser_file_.IsValid()); | 154 DCHECK(browser_file_.IsValid()); |
143 DCHECK_LT(0u, pending_frame_tree_node_ids_.size()); | 155 DCHECK_LT(0u, pending_frame_tree_node_ids_.size()); |
144 | 156 |
157 FrameMsg_SerializeAsMHTML_Params ipc_params; | |
158 ipc_params.job_id = job_id_; | |
159 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; | |
160 | |
145 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); | 161 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); |
146 pending_frame_tree_node_ids_.pop(); | 162 pending_frame_tree_node_ids_.pop(); |
147 bool is_last_frame = pending_frame_tree_node_ids_.empty(); | 163 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); |
148 | 164 |
149 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); | 165 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
150 if (!ftn) // The contents went away. | 166 if (!ftn) // The contents went away. |
151 return false; | 167 return false; |
152 RenderFrameHost* rfh = ftn->current_frame_host(); | 168 RenderFrameHost* rfh = ftn->current_frame_host(); |
153 | 169 |
154 // Get notified if the target of the IPC message dies between responding. | 170 // Get notified if the target of the IPC message dies between responding. |
155 observed_renderer_process_host_.RemoveAll(); | 171 observed_renderer_process_host_.RemoveAll(); |
156 observed_renderer_process_host_.Add(rfh->GetProcess()); | 172 observed_renderer_process_host_.Add(rfh->GetProcess()); |
157 | 173 |
158 IPC::PlatformFileForTransit renderer_file = IPC::GetFileHandleForProcess( | 174 // Tell the renderer to skip (= deduplicate) already covered MHTML parts. |
175 ipc_params.salt = salt_; | |
176 ipc_params.digests_of_uris_to_skip = digests_of_already_serialized_uris_; | |
177 | |
178 ipc_params.destination_file = IPC::GetFileHandleForProcess( | |
159 browser_file_.GetPlatformFile(), rfh->GetProcess()->GetHandle(), | 179 browser_file_.GetPlatformFile(), rfh->GetProcess()->GetHandle(), |
160 false); // |close_source_handle|. | 180 false); // |close_source_handle|. |
161 rfh->Send(new FrameMsg_SerializeAsMHTML( | 181 ipc_params.frame_routing_id_to_content_id = |
162 rfh->GetRoutingID(), job_id_, renderer_file, mhtml_boundary_marker_, | 182 CreateFrameRoutingIdToContentId(rfh->GetSiteInstance()); |
163 CreateFrameRoutingIdToContentId(rfh->GetSiteInstance()), is_last_frame)); | 183 rfh->Send(new FrameMsg_SerializeAsMHTML(rfh->GetRoutingID(), ipc_params)); |
164 return true; | 184 return true; |
165 } | 185 } |
166 | 186 |
167 void MHTMLGenerationManager::Job::RenderProcessExited( | 187 void MHTMLGenerationManager::Job::RenderProcessExited( |
168 RenderProcessHost* host, | 188 RenderProcessHost* host, |
169 base::TerminationStatus status, | 189 base::TerminationStatus status, |
170 int exit_code) { | 190 int exit_code) { |
171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
172 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); | 192 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); |
173 } | 193 } |
(...skipping 24 matching lines...) Expand all Loading... | |
198 return; | 218 return; |
199 } | 219 } |
200 | 220 |
201 BrowserThread::PostTaskAndReplyWithResult( | 221 BrowserThread::PostTaskAndReplyWithResult( |
202 BrowserThread::FILE, FROM_HERE, | 222 BrowserThread::FILE, FROM_HERE, |
203 base::Bind(&MHTMLGenerationManager::Job::CloseFileOnFileThread, | 223 base::Bind(&MHTMLGenerationManager::Job::CloseFileOnFileThread, |
204 base::Passed(browser_file_.Pass())), | 224 base::Passed(browser_file_.Pass())), |
205 callback); | 225 callback); |
206 } | 226 } |
207 | 227 |
228 bool MHTMLGenerationManager::Job::OnSerializeAsMHTMLResponse( | |
229 const std::set<std::string>& digests_of_uris_of_serialized_resources) { | |
230 digests_of_already_serialized_uris_.insert( | |
231 digests_of_uris_of_serialized_resources.begin(), | |
232 digests_of_uris_of_serialized_resources.end()); | |
233 | |
234 if (!HasMoreFramesToProcess()) | |
235 return true; // Success. | |
236 | |
237 return SendToNextRenderFrame(); | |
238 } | |
239 | |
208 // static | 240 // static |
209 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { | 241 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { |
210 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 242 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
211 DCHECK(file.IsValid()); | 243 DCHECK(file.IsValid()); |
212 int64 file_size = file.GetLength(); | 244 int64 file_size = file.GetLength(); |
213 file.Close(); | 245 file.Close(); |
214 return file_size; | 246 return file_size; |
215 } | 247 } |
216 | 248 |
217 // static | 249 // static |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 int job_id = NewJob(web_contents, callback); | 283 int job_id = NewJob(web_contents, callback); |
252 | 284 |
253 BrowserThread::PostTaskAndReplyWithResult( | 285 BrowserThread::PostTaskAndReplyWithResult( |
254 BrowserThread::FILE, FROM_HERE, | 286 BrowserThread::FILE, FROM_HERE, |
255 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), | 287 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), |
256 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 288 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
257 base::Unretained(this), // Safe b/c |this| is a singleton. | 289 base::Unretained(this), // Safe b/c |this| is a singleton. |
258 job_id)); | 290 job_id)); |
259 } | 291 } |
260 | 292 |
261 void MHTMLGenerationManager::OnSavedFrameAsMHTML( | 293 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
262 int job_id, | 294 int job_id, |
263 bool mhtml_generation_in_renderer_succeeded) { | 295 bool mhtml_generation_in_renderer_succeeded, |
296 const std::set<std::string>& digests_of_uris_of_serialized_resources) { | |
264 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 297 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
265 | 298 |
266 if (!mhtml_generation_in_renderer_succeeded) { | 299 if (!mhtml_generation_in_renderer_succeeded) { |
267 JobFinished(job_id, JobStatus::FAILURE); | 300 JobFinished(job_id, JobStatus::FAILURE); |
268 return; | 301 return; |
269 } | 302 } |
270 | 303 |
271 Job* job = FindJob(job_id); | 304 Job* job = FindJob(job_id); |
272 if (!job) | 305 if (!job) |
273 return; | 306 return; |
274 | 307 |
275 if (job->HasMoreFramesToProcess()) { | 308 if (!job->HasMoreFramesToProcess()) { |
276 if (!job->SendToNextRenderFrame()) { | 309 JobFinished(job_id, JobStatus::SUCCESS); |
277 JobFinished(job_id, JobStatus::FAILURE); | |
278 } | |
279 return; | 310 return; |
280 } | 311 } |
281 | 312 |
282 JobFinished(job_id, JobStatus::SUCCESS); | 313 if (!job->OnSerializeAsMHTMLResponse( |
314 digests_of_uris_of_serialized_resources)) { | |
315 JobFinished(job_id, JobStatus::FAILURE); | |
316 } | |
Randy Smith (Not in Mondays)
2015/12/30 00:45:49
nit, suggestion: The current implementation of OnS
Łukasz Anforowicz
2015/12/30 19:09:09
Done.
MHTMLGenerationManager::Job::OnSerializeAsM
| |
283 } | 317 } |
284 | 318 |
285 // static | 319 // static |
286 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { | 320 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { |
287 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 321 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
288 | 322 |
289 // SECURITY NOTE: A file descriptor to the file created below will be passed | 323 // SECURITY NOTE: A file descriptor to the file created below will be passed |
290 // to multiple renderer processes which (in out-of-process iframes mode) can | 324 // to multiple renderer processes which (in out-of-process iframes mode) can |
291 // act on behalf of separate web principals. Therefore it is important to | 325 // act on behalf of separate web principals. Therefore it is important to |
292 // only allow writing to the file and forbid reading from the file (as this | 326 // only allow writing to the file and forbid reading from the file (as this |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 ++it) { | 412 ++it) { |
379 if (it->second == job) { | 413 if (it->second == job) { |
380 JobFinished(it->first, JobStatus::FAILURE); | 414 JobFinished(it->first, JobStatus::FAILURE); |
381 return; | 415 return; |
382 } | 416 } |
383 } | 417 } |
384 NOTREACHED(); | 418 NOTREACHED(); |
385 } | 419 } |
386 | 420 |
387 } // namespace content | 421 } // namespace content |
OLD | NEW |