| 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/scoped_observer.h" | 15 #include "base/scoped_observer.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "content/browser/bad_message.h" | 18 #include "content/browser/bad_message.h" |
| 19 #include "content/browser/frame_host/frame_tree_node.h" | 19 #include "content/browser/frame_host/frame_tree_node.h" |
| 20 #include "content/browser/frame_host/render_frame_host_impl.h" | 20 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 21 #include "content/common/frame_messages.h" | 21 #include "content/common/frame_messages.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
| 24 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/browser/render_process_host_observer.h" | 25 #include "content/public/browser/render_process_host_observer.h" |
| 26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/common/mhtml_generation.h" |
| 27 #include "net/base/mime_util.h" | 28 #include "net/base/mime_util.h" |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| 30 | 31 |
| 31 // The class and all of its members live on the UI thread. Only static methods | 32 // The class and all of its members live on the UI thread. Only static methods |
| 32 // are executed on other threads. | 33 // are executed on other threads. |
| 33 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { | 34 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { |
| 34 public: | 35 public: |
| 35 Job(int job_id, | 36 Job(int job_id, |
| 36 WebContents* web_contents, | 37 WebContents* web_contents, |
| 37 bool use_binary_encoding, | 38 const MHTMLGenerationParams& params, |
| 38 const GenerateMHTMLCallback& callback); | 39 const GenerateMHTMLCallback& callback); |
| 39 ~Job() override; | 40 ~Job() override; |
| 40 | 41 |
| 41 int id() const { return job_id_; } | 42 int id() const { return job_id_; } |
| 42 void set_browser_file(base::File file) { browser_file_ = std::move(file); } | 43 void set_browser_file(base::File file) { browser_file_ = std::move(file); } |
| 43 | 44 |
| 44 const GenerateMHTMLCallback& callback() const { return callback_; } | 45 const GenerateMHTMLCallback& callback() const { return callback_; } |
| 45 | 46 |
| 46 // Handler for FrameHostMsg_SerializeAsMHTMLResponse (a notification from the | 47 // Handler for FrameHostMsg_SerializeAsMHTMLResponse (a notification from the |
| 47 // renderer that the MHTML generation for previous frame has finished). | 48 // renderer that the MHTML generation for previous frame has finished). |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // |frame_tree_node_to_content_id_| map, but with the keys translated from | 83 // |frame_tree_node_to_content_id_| map, but with the keys translated from |
| 83 // frame_tree_node_id into a |site_instance|-specific routing_id. | 84 // frame_tree_node_id into a |site_instance|-specific routing_id. |
| 84 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 85 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
| 85 SiteInstance* site_instance); | 86 SiteInstance* site_instance); |
| 86 | 87 |
| 87 // Id used to map renderer responses to jobs. | 88 // Id used to map renderer responses to jobs. |
| 88 // See also MHTMLGenerationManager::id_to_job_ map. | 89 // See also MHTMLGenerationManager::id_to_job_ map. |
| 89 int job_id_; | 90 int job_id_; |
| 90 | 91 |
| 91 // Whether to use binary encoding while generating MHTML. | 92 // Whether to use binary encoding while generating MHTML. |
| 92 bool use_binary_encoding_; | 93 MHTMLGenerationParams params_; |
| 93 | 94 |
| 94 // The IDs of frames that still need to be processed. | 95 // The IDs of frames that still need to be processed. |
| 95 std::queue<int> pending_frame_tree_node_ids_; | 96 std::queue<int> pending_frame_tree_node_ids_; |
| 96 | 97 |
| 97 // Identifies a frame to which we've sent FrameMsg_SerializeAsMHTML but for | 98 // Identifies a frame to which we've sent FrameMsg_SerializeAsMHTML but for |
| 98 // which we didn't yet process FrameHostMsg_SerializeAsMHTMLResponse via | 99 // which we didn't yet process FrameHostMsg_SerializeAsMHTMLResponse via |
| 99 // OnSerializeAsMHTMLResponse. | 100 // OnSerializeAsMHTMLResponse. |
| 100 int frame_tree_node_id_of_busy_frame_; | 101 int frame_tree_node_id_of_busy_frame_; |
| 101 | 102 |
| 102 // The handle to the file the MHTML is saved to for the browser process. | 103 // The handle to the file the MHTML is saved to for the browser process. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 118 | 119 |
| 119 // RAII helper for registering this Job as a RenderProcessHost observer. | 120 // RAII helper for registering this Job as a RenderProcessHost observer. |
| 120 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 121 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
| 121 observed_renderer_process_host_; | 122 observed_renderer_process_host_; |
| 122 | 123 |
| 123 DISALLOW_COPY_AND_ASSIGN(Job); | 124 DISALLOW_COPY_AND_ASSIGN(Job); |
| 124 }; | 125 }; |
| 125 | 126 |
| 126 MHTMLGenerationManager::Job::Job(int job_id, | 127 MHTMLGenerationManager::Job::Job(int job_id, |
| 127 WebContents* web_contents, | 128 WebContents* web_contents, |
| 128 bool use_binary_encoding, | 129 const MHTMLGenerationParams& params, |
| 129 const GenerateMHTMLCallback& callback) | 130 const GenerateMHTMLCallback& callback) |
| 130 : job_id_(job_id), | 131 : job_id_(job_id), |
| 131 use_binary_encoding_(use_binary_encoding), | 132 params_(params), |
| 132 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), | 133 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
| 133 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), | 134 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), |
| 134 salt_(base::GenerateGUID()), | 135 salt_(base::GenerateGUID()), |
| 135 callback_(callback), | 136 callback_(callback), |
| 136 observed_renderer_process_host_(this) { | 137 observed_renderer_process_host_(this) { |
| 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 138 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 138 web_contents->ForEachFrame(base::Bind( | 139 web_contents->ForEachFrame(base::Bind( |
| 139 &MHTMLGenerationManager::Job::AddFrame, | 140 &MHTMLGenerationManager::Job::AddFrame, |
| 140 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 141 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
| 141 | 142 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 171 return result; | 172 return result; |
| 172 } | 173 } |
| 173 | 174 |
| 174 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { | 175 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { |
| 175 DCHECK(browser_file_.IsValid()); | 176 DCHECK(browser_file_.IsValid()); |
| 176 DCHECK(!pending_frame_tree_node_ids_.empty()); | 177 DCHECK(!pending_frame_tree_node_ids_.empty()); |
| 177 | 178 |
| 178 FrameMsg_SerializeAsMHTML_Params ipc_params; | 179 FrameMsg_SerializeAsMHTML_Params ipc_params; |
| 179 ipc_params.job_id = job_id_; | 180 ipc_params.job_id = job_id_; |
| 180 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; | 181 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; |
| 181 ipc_params.mhtml_binary_encoding = use_binary_encoding_; | 182 ipc_params.mhtml_binary_encoding = params_.use_binary_encoding; |
| 183 ipc_params.mhtml_cache_control_policy = params_.cache_control_policy; |
| 182 | 184 |
| 183 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); | 185 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); |
| 184 pending_frame_tree_node_ids_.pop(); | 186 pending_frame_tree_node_ids_.pop(); |
| 185 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); | 187 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); |
| 186 | 188 |
| 187 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); | 189 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
| 188 if (!ftn) // The contents went away. | 190 if (!ftn) // The contents went away. |
| 189 return false; | 191 return false; |
| 190 RenderFrameHost* rfh = ftn->current_frame_host(); | 192 RenderFrameHost* rfh = ftn->current_frame_host(); |
| 191 | 193 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 return base::Singleton<MHTMLGenerationManager>::get(); | 292 return base::Singleton<MHTMLGenerationManager>::get(); |
| 291 } | 293 } |
| 292 | 294 |
| 293 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 295 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
| 294 | 296 |
| 295 MHTMLGenerationManager::~MHTMLGenerationManager() { | 297 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 296 STLDeleteValues(&id_to_job_); | 298 STLDeleteValues(&id_to_job_); |
| 297 } | 299 } |
| 298 | 300 |
| 299 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 301 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
| 300 bool use_binary_encoding, | 302 const MHTMLGenerationParams& params, |
| 301 const base::FilePath& file_path, | |
| 302 const GenerateMHTMLCallback& callback) { | 303 const GenerateMHTMLCallback& callback) { |
| 303 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 304 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 304 | 305 |
| 305 int job_id = NewJob(web_contents, use_binary_encoding, callback); | 306 int job_id = NewJob(web_contents, params, callback); |
| 306 | 307 |
| 307 BrowserThread::PostTaskAndReplyWithResult( | 308 BrowserThread::PostTaskAndReplyWithResult( |
| 308 BrowserThread::FILE, FROM_HERE, | 309 BrowserThread::FILE, FROM_HERE, |
| 309 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), | 310 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), |
| 310 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 311 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
| 311 base::Unretained(this), // Safe b/c |this| is a singleton. | 312 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 312 job_id)); | 313 job_id)); |
| 313 } | 314 } |
| 314 | 315 |
| 315 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( | 316 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
| 316 RenderFrameHostImpl* sender, | 317 RenderFrameHostImpl* sender, |
| 317 int job_id, | 318 int job_id, |
| 318 bool mhtml_generation_in_renderer_succeeded, | 319 MHTMLSerializationResult mhtml_generation_result, |
| 319 const std::set<std::string>& digests_of_uris_of_serialized_resources) { | 320 const std::set<std::string>& digests_of_uris_of_serialized_resources) { |
| 320 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 321 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 321 | 322 |
| 322 Job* job = FindJob(job_id); | 323 Job* job = FindJob(job_id); |
| 323 if (!job) { | 324 if (!job) { |
| 324 ReceivedBadMessage(sender->GetProcess(), | 325 ReceivedBadMessage(sender->GetProcess(), |
| 325 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); | 326 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); |
| 326 return; | 327 return; |
| 327 } | 328 } |
| 328 | 329 |
| 329 if (!mhtml_generation_in_renderer_succeeded) { | 330 if (mhtml_generation_result != MHTMLSerializationResult::OK) { |
| 330 JobFinished(job, JobStatus::FAILURE); | 331 JobFinished(job, JobStatus::FAILURE); |
| 331 return; | 332 return; |
| 332 } | 333 } |
| 333 | 334 |
| 334 if (!job->OnSerializeAsMHTMLResponse( | 335 if (!job->OnSerializeAsMHTMLResponse( |
| 335 sender, digests_of_uris_of_serialized_resources)) { | 336 sender, digests_of_uris_of_serialized_resources)) { |
| 336 JobFinished(job, JobStatus::FAILURE); | 337 JobFinished(job, JobStatus::FAILURE); |
| 337 return; | 338 return; |
| 338 } | 339 } |
| 339 | 340 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 int64_t file_size) { | 397 int64_t file_size) { |
| 397 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 398 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 398 | 399 |
| 399 Job* job = FindJob(job_id); | 400 Job* job = FindJob(job_id); |
| 400 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); | 401 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); |
| 401 id_to_job_.erase(job_id); | 402 id_to_job_.erase(job_id); |
| 402 delete job; | 403 delete job; |
| 403 } | 404 } |
| 404 | 405 |
| 405 int MHTMLGenerationManager::NewJob(WebContents* web_contents, | 406 int MHTMLGenerationManager::NewJob(WebContents* web_contents, |
| 406 bool use_binary_encoding, | 407 const MHTMLGenerationParams& params, |
| 407 const GenerateMHTMLCallback& callback) { | 408 const GenerateMHTMLCallback& callback) { |
| 408 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 409 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 409 | 410 |
| 410 int job_id = next_job_id_++; | 411 int job_id = next_job_id_++; |
| 411 id_to_job_[job_id] = | 412 id_to_job_[job_id] = new Job(job_id, web_contents, params, callback); |
| 412 new Job(job_id, web_contents, use_binary_encoding, callback); | |
| 413 return job_id; | 413 return job_id; |
| 414 } | 414 } |
| 415 | 415 |
| 416 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { | 416 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { |
| 417 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 417 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 418 | 418 |
| 419 IDToJobMap::iterator iter = id_to_job_.find(job_id); | 419 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 420 if (iter == id_to_job_.end()) { | 420 if (iter == id_to_job_.end()) { |
| 421 NOTREACHED(); | 421 NOTREACHED(); |
| 422 return nullptr; | 422 return nullptr; |
| 423 } | 423 } |
| 424 return iter->second; | 424 return iter->second; |
| 425 } | 425 } |
| 426 | 426 |
| 427 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 427 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 428 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 428 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 429 DCHECK(job); | 429 DCHECK(job); |
| 430 JobFinished(job, JobStatus::FAILURE); | 430 JobFinished(job, JobStatus::FAILURE); |
| 431 } | 431 } |
| 432 | 432 |
| 433 } // namespace content | 433 } // namespace content |
| OLD | NEW |