Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: content/browser/download/mhtml_generation_manager.cc

Issue 1547593002: Introducing a net::GenerateMimeMultipartBoundary helper. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Self-review. Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
14 #include "base/scoped_observer.h" 13 #include "base/scoped_observer.h"
15 #include "base/stl_util.h" 14 #include "base/stl_util.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
18 #include "content/browser/frame_host/frame_tree_node.h" 16 #include "content/browser/frame_host/frame_tree_node.h"
19 #include "content/common/frame_messages.h" 17 #include "content/common/frame_messages.h"
20 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/render_frame_host.h" 19 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/render_process_host.h" 20 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/render_process_host_observer.h" 21 #include "content/public/browser/render_process_host_observer.h"
24 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
23 #include "net/base/mime_util.h"
25 24
26 namespace content { 25 namespace content {
27 26
28 // The class and all of its members live on the UI thread. Only static methods 27 // The class and all of its members live on the UI thread. Only static methods
29 // are executed on other threads. 28 // are executed on other threads.
30 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { 29 class MHTMLGenerationManager::Job : public RenderProcessHostObserver {
31 public: 30 public:
32 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback); 31 Job(int job_id, WebContents* web_contents, GenerateMHTMLCallback callback);
33 ~Job() override; 32 ~Job() override;
34 33
(...skipping 15 matching lines...) Expand all
50 // file size. 49 // file size.
51 void CloseFile(base::Callback<void(int64 file_size)> callback); 50 void CloseFile(base::Callback<void(int64 file_size)> callback);
52 51
53 // RenderProcessHostObserver: 52 // RenderProcessHostObserver:
54 void RenderProcessExited(RenderProcessHost* host, 53 void RenderProcessExited(RenderProcessHost* host,
55 base::TerminationStatus status, 54 base::TerminationStatus status,
56 int exit_code) override; 55 int exit_code) override;
57 void RenderProcessHostDestroyed(RenderProcessHost* host) override; 56 void RenderProcessHostDestroyed(RenderProcessHost* host) override;
58 57
59 private: 58 private:
60 static std::string GenerateMHTMLBoundaryMarker();
61 static int64 CloseFileOnFileThread(base::File file); 59 static int64 CloseFileOnFileThread(base::File file);
62 void AddFrame(RenderFrameHost* render_frame_host); 60 void AddFrame(RenderFrameHost* render_frame_host);
63 61
64 // Creates a new map with values (content ids) the same as in 62 // 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 63 // |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. 64 // frame_tree_node_id into a |site_instance|-specific routing_id.
67 std::map<int, std::string> CreateFrameRoutingIdToContentId( 65 std::map<int, std::string> CreateFrameRoutingIdToContentId(
68 SiteInstance* site_instance); 66 SiteInstance* site_instance);
69 67
70 // Id used to map renderer responses to jobs. 68 // Id used to map renderer responses to jobs.
(...skipping 20 matching lines...) Expand all
91 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> 89 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job>
92 observed_renderer_process_host_; 90 observed_renderer_process_host_;
93 91
94 DISALLOW_COPY_AND_ASSIGN(Job); 92 DISALLOW_COPY_AND_ASSIGN(Job);
95 }; 93 };
96 94
97 MHTMLGenerationManager::Job::Job(int job_id, 95 MHTMLGenerationManager::Job::Job(int job_id,
98 WebContents* web_contents, 96 WebContents* web_contents,
99 GenerateMHTMLCallback callback) 97 GenerateMHTMLCallback callback)
100 : job_id_(job_id), 98 : job_id_(job_id),
101 mhtml_boundary_marker_(GenerateMHTMLBoundaryMarker()), 99 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()),
102 callback_(callback), 100 callback_(callback),
103 observed_renderer_process_host_(this) { 101 observed_renderer_process_host_(this) {
104 DCHECK_CURRENTLY_ON(BrowserThread::UI); 102 DCHECK_CURRENTLY_ON(BrowserThread::UI);
105 web_contents->ForEachFrame(base::Bind( 103 web_contents->ForEachFrame(base::Bind(
106 &MHTMLGenerationManager::Job::AddFrame, 104 &MHTMLGenerationManager::Job::AddFrame,
107 base::Unretained(this))); // Safe because ForEachFrame is synchronous. 105 base::Unretained(this))); // Safe because ForEachFrame is synchronous.
108 106
109 // Main frame needs to be processed first. 107 // Main frame needs to be processed first.
110 DCHECK(!pending_frame_tree_node_ids_.empty()); 108 DCHECK(!pending_frame_tree_node_ids_.empty());
111 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) 109 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front())
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 205
208 // static 206 // static
209 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { 207 int64 MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) {
210 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 208 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
211 DCHECK(file.IsValid()); 209 DCHECK(file.IsValid());
212 int64 file_size = file.GetLength(); 210 int64 file_size = file.GetLength();
213 file.Close(); 211 file.Close();
214 return file_size; 212 return file_size;
215 } 213 }
216 214
217 // static
218 std::string MHTMLGenerationManager::Job::GenerateMHTMLBoundaryMarker() {
219 // 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
222 // Trying to generate random boundaries similar to IE/UnMHT
223 // (ex: ----=_NextPart_000_001B_01CC157B.96F808A0).
224 uint8_t random_values[10];
225 base::RandBytes(&random_values, sizeof(random_values));
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
236 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { 215 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() {
237 return base::Singleton<MHTMLGenerationManager>::get(); 216 return base::Singleton<MHTMLGenerationManager>::get();
238 } 217 }
239 218
240 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} 219 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {}
241 220
242 MHTMLGenerationManager::~MHTMLGenerationManager() { 221 MHTMLGenerationManager::~MHTMLGenerationManager() {
243 STLDeleteValues(&id_to_job_); 222 STLDeleteValues(&id_to_job_);
244 } 223 }
245 224
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 ++it) { 357 ++it) {
379 if (it->second == job) { 358 if (it->second == job) {
380 JobFinished(it->first, JobStatus::FAILURE); 359 JobFinished(it->first, JobStatus::FAILURE);
381 return; 360 return;
382 } 361 }
383 } 362 }
384 NOTREACHED(); 363 NOTREACHED();
385 } 364 }
386 365
387 } // namespace content 366 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698