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> | |
8 #include <queue> | |
9 | |
7 #include "base/bind.h" | 10 #include "base/bind.h" |
8 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/guid.h" | |
13 #include "base/rand_util.h" | |
9 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
10 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
11 #include "content/browser/renderer_host/render_view_host_impl.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/stringprintf.h" | |
18 #include "content/browser/frame_host/frame_tree_node.h" | |
19 #include "content/common/frame_messages.h" | |
12 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
14 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/render_process_host_observer.h" | 23 #include "content/public/browser/render_process_host_observer.h" |
16 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
17 #include "content/common/view_messages.h" | |
18 | 25 |
19 namespace content { | 26 namespace content { |
20 | 27 |
21 // The class and all of its members live on the UI thread. Only static methods | 28 // The class and all of its members live on the UI thread. Only static methods |
22 // are executed on other threads. | 29 // are executed on other threads. |
23 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { | 30 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { |
24 public: | 31 public: |
25 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback); | 32 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback); |
26 ~Job() override; | 33 ~Job() override; |
27 | 34 |
28 void set_browser_file(base::File file) { browser_file_ = file.Pass(); } | 35 void set_browser_file(base::File file) { browser_file_ = file.Pass(); } |
29 | 36 |
30 GenerateMHTMLCallback callback() const { return callback_; } | 37 GenerateMHTMLCallback callback() const { return callback_; } |
31 | 38 |
32 // Sends IPC to the renderer, asking for MHTML generation. | 39 // Sends IPC to the renderer, asking for MHTML generation of the next frame. |
40 // | |
33 // Returns true if the message was sent successfully; false otherwise. | 41 // Returns true if the message was sent successfully; false otherwise. |
34 bool SendToRenderView(); | 42 bool SendToNextRenderFrame(); |
43 | |
44 // Indicates if more calls to SendToNextRenderFrame are needed. | |
45 bool GotMoreFramesToProcess() { | |
dcheng
2015/12/09 08:02:37
Nit: HasMoreFramesToProcess() const?
Got sounds l
Łukasz Anforowicz
2015/12/09 17:33:57
Makes sense. Done.
| |
46 return !pending_frame_tree_node_ids_.empty(); | |
47 } | |
35 | 48 |
36 // Close the file on the file thread and respond back on the UI thread with | 49 // Close the file on the file thread and respond back on the UI thread with |
37 // file size. | 50 // file size. |
38 void CloseFile(base::Callback<void(int64 file_size)> callback); | 51 void CloseFile(base::Callback<void(int64 file_size)> callback); |
39 | 52 |
40 // RenderProcessHostObserver: | 53 // RenderProcessHostObserver: |
41 void RenderProcessExited(RenderProcessHost* host, | 54 void RenderProcessExited(RenderProcessHost* host, |
42 base::TerminationStatus status, | 55 base::TerminationStatus status, |
43 int exit_code) override; | 56 int exit_code) override; |
44 void RenderProcessHostDestroyed(RenderProcessHost* host) override; | 57 void RenderProcessHostDestroyed(RenderProcessHost* host) override; |
45 | 58 |
46 private: | 59 private: |
60 static std::string GenerateMhtmlBoundaryMarker(); | |
47 static int64 CloseFileOnFileThread(base::File file); | 61 static int64 CloseFileOnFileThread(base::File file); |
62 void AddFrame(RenderFrameHost* render_frame_host); | |
63 | |
64 // Translates |frame_tree_node_to_content_id_| into | |
65 // a |site_instance|-specific, routing-id-based map. | |
66 std::map<int, std::string> CreateFrameRoutingIdToContentId( | |
67 SiteInstance* site_instance); | |
48 | 68 |
49 // Id used to map renderer responses to jobs. | 69 // Id used to map renderer responses to jobs. |
50 // See also MHTMLGenerationManager::id_to_job_ map. | 70 // See also MHTMLGenerationManager::id_to_job_ map. |
51 int job_id_; | 71 int job_id_; |
52 | 72 |
53 // The handle to the file the MHTML is saved to for the browser process. | 73 // The handle to the file the MHTML is saved to for the browser process. |
54 base::File browser_file_; | 74 base::File browser_file_; |
55 | 75 |
56 // The IDs mapping to a specific contents. | 76 // The IDs of frames we still need to process. |
57 int process_id_; | 77 std::queue<int> pending_frame_tree_node_ids_; |
58 int routing_id_; | 78 |
79 // Map from frames into content ids (see WebPageSerializer::generateMHTMLParts | |
80 // for more details about what "content ids" are and how they are used). | |
81 std::map<int, std::string> frame_tree_node_to_content_id_; | |
82 | |
83 // MIME multipart boundary to use in the MHTML doc. | |
84 std::string mhtml_boundary_marker_; | |
59 | 85 |
60 // The callback to call once generation is complete. | 86 // The callback to call once generation is complete. |
61 GenerateMHTMLCallback callback_; | 87 GenerateMHTMLCallback callback_; |
62 | 88 |
63 // RAII helper for registering this Job as a RenderProcessHost observer. | 89 // RAII helper for registering this Job as a RenderProcessHost observer. |
64 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 90 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
65 observed_renderer_process_host_; | 91 observed_renderer_process_host_; |
66 | 92 |
67 DISALLOW_COPY_AND_ASSIGN(Job); | 93 DISALLOW_COPY_AND_ASSIGN(Job); |
68 }; | 94 }; |
69 | 95 |
70 MHTMLGenerationManager::Job::Job(int job_id, | 96 MHTMLGenerationManager::Job::Job(int job_id, |
71 WebContents* web_contents, | 97 WebContents* web_contents, |
72 GenerateMHTMLCallback callback) | 98 GenerateMHTMLCallback callback) |
73 : job_id_(job_id), | 99 : job_id_(job_id), |
74 process_id_(web_contents->GetRenderProcessHost()->GetID()), | 100 mhtml_boundary_marker_(GenerateMhtmlBoundaryMarker()), |
75 routing_id_(web_contents->GetRenderViewHost()->GetRoutingID()), | |
76 callback_(callback), | 101 callback_(callback), |
77 observed_renderer_process_host_(this) { | 102 observed_renderer_process_host_(this) { |
78 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 103 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
104 web_contents->ForEachFrame(base::Bind( | |
105 &MHTMLGenerationManager::Job::AddFrame, | |
106 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | |
107 | |
108 // Main frame needs to be processed first. | |
109 DCHECK(!pending_frame_tree_node_ids_.empty()); | |
110 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) | |
111 ->parent() == nullptr); | |
79 } | 112 } |
80 | 113 |
81 MHTMLGenerationManager::Job::~Job() { | 114 MHTMLGenerationManager::Job::~Job() { |
82 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 115 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
83 } | 116 } |
84 | 117 |
85 bool MHTMLGenerationManager::Job::SendToRenderView() { | 118 std::map<int, std::string> |
86 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 119 MHTMLGenerationManager::Job::CreateFrameRoutingIdToContentId( |
120 SiteInstance* site_instance) { | |
121 std::map<int, std::string> result; | |
122 for (const auto& it : frame_tree_node_to_content_id_) { | |
123 int ftn_id = it.first; | |
124 const std::string& content_id = it.second; | |
125 | |
126 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(ftn_id); | |
127 if (!ftn) | |
128 continue; | |
129 | |
130 int routing_id = | |
131 ftn->render_manager()->GetRoutingIdForSiteInstance(site_instance); | |
132 if (routing_id == MSG_ROUTING_NONE) | |
133 continue; | |
134 | |
135 result[routing_id] = content_id; | |
136 } | |
137 return result; | |
138 } | |
139 | |
140 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { | |
87 DCHECK(browser_file_.IsValid()); | 141 DCHECK(browser_file_.IsValid()); |
142 DCHECK_LT(0u, pending_frame_tree_node_ids_.size()); | |
88 | 143 |
89 RenderViewHost* rvh = RenderViewHost::FromID(process_id_, routing_id_); | 144 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); |
90 if (!rvh) { // The contents went away. | 145 pending_frame_tree_node_ids_.pop(); |
146 bool is_last_frame = pending_frame_tree_node_ids_.empty(); | |
147 | |
148 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); | |
149 if (!ftn) // The contents went away. | |
91 return false; | 150 return false; |
92 } | 151 RenderFrameHost* rfh = ftn->current_frame_host(); |
93 | 152 |
94 observed_renderer_process_host_.Add(rvh->GetMainFrame()->GetProcess()); | 153 // Get notified if the target of the IPC message dies between responding. |
154 observed_renderer_process_host_.RemoveAll(); | |
155 observed_renderer_process_host_.Add(rfh->GetProcess()); | |
95 | 156 |
96 IPC::PlatformFileForTransit renderer_file = IPC::GetFileHandleForProcess( | 157 IPC::PlatformFileForTransit renderer_file = IPC::GetFileHandleForProcess( |
97 browser_file_.GetPlatformFile(), rvh->GetProcess()->GetHandle(), | 158 browser_file_.GetPlatformFile(), rfh->GetProcess()->GetHandle(), |
98 false); // |close_source_handle|. | 159 false); // |close_source_handle|. |
99 rvh->Send( | 160 rfh->Send(new FrameMsg_SerializeAsMHTML( |
100 new ViewMsg_SavePageAsMHTML(rvh->GetRoutingID(), job_id_, renderer_file)); | 161 rfh->GetRoutingID(), job_id_, renderer_file, mhtml_boundary_marker_, |
162 CreateFrameRoutingIdToContentId(rfh->GetSiteInstance()), is_last_frame)); | |
101 return true; | 163 return true; |
102 } | 164 } |
103 | 165 |
104 void MHTMLGenerationManager::Job::RenderProcessExited( | 166 void MHTMLGenerationManager::Job::RenderProcessExited( |
105 RenderProcessHost* host, | 167 RenderProcessHost* host, |
106 base::TerminationStatus status, | 168 base::TerminationStatus status, |
107 int exit_code) { | 169 int exit_code) { |
108 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 170 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
109 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); | 171 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); |
110 } | 172 } |
111 | 173 |
174 void MHTMLGenerationManager::Job::AddFrame(RenderFrameHost* render_frame_host) { | |
175 auto* rfhi = static_cast<RenderFrameHostImpl*>(render_frame_host); | |
176 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); | |
177 pending_frame_tree_node_ids_.push(frame_tree_node_id); | |
178 | |
179 std::string guid = base::GenerateGUID(); | |
180 std::string content_id = base::StringPrintf("<frame-%d-%s@mhtml.blink>", | |
181 frame_tree_node_id, guid.c_str()); | |
182 frame_tree_node_to_content_id_[frame_tree_node_id] = content_id; | |
183 } | |
184 | |
112 void MHTMLGenerationManager::Job::RenderProcessHostDestroyed( | 185 void MHTMLGenerationManager::Job::RenderProcessHostDestroyed( |
113 RenderProcessHost* host) { | 186 RenderProcessHost* host) { |
114 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 187 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
115 observed_renderer_process_host_.Remove(host); | 188 observed_renderer_process_host_.Remove(host); |
116 } | 189 } |
117 | 190 |
118 void MHTMLGenerationManager::Job::CloseFile( | 191 void MHTMLGenerationManager::Job::CloseFile( |
119 base::Callback<void(int64)> callback) { | 192 base::Callback<void(int64)> callback) { |
120 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 193 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
121 | 194 |
(...skipping 11 matching lines...) Expand all Loading... | |
133 | 206 |
134 // static | 207 // static |
135 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { | 208 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { |
136 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 209 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
137 DCHECK(file.IsValid()); | 210 DCHECK(file.IsValid()); |
138 int64 file_size = file.GetLength(); | 211 int64 file_size = file.GetLength(); |
139 file.Close(); | 212 file.Close(); |
140 return file_size; | 213 return file_size; |
141 } | 214 } |
142 | 215 |
216 // static | |
217 std::string MHTMLGenerationManager::Job::GenerateMhtmlBoundaryMarker() { | |
dcheng
2015/12/09 08:02:37
I think you should stick either with MHTML or Mhtm
Łukasz Anforowicz
2015/12/09 17:33:57
Done.
| |
218 // TODO(lukasza): Introduce and use a shared helper function in | |
219 // net/base/mime_util.h instead of having the ad-hoc code below. | |
220 | |
221 // Trying to generate random boundaries similar to IE/UnMHT | |
222 // (ex: ----=_NextPart_000_001B_01CC157B.96F808A0). | |
223 const size_t kRandomValuesLength = 10; | |
224 uint8_t random_values[kRandomValuesLength]; | |
dcheng
2015/12/09 08:02:37
Just put 10 in here.
Łukasz Anforowicz
2015/12/09 17:33:57
Done.
| |
225 base::RandBytes(&random_values, kRandomValuesLength); | |
dcheng
2015/12/09 08:02:37
And then use sizeof() here.
Łukasz Anforowicz
2015/12/09 17:33:57
Done.
| |
226 | |
227 std::string result("----=_NextPart_000_"); | |
228 result += base::HexEncode(random_values + 0, 2); | |
229 result += '_'; | |
230 result += base::HexEncode(random_values + 2, 4); | |
231 result += '.'; | |
232 result += base::HexEncode(random_values + 6, 4); | |
233 return result; | |
234 } | |
235 | |
143 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { | 236 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { |
144 return base::Singleton<MHTMLGenerationManager>::get(); | 237 return base::Singleton<MHTMLGenerationManager>::get(); |
145 } | 238 } |
146 | 239 |
147 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 240 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
148 | 241 |
149 MHTMLGenerationManager::~MHTMLGenerationManager() { | 242 MHTMLGenerationManager::~MHTMLGenerationManager() { |
150 STLDeleteValues(&id_to_job_); | 243 STLDeleteValues(&id_to_job_); |
151 } | 244 } |
152 | 245 |
153 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 246 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
154 const base::FilePath& file_path, | 247 const base::FilePath& file_path, |
155 const GenerateMHTMLCallback& callback) { | 248 const GenerateMHTMLCallback& callback) { |
156 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 249 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
157 | 250 |
158 int job_id = NewJob(web_contents, callback); | 251 int job_id = NewJob(web_contents, callback); |
159 | 252 |
160 BrowserThread::PostTaskAndReplyWithResult( | 253 BrowserThread::PostTaskAndReplyWithResult( |
161 BrowserThread::FILE, FROM_HERE, | 254 BrowserThread::FILE, FROM_HERE, |
162 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), | 255 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), |
163 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 256 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
164 base::Unretained(this), // Safe b/c |this| is a singleton. | 257 base::Unretained(this), // Safe b/c |this| is a singleton. |
165 job_id)); | 258 job_id)); |
166 } | 259 } |
167 | 260 |
168 void MHTMLGenerationManager::OnSavedPageAsMHTML( | 261 void MHTMLGenerationManager::OnSavedPageAsMHTML( |
169 int job_id, | 262 int job_id, |
170 bool mhtml_generation_in_renderer_succeeded) { | 263 bool mhtml_generation_in_renderer_succeeded) { |
171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 264 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
172 JobStatus job_status = mhtml_generation_in_renderer_succeeded | 265 |
173 ? JobStatus::SUCCESS | 266 if (!mhtml_generation_in_renderer_succeeded) { |
174 : JobStatus::FAILURE; | 267 JobFinished(job_id, JobStatus::FAILURE); |
175 JobFinished(job_id, job_status); | 268 return; |
269 } | |
270 | |
271 Job* job = FindJob(job_id); | |
272 if (!job) | |
273 return; | |
274 | |
275 if (job->GotMoreFramesToProcess()) { | |
276 if (!job->SendToNextRenderFrame()) { | |
277 JobFinished(job_id, JobStatus::FAILURE); | |
278 } | |
279 return; | |
280 } | |
281 | |
282 JobFinished(job_id, JobStatus::SUCCESS); | |
176 } | 283 } |
177 | 284 |
178 // static | 285 // static |
179 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { | 286 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { |
180 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 287 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
181 base::File browser_file( | 288 |
182 file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 289 // 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 | |
291 // 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 | |
293 // would allow reading content generated by other renderers / other web | |
294 // principals). | |
295 uint32 file_flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; | |
296 | |
297 base::File browser_file(file_path, file_flags); | |
183 if (!browser_file.IsValid()) { | 298 if (!browser_file.IsValid()) { |
184 LOG(ERROR) << "Failed to create file to save MHTML at: " << | 299 LOG(ERROR) << "Failed to create file to save MHTML at: " << |
185 file_path.value(); | 300 file_path.value(); |
186 } | 301 } |
187 return browser_file.Pass(); | 302 return browser_file.Pass(); |
188 } | 303 } |
189 | 304 |
190 void MHTMLGenerationManager::OnFileAvailable(int job_id, | 305 void MHTMLGenerationManager::OnFileAvailable(int job_id, |
191 base::File browser_file) { | 306 base::File browser_file) { |
192 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 307 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
193 | 308 |
194 if (!browser_file.IsValid()) { | 309 if (!browser_file.IsValid()) { |
195 LOG(ERROR) << "Failed to create file"; | 310 LOG(ERROR) << "Failed to create file"; |
196 JobFinished(job_id, JobStatus::FAILURE); | 311 JobFinished(job_id, JobStatus::FAILURE); |
197 return; | 312 return; |
198 } | 313 } |
199 | 314 |
200 Job* job = FindJob(job_id); | 315 Job* job = FindJob(job_id); |
201 if (!job) | 316 if (!job) |
202 return; | 317 return; |
203 | 318 |
204 job->set_browser_file(browser_file.Pass()); | 319 job->set_browser_file(browser_file.Pass()); |
205 | 320 |
206 if (!job->SendToRenderView()) { | 321 if (!job->SendToNextRenderFrame()) { |
207 JobFinished(job_id, JobStatus::FAILURE); | 322 JobFinished(job_id, JobStatus::FAILURE); |
208 } | 323 } |
209 } | 324 } |
210 | 325 |
211 void MHTMLGenerationManager::JobFinished(int job_id, JobStatus job_status) { | 326 void MHTMLGenerationManager::JobFinished(int job_id, JobStatus job_status) { |
212 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 327 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
213 | 328 |
214 Job* job = FindJob(job_id); | 329 Job* job = FindJob(job_id); |
215 if (!job) | 330 if (!job) |
216 return; | 331 return; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 ++it) { | 378 ++it) { |
264 if (it->second == job) { | 379 if (it->second == job) { |
265 JobFinished(it->first, JobStatus::FAILURE); | 380 JobFinished(it->first, JobStatus::FAILURE); |
266 return; | 381 return; |
267 } | 382 } |
268 } | 383 } |
269 NOTREACHED(); | 384 NOTREACHED(); |
270 } | 385 } |
271 | 386 |
272 } // namespace content | 387 } // namespace content |
OLD | NEW |