| 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/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/scoped_observer.h" | 17 #include "base/scoped_observer.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 21 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 22 #include "content/browser/bad_message.h" | 23 #include "content/browser/bad_message.h" |
| 23 #include "content/browser/frame_host/frame_tree_node.h" | 24 #include "content/browser/frame_host/frame_tree_node.h" |
| 24 #include "content/browser/frame_host/render_frame_host_impl.h" | 25 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 25 #include "content/common/frame_messages.h" | 26 #include "content/common/frame_messages.h" |
| 26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/render_frame_host.h" | 28 #include "content/public/browser/render_frame_host.h" |
| 28 #include "content/public/browser/render_process_host.h" | 29 #include "content/public/browser/render_process_host.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 67 |
| 67 // Indicates if more calls to SendToNextRenderFrame are needed. | 68 // Indicates if more calls to SendToNextRenderFrame are needed. |
| 68 bool IsDone() const { | 69 bool IsDone() const { |
| 69 bool waiting_for_response_from_renderer = | 70 bool waiting_for_response_from_renderer = |
| 70 frame_tree_node_id_of_busy_frame_ != | 71 frame_tree_node_id_of_busy_frame_ != |
| 71 FrameTreeNode::kFrameTreeNodeInvalidId; | 72 FrameTreeNode::kFrameTreeNodeInvalidId; |
| 72 bool no_more_requests_to_send = pending_frame_tree_node_ids_.empty(); | 73 bool no_more_requests_to_send = pending_frame_tree_node_ids_.empty(); |
| 73 return !waiting_for_response_from_renderer && no_more_requests_to_send; | 74 return !waiting_for_response_from_renderer && no_more_requests_to_send; |
| 74 } | 75 } |
| 75 | 76 |
| 76 // Close the file on the file thread and respond back on the UI thread with | 77 // Write the MHTML footer and close the file on the file thread and respond |
| 77 // file size. | 78 // back on the UI thread with the updated status and file size (which will be |
| 78 void CloseFile(base::Callback<void(int64_t file_size)> callback); | 79 // negative in case of errors). |
| 80 void CloseFile( |
| 81 base::Callback<void(const std::tuple<MhtmlSaveStatus, int64_t>&)> |
| 82 callback, |
| 83 MhtmlSaveStatus save_status); |
| 79 | 84 |
| 80 // RenderProcessHostObserver: | 85 // RenderProcessHostObserver: |
| 81 void RenderProcessExited(RenderProcessHost* host, | 86 void RenderProcessExited(RenderProcessHost* host, |
| 82 base::TerminationStatus status, | 87 base::TerminationStatus status, |
| 83 int exit_code) override; | 88 int exit_code) override; |
| 84 void RenderProcessHostDestroyed(RenderProcessHost* host) override; | 89 void RenderProcessHostDestroyed(RenderProcessHost* host) override; |
| 85 | 90 |
| 86 void MarkAsFinished(); | 91 void MarkAsFinished(); |
| 87 | 92 |
| 88 void ReportRendererMainThreadTime(base::TimeDelta renderer_main_thread_time); | 93 void ReportRendererMainThreadTime(base::TimeDelta renderer_main_thread_time); |
| 89 | 94 |
| 90 private: | 95 private: |
| 91 static int64_t CloseFileOnFileThread(base::File file); | 96 // Writes the MHTML footer to the file and closes it. |
| 97 // |
| 98 // Note: The same |boundary| marker must be used for all "boundaries" -- in |
| 99 // the header, parts and footer -- that belong to the same MHTML document (see |
| 100 // also rfc1341, section 7.2.1, "boundary" description). |
| 101 static std::tuple<MhtmlSaveStatus, int64_t> CloseFileOnFileThread( |
| 102 MhtmlSaveStatus save_status, |
| 103 const std::string& boundary, |
| 104 base::File file); |
| 92 void AddFrame(RenderFrameHost* render_frame_host); | 105 void AddFrame(RenderFrameHost* render_frame_host); |
| 93 | 106 |
| 94 // Creates a new map with values (content ids) the same as in | 107 // Creates a new map with values (content ids) the same as in |
| 95 // |frame_tree_node_to_content_id_| map, but with the keys translated from | 108 // |frame_tree_node_to_content_id_| map, but with the keys translated from |
| 96 // frame_tree_node_id into a |site_instance|-specific routing_id. | 109 // frame_tree_node_id into a |site_instance|-specific routing_id. |
| 97 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 110 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
| 98 SiteInstance* site_instance); | 111 SiteInstance* site_instance); |
| 99 | 112 |
| 100 // Id used to map renderer responses to jobs. | 113 // Id used to map renderer responses to jobs. |
| 101 // See also MHTMLGenerationManager::id_to_job_ map. | 114 // See also MHTMLGenerationManager::id_to_job_ map. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 122 int frame_tree_node_id_of_busy_frame_; | 135 int frame_tree_node_id_of_busy_frame_; |
| 123 | 136 |
| 124 // The handle to the file the MHTML is saved to for the browser process. | 137 // The handle to the file the MHTML is saved to for the browser process. |
| 125 base::File browser_file_; | 138 base::File browser_file_; |
| 126 | 139 |
| 127 // Map from frames to content ids (see WebFrameSerializer::generateMHTMLParts | 140 // Map from frames to content ids (see WebFrameSerializer::generateMHTMLParts |
| 128 // for more details about what "content ids" are and how they are used). | 141 // for more details about what "content ids" are and how they are used). |
| 129 std::map<int, std::string> frame_tree_node_to_content_id_; | 142 std::map<int, std::string> frame_tree_node_to_content_id_; |
| 130 | 143 |
| 131 // MIME multipart boundary to use in the MHTML doc. | 144 // MIME multipart boundary to use in the MHTML doc. |
| 132 std::string mhtml_boundary_marker_; | 145 const std::string mhtml_boundary_marker_; |
| 133 | 146 |
| 134 // Digests of URIs of already generated MHTML parts. | 147 // Digests of URIs of already generated MHTML parts. |
| 135 std::set<std::string> digests_of_already_serialized_uris_; | 148 std::set<std::string> digests_of_already_serialized_uris_; |
| 136 std::string salt_; | 149 std::string salt_; |
| 137 | 150 |
| 138 // The callback to call once generation is complete. | 151 // The callback to call once generation is complete. |
| 139 const GenerateMHTMLCallback callback_; | 152 const GenerateMHTMLCallback callback_; |
| 140 | 153 |
| 141 // Whether the job is finished (set to true only for the short duration of | 154 // Whether the job is finished (set to true only for the short duration of |
| 142 // time between MHTMLGenerationManager::JobFinished is called and the job is | 155 // time between MHTMLGenerationManager::JobFinished is called and the job is |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 219 |
| 207 FrameMsg_SerializeAsMHTML_Params ipc_params; | 220 FrameMsg_SerializeAsMHTML_Params ipc_params; |
| 208 ipc_params.job_id = job_id_; | 221 ipc_params.job_id = job_id_; |
| 209 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; | 222 ipc_params.mhtml_boundary_marker = mhtml_boundary_marker_; |
| 210 ipc_params.mhtml_binary_encoding = params_.use_binary_encoding; | 223 ipc_params.mhtml_binary_encoding = params_.use_binary_encoding; |
| 211 ipc_params.mhtml_cache_control_policy = params_.cache_control_policy; | 224 ipc_params.mhtml_cache_control_policy = params_.cache_control_policy; |
| 212 ipc_params.mhtml_popup_overlay_removal = params_.remove_popup_overlay; | 225 ipc_params.mhtml_popup_overlay_removal = params_.remove_popup_overlay; |
| 213 | 226 |
| 214 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); | 227 int frame_tree_node_id = pending_frame_tree_node_ids_.front(); |
| 215 pending_frame_tree_node_ids_.pop(); | 228 pending_frame_tree_node_ids_.pop(); |
| 216 ipc_params.is_last_frame = pending_frame_tree_node_ids_.empty(); | |
| 217 | 229 |
| 218 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); | 230 FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
| 219 if (!ftn) // The contents went away. | 231 if (!ftn) // The contents went away. |
| 220 return MhtmlSaveStatus::FRAME_NO_LONGER_EXISTS; | 232 return MhtmlSaveStatus::FRAME_NO_LONGER_EXISTS; |
| 221 RenderFrameHost* rfh = ftn->current_frame_host(); | 233 RenderFrameHost* rfh = ftn->current_frame_host(); |
| 222 | 234 |
| 223 // Get notified if the target of the IPC message dies between responding. | 235 // Get notified if the target of the IPC message dies between responding. |
| 224 observed_renderer_process_host_.RemoveAll(); | 236 observed_renderer_process_host_.RemoveAll(); |
| 225 observed_renderer_process_host_.Add(rfh->GetProcess()); | 237 observed_renderer_process_host_.Add(rfh->GetProcess()); |
| 226 | 238 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 253 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 265 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 254 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); | 266 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); |
| 255 } | 267 } |
| 256 | 268 |
| 257 void MHTMLGenerationManager::Job::MarkAsFinished() { | 269 void MHTMLGenerationManager::Job::MarkAsFinished() { |
| 258 DCHECK(!is_finished_); | 270 DCHECK(!is_finished_); |
| 259 if (is_finished_) | 271 if (is_finished_) |
| 260 return; | 272 return; |
| 261 | 273 |
| 262 is_finished_ = true; | 274 is_finished_ = true; |
| 275 |
| 276 // Stopping RenderProcessExited notifications is needed to avoid calling |
| 277 // JobFinished twice. See also https://crbug.com/612098. |
| 278 observed_renderer_process_host_.RemoveAll(); |
| 279 |
| 263 TRACE_EVENT_NESTABLE_ASYNC_INSTANT0("page-serialization", "JobFinished", | 280 TRACE_EVENT_NESTABLE_ASYNC_INSTANT0("page-serialization", "JobFinished", |
| 264 this); | 281 this); |
| 265 | 282 |
| 266 // End of job timing reports. | 283 // End of job timing reports. |
| 267 if (!wait_on_renderer_start_time_.is_null()) { | 284 if (!wait_on_renderer_start_time_.is_null()) { |
| 268 base::TimeDelta renderer_wait_time = | 285 base::TimeDelta renderer_wait_time = |
| 269 base::TimeTicks::Now() - wait_on_renderer_start_time_; | 286 base::TimeTicks::Now() - wait_on_renderer_start_time_; |
| 270 UMA_HISTOGRAM_TIMES( | 287 UMA_HISTOGRAM_TIMES( |
| 271 "PageSerialization.MhtmlGeneration.BrowserWaitForRendererTime." | 288 "PageSerialization.MhtmlGeneration.BrowserWaitForRendererTime." |
| 272 "SingleFrame", | 289 "SingleFrame", |
| 273 renderer_wait_time); | 290 renderer_wait_time); |
| 274 all_renderers_wait_time_ += renderer_wait_time; | 291 all_renderers_wait_time_ += renderer_wait_time; |
| 275 } | 292 } |
| 276 if (!all_renderers_wait_time_.is_zero()) { | 293 if (!all_renderers_wait_time_.is_zero()) { |
| 277 UMA_HISTOGRAM_TIMES( | 294 UMA_HISTOGRAM_TIMES( |
| 278 "PageSerialization.MhtmlGeneration.BrowserWaitForRendererTime." | 295 "PageSerialization.MhtmlGeneration.BrowserWaitForRendererTime." |
| 279 "FrameTree", | 296 "FrameTree", |
| 280 all_renderers_wait_time_); | 297 all_renderers_wait_time_); |
| 281 } | 298 } |
| 282 if (!all_renderers_main_thread_time_.is_zero()) { | 299 if (!all_renderers_main_thread_time_.is_zero()) { |
| 283 UMA_HISTOGRAM_TIMES( | 300 UMA_HISTOGRAM_TIMES( |
| 284 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.FrameTree", | 301 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.FrameTree", |
| 285 all_renderers_main_thread_time_); | 302 all_renderers_main_thread_time_); |
| 286 } | 303 } |
| 287 if (!longest_renderer_main_thread_time_.is_zero()) { | 304 if (!longest_renderer_main_thread_time_.is_zero()) { |
| 288 UMA_HISTOGRAM_TIMES( | 305 UMA_HISTOGRAM_TIMES( |
| 289 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.SlowestFrame", | 306 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.SlowestFrame", |
| 290 longest_renderer_main_thread_time_); | 307 longest_renderer_main_thread_time_); |
| 291 } | 308 } |
| 292 | |
| 293 // Stopping RenderProcessExited notifications is needed to avoid calling | |
| 294 // JobFinished twice. See also https://crbug.com/612098. | |
| 295 observed_renderer_process_host_.RemoveAll(); | |
| 296 } | 309 } |
| 297 | 310 |
| 298 void MHTMLGenerationManager::Job::ReportRendererMainThreadTime( | 311 void MHTMLGenerationManager::Job::ReportRendererMainThreadTime( |
| 299 base::TimeDelta renderer_main_thread_time) { | 312 base::TimeDelta renderer_main_thread_time) { |
| 300 DCHECK(renderer_main_thread_time > base::TimeDelta()); | 313 DCHECK(renderer_main_thread_time > base::TimeDelta()); |
| 301 if (renderer_main_thread_time > base::TimeDelta()) | 314 if (renderer_main_thread_time > base::TimeDelta()) |
| 302 all_renderers_main_thread_time_ += renderer_main_thread_time; | 315 all_renderers_main_thread_time_ += renderer_main_thread_time; |
| 303 if (renderer_main_thread_time > longest_renderer_main_thread_time_) | 316 if (renderer_main_thread_time > longest_renderer_main_thread_time_) |
| 304 longest_renderer_main_thread_time_ = renderer_main_thread_time; | 317 longest_renderer_main_thread_time_ = renderer_main_thread_time; |
| 305 } | 318 } |
| 306 | 319 |
| 307 void MHTMLGenerationManager::Job::AddFrame(RenderFrameHost* render_frame_host) { | 320 void MHTMLGenerationManager::Job::AddFrame(RenderFrameHost* render_frame_host) { |
| 308 auto* rfhi = static_cast<RenderFrameHostImpl*>(render_frame_host); | 321 auto* rfhi = static_cast<RenderFrameHostImpl*>(render_frame_host); |
| 309 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); | 322 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); |
| 310 pending_frame_tree_node_ids_.push(frame_tree_node_id); | 323 pending_frame_tree_node_ids_.push(frame_tree_node_id); |
| 311 | 324 |
| 312 std::string guid = base::GenerateGUID(); | 325 std::string guid = base::GenerateGUID(); |
| 313 std::string content_id = base::StringPrintf("<frame-%d-%s@mhtml.blink>", | 326 std::string content_id = base::StringPrintf("<frame-%d-%s@mhtml.blink>", |
| 314 frame_tree_node_id, guid.c_str()); | 327 frame_tree_node_id, guid.c_str()); |
| 315 frame_tree_node_to_content_id_[frame_tree_node_id] = content_id; | 328 frame_tree_node_to_content_id_[frame_tree_node_id] = content_id; |
| 316 } | 329 } |
| 317 | 330 |
| 318 void MHTMLGenerationManager::Job::RenderProcessHostDestroyed( | 331 void MHTMLGenerationManager::Job::RenderProcessHostDestroyed( |
| 319 RenderProcessHost* host) { | 332 RenderProcessHost* host) { |
| 320 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 333 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 321 observed_renderer_process_host_.Remove(host); | 334 observed_renderer_process_host_.Remove(host); |
| 322 } | 335 } |
| 323 | 336 |
| 324 void MHTMLGenerationManager::Job::CloseFile( | 337 void MHTMLGenerationManager::Job::CloseFile( |
| 325 base::Callback<void(int64_t)> callback) { | 338 base::Callback<void(const std::tuple<MhtmlSaveStatus, int64_t>&)> callback, |
| 339 MhtmlSaveStatus save_status) { |
| 326 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 340 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 341 DCHECK(!mhtml_boundary_marker_.empty()); |
| 327 | 342 |
| 328 if (!browser_file_.IsValid()) { | 343 if (!browser_file_.IsValid()) { |
| 329 callback.Run(-1); | 344 // Only update the status if that won't hide an earlier error. |
| 345 if (save_status == MhtmlSaveStatus::SUCCESS) |
| 346 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; |
| 347 callback.Run(std::make_tuple(save_status, -1)); |
| 330 return; | 348 return; |
| 331 } | 349 } |
| 332 | 350 |
| 351 // If no previous error occurred the boundary should be sent. |
| 333 BrowserThread::PostTaskAndReplyWithResult( | 352 BrowserThread::PostTaskAndReplyWithResult( |
| 334 BrowserThread::FILE, FROM_HERE, | 353 BrowserThread::FILE, FROM_HERE, |
| 335 base::Bind(&MHTMLGenerationManager::Job::CloseFileOnFileThread, | 354 base::Bind( |
| 336 base::Passed(std::move(browser_file_))), | 355 &MHTMLGenerationManager::Job::CloseFileOnFileThread, save_status, |
| 356 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ |
| 357 : std::string()), |
| 358 base::Passed(&browser_file_)), |
| 337 callback); | 359 callback); |
| 338 } | 360 } |
| 339 | 361 |
| 340 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( | 362 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( |
| 341 RenderFrameHostImpl* sender) { | 363 RenderFrameHostImpl* sender) { |
| 342 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); | 364 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); |
| 343 if (sender_id != frame_tree_node_id_of_busy_frame_) | 365 if (sender_id != frame_tree_node_id_of_busy_frame_) |
| 344 return false; | 366 return false; |
| 345 | 367 |
| 346 // We only expect one message per frame - let's make sure subsequent messages | 368 // We only expect one message per frame - let's make sure subsequent messages |
| (...skipping 24 matching lines...) Expand all Loading... |
| 371 digests_of_uris_of_serialized_resources.end()); | 393 digests_of_uris_of_serialized_resources.end()); |
| 372 | 394 |
| 373 // Report success if all frames have been processed. | 395 // Report success if all frames have been processed. |
| 374 if (pending_frame_tree_node_ids_.empty()) | 396 if (pending_frame_tree_node_ids_.empty()) |
| 375 return MhtmlSaveStatus::SUCCESS; | 397 return MhtmlSaveStatus::SUCCESS; |
| 376 | 398 |
| 377 return SendToNextRenderFrame(); | 399 return SendToNextRenderFrame(); |
| 378 } | 400 } |
| 379 | 401 |
| 380 // static | 402 // static |
| 381 int64_t MHTMLGenerationManager::Job::CloseFileOnFileThread(base::File file) { | 403 std::tuple<MhtmlSaveStatus, int64_t> |
| 404 MHTMLGenerationManager::Job::CloseFileOnFileThread(MhtmlSaveStatus save_status, |
| 405 const std::string& boundary, |
| 406 base::File file) { |
| 382 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 407 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 383 DCHECK(file.IsValid()); | 408 |
| 384 int64_t file_size = file.GetLength(); | 409 // If no previous error occurred the boundary should have been provided. |
| 385 file.Close(); | 410 if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 386 return file_size; | 411 TRACE_EVENT0("page-serialization", |
| 412 "MHTMLGenerationManager::Job MHTML footer writing"); |
| 413 DCHECK(!boundary.empty()); |
| 414 std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str()); |
| 415 DCHECK(base::IsStringASCII(footer)); |
| 416 if (file.WriteAtCurrentPos(footer.data(), footer.size()) < 0) |
| 417 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; |
| 418 } |
| 419 |
| 420 // If the file is still valid try to close it. Only update the status if that |
| 421 // won't hide an earlier error. |
| 422 int64_t file_size = -1; |
| 423 if (file.IsValid()) { |
| 424 file_size = file.GetLength(); |
| 425 file.Close(); |
| 426 } else if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 427 save_status = MhtmlSaveStatus::FILE_CLOSING_ERROR; |
| 428 } |
| 429 |
| 430 return std::make_tuple(save_status, file_size); |
| 387 } | 431 } |
| 388 | 432 |
| 389 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { | 433 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { |
| 390 return base::Singleton<MHTMLGenerationManager>::get(); | 434 return base::Singleton<MHTMLGenerationManager>::get(); |
| 391 } | 435 } |
| 392 | 436 |
| 393 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 437 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
| 394 | 438 |
| 395 MHTMLGenerationManager::~MHTMLGenerationManager() { | 439 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 396 } | 440 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 427 NOTREACHED(); | 471 NOTREACHED(); |
| 428 ReceivedBadMessage(sender->GetProcess(), | 472 ReceivedBadMessage(sender->GetProcess(), |
| 429 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); | 473 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); |
| 430 return; | 474 return; |
| 431 } | 475 } |
| 432 | 476 |
| 433 TRACE_EVENT_NESTABLE_ASYNC_END0("page-serialization", "WaitingOnRenderer", | 477 TRACE_EVENT_NESTABLE_ASYNC_END0("page-serialization", "WaitingOnRenderer", |
| 434 job); | 478 job); |
| 435 job->ReportRendererMainThreadTime(renderer_main_thread_time); | 479 job->ReportRendererMainThreadTime(renderer_main_thread_time); |
| 436 | 480 |
| 481 // If the renderer succeeded notify the Job and update the status. |
| 437 if (save_status == MhtmlSaveStatus::SUCCESS) { | 482 if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 438 save_status = job->OnSerializeAsMHTMLResponse( | 483 save_status = job->OnSerializeAsMHTMLResponse( |
| 439 digests_of_uris_of_serialized_resources); | 484 digests_of_uris_of_serialized_resources); |
| 440 } | 485 } |
| 441 | 486 |
| 487 // If there was a failure (either from the renderer or from the job) then |
| 488 // terminate the job and return. |
| 442 if (save_status != MhtmlSaveStatus::SUCCESS) { | 489 if (save_status != MhtmlSaveStatus::SUCCESS) { |
| 443 JobFinished(job, save_status); | 490 JobFinished(job, save_status); |
| 444 return; | 491 return; |
| 445 } | 492 } |
| 446 | 493 |
| 494 // Otherwise report completion if the job is done. |
| 447 if (job->IsDone()) | 495 if (job->IsDone()) |
| 448 JobFinished(job, MhtmlSaveStatus::SUCCESS); | 496 JobFinished(job, MhtmlSaveStatus::SUCCESS); |
| 449 } | 497 } |
| 450 | 498 |
| 451 // static | 499 // static |
| 452 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { | 500 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { |
| 453 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 501 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 454 | 502 |
| 455 // SECURITY NOTE: A file descriptor to the file created below will be passed | 503 // SECURITY NOTE: A file descriptor to the file created below will be passed |
| 456 // to multiple renderer processes which (in out-of-process iframes mode) can | 504 // to multiple renderer processes which (in out-of-process iframes mode) can |
| (...skipping 29 matching lines...) Expand all Loading... |
| 486 MhtmlSaveStatus save_status = job->SendToNextRenderFrame(); | 534 MhtmlSaveStatus save_status = job->SendToNextRenderFrame(); |
| 487 if (save_status != MhtmlSaveStatus::SUCCESS) { | 535 if (save_status != MhtmlSaveStatus::SUCCESS) { |
| 488 JobFinished(job, save_status); | 536 JobFinished(job, save_status); |
| 489 } | 537 } |
| 490 } | 538 } |
| 491 | 539 |
| 492 void MHTMLGenerationManager::JobFinished(Job* job, | 540 void MHTMLGenerationManager::JobFinished(Job* job, |
| 493 MhtmlSaveStatus save_status) { | 541 MhtmlSaveStatus save_status) { |
| 494 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 542 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 495 DCHECK(job); | 543 DCHECK(job); |
| 496 | |
| 497 job->MarkAsFinished(); | 544 job->MarkAsFinished(); |
| 498 job->CloseFile( | 545 job->CloseFile( |
| 499 base::Bind(&MHTMLGenerationManager::OnFileClosed, | 546 base::Bind(&MHTMLGenerationManager::OnFileClosed, |
| 500 base::Unretained(this), // Safe b/c |this| is a singleton. | 547 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 501 job->id(), save_status)); | 548 job->id()), |
| 549 save_status); |
| 502 } | 550 } |
| 503 | 551 |
| 504 void MHTMLGenerationManager::OnFileClosed(int job_id, | 552 void MHTMLGenerationManager::OnFileClosed( |
| 505 MhtmlSaveStatus save_status, | 553 int job_id, |
| 506 int64_t file_size) { | 554 const std::tuple<MhtmlSaveStatus, int64_t>& save_status_size) { |
| 507 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 555 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 508 | 556 MhtmlSaveStatus save_status = std::get<0>(save_status_size); |
| 509 // Checks if an error happened while closing the file. | 557 int64_t file_size = std::get<1>(save_status_size); |
| 510 if (save_status == MhtmlSaveStatus::SUCCESS && file_size < 0) | |
| 511 save_status = MhtmlSaveStatus::FILE_CLOSING_ERROR; | |
| 512 | 558 |
| 513 Job* job = FindJob(job_id); | 559 Job* job = FindJob(job_id); |
| 560 DCHECK(job); |
| 514 TRACE_EVENT_NESTABLE_ASYNC_END2( | 561 TRACE_EVENT_NESTABLE_ASYNC_END2( |
| 515 "page-serialization", "SavingMhtmlJob", job, "job save status", | 562 "page-serialization", "SavingMhtmlJob", job, "job save status", |
| 516 GetMhtmlSaveStatusLabel(save_status), "file size", file_size); | 563 GetMhtmlSaveStatusLabel(save_status), "file size", file_size); |
| 517 UMA_HISTOGRAM_TIMES("PageSerialization.MhtmlGeneration.FullPageSavingTime", | 564 UMA_HISTOGRAM_TIMES("PageSerialization.MhtmlGeneration.FullPageSavingTime", |
| 518 base::TimeTicks::Now() - job->creation_time()); | 565 base::TimeTicks::Now() - job->creation_time()); |
| 519 UMA_HISTOGRAM_ENUMERATION("PageSerialization.MhtmlGeneration.FinalSaveStatus", | 566 UMA_HISTOGRAM_ENUMERATION("PageSerialization.MhtmlGeneration.FinalSaveStatus", |
| 520 static_cast<int>(save_status), | 567 static_cast<int>(save_status), |
| 521 static_cast<int>(MhtmlSaveStatus::LAST)); | 568 static_cast<int>(MhtmlSaveStatus::LAST)); |
| 522 job->callback().Run(save_status == MhtmlSaveStatus::SUCCESS ? file_size : -1); | 569 job->callback().Run(save_status == MhtmlSaveStatus::SUCCESS ? file_size : -1); |
| 523 id_to_job_.erase(job_id); | 570 id_to_job_.erase(job_id); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 545 return iter->second.get(); | 592 return iter->second.get(); |
| 546 } | 593 } |
| 547 | 594 |
| 548 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 595 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 549 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 596 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 550 DCHECK(job); | 597 DCHECK(job); |
| 551 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); | 598 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); |
| 552 } | 599 } |
| 553 | 600 |
| 554 } // namespace content | 601 } // namespace content |
| OLD | NEW |