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/macros.h" |
13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
14 #include "base/scoped_observer.h" | 15 #include "base/scoped_observer.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "content/browser/frame_host/frame_tree_node.h" | 19 #include "content/browser/frame_host/frame_tree_node.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" |
(...skipping 18 matching lines...) Expand all Loading... |
41 // Returns true if the message was sent successfully; false otherwise. | 42 // Returns true if the message was sent successfully; false otherwise. |
42 bool SendToNextRenderFrame(); | 43 bool SendToNextRenderFrame(); |
43 | 44 |
44 // Indicates if more calls to SendToNextRenderFrame are needed. | 45 // Indicates if more calls to SendToNextRenderFrame are needed. |
45 bool HasMoreFramesToProcess() const { | 46 bool HasMoreFramesToProcess() const { |
46 return !pending_frame_tree_node_ids_.empty(); | 47 return !pending_frame_tree_node_ids_.empty(); |
47 } | 48 } |
48 | 49 |
49 // Close the file on the file thread and respond back on the UI thread with | 50 // Close the file on the file thread and respond back on the UI thread with |
50 // file size. | 51 // file size. |
51 void CloseFile(base::Callback<void(int64 file_size)> callback); | 52 void CloseFile(base::Callback<void(int64_t file_size)> callback); |
52 | 53 |
53 // RenderProcessHostObserver: | 54 // RenderProcessHostObserver: |
54 void RenderProcessExited(RenderProcessHost* host, | 55 void RenderProcessExited(RenderProcessHost* host, |
55 base::TerminationStatus status, | 56 base::TerminationStatus status, |
56 int exit_code) override; | 57 int exit_code) override; |
57 void RenderProcessHostDestroyed(RenderProcessHost* host) override; | 58 void RenderProcessHostDestroyed(RenderProcessHost* host) override; |
58 | 59 |
59 private: | 60 private: |
60 static std::string GenerateMHTMLBoundaryMarker(); | 61 static std::string GenerateMHTMLBoundaryMarker(); |
61 static int64 CloseFileOnFileThread(base::File file); | 62 static int64_t CloseFileOnFileThread(base::File file); |
62 void AddFrame(RenderFrameHost* render_frame_host); | 63 void AddFrame(RenderFrameHost* render_frame_host); |
63 | 64 |
64 // Creates a new map with values (content ids) the same as in | 65 // 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 | 66 // |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. | 67 // frame_tree_node_id into a |site_instance|-specific routing_id. |
67 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 68 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
68 SiteInstance* site_instance); | 69 SiteInstance* site_instance); |
69 | 70 |
70 // Id used to map renderer responses to jobs. | 71 // Id used to map renderer responses to jobs. |
71 // See also MHTMLGenerationManager::id_to_job_ map. | 72 // See also MHTMLGenerationManager::id_to_job_ map. |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 frame_tree_node_to_content_id_[frame_tree_node_id] = content_id; | 184 frame_tree_node_to_content_id_[frame_tree_node_id] = content_id; |
184 } | 185 } |
185 | 186 |
186 void MHTMLGenerationManager::Job::RenderProcessHostDestroyed( | 187 void MHTMLGenerationManager::Job::RenderProcessHostDestroyed( |
187 RenderProcessHost* host) { | 188 RenderProcessHost* host) { |
188 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 189 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
189 observed_renderer_process_host_.Remove(host); | 190 observed_renderer_process_host_.Remove(host); |
190 } | 191 } |
191 | 192 |
192 void MHTMLGenerationManager::Job::CloseFile( | 193 void MHTMLGenerationManager::Job::CloseFile( |
193 base::Callback<void(int64)> callback) { | 194 base::Callback<void(int64_t)> callback) { |
194 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 195 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
195 | 196 |
196 if (!browser_file_.IsValid()) { | 197 if (!browser_file_.IsValid()) { |
197 callback.Run(-1); | 198 callback.Run(-1); |
198 return; | 199 return; |
199 } | 200 } |
200 | 201 |
201 BrowserThread::PostTaskAndReplyWithResult( | 202 BrowserThread::PostTaskAndReplyWithResult( |
202 BrowserThread::FILE, FROM_HERE, | 203 BrowserThread::FILE, FROM_HERE, |
203 base::Bind(&MHTMLGenerationManager::Job::CloseFileOnFileThread, | 204 base::Bind(&MHTMLGenerationManager::Job::CloseFileOnFileThread, |
204 base::Passed(browser_file_.Pass())), | 205 base::Passed(browser_file_.Pass())), |
205 callback); | 206 callback); |
206 } | 207 } |
207 | 208 |
208 // static | 209 // static |
209 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { | 210 int64_t MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { |
210 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 211 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
211 DCHECK(file.IsValid()); | 212 DCHECK(file.IsValid()); |
212 int64 file_size = file.GetLength(); | 213 int64_t file_size = file.GetLength(); |
213 file.Close(); | 214 file.Close(); |
214 return file_size; | 215 return file_size; |
215 } | 216 } |
216 | 217 |
217 // static | 218 // static |
218 std::string MHTMLGenerationManager::Job::GenerateMHTMLBoundaryMarker() { | 219 std::string MHTMLGenerationManager::Job::GenerateMHTMLBoundaryMarker() { |
219 // TODO(lukasza): Introduce and use a shared helper function in | 220 // TODO(lukasza): Introduce and use a shared helper function in |
220 // net/base/mime_util.h instead of having the ad-hoc code below. | 221 // net/base/mime_util.h instead of having the ad-hoc code below. |
221 | 222 |
222 // Trying to generate random boundaries similar to IE/UnMHT | 223 // Trying to generate random boundaries similar to IE/UnMHT |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 // static | 286 // static |
286 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { | 287 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { |
287 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 288 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
288 | 289 |
289 // SECURITY NOTE: A file descriptor to the file created below will be passed | 290 // 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 // 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 // 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 // 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 // would allow reading content generated by other renderers / other web |
294 // principals). | 295 // principals). |
295 uint32 file_flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; | 296 uint32_t file_flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; |
296 | 297 |
297 base::File browser_file(file_path, file_flags); | 298 base::File browser_file(file_path, file_flags); |
298 if (!browser_file.IsValid()) { | 299 if (!browser_file.IsValid()) { |
299 LOG(ERROR) << "Failed to create file to save MHTML at: " << | 300 LOG(ERROR) << "Failed to create file to save MHTML at: " << |
300 file_path.value(); | 301 file_path.value(); |
301 } | 302 } |
302 return browser_file.Pass(); | 303 return browser_file.Pass(); |
303 } | 304 } |
304 | 305 |
305 void MHTMLGenerationManager::OnFileAvailable(int job_id, | 306 void MHTMLGenerationManager::OnFileAvailable(int job_id, |
(...skipping 25 matching lines...) Expand all Loading... |
331 return; | 332 return; |
332 | 333 |
333 job->CloseFile( | 334 job->CloseFile( |
334 base::Bind(&MHTMLGenerationManager::OnFileClosed, | 335 base::Bind(&MHTMLGenerationManager::OnFileClosed, |
335 base::Unretained(this), // Safe b/c |this| is a singleton. | 336 base::Unretained(this), // Safe b/c |this| is a singleton. |
336 job_id, job_status)); | 337 job_id, job_status)); |
337 } | 338 } |
338 | 339 |
339 void MHTMLGenerationManager::OnFileClosed(int job_id, | 340 void MHTMLGenerationManager::OnFileClosed(int job_id, |
340 JobStatus job_status, | 341 JobStatus job_status, |
341 int64 file_size) { | 342 int64_t file_size) { |
342 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 343 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
343 | 344 |
344 Job* job = FindJob(job_id); | 345 Job* job = FindJob(job_id); |
345 if (!job) | 346 if (!job) |
346 return; | 347 return; |
347 | 348 |
348 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); | 349 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); |
349 id_to_job_.erase(job_id); | 350 id_to_job_.erase(job_id); |
350 delete job; | 351 delete job; |
351 } | 352 } |
(...skipping 26 matching lines...) Expand all Loading... |
378 ++it) { | 379 ++it) { |
379 if (it->second == job) { | 380 if (it->second == job) { |
380 JobFinished(it->first, JobStatus::FAILURE); | 381 JobFinished(it->first, JobStatus::FAILURE); |
381 return; | 382 return; |
382 } | 383 } |
383 } | 384 } |
384 NOTREACHED(); | 385 NOTREACHED(); |
385 } | 386 } |
386 | 387 |
387 } // namespace content | 388 } // namespace content |
OLD | NEW |