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 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/guid.h" | 13 #include "base/guid.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/rand_util.h" | |
16 #include "base/scoped_observer.h" | 15 #include "base/scoped_observer.h" |
17 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
18 #include "base/strings/string_number_conversions.h" | |
19 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
20 #include "content/browser/bad_message.h" | 18 #include "content/browser/bad_message.h" |
21 #include "content/browser/frame_host/frame_tree_node.h" | 19 #include "content/browser/frame_host/frame_tree_node.h" |
22 #include "content/browser/frame_host/render_frame_host_impl.h" | 20 #include "content/browser/frame_host/render_frame_host_impl.h" |
23 #include "content/common/frame_messages.h" | 21 #include "content/common/frame_messages.h" |
24 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
26 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
27 #include "content/public/browser/render_process_host_observer.h" | 25 #include "content/public/browser/render_process_host_observer.h" |
28 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "net/base/mime_util.h" |
29 | 28 |
30 namespace content { | 29 namespace content { |
31 | 30 |
32 // The class and all of its members live on the UI thread. Only static methods | 31 // The class and all of its members live on the UI thread. Only static methods |
33 // are executed on other threads. | 32 // are executed on other threads. |
34 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { | 33 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { |
35 public: | 34 public: |
36 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback); | 35 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback); |
37 ~Job() override; | 36 ~Job() override; |
38 | 37 |
(...skipping 27 matching lines...) Expand all Loading... |
66 // file size. | 65 // file size. |
67 void CloseFile(base::Callback<void(int64_t file_size)> callback); | 66 void CloseFile(base::Callback<void(int64_t file_size)> callback); |
68 | 67 |
69 // RenderProcessHostObserver: | 68 // RenderProcessHostObserver: |
70 void RenderProcessExited(RenderProcessHost* host, | 69 void RenderProcessExited(RenderProcessHost* host, |
71 base::TerminationStatus status, | 70 base::TerminationStatus status, |
72 int exit_code) override; | 71 int exit_code) override; |
73 void RenderProcessHostDestroyed(RenderProcessHost* host) override; | 72 void RenderProcessHostDestroyed(RenderProcessHost* host) override; |
74 | 73 |
75 private: | 74 private: |
76 static std::string GenerateMHTMLBoundaryMarker(); | |
77 static int64_t CloseFileOnFileThread(base::File file); | 75 static int64_t CloseFileOnFileThread(base::File file); |
78 void AddFrame(RenderFrameHost* render_frame_host); | 76 void AddFrame(RenderFrameHost* render_frame_host); |
79 | 77 |
80 // Creates a new map with values (content ids) the same as in | 78 // Creates a new map with values (content ids) the same as in |
81 // |frame_tree_node_to_content_id_| map, but with the keys translated from | 79 // |frame_tree_node_to_content_id_| map, but with the keys translated from |
82 // frame_tree_node_id into a |site_instance|-specific routing_id. | 80 // frame_tree_node_id into a |site_instance|-specific routing_id. |
83 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 81 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
84 SiteInstance* site_instance); | 82 SiteInstance* site_instance); |
85 | 83 |
86 // Id used to map renderer responses to jobs. | 84 // Id used to map renderer responses to jobs. |
(...skipping 30 matching lines...) Expand all Loading... |
117 observed_renderer_process_host_; | 115 observed_renderer_process_host_; |
118 | 116 |
119 DISALLOW_COPY_AND_ASSIGN(Job); | 117 DISALLOW_COPY_AND_ASSIGN(Job); |
120 }; | 118 }; |
121 | 119 |
122 MHTMLGenerationManager::Job::Job(int job_id, | 120 MHTMLGenerationManager::Job::Job(int job_id, |
123 WebContents* web_contents, | 121 WebContents* web_contents, |
124 GenerateMHTMLCallback callback) | 122 GenerateMHTMLCallback callback) |
125 : job_id_(job_id), | 123 : job_id_(job_id), |
126 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), | 124 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
127 mhtml_boundary_marker_(GenerateMHTMLBoundaryMarker()), | 125 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), |
128 salt_(base::GenerateGUID()), | 126 salt_(base::GenerateGUID()), |
129 callback_(callback), | 127 callback_(callback), |
130 observed_renderer_process_host_(this) { | 128 observed_renderer_process_host_(this) { |
131 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
132 web_contents->ForEachFrame(base::Bind( | 130 web_contents->ForEachFrame(base::Bind( |
133 &MHTMLGenerationManager::Job::AddFrame, | 131 &MHTMLGenerationManager::Job::AddFrame, |
134 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 132 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
135 | 133 |
136 // Main frame needs to be processed first. | 134 // Main frame needs to be processed first. |
137 DCHECK(!pending_frame_tree_node_ids_.empty()); | 135 DCHECK(!pending_frame_tree_node_ids_.empty()); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 271 |
274 // static | 272 // static |
275 int64_t MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { | 273 int64_t MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { |
276 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 274 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
277 DCHECK(file.IsValid()); | 275 DCHECK(file.IsValid()); |
278 int64_t file_size = file.GetLength(); | 276 int64_t file_size = file.GetLength(); |
279 file.Close(); | 277 file.Close(); |
280 return file_size; | 278 return file_size; |
281 } | 279 } |
282 | 280 |
283 // static | |
284 std::string MHTMLGenerationManager::Job::GenerateMHTMLBoundaryMarker() { | |
285 // TODO(lukasza): Introduce and use a shared helper function in | |
286 // net/base/mime_util.h instead of having the ad-hoc code below. | |
287 | |
288 // Trying to generate random boundaries similar to IE/UnMHT | |
289 // (ex: ----=_NextPart_000_001B_01CC157B.96F808A0). | |
290 uint8_t random_values[10]; | |
291 base::RandBytes(random_values, sizeof(random_values)); | |
292 | |
293 std::string result("----=_NextPart_000_"); | |
294 result += base::HexEncode(random_values + 0, 2); | |
295 result += '_'; | |
296 result += base::HexEncode(random_values + 2, 4); | |
297 result += '.'; | |
298 result += base::HexEncode(random_values + 6, 4); | |
299 return result; | |
300 } | |
301 | |
302 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { | 281 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { |
303 return base::Singleton<MHTMLGenerationManager>::get(); | 282 return base::Singleton<MHTMLGenerationManager>::get(); |
304 } | 283 } |
305 | 284 |
306 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 285 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
307 | 286 |
308 MHTMLGenerationManager::~MHTMLGenerationManager() { | 287 MHTMLGenerationManager::~MHTMLGenerationManager() { |
309 STLDeleteValues(&id_to_job_); | 288 STLDeleteValues(&id_to_job_); |
310 } | 289 } |
311 | 290 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 return iter->second; | 416 return iter->second; |
438 } | 417 } |
439 | 418 |
440 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 419 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
441 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 420 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
442 DCHECK(job); | 421 DCHECK(job); |
443 JobFinished(job, JobStatus::FAILURE); | 422 JobFinished(job, JobStatus::FAILURE); |
444 } | 423 } |
445 | 424 |
446 } // namespace content | 425 } // namespace content |
OLD | NEW |