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/browser/frame_host/render_frame_host_impl.h" |
19 #include "content/common/frame_messages.h" | 20 #include "content/common/frame_messages.h" |
20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/render_process_host_observer.h" | 24 #include "content/public/browser/render_process_host_observer.h" |
24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.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 RenderFrameHostImpl* sender, |
| 46 const std::set<std::string>& digests_of_uris_of_serialized_resources); |
| 47 |
39 // Sends IPC to the renderer, asking for MHTML generation of the next frame. | 48 // Sends IPC to the renderer, asking for MHTML generation of the next frame. |
40 // | 49 // |
41 // Returns true if the message was sent successfully; false otherwise. | 50 // Returns true if the message was sent successfully; false otherwise. |
42 bool SendToNextRenderFrame(); | 51 bool SendToNextRenderFrame(); |
43 | 52 |
44 // Indicates if more calls to SendToNextRenderFrame are needed. | 53 // Indicates if more calls to SendToNextRenderFrame are needed. |
45 bool HasMoreFramesToProcess() const { | 54 bool IsDone() const { |
46 return !pending_frame_tree_node_ids_.empty(); | 55 bool waiting_for_response_from_renderer = |
| 56 frame_tree_node_id_of_busy_frame_ != |
| 57 FrameTreeNode::kFrameTreeNodeInvalidId; |
| 58 bool no_more_requests_to_send = pending_frame_tree_node_ids_.empty(); |
| 59 return !waiting_for_response_from_renderer && no_more_requests_to_send; |
47 } | 60 } |
48 | 61 |
49 // Close the file on the file thread and respond back on the UI thread with | 62 // Close the file on the file thread and respond back on the UI thread with |
50 // file size. | 63 // file size. |
51 void CloseFile(base::Callback<void(int64 file_size)> callback); | 64 void CloseFile(base::Callback<void(int64 file_size)> callback); |
52 | 65 |
53 // RenderProcessHostObserver: | 66 // RenderProcessHostObserver: |
54 void RenderProcessExited(RenderProcessHost* host, | 67 void RenderProcessExited(RenderProcessHost* host, |
55 base::TerminationStatus status, | 68 base::TerminationStatus status, |
56 int exit_code) override; | 69 int exit_code) override; |
57 void RenderProcessHostDestroyed(RenderProcessHost* host) override; | 70 void RenderProcessHostDestroyed(RenderProcessHost* host) override; |
58 | 71 |
59 private: | 72 private: |
60 static std::string GenerateMHTMLBoundaryMarker(); | 73 static std::string GenerateMHTMLBoundaryMarker(); |
61 static int64 CloseFileOnFileThread(base::File file); | 74 static int64 CloseFileOnFileThread(base::File file); |
62 void AddFrame(RenderFrameHost* render_frame_host); | 75 void AddFrame(RenderFrameHost* render_frame_host); |
63 | 76 |
64 // Creates a new map with values (content ids) the same as in | 77 // Creates a new map with values (content ids) the same as in |
65 // |frame_tree_node_to_content_id_| map, but with the keys translated from | 78 // |frame_tree_node_to_content_id_| map, but with the keys translated from |
66 // frame_tree_node_id into a |site_instance|-specific routing_id. | 79 // frame_tree_node_id into a |site_instance|-specific routing_id. |
67 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 80 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
68 SiteInstance* site_instance); | 81 SiteInstance* site_instance); |
69 | 82 |
70 // Id used to map renderer responses to jobs. | 83 // Id used to map renderer responses to jobs. |
71 // See also MHTMLGenerationManager::id_to_job_ map. | 84 // See also MHTMLGenerationManager::id_to_job_ map. |
72 int job_id_; | 85 int job_id_; |
73 | 86 |
| 87 // The IDs of frames that still need to be processed. |
| 88 std::queue<int> pending_frame_tree_node_ids_; |
| 89 |
| 90 // Identifies a frame to which we've sent FrameMsg_SerializeAsMHTML but for |
| 91 // which we didn't yet process FrameHostMsg_SerializeAsMHTMLResponse via |
| 92 // OnSerializeAsMHTMLResponse. |
| 93 int frame_tree_node_id_of_busy_frame_; |
| 94 |
74 // The handle to the file the MHTML is saved to for the browser process. | 95 // The handle to the file the MHTML is saved to for the browser process. |
75 base::File browser_file_; | 96 base::File browser_file_; |
76 | 97 |
77 // The IDs of frames we still need to process. | |
78 std::queue<int> pending_frame_tree_node_ids_; | |
79 | |
80 // Map from frames into content ids (see WebPageSerializer::generateMHTMLParts | 98 // Map from frames into content ids (see WebPageSerializer::generateMHTMLParts |
81 // for more details about what "content ids" are and how they are used). | 99 // 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_; | 100 std::map<int, std::string> frame_tree_node_to_content_id_; |
83 | 101 |
84 // MIME multipart boundary to use in the MHTML doc. | 102 // MIME multipart boundary to use in the MHTML doc. |
85 std::string mhtml_boundary_marker_; | 103 std::string mhtml_boundary_marker_; |
86 | 104 |
| 105 // Digests of URIs of already generated MHTML parts. |
| 106 std::set<std::string> digests_of_already_serialized_uris_; |
| 107 std::string salt_; |
| 108 |
87 // The callback to call once generation is complete. | 109 // The callback to call once generation is complete. |
88 GenerateMHTMLCallback callback_; | 110 GenerateMHTMLCallback callback_; |
89 | 111 |
90 // RAII helper for registering this Job as a RenderProcessHost observer. | 112 // RAII helper for registering this Job as a RenderProcessHost observer. |
91 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 113 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
92 observed_renderer_process_host_; | 114 observed_renderer_process_host_; |
93 | 115 |
94 DISALLOW_COPY_AND_ASSIGN(Job); | 116 DISALLOW_COPY_AND_ASSIGN(Job); |
95 }; | 117 }; |
96 | 118 |
97 MHTMLGenerationManager::Job::Job(int job_id, | 119 MHTMLGenerationManager::Job::Job(int job_id, |
98 WebContents* web_contents, | 120 WebContents* web_contents, |
99 GenerateMHTMLCallback callback) | 121 GenerateMHTMLCallback callback) |
100 : job_id_(job_id), | 122 : job_id_(job_id), |
| 123 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
101 mhtml_boundary_marker_(GenerateMHTMLBoundaryMarker()), | 124 mhtml_boundary_marker_(GenerateMHTMLBoundaryMarker()), |
| 125 salt_(base::GenerateGUID()), |
102 callback_(callback), | 126 callback_(callback), |
103 observed_renderer_process_host_(this) { | 127 observed_renderer_process_host_(this) { |
104 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
105 web_contents->ForEachFrame(base::Bind( | 129 web_contents->ForEachFrame(base::Bind( |
106 &MHTMLGenerationManager::Job::AddFrame, | 130 &MHTMLGenerationManager::Job::AddFrame, |
107 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 131 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
108 | 132 |
109 // Main frame needs to be processed first. | 133 // Main frame needs to be processed first. |
110 DCHECK(!pending_frame_tree_node_ids_.empty()); | 134 DCHECK(!pending_frame_tree_node_ids_.empty()); |
111 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) | 135 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) |
(...skipping 23 matching lines...) Expand all Loading... |
135 | 159 |
136 result[routing_id] = content_id; | 160 result[routing_id] = content_id; |
137 } | 161 } |
138 return result; | 162 return result; |
139 } | 163 } |
140 | 164 |
141 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { | 165 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { |
142 DCHECK(browser_file_.IsValid()); | 166 DCHECK(browser_file_.IsValid()); |
143 DCHECK_LT(0u, pending_frame_tree_node_ids_.size()); | 167 DCHECK_LT(0u, pending_frame_tree_node_ids_.size()); |
144 | 168 |
| 169 FrameMsg_SerializeAsMHTML_Params ipc_params; |
| 170 ipc_params.job_id = job_id_; |
| 171 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; |
| 172 |
145 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); | 173 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); |
146 pending_frame_tree_node_ids_.pop(); | 174 pending_frame_tree_node_ids_.pop(); |
147 bool is_last_frame = pending_frame_tree_node_ids_.empty(); | 175 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); |
148 | 176 |
149 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); | 177 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
150 if (!ftn) // The contents went away. | 178 if (!ftn) // The contents went away. |
151 return false; | 179 return false; |
152 RenderFrameHost* rfh = ftn->current_frame_host(); | 180 RenderFrameHost* rfh = ftn->current_frame_host(); |
153 | 181 |
154 // Get notified if the target of the IPC message dies between responding. | 182 // Get notified if the target of the IPC message dies between responding. |
155 observed_renderer_process_host_.RemoveAll(); | 183 observed_renderer_process_host_.RemoveAll(); |
156 observed_renderer_process_host_.Add(rfh->GetProcess()); | 184 observed_renderer_process_host_.Add(rfh->GetProcess()); |
157 | 185 |
158 IPC::PlatformFileForTransit renderer_file = IPC::GetFileHandleForProcess( | 186 // Tell the renderer to skip (= deduplicate) already covered MHTML parts. |
| 187 ipc_params.salt = salt_; |
| 188 ipc_params.digests_of_uris_to_skip = digests_of_already_serialized_uris_; |
| 189 |
| 190 ipc_params.destination_file = IPC::GetFileHandleForProcess( |
159 browser_file_.GetPlatformFile(), rfh->GetProcess()->GetHandle(), | 191 browser_file_.GetPlatformFile(), rfh->GetProcess()->GetHandle(), |
160 false); // |close_source_handle|. | 192 false); // |close_source_handle|. |
161 rfh->Send(new FrameMsg_SerializeAsMHTML( | 193 ipc_params.frame_routing_id_to_content_id = |
162 rfh->GetRoutingID(), job_id_, renderer_file, mhtml_boundary_marker_, | 194 CreateFrameRoutingIdToContentId(rfh->GetSiteInstance()); |
163 CreateFrameRoutingIdToContentId(rfh->GetSiteInstance()), is_last_frame)); | 195 |
| 196 // Send the IPC asking the renderer to serialize the frame. |
| 197 DCHECK_EQ(FrameTreeNode::kFrameTreeNodeInvalidId, |
| 198 frame_tree_node_id_of_busy_frame_); |
| 199 frame_tree_node_id_of_busy_frame_ = frame_tree_node_id; |
| 200 rfh->Send(new FrameMsg_SerializeAsMHTML(rfh->GetRoutingID(), ipc_params)); |
164 return true; | 201 return true; |
165 } | 202 } |
166 | 203 |
167 void MHTMLGenerationManager::Job::RenderProcessExited( | 204 void MHTMLGenerationManager::Job::RenderProcessExited( |
168 RenderProcessHost* host, | 205 RenderProcessHost* host, |
169 base::TerminationStatus status, | 206 base::TerminationStatus status, |
170 int exit_code) { | 207 int exit_code) { |
171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 208 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
172 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); | 209 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); |
173 } | 210 } |
(...skipping 24 matching lines...) Expand all Loading... |
198 return; | 235 return; |
199 } | 236 } |
200 | 237 |
201 BrowserThread::PostTaskAndReplyWithResult( | 238 BrowserThread::PostTaskAndReplyWithResult( |
202 BrowserThread::FILE, FROM_HERE, | 239 BrowserThread::FILE, FROM_HERE, |
203 base::Bind(&MHTMLGenerationManager::Job::CloseFileOnFileThread, | 240 base::Bind(&MHTMLGenerationManager::Job::CloseFileOnFileThread, |
204 base::Passed(browser_file_.Pass())), | 241 base::Passed(browser_file_.Pass())), |
205 callback); | 242 callback); |
206 } | 243 } |
207 | 244 |
| 245 bool MHTMLGenerationManager::Job::OnSerializeAsMHTMLResponse( |
| 246 RenderFrameHostImpl* sender, |
| 247 const std::set<std::string>& digests_of_uris_of_serialized_resources) { |
| 248 // Sanitize renderer input / reject unexpected messages. |
| 249 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); |
| 250 if (sender_id != frame_tree_node_id_of_busy_frame_) { |
| 251 NOTREACHED(); |
| 252 return false; // Report failure. |
| 253 } |
| 254 frame_tree_node_id_of_busy_frame_ = FrameTreeNode::kFrameTreeNodeInvalidId; |
| 255 |
| 256 // Renderer should be deduping resources with the same uris. |
| 257 DCHECK_EQ(0u, base::STLSetIntersection<std::set<std::string>>( |
| 258 digests_of_already_serialized_uris_, |
| 259 digests_of_uris_of_serialized_resources).size()); |
| 260 digests_of_already_serialized_uris_.insert( |
| 261 digests_of_uris_of_serialized_resources.begin(), |
| 262 digests_of_uris_of_serialized_resources.end()); |
| 263 |
| 264 if (pending_frame_tree_node_ids_.empty()) |
| 265 return true; // Report success. |
| 266 |
| 267 return SendToNextRenderFrame(); |
| 268 } |
| 269 |
208 // static | 270 // static |
209 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { | 271 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { |
210 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 272 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
211 DCHECK(file.IsValid()); | 273 DCHECK(file.IsValid()); |
212 int64 file_size = file.GetLength(); | 274 int64 file_size = file.GetLength(); |
213 file.Close(); | 275 file.Close(); |
214 return file_size; | 276 return file_size; |
215 } | 277 } |
216 | 278 |
217 // static | 279 // static |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 int job_id = NewJob(web_contents, callback); | 313 int job_id = NewJob(web_contents, callback); |
252 | 314 |
253 BrowserThread::PostTaskAndReplyWithResult( | 315 BrowserThread::PostTaskAndReplyWithResult( |
254 BrowserThread::FILE, FROM_HERE, | 316 BrowserThread::FILE, FROM_HERE, |
255 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), | 317 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), |
256 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 318 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
257 base::Unretained(this), // Safe b/c |this| is a singleton. | 319 base::Unretained(this), // Safe b/c |this| is a singleton. |
258 job_id)); | 320 job_id)); |
259 } | 321 } |
260 | 322 |
261 void MHTMLGenerationManager::OnSavedFrameAsMHTML( | 323 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
| 324 RenderFrameHostImpl* sender, |
262 int job_id, | 325 int job_id, |
263 bool mhtml_generation_in_renderer_succeeded) { | 326 bool mhtml_generation_in_renderer_succeeded, |
| 327 const std::set<std::string>& digests_of_uris_of_serialized_resources) { |
264 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 328 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
265 | 329 |
266 if (!mhtml_generation_in_renderer_succeeded) { | 330 if (!mhtml_generation_in_renderer_succeeded) { |
267 JobFinished(job_id, JobStatus::FAILURE); | 331 JobFinished(job_id, JobStatus::FAILURE); |
268 return; | 332 return; |
269 } | 333 } |
270 | 334 |
271 Job* job = FindJob(job_id); | 335 Job* job = FindJob(job_id); |
272 if (!job) | 336 if (!job) |
273 return; | 337 return; |
274 | 338 |
275 if (job->HasMoreFramesToProcess()) { | 339 if (!job->OnSerializeAsMHTMLResponse( |
276 if (!job->SendToNextRenderFrame()) { | 340 sender, digests_of_uris_of_serialized_resources)) { |
277 JobFinished(job_id, JobStatus::FAILURE); | 341 JobFinished(job_id, JobStatus::FAILURE); |
278 } | |
279 return; | 342 return; |
280 } | 343 } |
281 | 344 |
282 JobFinished(job_id, JobStatus::SUCCESS); | 345 if (job->IsDone()) |
| 346 JobFinished(job_id, JobStatus::SUCCESS); |
283 } | 347 } |
284 | 348 |
285 // static | 349 // static |
286 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { | 350 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { |
287 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 351 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
288 | 352 |
289 // SECURITY NOTE: A file descriptor to the file created below will be passed | 353 // 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 | 354 // 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 | 355 // 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 | 356 // 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) { | 442 ++it) { |
379 if (it->second == job) { | 443 if (it->second == job) { |
380 JobFinished(it->first, JobStatus::FAILURE); | 444 JobFinished(it->first, JobStatus::FAILURE); |
381 return; | 445 return; |
382 } | 446 } |
383 } | 447 } |
384 NOTREACHED(); | 448 NOTREACHED(); |
385 } | 449 } |
386 | 450 |
387 } // namespace content | 451 } // namespace content |
OLD | NEW |