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