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

Side by Side Diff: content/renderer/render_frame_impl.cc

Issue 2379823003: Move MHTML file writing out of the renderer main thread. (Closed)
Patch Set: A few move semantics fixes (I think). Created 4 years, 2 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/renderer/render_frame_impl.h" 5 #include "content/renderer/render_frame_impl.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/auto_reset.h" 12 #include "base/auto_reset.h"
13 #include "base/bind_helpers.h"
13 #include "base/command_line.h" 14 #include "base/command_line.h"
14 #include "base/debug/alias.h" 15 #include "base/debug/alias.h"
15 #include "base/debug/asan_invalid_access.h" 16 #include "base/debug/asan_invalid_access.h"
16 #include "base/debug/crash_logging.h" 17 #include "base/debug/crash_logging.h"
17 #include "base/debug/dump_without_crashing.h" 18 #include "base/debug/dump_without_crashing.h"
18 #include "base/files/file.h" 19 #include "base/files/file.h"
19 #include "base/i18n/char_iterator.h" 20 #include "base/i18n/char_iterator.h"
20 #include "base/logging.h" 21 #include "base/logging.h"
21 #include "base/macros.h" 22 #include "base/macros.h"
22 #include "base/memory/ptr_util.h" 23 #include "base/memory/ptr_util.h"
23 #include "base/memory/shared_memory.h" 24 #include "base/memory/shared_memory.h"
24 #include "base/memory/weak_ptr.h" 25 #include "base/memory/weak_ptr.h"
25 #include "base/metrics/field_trial.h" 26 #include "base/metrics/field_trial.h"
26 #include "base/metrics/histogram_macros.h" 27 #include "base/metrics/histogram_macros.h"
27 #include "base/process/process.h" 28 #include "base/process/process.h"
28 #include "base/stl_util.h" 29 #include "base/stl_util.h"
29 #include "base/strings/string16.h" 30 #include "base/strings/string16.h"
30 #include "base/strings/utf_string_conversions.h" 31 #include "base/strings/utf_string_conversions.h"
32 #include "base/task_runner_util.h"
31 #include "base/threading/thread_task_runner_handle.h" 33 #include "base/threading/thread_task_runner_handle.h"
32 #include "base/time/time.h" 34 #include "base/time/time.h"
33 #include "base/trace_event/trace_event.h" 35 #include "base/trace_event/trace_event.h"
34 #include "build/build_config.h" 36 #include "build/build_config.h"
35 #include "cc/base/switches.h" 37 #include "cc/base/switches.h"
36 #include "content/child/appcache/appcache_dispatcher.h" 38 #include "content/child/appcache/appcache_dispatcher.h"
37 #include "content/child/quota_dispatcher.h" 39 #include "content/child/quota_dispatcher.h"
38 #include "content/child/request_extra_data.h" 40 #include "content/child/request_extra_data.h"
39 #include "content/child/service_worker/service_worker_handle_reference.h" 41 #include "content/child/service_worker/service_worker_handle_reference.h"
40 #include "content/child/service_worker/service_worker_network_provider.h" 42 #include "content/child/service_worker/service_worker_network_provider.h"
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 724
723 private: 725 private:
724 const std::map<GURL, base::FilePath>& url_to_local_path_; 726 const std::map<GURL, base::FilePath>& url_to_local_path_;
725 const std::map<int, base::FilePath>& frame_routing_id_to_local_path_; 727 const std::map<int, base::FilePath>& frame_routing_id_to_local_path_;
726 }; 728 };
727 729
728 // Implementation of WebFrameSerializer::MHTMLPartsGenerationDelegate that 730 // Implementation of WebFrameSerializer::MHTMLPartsGenerationDelegate that
729 // 1. Bases shouldSkipResource and getContentID responses on contents of 731 // 1. Bases shouldSkipResource and getContentID responses on contents of
730 // FrameMsg_SerializeAsMHTML_Params. 732 // FrameMsg_SerializeAsMHTML_Params.
731 // 2. Stores digests of urls of serialized resources (i.e. urls reported via 733 // 2. Stores digests of urls of serialized resources (i.e. urls reported via
732 // shouldSkipResource) into |digests_of_uris_of_serialized_resources| passed 734 // shouldSkipResource) into |serialized_resources_uri_digests| passed
733 // to the constructor. 735 // to the constructor.
734 class MHTMLPartsGenerationDelegate 736 class MHTMLPartsGenerationDelegate
735 : public WebFrameSerializer::MHTMLPartsGenerationDelegate { 737 : public WebFrameSerializer::MHTMLPartsGenerationDelegate {
736 public: 738 public:
737 MHTMLPartsGenerationDelegate( 739 MHTMLPartsGenerationDelegate(
738 const FrameMsg_SerializeAsMHTML_Params& params, 740 const FrameMsg_SerializeAsMHTML_Params& params,
739 std::set<std::string>* digests_of_uris_of_serialized_resources) 741 std::set<std::string>* serialized_resources_uri_digests)
740 : params_(params), 742 : params_(params),
741 digests_of_uris_of_serialized_resources_( 743 serialized_resources_uri_digests_(serialized_resources_uri_digests) {
742 digests_of_uris_of_serialized_resources) { 744 DCHECK(serialized_resources_uri_digests_);
743 DCHECK(digests_of_uris_of_serialized_resources_);
744 } 745 }
745 746
746 bool shouldSkipResource(const WebURL& url) override { 747 bool shouldSkipResource(const WebURL& url) override {
747 std::string digest = 748 std::string digest =
748 crypto::SHA256HashString(params_.salt + GURL(url).spec()); 749 crypto::SHA256HashString(params_.salt + GURL(url).spec());
749 750
750 // Skip if the |url| already covered by serialization of an *earlier* frame. 751 // Skip if the |url| already covered by serialization of an *earlier* frame.
751 if (base::ContainsKey(params_.digests_of_uris_to_skip, digest)) 752 if (base::ContainsKey(params_.digests_of_uris_to_skip, digest))
752 return true; 753 return true;
753 754
754 // Let's record |url| as being serialized for the *current* frame. 755 // Let's record |url| as being serialized for the *current* frame.
755 auto pair = digests_of_uris_of_serialized_resources_->insert(digest); 756 auto pair = serialized_resources_uri_digests_->insert(digest);
756 bool insertion_took_place = pair.second; 757 bool insertion_took_place = pair.second;
757 DCHECK(insertion_took_place); // Blink should dedupe within a frame. 758 DCHECK(insertion_took_place); // Blink should dedupe within a frame.
758 759
759 return false; 760 return false;
760 } 761 }
761 762
762 WebString getContentID(WebFrame* frame) override { 763 WebString getContentID(WebFrame* frame) override {
763 int routing_id = GetRoutingIdForFrameOrProxy(frame); 764 int routing_id = GetRoutingIdForFrameOrProxy(frame);
764 765
765 auto it = params_.frame_routing_id_to_content_id.find(routing_id); 766 auto it = params_.frame_routing_id_to_content_id.find(routing_id);
766 if (it == params_.frame_routing_id_to_content_id.end()) 767 if (it == params_.frame_routing_id_to_content_id.end())
767 return WebString(); 768 return WebString();
768 769
769 const std::string& content_id = it->second; 770 const std::string& content_id = it->second;
770 return WebString::fromUTF8(content_id); 771 return WebString::fromUTF8(content_id);
771 } 772 }
772 773
773 blink::WebFrameSerializerCacheControlPolicy cacheControlPolicy() override { 774 blink::WebFrameSerializerCacheControlPolicy cacheControlPolicy() override {
774 return params_.mhtml_cache_control_policy; 775 return params_.mhtml_cache_control_policy;
775 } 776 }
776 777
777 bool useBinaryEncoding() override { return params_.mhtml_binary_encoding; } 778 bool useBinaryEncoding() override { return params_.mhtml_binary_encoding; }
778 779
779 private: 780 private:
780 const FrameMsg_SerializeAsMHTML_Params& params_; 781 const FrameMsg_SerializeAsMHTML_Params& params_;
781 std::set<std::string>* digests_of_uris_of_serialized_resources_; 782 std::set<std::string>* serialized_resources_uri_digests_;
782 783
783 DISALLOW_COPY_AND_ASSIGN(MHTMLPartsGenerationDelegate); 784 DISALLOW_COPY_AND_ASSIGN(MHTMLPartsGenerationDelegate);
784 }; 785 };
785 786
787 // Writes to file the data serialized and encoded into the WebData instances.
788 bool WriteMHTMLToDisk(bool success,
789 std::vector<std::vector<char>> mhtml_contents,
790 base::File file) {
791 TRACE_EVENT_BEGIN0("page-serialization",
792 "WriteMHTMLToDisk (RenderFrameImpl)");
793 DCHECK(!RenderThread::Get()) << "Should not run in the main renderer thread";
794 base::TimeTicks start;
795 if (success) {
796 start = base::TimeTicks::Now();
797 for (const std::vector<char>& data : mhtml_contents) {
798 if (file.WriteAtCurrentPos(&data.front(), data.size()) < 0) {
799 success = false;
800 break;
801 }
802 }
803 }
804 file.Close(); // Need to flush file contents before sending IPC response.
805
806 // Only record histogram if this writing call was not a no-op (|success| being
807 // false from the start).
808 if (!start.is_null()) {
809 UMA_HISTOGRAM_TIMES(
810 "PageSerialization.MhtmlGeneration.WriteToDiskTime.SingleFrame",
811 base::TimeTicks::Now() - start);
812 }
813 TRACE_EVENT_END1("page-serialization", "WriteMHTMLToDisk (RenderFrameImpl)",
814 "success", success);
815 return success;
816 }
817
786 bool IsHttpPost(const blink::WebURLRequest& request) { 818 bool IsHttpPost(const blink::WebURLRequest& request) {
787 return request.httpMethod().utf8() == "POST"; 819 return request.httpMethod().utf8() == "POST";
788 } 820 }
789 821
790 #if defined(OS_ANDROID) 822 #if defined(OS_ANDROID)
791 // Returns true if WMPI should be used for playback, false otherwise. 823 // Returns true if WMPI should be used for playback, false otherwise.
792 // 824 //
793 // Note that HLS and MP4 detection are pre-redirect and path-based. It is 825 // Note that HLS and MP4 detection are pre-redirect and path-based. It is
794 // possible to load such a URL and find different content. 826 // possible to load such a URL and find different content.
795 bool UseWebMediaPlayerImpl(const GURL& url) { 827 bool UseWebMediaPlayerImpl(const GURL& url) {
(...skipping 4399 matching lines...) Expand 10 before | Expand all | Expand 10 after
5195 void RenderFrameImpl::OnSerializeAsMHTML( 5227 void RenderFrameImpl::OnSerializeAsMHTML(
5196 const FrameMsg_SerializeAsMHTML_Params& params) { 5228 const FrameMsg_SerializeAsMHTML_Params& params) {
5197 TRACE_EVENT0("page-serialization", "RenderFrameImpl::OnSerializeAsMHTML"); 5229 TRACE_EVENT0("page-serialization", "RenderFrameImpl::OnSerializeAsMHTML");
5198 base::TimeTicks start_time = base::TimeTicks::Now(); 5230 base::TimeTicks start_time = base::TimeTicks::Now();
5199 // Unpack IPC payload. 5231 // Unpack IPC payload.
5200 base::File file = IPC::PlatformFileForTransitToFile(params.destination_file); 5232 base::File file = IPC::PlatformFileForTransitToFile(params.destination_file);
5201 const WebString mhtml_boundary = 5233 const WebString mhtml_boundary =
5202 WebString::fromUTF8(params.mhtml_boundary_marker); 5234 WebString::fromUTF8(params.mhtml_boundary_marker);
5203 DCHECK(!mhtml_boundary.isEmpty()); 5235 DCHECK(!mhtml_boundary.isEmpty());
5204 5236
5205 // Three WebData instances for header, parts and footer. 5237 // Holds WebData instances all or some of header, contents/parts and footer.
5206 WebData mhtml_contents[3]; 5238 std::vector<std::vector<char>> mhtml_contents;
5207 std::set<std::string> digests_of_uris_of_serialized_resources; 5239 std::set<std::string> serialized_resources_uri_digests;
5208 MHTMLPartsGenerationDelegate delegate( 5240 MHTMLPartsGenerationDelegate delegate(params,
5209 params, &digests_of_uris_of_serialized_resources); 5241 &serialized_resources_uri_digests);
5210 5242
5211 bool success = true; 5243 bool success = true;
5212 5244
5213 // Generate MHTML header if needed. 5245 // Generate MHTML header if needed.
5214 if (IsMainFrame()) { 5246 if (IsMainFrame()) {
5215 TRACE_EVENT0("page-serialization", 5247 TRACE_EVENT0("page-serialization",
5216 "RenderFrameImpl::OnSerializeAsMHTML header"); 5248 "RenderFrameImpl::OnSerializeAsMHTML header");
5217 // |data| can be empty if the main frame should be skipped. If the main 5249 // |data| can be empty if the main frame should be skipped. If the main
5218 // frame is skipped, then the whole archive is bad, so bail to the error 5250 // frame is skipped, then the whole archive is bad, so bail to the error
5219 // condition. 5251 // condition.
5220 mhtml_contents[0] = WebFrameSerializer::generateMHTMLHeader( 5252 mhtml_contents.emplace_back(WebFrameSerializer::generateMHTMLHeader(
5221 mhtml_boundary, GetWebFrame(), &delegate); 5253 mhtml_boundary, GetWebFrame(), &delegate));
5222 success = !mhtml_contents[0].isEmpty(); 5254 success = !mhtml_contents.back().empty();
5223 } 5255 }
5224 5256
5225 // Generate MHTML parts. Note that if this is not the main frame, then even 5257 // Generate MHTML parts. Note that if this is not the main frame, then even
5226 // skipping the whole parts generation step is not an error - it simply 5258 // skipping the whole parts generation step is not an error - it simply
5227 // results in an omitted resource in the final file. 5259 // results in an omitted resource in the final file.
5228 if (success) { 5260 if (success) {
5229 TRACE_EVENT0("page-serialization", 5261 TRACE_EVENT0("page-serialization",
5230 "RenderFrameImpl::OnSerializeAsMHTML parts serialization"); 5262 "RenderFrameImpl::OnSerializeAsMHTML parts serialization");
5231 // |data| can be empty if the frame should be skipped, but this is OK. 5263 // |data| can be empty if the frame should be skipped, but this is OK.
5232 mhtml_contents[1] = WebFrameSerializer::generateMHTMLParts( 5264 mhtml_contents.emplace_back(WebFrameSerializer::generateMHTMLParts(
5233 mhtml_boundary, GetWebFrame(), &delegate); 5265 mhtml_boundary, GetWebFrame(), &delegate));
5234 } 5266 }
5235 5267
5236 // Generate MHTML footer if needed. 5268 // Generate MHTML footer if needed.
5237 if (success && params.is_last_frame) { 5269 if (success && params.is_last_frame) {
5238 TRACE_EVENT0("page-serialization", 5270 TRACE_EVENT0("page-serialization",
5239 "RenderFrameImpl::OnSerializeAsMHTML footer"); 5271 "RenderFrameImpl::OnSerializeAsMHTML footer");
5240 mhtml_contents[2] = WebFrameSerializer::generateMHTMLFooter(mhtml_boundary); 5272 mhtml_contents.emplace_back(
5273 WebFrameSerializer::generateMHTMLFooter(mhtml_boundary));
5241 } 5274 }
5242 5275
5243 // Writes all serialized data to file. 5276 // Note: we assume RenderFrameImpl::OnWriteMHTMLToDiskComplete and the rest of
5244 // TODO(jcivelli): write the chunks in deferred tasks to give a chance to 5277 // this function will be fast enough to not need to be accounted for in this
5245 // the message loop to process other events. 5278 // metric.
5246 if (success) {
5247 TRACE_EVENT0("page-serialization",
5248 "RenderFrameImpl::OnSerializeAsMHTML writing to file");
5249 SCOPED_UMA_HISTOGRAM_TIMER(
5250 "PageSerialization.MhtmlGeneration.WriteToDiskTime.SingleFrame");
5251 for (const WebData& data : mhtml_contents) {
5252 if (file.WriteAtCurrentPos(data.data(), data.size()) < 0) {
5253 success = false;
5254 break;
5255 }
5256 }
5257 }
5258
5259 // Cleanup and notify the browser process about completion.
5260 file.Close(); // Need to flush file contents before sending IPC response.
5261 base::TimeDelta main_thread_use_time = base::TimeTicks::Now() - start_time; 5279 base::TimeDelta main_thread_use_time = base::TimeTicks::Now() - start_time;
5262 Send(new FrameHostMsg_SerializeAsMHTMLResponse(
5263 routing_id_, params.job_id, success,
5264 digests_of_uris_of_serialized_resources, main_thread_use_time));
5265 UMA_HISTOGRAM_TIMES( 5280 UMA_HISTOGRAM_TIMES(
5266 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.SingleFrame", 5281 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.SingleFrame",
5267 main_thread_use_time); 5282 main_thread_use_time);
5283
5284 base::PostTaskAndReplyWithResult(
5285 RenderThreadImpl::current()->GetFileThreadTaskRunner().get(), FROM_HERE,
5286 base::Bind(&WriteMHTMLToDisk, success,
5287 base::Passed(std::move(mhtml_contents)),
5288 base::Passed(std::move(file))),
5289 base::Bind(&RenderFrameImpl::OnWriteMHTMLToDiskComplete,
5290 base::Unretained(this), params.job_id,
5291 base::Passed(std::move(serialized_resources_uri_digests)),
5292 main_thread_use_time));
5293 }
5294
5295 void RenderFrameImpl::OnWriteMHTMLToDiskComplete(
5296 int job_id,
5297 std::set<std::string> serialized_resources_uri_digests,
5298 base::TimeDelta main_thread_use_time,
5299 bool success) {
5300 DCHECK(RenderThread::Get()) << "Must run in the main renderer thread";
5301 // Notify the browser process about completion.
5302 // Note: this method must be short enough to not need to be accounted for in
5303 // PageSerialization.MhtmlGeneration.RendererMainThreadTime.SingleFrame.
5304 Send(new FrameHostMsg_SerializeAsMHTMLResponse(
5305 routing_id_, job_id, success, serialized_resources_uri_digests,
5306 main_thread_use_time));
5268 } 5307 }
5269 5308
5270 void RenderFrameImpl::OnFind(int request_id, 5309 void RenderFrameImpl::OnFind(int request_id,
5271 const base::string16& search_text, 5310 const base::string16& search_text,
5272 const WebFindOptions& options) { 5311 const WebFindOptions& options) {
5273 DCHECK(!search_text.empty()); 5312 DCHECK(!search_text.empty());
5274 5313
5275 blink::WebPlugin* plugin = GetWebPluginForFind(); 5314 blink::WebPlugin* plugin = GetWebPluginForFind();
5276 // Check if the plugin still exists in the document. 5315 // Check if the plugin still exists in the document.
5277 if (plugin) { 5316 if (plugin) {
(...skipping 1164 matching lines...) Expand 10 before | Expand all | Expand 10 after
6442 // event target. Potentially a Pepper plugin will receive the event. 6481 // event target. Potentially a Pepper plugin will receive the event.
6443 // In order to tell whether a plugin gets the last mouse event and which it 6482 // In order to tell whether a plugin gets the last mouse event and which it
6444 // is, we set |pepper_last_mouse_event_target_| to null here. If a plugin gets 6483 // is, we set |pepper_last_mouse_event_target_| to null here. If a plugin gets
6445 // the event, it will notify us via DidReceiveMouseEvent() and set itself as 6484 // the event, it will notify us via DidReceiveMouseEvent() and set itself as
6446 // |pepper_last_mouse_event_target_|. 6485 // |pepper_last_mouse_event_target_|.
6447 pepper_last_mouse_event_target_ = nullptr; 6486 pepper_last_mouse_event_target_ = nullptr;
6448 #endif 6487 #endif
6449 } 6488 }
6450 6489
6451 } // namespace content 6490 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698