| 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_params.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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 // Creates a new map with values (content ids) the same as in | 82 // Creates a new map with values (content ids) the same as in |
| 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 // User-configurable parameters. Includes the file location, binary encoding |
| 92 bool use_binary_encoding_; | 93 // choices, and whether to skip storing resources marked |
| 94 // Cache-Control: no-store. |
| 95 MHTMLGenerationParams params_; |
| 93 | 96 |
| 94 // The IDs of frames that still need to be processed. | 97 // The IDs of frames that still need to be processed. |
| 95 std::queue<int> pending_frame_tree_node_ids_; | 98 std::queue<int> pending_frame_tree_node_ids_; |
| 96 | 99 |
| 97 // Identifies a frame to which we've sent FrameMsg_SerializeAsMHTML but for | 100 // Identifies a frame to which we've sent FrameMsg_SerializeAsMHTML but for |
| 98 // which we didn't yet process FrameHostMsg_SerializeAsMHTMLResponse via | 101 // which we didn't yet process FrameHostMsg_SerializeAsMHTMLResponse via |
| 99 // OnSerializeAsMHTMLResponse. | 102 // OnSerializeAsMHTMLResponse. |
| 100 int frame_tree_node_id_of_busy_frame_; | 103 int frame_tree_node_id_of_busy_frame_; |
| 101 | 104 |
| 102 // The handle to the file the MHTML is saved to for the browser process. | 105 // The handle to the file the MHTML is saved to for the browser process. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 118 | 121 |
| 119 // RAII helper for registering this Job as a RenderProcessHost observer. | 122 // RAII helper for registering this Job as a RenderProcessHost observer. |
| 120 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 123 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
| 121 observed_renderer_process_host_; | 124 observed_renderer_process_host_; |
| 122 | 125 |
| 123 DISALLOW_COPY_AND_ASSIGN(Job); | 126 DISALLOW_COPY_AND_ASSIGN(Job); |
| 124 }; | 127 }; |
| 125 | 128 |
| 126 MHTMLGenerationManager::Job::Job(int job_id, | 129 MHTMLGenerationManager::Job::Job(int job_id, |
| 127 WebContents* web_contents, | 130 WebContents* web_contents, |
| 128 bool use_binary_encoding, | 131 const MHTMLGenerationParams& params, |
| 129 const GenerateMHTMLCallback& callback) | 132 const GenerateMHTMLCallback& callback) |
| 130 : job_id_(job_id), | 133 : job_id_(job_id), |
| 131 use_binary_encoding_(use_binary_encoding), | 134 params_(params), |
| 132 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), | 135 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
| 133 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), | 136 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), |
| 134 salt_(base::GenerateGUID()), | 137 salt_(base::GenerateGUID()), |
| 135 callback_(callback), | 138 callback_(callback), |
| 136 observed_renderer_process_host_(this) { | 139 observed_renderer_process_host_(this) { |
| 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 140 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 138 web_contents->ForEachFrame(base::Bind( | 141 web_contents->ForEachFrame(base::Bind( |
| 139 &MHTMLGenerationManager::Job::AddFrame, | 142 &MHTMLGenerationManager::Job::AddFrame, |
| 140 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 143 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
| 141 | 144 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 171 return result; | 174 return result; |
| 172 } | 175 } |
| 173 | 176 |
| 174 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { | 177 bool MHTMLGenerationManager::Job::SendToNextRenderFrame() { |
| 175 DCHECK(browser_file_.IsValid()); | 178 DCHECK(browser_file_.IsValid()); |
| 176 DCHECK(!pending_frame_tree_node_ids_.empty()); | 179 DCHECK(!pending_frame_tree_node_ids_.empty()); |
| 177 | 180 |
| 178 FrameMsg_SerializeAsMHTML_Params ipc_params; | 181 FrameMsg_SerializeAsMHTML_Params ipc_params; |
| 179 ipc_params.job_id = job_id_; | 182 ipc_params.job_id = job_id_; |
| 180 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; | 183 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; |
| 181 ipc_params.mhtml_binary_encoding = use_binary_encoding_; | 184 ipc_params.mhtml_binary_encoding = params_.use_binary_encoding; |
| 185 ipc_params.mhtml_cache_control_policy = params_.cache_control_policy; |
| 182 | 186 |
| 183 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); | 187 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); |
| 184 pending_frame_tree_node_ids_.pop(); | 188 pending_frame_tree_node_ids_.pop(); |
| 185 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); | 189 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); |
| 186 | 190 |
| 187 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); | 191 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
| 188 if (!ftn) // The contents went away. | 192 if (!ftn) // The contents went away. |
| 189 return false; | 193 return false; |
| 190 RenderFrameHost* rfh = ftn->current_frame_host(); | 194 RenderFrameHost* rfh = ftn->current_frame_host(); |
| 191 | 195 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 return base::Singleton<MHTMLGenerationManager>::get(); | 294 return base::Singleton<MHTMLGenerationManager>::get(); |
| 291 } | 295 } |
| 292 | 296 |
| 293 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 297 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
| 294 | 298 |
| 295 MHTMLGenerationManager::~MHTMLGenerationManager() { | 299 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 296 STLDeleteValues(&id_to_job_); | 300 STLDeleteValues(&id_to_job_); |
| 297 } | 301 } |
| 298 | 302 |
| 299 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 303 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
| 300 bool use_binary_encoding, | 304 const MHTMLGenerationParams& params, |
| 301 const base::FilePath& file_path, | |
| 302 const GenerateMHTMLCallback& callback) { | 305 const GenerateMHTMLCallback& callback) { |
| 303 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 306 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 304 | 307 |
| 305 int job_id = NewJob(web_contents, use_binary_encoding, callback); | 308 int job_id = NewJob(web_contents, params, callback); |
| 306 | 309 |
| 307 BrowserThread::PostTaskAndReplyWithResult( | 310 BrowserThread::PostTaskAndReplyWithResult( |
| 308 BrowserThread::FILE, FROM_HERE, | 311 BrowserThread::FILE, FROM_HERE, |
| 309 base::Bind(&MHTMLGenerationManager::CreateFile, file_path), | 312 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), |
| 310 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 313 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
| 311 base::Unretained(this), // Safe b/c |this| is a singleton. | 314 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 312 job_id)); | 315 job_id)); |
| 313 } | 316 } |
| 314 | 317 |
| 315 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( | 318 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
| 316 RenderFrameHostImpl* sender, | 319 RenderFrameHostImpl* sender, |
| 317 int job_id, | 320 int job_id, |
| 318 bool mhtml_generation_in_renderer_succeeded, | 321 bool mhtml_generation_in_renderer_succeeded, |
| 319 const std::set<std::string>& digests_of_uris_of_serialized_resources) { | 322 const std::set<std::string>& digests_of_uris_of_serialized_resources) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 int64_t file_size) { | 399 int64_t file_size) { |
| 397 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 400 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 398 | 401 |
| 399 Job* job = FindJob(job_id); | 402 Job* job = FindJob(job_id); |
| 400 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); | 403 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); |
| 401 id_to_job_.erase(job_id); | 404 id_to_job_.erase(job_id); |
| 402 delete job; | 405 delete job; |
| 403 } | 406 } |
| 404 | 407 |
| 405 int MHTMLGenerationManager::NewJob(WebContents* web_contents, | 408 int MHTMLGenerationManager::NewJob(WebContents* web_contents, |
| 406 bool use_binary_encoding, | 409 const MHTMLGenerationParams& params, |
| 407 const GenerateMHTMLCallback& callback) { | 410 const GenerateMHTMLCallback& callback) { |
| 408 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 411 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 409 | 412 |
| 410 int job_id = next_job_id_++; | 413 int job_id = next_job_id_++; |
| 411 id_to_job_[job_id] = | 414 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; | 415 return job_id; |
| 414 } | 416 } |
| 415 | 417 |
| 416 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { | 418 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { |
| 417 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 419 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 418 | 420 |
| 419 IDToJobMap::iterator iter = id_to_job_.find(job_id); | 421 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 420 if (iter == id_to_job_.end()) { | 422 if (iter == id_to_job_.end()) { |
| 421 NOTREACHED(); | 423 NOTREACHED(); |
| 422 return nullptr; | 424 return nullptr; |
| 423 } | 425 } |
| 424 return iter->second; | 426 return iter->second; |
| 425 } | 427 } |
| 426 | 428 |
| 427 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 429 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 428 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 430 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 429 DCHECK(job); | 431 DCHECK(job); |
| 430 JobFinished(job, JobStatus::FAILURE); | 432 JobFinished(job, JobStatus::FAILURE); |
| 431 } | 433 } |
| 432 | 434 |
| 433 } // namespace content | 435 } // namespace content |
| OLD | NEW |