| 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 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "net/base/mime_util.h" | 27 #include "net/base/mime_util.h" |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 | 30 |
| 31 // 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 |
| 32 // are executed on other threads. | 32 // are executed on other threads. |
| 33 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { | 33 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { |
| 34 public: | 34 public: |
| 35 Job(int job_id, | 35 Job(int job_id, |
| 36 WebContents* web_contents, | 36 WebContents* web_contents, |
| 37 bool use_binary_encoding, |
| 37 const GenerateMHTMLCallback& callback); | 38 const GenerateMHTMLCallback& callback); |
| 38 ~Job() override; | 39 ~Job() override; |
| 39 | 40 |
| 40 int id() const { return job_id_; } | 41 int id() const { return job_id_; } |
| 41 void set_browser_file(base::File file) { browser_file_ = std::move(file); } | 42 void set_browser_file(base::File file) { browser_file_ = std::move(file); } |
| 42 | 43 |
| 43 const GenerateMHTMLCallback& callback() const { return callback_; } | 44 const GenerateMHTMLCallback& callback() const { return callback_; } |
| 44 | 45 |
| 45 // Handler for FrameHostMsg_SerializeAsMHTMLResponse (a notification from the | 46 // Handler for FrameHostMsg_SerializeAsMHTMLResponse (a notification from the |
| 46 // renderer that the MHTML generation for previous frame has finished). | 47 // renderer that the MHTML generation for previous frame has finished). |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // Creates a new map with values (content ids) the same as in | 81 // 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 | 82 // |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. | 83 // frame_tree_node_id into a |site_instance|-specific routing_id. |
| 83 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 84 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
| 84 SiteInstance* site_instance); | 85 SiteInstance* site_instance); |
| 85 | 86 |
| 86 // Id used to map renderer responses to jobs. | 87 // Id used to map renderer responses to jobs. |
| 87 // See also MHTMLGenerationManager::id_to_job_ map. | 88 // See also MHTMLGenerationManager::id_to_job_ map. |
| 88 int job_id_; | 89 int job_id_; |
| 89 | 90 |
| 91 // Whether to use binary encoding while generating MHTML. |
| 92 bool use_binary_encoding_; |
| 93 |
| 90 // The IDs of frames that still need to be processed. | 94 // The IDs of frames that still need to be processed. |
| 91 std::queue<int> pending_frame_tree_node_ids_; | 95 std::queue<int> pending_frame_tree_node_ids_; |
| 92 | 96 |
| 93 // Identifies a frame to which we've sent FrameMsg_SerializeAsMHTML but for | 97 // Identifies a frame to which we've sent FrameMsg_SerializeAsMHTML but for |
| 94 // which we didn't yet process FrameHostMsg_SerializeAsMHTMLResponse via | 98 // which we didn't yet process FrameHostMsg_SerializeAsMHTMLResponse via |
| 95 // OnSerializeAsMHTMLResponse. | 99 // OnSerializeAsMHTMLResponse. |
| 96 int frame_tree_node_id_of_busy_frame_; | 100 int frame_tree_node_id_of_busy_frame_; |
| 97 | 101 |
| 98 // The handle to the file the MHTML is saved to for the browser process. | 102 // The handle to the file the MHTML is saved to for the browser process. |
| 99 base::File browser_file_; | 103 base::File browser_file_; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 114 | 118 |
| 115 // RAII helper for registering this Job as a RenderProcessHost observer. | 119 // RAII helper for registering this Job as a RenderProcessHost observer. |
| 116 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 120 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
| 117 observed_renderer_process_host_; | 121 observed_renderer_process_host_; |
| 118 | 122 |
| 119 DISALLOW_COPY_AND_ASSIGN(Job); | 123 DISALLOW_COPY_AND_ASSIGN(Job); |
| 120 }; | 124 }; |
| 121 | 125 |
| 122 MHTMLGenerationManager::Job::Job(int job_id, | 126 MHTMLGenerationManager::Job::Job(int job_id, |
| 123 WebContents* web_contents, | 127 WebContents* web_contents, |
| 128 bool use_binary_encoding, |
| 124 const GenerateMHTMLCallback& callback) | 129 const GenerateMHTMLCallback& callback) |
| 125 : job_id_(job_id), | 130 : job_id_(job_id), |
| 131 use_binary_encoding_(use_binary_encoding), |
| 126 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), | 132 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
| 127 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), | 133 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), |
| 128 salt_(base::GenerateGUID()), | 134 salt_(base::GenerateGUID()), |
| 129 callback_(callback), | 135 callback_(callback), |
| 130 observed_renderer_process_host_(this) { | 136 observed_renderer_process_host_(this) { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 132 web_contents->ForEachFrame(base::Bind( | 138 web_contents->ForEachFrame(base::Bind( |
| 133 &MHTMLGenerationManager::Job::AddFrame, | 139 &MHTMLGenerationManager::Job::AddFrame, |
| 134 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 140 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
| 135 | 141 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 165 return result; | 171 return result; |
| 166 } | 172 } |
| 167 | 173 |
| 168 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { | 174 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { |
| 169 DCHECK(browser_file_.IsValid()); | 175 DCHECK(browser_file_.IsValid()); |
| 170 DCHECK(!pending_frame_tree_node_ids_.empty()); | 176 DCHECK(!pending_frame_tree_node_ids_.empty()); |
| 171 | 177 |
| 172 FrameMsg_SerializeAsMHTML_Params ipc_params; | 178 FrameMsg_SerializeAsMHTML_Params ipc_params; |
| 173 ipc_params.job_id = job_id_; | 179 ipc_params.job_id = job_id_; |
| 174 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; | 180 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; |
| 181 ipc_params.mhtml_binary_encoding = use_binary_encoding_; |
| 175 | 182 |
| 176 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); | 183 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); |
| 177 pending_frame_tree_node_ids_.pop(); | 184 pending_frame_tree_node_ids_.pop(); |
| 178 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); | 185 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); |
| 179 | 186 |
| 180 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); | 187 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
| 181 if (!ftn) // The contents went away. | 188 if (!ftn) // The contents went away. |
| 182 return false; | 189 return false; |
| 183 RenderFrameHost* rfh = ftn->current_frame_host(); | 190 RenderFrameHost* rfh = ftn->current_frame_host(); |
| 184 | 191 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 return base::Singleton<MHTMLGenerationManager>::get(); | 290 return base::Singleton<MHTMLGenerationManager>::get(); |
| 284 } | 291 } |
| 285 | 292 |
| 286 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 293 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
| 287 | 294 |
| 288 MHTMLGenerationManager::~MHTMLGenerationManager() { | 295 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 289 STLDeleteValues(&id_to_job_); | 296 STLDeleteValues(&id_to_job_); |
| 290 } | 297 } |
| 291 | 298 |
| 292 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 299 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
| 300 bool use_binary_encoding, |
| 293 const base::FilePath& file_path, | 301 const base::FilePath& file_path, |
| 294 const GenerateMHTMLCallback& callback) { | 302 const GenerateMHTMLCallback& callback) { |
| 295 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 303 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 296 | 304 |
| 297 int job_id = NewJob(web_contents, callback); | 305 int job_id = NewJob(web_contents, use_binary_encoding, callback); |
| 298 | 306 |
| 299 BrowserThread::PostTaskAndReplyWithResult( | 307 BrowserThread::PostTaskAndReplyWithResult( |
| 300 BrowserThread::FILE, FROM_HERE, | 308 BrowserThread::FILE, FROM_HERE, |
| 301 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), | 309 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), |
| 302 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 310 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
| 303 base::Unretained(this), // Safe b/c |this| is a singleton. | 311 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 304 job_id)); | 312 job_id)); |
| 305 } | 313 } |
| 306 | 314 |
| 307 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( | 315 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 int64_t file_size) { | 396 int64_t file_size) { |
| 389 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 397 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 390 | 398 |
| 391 Job* job = FindJob(job_id); | 399 Job* job = FindJob(job_id); |
| 392 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); | 400 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); |
| 393 id_to_job_.erase(job_id); | 401 id_to_job_.erase(job_id); |
| 394 delete job; | 402 delete job; |
| 395 } | 403 } |
| 396 | 404 |
| 397 int MHTMLGenerationManager::NewJob(WebContents* web_contents, | 405 int MHTMLGenerationManager::NewJob(WebContents* web_contents, |
| 406 bool use_binary_encoding, |
| 398 const GenerateMHTMLCallback& callback) { | 407 const GenerateMHTMLCallback& callback) { |
| 399 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 408 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 400 | 409 |
| 401 int job_id = next_job_id_++; | 410 int job_id = next_job_id_++; |
| 402 id_to_job_[job_id] = new Job(job_id, web_contents, callback); | 411 id_to_job_[job_id] = |
| 412 new Job(job_id, web_contents, use_binary_encoding, callback); |
| 403 return job_id; | 413 return job_id; |
| 404 } | 414 } |
| 405 | 415 |
| 406 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { | 416 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { |
| 407 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 417 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 408 | 418 |
| 409 IDToJobMap::iterator iter = id_to_job_.find(job_id); | 419 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 410 if (iter == id_to_job_.end()) { | 420 if (iter == id_to_job_.end()) { |
| 411 NOTREACHED(); | 421 NOTREACHED(); |
| 412 return nullptr; | 422 return nullptr; |
| 413 } | 423 } |
| 414 return iter->second; | 424 return iter->second; |
| 415 } | 425 } |
| 416 | 426 |
| 417 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 427 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 418 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 428 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 419 DCHECK(job); | 429 DCHECK(job); |
| 420 JobFinished(job, JobStatus::FAILURE); | 430 JobFinished(job, JobStatus::FAILURE); |
| 421 } | 431 } |
| 422 | 432 |
| 423 } // namespace content | 433 } // namespace content |
| OLD | NEW |