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

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: Removed unneeded std::move calls that clang was complaining about. 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 678 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 721
720 private: 722 private:
721 const std::map<GURL, base::FilePath>& url_to_local_path_; 723 const std::map<GURL, base::FilePath>& url_to_local_path_;
722 const std::map<int, base::FilePath>& frame_routing_id_to_local_path_; 724 const std::map<int, base::FilePath>& frame_routing_id_to_local_path_;
723 }; 725 };
724 726
725 // Implementation of WebFrameSerializer::MHTMLPartsGenerationDelegate that 727 // Implementation of WebFrameSerializer::MHTMLPartsGenerationDelegate that
726 // 1. Bases shouldSkipResource and getContentID responses on contents of 728 // 1. Bases shouldSkipResource and getContentID responses on contents of
727 // FrameMsg_SerializeAsMHTML_Params. 729 // FrameMsg_SerializeAsMHTML_Params.
728 // 2. Stores digests of urls of serialized resources (i.e. urls reported via 730 // 2. Stores digests of urls of serialized resources (i.e. urls reported via
729 // shouldSkipResource) into |digests_of_uris_of_serialized_resources| passed 731 // shouldSkipResource) into |serialized_resources_uri_digests| passed
730 // to the constructor. 732 // to the constructor.
731 class MHTMLPartsGenerationDelegate 733 class MHTMLPartsGenerationDelegate
732 : public WebFrameSerializer::MHTMLPartsGenerationDelegate { 734 : public WebFrameSerializer::MHTMLPartsGenerationDelegate {
733 public: 735 public:
734 MHTMLPartsGenerationDelegate( 736 MHTMLPartsGenerationDelegate(
735 const FrameMsg_SerializeAsMHTML_Params& params, 737 const FrameMsg_SerializeAsMHTML_Params& params,
736 std::set<std::string>* digests_of_uris_of_serialized_resources) 738 std::set<std::string>* serialized_resources_uri_digests)
737 : params_(params), 739 : params_(params),
738 digests_of_uris_of_serialized_resources_( 740 serialized_resources_uri_digests_(serialized_resources_uri_digests) {
739 digests_of_uris_of_serialized_resources) { 741 DCHECK(serialized_resources_uri_digests_);
740 DCHECK(digests_of_uris_of_serialized_resources_);
741 } 742 }
742 743
743 bool shouldSkipResource(const WebURL& url) override { 744 bool shouldSkipResource(const WebURL& url) override {
744 std::string digest = 745 std::string digest =
745 crypto::SHA256HashString(params_.salt + GURL(url).spec()); 746 crypto::SHA256HashString(params_.salt + GURL(url).spec());
746 747
747 // Skip if the |url| already covered by serialization of an *earlier* frame. 748 // Skip if the |url| already covered by serialization of an *earlier* frame.
748 if (base::ContainsKey(params_.digests_of_uris_to_skip, digest)) 749 if (base::ContainsKey(params_.digests_of_uris_to_skip, digest))
749 return true; 750 return true;
750 751
751 // Let's record |url| as being serialized for the *current* frame. 752 // Let's record |url| as being serialized for the *current* frame.
752 auto pair = digests_of_uris_of_serialized_resources_->insert(digest); 753 auto pair = serialized_resources_uri_digests_->insert(digest);
753 bool insertion_took_place = pair.second; 754 bool insertion_took_place = pair.second;
754 DCHECK(insertion_took_place); // Blink should dedupe within a frame. 755 DCHECK(insertion_took_place); // Blink should dedupe within a frame.
755 756
756 return false; 757 return false;
757 } 758 }
758 759
759 WebString getContentID(WebFrame* frame) override { 760 WebString getContentID(WebFrame* frame) override {
760 int routing_id = GetRoutingIdForFrameOrProxy(frame); 761 int routing_id = GetRoutingIdForFrameOrProxy(frame);
761 762
762 auto it = params_.frame_routing_id_to_content_id.find(routing_id); 763 auto it = params_.frame_routing_id_to_content_id.find(routing_id);
763 if (it == params_.frame_routing_id_to_content_id.end()) 764 if (it == params_.frame_routing_id_to_content_id.end())
764 return WebString(); 765 return WebString();
765 766
766 const std::string& content_id = it->second; 767 const std::string& content_id = it->second;
767 return WebString::fromUTF8(content_id); 768 return WebString::fromUTF8(content_id);
768 } 769 }
769 770
770 blink::WebFrameSerializerCacheControlPolicy cacheControlPolicy() override { 771 blink::WebFrameSerializerCacheControlPolicy cacheControlPolicy() override {
771 return params_.mhtml_cache_control_policy; 772 return params_.mhtml_cache_control_policy;
772 } 773 }
773 774
774 bool useBinaryEncoding() override { return params_.mhtml_binary_encoding; } 775 bool useBinaryEncoding() override { return params_.mhtml_binary_encoding; }
775 776
776 private: 777 private:
777 const FrameMsg_SerializeAsMHTML_Params& params_; 778 const FrameMsg_SerializeAsMHTML_Params& params_;
778 std::set<std::string>* digests_of_uris_of_serialized_resources_; 779 std::set<std::string>* serialized_resources_uri_digests_;
779 780
780 DISALLOW_COPY_AND_ASSIGN(MHTMLPartsGenerationDelegate); 781 DISALLOW_COPY_AND_ASSIGN(MHTMLPartsGenerationDelegate);
781 }; 782 };
782 783
784 // Writes to file the data serialized and encoded into the WebData instances.
785 bool WriteMHTMLToDisk(bool success,
786 std::vector<std::vector<char>> mhtml_contents,
787 base::File file) {
788 TRACE_EVENT_BEGIN0("page-serialization",
789 "WriteMHTMLToDisk (RenderFrameImpl)");
790 DCHECK(!RenderThread::Get()) << "Should not run in the main renderer thread";
791 base::TimeTicks start;
792 if (success) {
793 start = base::TimeTicks::Now();
794 for (const std::vector<char>& data : mhtml_contents) {
795 if (file.WriteAtCurrentPos(&data.front(), data.size()) < 0) {
796 success = false;
797 break;
798 }
799 }
800 }
801 file.Close(); // Need to flush file contents before sending IPC response.
802
803 // Only record histogram if this writing call was not a no-op (|success| being
804 // false from the start).
805 if (!start.is_null()) {
806 UMA_HISTOGRAM_TIMES(
807 "PageSerialization.MhtmlGeneration.WriteToDiskTime.SingleFrame",
808 base::TimeTicks::Now() - start);
809 }
810 TRACE_EVENT_END1("page-serialization", "WriteMHTMLToDisk (RenderFrameImpl)",
811 "success", success);
812 return success;
813 }
814
783 bool IsHttpPost(const blink::WebURLRequest& request) { 815 bool IsHttpPost(const blink::WebURLRequest& request) {
784 return request.httpMethod().utf8() == "POST"; 816 return request.httpMethod().utf8() == "POST";
785 } 817 }
786 818
787 #if defined(OS_ANDROID) 819 #if defined(OS_ANDROID)
788 // Returns true if WMPI should be used for playback, false otherwise. 820 // Returns true if WMPI should be used for playback, false otherwise.
789 // 821 //
790 // Note that HLS and MP4 detection are pre-redirect and path-based. It is 822 // Note that HLS and MP4 detection are pre-redirect and path-based. It is
791 // possible to load such a URL and find different content. 823 // possible to load such a URL and find different content.
792 bool UseWebMediaPlayerImpl(const GURL& url) { 824 bool UseWebMediaPlayerImpl(const GURL& url) {
(...skipping 4394 matching lines...) Expand 10 before | Expand all | Expand 10 after
5187 void RenderFrameImpl::OnSerializeAsMHTML( 5219 void RenderFrameImpl::OnSerializeAsMHTML(
5188 const FrameMsg_SerializeAsMHTML_Params& params) { 5220 const FrameMsg_SerializeAsMHTML_Params& params) {
5189 TRACE_EVENT0("page-serialization", "RenderFrameImpl::OnSerializeAsMHTML"); 5221 TRACE_EVENT0("page-serialization", "RenderFrameImpl::OnSerializeAsMHTML");
5190 base::TimeTicks start_time = base::TimeTicks::Now(); 5222 base::TimeTicks start_time = base::TimeTicks::Now();
5191 // Unpack IPC payload. 5223 // Unpack IPC payload.
5192 base::File file = IPC::PlatformFileForTransitToFile(params.destination_file); 5224 base::File file = IPC::PlatformFileForTransitToFile(params.destination_file);
5193 const WebString mhtml_boundary = 5225 const WebString mhtml_boundary =
5194 WebString::fromUTF8(params.mhtml_boundary_marker); 5226 WebString::fromUTF8(params.mhtml_boundary_marker);
5195 DCHECK(!mhtml_boundary.isEmpty()); 5227 DCHECK(!mhtml_boundary.isEmpty());
5196 5228
5197 // Three WebData instances for header, parts and footer. 5229 // Holds WebData instances all or some of header, contents/parts and footer.
5198 WebData mhtml_contents[3]; 5230 std::vector<std::vector<char>> mhtml_contents;
5199 std::set<std::string> digests_of_uris_of_serialized_resources; 5231 std::set<std::string> serialized_resources_uri_digests;
5200 MHTMLPartsGenerationDelegate delegate( 5232 MHTMLPartsGenerationDelegate delegate(params,
5201 params, &digests_of_uris_of_serialized_resources); 5233 &serialized_resources_uri_digests);
5202 5234
5203 bool success = true; 5235 bool success = true;
5204 5236
5205 // Generate MHTML header if needed. 5237 // Generate MHTML header if needed.
5206 if (IsMainFrame()) { 5238 if (IsMainFrame()) {
5207 TRACE_EVENT0("page-serialization", 5239 TRACE_EVENT0("page-serialization",
5208 "RenderFrameImpl::OnSerializeAsMHTML header"); 5240 "RenderFrameImpl::OnSerializeAsMHTML header");
5209 // |data| can be empty if the main frame should be skipped. If the main 5241 // |data| can be empty if the main frame should be skipped. If the main
5210 // frame is skipped, then the whole archive is bad, so bail to the error 5242 // frame is skipped, then the whole archive is bad, so bail to the error
5211 // condition. 5243 // condition.
5212 mhtml_contents[0] = WebFrameSerializer::generateMHTMLHeader( 5244 mhtml_contents.emplace_back(WebFrameSerializer::generateMHTMLHeader(
5213 mhtml_boundary, GetWebFrame(), &delegate); 5245 mhtml_boundary, GetWebFrame(), &delegate));
5214 success = !mhtml_contents[0].isEmpty(); 5246 success = !mhtml_contents.back().empty();
5215 } 5247 }
5216 5248
5217 // Generate MHTML parts. Note that if this is not the main frame, then even 5249 // Generate MHTML parts. Note that if this is not the main frame, then even
5218 // skipping the whole parts generation step is not an error - it simply 5250 // skipping the whole parts generation step is not an error - it simply
5219 // results in an omitted resource in the final file. 5251 // results in an omitted resource in the final file.
5220 if (success) { 5252 if (success) {
5221 TRACE_EVENT0("page-serialization", 5253 TRACE_EVENT0("page-serialization",
5222 "RenderFrameImpl::OnSerializeAsMHTML parts serialization"); 5254 "RenderFrameImpl::OnSerializeAsMHTML parts serialization");
5223 // |data| can be empty if the frame should be skipped, but this is OK. 5255 // |data| can be empty if the frame should be skipped, but this is OK.
5224 mhtml_contents[1] = WebFrameSerializer::generateMHTMLParts( 5256 mhtml_contents.emplace_back(WebFrameSerializer::generateMHTMLParts(
5225 mhtml_boundary, GetWebFrame(), &delegate); 5257 mhtml_boundary, GetWebFrame(), &delegate));
5226 } 5258 }
5227 5259
5228 // Generate MHTML footer if needed. 5260 // Generate MHTML footer if needed.
5229 if (success && params.is_last_frame) { 5261 if (success && params.is_last_frame) {
5230 TRACE_EVENT0("page-serialization", 5262 TRACE_EVENT0("page-serialization",
5231 "RenderFrameImpl::OnSerializeAsMHTML footer"); 5263 "RenderFrameImpl::OnSerializeAsMHTML footer");
5232 mhtml_contents[2] = WebFrameSerializer::generateMHTMLFooter(mhtml_boundary); 5264 mhtml_contents.emplace_back(
5265 WebFrameSerializer::generateMHTMLFooter(mhtml_boundary));
5233 } 5266 }
5234 5267
5235 // Writes all serialized data to file. 5268 // Note: we assume RenderFrameImpl::OnWriteMHTMLToDiskComplete and the rest of
5236 // TODO(jcivelli): write the chunks in deferred tasks to give a chance to 5269 // this function will be fast enough to not need to be accounted for in this
5237 // the message loop to process other events. 5270 // metric.
5238 if (success) {
5239 TRACE_EVENT0("page-serialization",
5240 "RenderFrameImpl::OnSerializeAsMHTML writing to file");
5241 SCOPED_UMA_HISTOGRAM_TIMER(
5242 "PageSerialization.MhtmlGeneration.WriteToDiskTime.SingleFrame");
5243 for (const WebData& data : mhtml_contents) {
5244 if (file.WriteAtCurrentPos(data.data(), data.size()) < 0) {
5245 success = false;
5246 break;
5247 }
5248 }
5249 }
5250
5251 // Cleanup and notify the browser process about completion.
5252 file.Close(); // Need to flush file contents before sending IPC response.
5253 base::TimeDelta main_thread_use_time = base::TimeTicks::Now() - start_time; 5271 base::TimeDelta main_thread_use_time = base::TimeTicks::Now() - start_time;
5254 Send(new FrameHostMsg_SerializeAsMHTMLResponse(
5255 routing_id_, params.job_id, success,
5256 digests_of_uris_of_serialized_resources, main_thread_use_time));
5257 UMA_HISTOGRAM_TIMES( 5272 UMA_HISTOGRAM_TIMES(
5258 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.SingleFrame", 5273 "PageSerialization.MhtmlGeneration.RendererMainThreadTime.SingleFrame",
5259 main_thread_use_time); 5274 main_thread_use_time);
5275
5276 base::PostTaskAndReplyWithResult(
5277 RenderThreadImpl::current()->GetFileThreadTaskRunner().get(), FROM_HERE,
5278 base::Bind(&WriteMHTMLToDisk, success,
5279 base::Passed(std::move(mhtml_contents)),
5280 base::Passed(std::move(file))),
5281 base::Bind(&RenderFrameImpl::OnWriteMHTMLToDiskComplete,
5282 base::Unretained(this), params.job_id,
5283 base::Passed(std::move(serialized_resources_uri_digests)),
5284 main_thread_use_time));
5285 }
5286
5287 void RenderFrameImpl::OnWriteMHTMLToDiskComplete(
5288 int job_id,
5289 std::set<std::string> serialized_resources_uri_digests,
5290 base::TimeDelta main_thread_use_time,
5291 bool success) {
5292 DCHECK(RenderThread::Get()) << "Must run in the main renderer thread";
5293 // Notify the browser process about completion.
5294 // Note: this method must be short enough to not need to be accounted for in
5295 // PageSerialization.MhtmlGeneration.RendererMainThreadTime.SingleFrame.
5296 Send(new FrameHostMsg_SerializeAsMHTMLResponse(
5297 routing_id_, job_id, success, serialized_resources_uri_digests,
5298 main_thread_use_time));
5260 } 5299 }
5261 5300
5262 void RenderFrameImpl::OnFind(int request_id, 5301 void RenderFrameImpl::OnFind(int request_id,
5263 const base::string16& search_text, 5302 const base::string16& search_text,
5264 const WebFindOptions& options) { 5303 const WebFindOptions& options) {
5265 DCHECK(!search_text.empty()); 5304 DCHECK(!search_text.empty());
5266 5305
5267 blink::WebPlugin* plugin = GetWebPluginForFind(); 5306 blink::WebPlugin* plugin = GetWebPluginForFind();
5268 // Check if the plugin still exists in the document. 5307 // Check if the plugin still exists in the document.
5269 if (plugin) { 5308 if (plugin) {
(...skipping 1160 matching lines...) Expand 10 before | Expand all | Expand 10 after
6430 // event target. Potentially a Pepper plugin will receive the event. 6469 // event target. Potentially a Pepper plugin will receive the event.
6431 // In order to tell whether a plugin gets the last mouse event and which it 6470 // In order to tell whether a plugin gets the last mouse event and which it
6432 // is, we set |pepper_last_mouse_event_target_| to null here. If a plugin gets 6471 // is, we set |pepper_last_mouse_event_target_| to null here. If a plugin gets
6433 // the event, it will notify us via DidReceiveMouseEvent() and set itself as 6472 // the event, it will notify us via DidReceiveMouseEvent() and set itself as
6434 // |pepper_last_mouse_event_target_|. 6473 // |pepper_last_mouse_event_target_|.
6435 pepper_last_mouse_event_target_ = nullptr; 6474 pepper_last_mouse_event_target_ = nullptr;
6436 #endif 6475 #endif
6437 } 6476 }
6438 6477
6439 } // namespace content 6478 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698