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" | 15 #include "base/rand_util.h" |
16 #include "base/scoped_observer.h" | 16 #include "base/scoped_observer.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
18 #include "base/strings/string_number_conversions.h" | |
19 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
20 #include "content/browser/frame_host/frame_tree_node.h" | 19 #include "content/browser/frame_host/frame_tree_node.h" |
21 #include "content/browser/frame_host/render_frame_host_impl.h" | 20 #include "content/browser/frame_host/render_frame_host_impl.h" |
22 #include "content/common/frame_messages.h" | 21 #include "content/common/frame_messages.h" |
23 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
25 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
26 #include "content/public/browser/render_process_host_observer.h" | 25 #include "content/public/browser/render_process_host_observer.h" |
27 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
28 #include "url/gurl.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 26 matching lines...) Expand all Loading... |
65 // file size. | 64 // file size. |
66 void CloseFile(base::Callback<void(int64_t file_size)> callback); | 65 void CloseFile(base::Callback<void(int64_t file_size)> callback); |
67 | 66 |
68 // RenderProcessHostObserver: | 67 // RenderProcessHostObserver: |
69 void RenderProcessExited(RenderProcessHost* host, | 68 void RenderProcessExited(RenderProcessHost* host, |
70 base::TerminationStatus status, | 69 base::TerminationStatus status, |
71 int exit_code) override; | 70 int exit_code) override; |
72 void RenderProcessHostDestroyed(RenderProcessHost* host) override; | 71 void RenderProcessHostDestroyed(RenderProcessHost* host) override; |
73 | 72 |
74 private: | 73 private: |
75 static std::string GenerateMHTMLBoundaryMarker(); | |
76 static int64_t CloseFileOnFileThread(base::File file); | 74 static int64_t CloseFileOnFileThread(base::File file); |
77 void AddFrame(RenderFrameHost* render_frame_host); | 75 void AddFrame(RenderFrameHost* render_frame_host); |
78 | 76 |
79 // 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 |
80 // |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 |
81 // frame_tree_node_id into a |site_instance|-specific routing_id. | 79 // frame_tree_node_id into a |site_instance|-specific routing_id. |
82 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 80 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
83 SiteInstance* site_instance); | 81 SiteInstance* site_instance); |
84 | 82 |
85 // Id used to map renderer responses to jobs. | 83 // Id used to map renderer responses to jobs. |
(...skipping 30 matching lines...) Expand all Loading... |
116 observed_renderer_process_host_; | 114 observed_renderer_process_host_; |
117 | 115 |
118 DISALLOW_COPY_AND_ASSIGN(Job); | 116 DISALLOW_COPY_AND_ASSIGN(Job); |
119 }; | 117 }; |
120 | 118 |
121 MHTMLGenerationManager::Job::Job(int job_id, | 119 MHTMLGenerationManager::Job::Job(int job_id, |
122 WebContents* web_contents, | 120 WebContents* web_contents, |
123 GenerateMHTMLCallback callback) | 121 GenerateMHTMLCallback callback) |
124 : job_id_(job_id), | 122 : job_id_(job_id), |
125 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), | 123 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
126 mhtml_boundary_marker_(GenerateMHTMLBoundaryMarker()), | 124 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), |
127 salt_(base::GenerateGUID()), | 125 salt_(base::GenerateGUID()), |
128 callback_(callback), | 126 callback_(callback), |
129 observed_renderer_process_host_(this) { | 127 observed_renderer_process_host_(this) { |
130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
131 web_contents->ForEachFrame(base::Bind( | 129 web_contents->ForEachFrame(base::Bind( |
132 &MHTMLGenerationManager::Job::AddFrame, | 130 &MHTMLGenerationManager::Job::AddFrame, |
133 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 131 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
134 | 132 |
135 // Main frame needs to be processed first. | 133 // Main frame needs to be processed first. |
136 DCHECK(!pending_frame_tree_node_ids_.empty()); | 134 DCHECK(!pending_frame_tree_node_ids_.empty()); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 | 269 |
272 // static | 270 // static |
273 int64_t MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { | 271 int64_t MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { |
274 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 272 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
275 DCHECK(file.IsValid()); | 273 DCHECK(file.IsValid()); |
276 int64_t file_size = file.GetLength(); | 274 int64_t file_size = file.GetLength(); |
277 file.Close(); | 275 file.Close(); |
278 return file_size; | 276 return file_size; |
279 } | 277 } |
280 | 278 |
281 // static | |
282 std::string MHTMLGenerationManager::Job::GenerateMHTMLBoundaryMarker() { | |
283 // TODO(lukasza): Introduce and use a shared helper function in | |
284 // net/base/mime_util.h instead of having the ad-hoc code below. | |
285 | |
286 // Trying to generate random boundaries similar to IE/UnMHT | |
287 // (ex: ----=_NextPart_000_001B_01CC157B.96F808A0). | |
288 uint8_t random_values[10]; | |
289 base::RandBytes(random_values, sizeof(random_values)); | |
290 | |
291 std::string result("----=_NextPart_000_"); | |
292 result += base::HexEncode(random_values + 0, 2); | |
293 result += '_'; | |
294 result += base::HexEncode(random_values + 2, 4); | |
295 result += '.'; | |
296 result += base::HexEncode(random_values + 6, 4); | |
297 return result; | |
298 } | |
299 | |
300 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { | 279 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { |
301 return base::Singleton<MHTMLGenerationManager>::get(); | 280 return base::Singleton<MHTMLGenerationManager>::get(); |
302 } | 281 } |
303 | 282 |
304 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 283 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
305 | 284 |
306 MHTMLGenerationManager::~MHTMLGenerationManager() { | 285 MHTMLGenerationManager::~MHTMLGenerationManager() { |
307 STLDeleteValues(&id_to_job_); | 286 STLDeleteValues(&id_to_job_); |
308 } | 287 } |
309 | 288 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 ++it) { | 423 ++it) { |
445 if (it->second == job) { | 424 if (it->second == job) { |
446 JobFinished(it->first, JobStatus::FAILURE); | 425 JobFinished(it->first, JobStatus::FAILURE); |
447 return; | 426 return; |
448 } | 427 } |
449 } | 428 } |
450 NOTREACHED(); | 429 NOTREACHED(); |
451 } | 430 } |
452 | 431 |
453 } // namespace content | 432 } // namespace content |
OLD | NEW |