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

Side by Side Diff: content/browser/loader/resource_dispatcher_host_impl.cc

Issue 2251643003: Remove the BeginSaveFile and BeginDownload methods from ResourceDispatcherHostImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix content_unittests compile failures. The BeginDownloadHelper function is now a static public fun… Created 4 years, 4 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 (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 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc e-loading 5 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc e-loading
6 6
7 #include "content/browser/loader/resource_dispatcher_host_impl.h" 7 #include "content/browser/loader/resource_dispatcher_host_impl.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 21 matching lines...) Expand all
32 #include "base/stl_util.h" 32 #include "base/stl_util.h"
33 #include "base/strings/string_util.h" 33 #include "base/strings/string_util.h"
34 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 34 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
35 #include "base/time/time.h" 35 #include "base/time/time.h"
36 #include "content/browser/appcache/appcache_interceptor.h" 36 #include "content/browser/appcache/appcache_interceptor.h"
37 #include "content/browser/appcache/chrome_appcache_service.h" 37 #include "content/browser/appcache/chrome_appcache_service.h"
38 #include "content/browser/bad_message.h" 38 #include "content/browser/bad_message.h"
39 #include "content/browser/blob_storage/chrome_blob_storage_context.h" 39 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
40 #include "content/browser/cert_store_impl.h" 40 #include "content/browser/cert_store_impl.h"
41 #include "content/browser/child_process_security_policy_impl.h" 41 #include "content/browser/child_process_security_policy_impl.h"
42 #include "content/browser/download/download_resource_handler.h"
43 #include "content/browser/download/save_file_resource_handler.h"
44 #include "content/browser/frame_host/frame_tree.h" 42 #include "content/browser/frame_host/frame_tree.h"
45 #include "content/browser/frame_host/navigation_request_info.h" 43 #include "content/browser/frame_host/navigation_request_info.h"
46 #include "content/browser/frame_host/navigator.h" 44 #include "content/browser/frame_host/navigator.h"
47 #include "content/browser/loader/async_resource_handler.h" 45 #include "content/browser/loader/async_resource_handler.h"
48 #include "content/browser/loader/async_revalidation_manager.h" 46 #include "content/browser/loader/async_revalidation_manager.h"
49 #include "content/browser/loader/cross_site_resource_handler.h" 47 #include "content/browser/loader/cross_site_resource_handler.h"
50 #include "content/browser/loader/detachable_resource_handler.h" 48 #include "content/browser/loader/detachable_resource_handler.h"
51 #include "content/browser/loader/loader_delegate.h" 49 #include "content/browser/loader/loader_delegate.h"
52 #include "content/browser/loader/mime_type_resource_handler.h" 50 #include "content/browser/loader/mime_type_resource_handler.h"
53 #include "content/browser/loader/mojo_async_resource_handler.h" 51 #include "content/browser/loader/mojo_async_resource_handler.h"
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 loaders_to_cancel.clear(); 601 loaders_to_cancel.clear();
604 602
605 if (async_revalidation_manager_) { 603 if (async_revalidation_manager_) {
606 // Cancelling async revalidations should not result in the creation of new 604 // Cancelling async revalidations should not result in the creation of new
607 // requests. Do it before the CHECKs to ensure this does not happen. 605 // requests. Do it before the CHECKs to ensure this does not happen.
608 async_revalidation_manager_->CancelAsyncRevalidationsForResourceContext( 606 async_revalidation_manager_->CancelAsyncRevalidationsForResourceContext(
609 context); 607 context);
610 } 608 }
611 } 609 }
612 610
613 DownloadInterruptReason ResourceDispatcherHostImpl::BeginDownload(
614 std::unique_ptr<net::URLRequest> request,
615 const Referrer& referrer,
616 bool is_content_initiated,
617 ResourceContext* context,
618 int render_process_id,
619 int render_view_route_id,
620 int render_frame_route_id,
621 bool do_not_prompt_for_login) {
622 if (is_shutdown_)
623 return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN;
624
625 const GURL& url = request->original_url();
626 SetReferrerForRequest(request.get(), referrer);
627
628 // We treat a download as a main frame load, and thus update the policy URL on
629 // redirects.
630 //
631 // TODO(davidben): Is this correct? If this came from a
632 // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set
633 // appropriately?
634 request->set_first_party_url_policy(
635 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
636
637 // Check if the renderer is permitted to request the requested URL.
638 if (!ChildProcessSecurityPolicyImpl::GetInstance()->
639 CanRequestURL(render_process_id, url)) {
640 DVLOG(1) << "Denied unauthorized download request for "
641 << url.possibly_invalid_spec();
642 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
643 }
644
645 request_id_--;
646
647 const net::URLRequestContext* request_context = request->context();
648 if (!request_context->job_factory()->IsHandledURL(url)) {
649 DVLOG(1) << "Download request for unsupported protocol: "
650 << url.possibly_invalid_spec();
651 return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST;
652 }
653
654 ResourceRequestInfoImpl* extra_info =
655 CreateRequestInfo(render_process_id, render_view_route_id,
656 render_frame_route_id, true, context);
657 extra_info->set_do_not_prompt_for_login(do_not_prompt_for_login);
658 extra_info->AssociateWithRequest(request.get()); // Request takes ownership.
659
660 if (request->url().SchemeIs(url::kBlobScheme) &&
661 !storage::BlobProtocolHandler::GetRequestBlobDataHandle(request.get())) {
662 ChromeBlobStorageContext* blob_context =
663 GetChromeBlobStorageContextForResourceContext(context);
664 storage::BlobProtocolHandler::SetRequestedBlobDataHandle(
665 request.get(),
666 blob_context->context()->GetBlobDataFromPublicURL(request->url()));
667 }
668
669 // From this point forward, the |DownloadResourceHandler| is responsible for
670 // |started_callback|.
671 std::unique_ptr<ResourceHandler> handler(CreateResourceHandlerForDownload(
672 request.get(), is_content_initiated, true));
673
674 BeginRequestInternal(std::move(request), std::move(handler));
675
676 return DOWNLOAD_INTERRUPT_REASON_NONE;
677 }
678
679 void ResourceDispatcherHostImpl::ClearLoginDelegateForRequest( 611 void ResourceDispatcherHostImpl::ClearLoginDelegateForRequest(
680 net::URLRequest* request) { 612 net::URLRequest* request) {
681 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request); 613 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request);
682 if (info) { 614 if (info) {
683 ResourceLoader* loader = GetLoader(info->GetGlobalRequestID()); 615 ResourceLoader* loader = GetLoader(info->GetGlobalRequestID());
684 if (loader) 616 if (loader)
685 loader->ClearLoginDelegate(); 617 loader->ClearLoginDelegate();
686 } 618 }
687 } 619 }
688 620
(...skipping 20 matching lines...) Expand all
709 FROM_HERE, 641 FROM_HERE,
710 base::Bind(&ResourceDispatcherHostImpl::OnShutdown, 642 base::Bind(&ResourceDispatcherHostImpl::OnShutdown,
711 base::Unretained(this))); 643 base::Unretained(this)));
712 } 644 }
713 645
714 std::unique_ptr<ResourceHandler> 646 std::unique_ptr<ResourceHandler>
715 ResourceDispatcherHostImpl::CreateResourceHandlerForDownload( 647 ResourceDispatcherHostImpl::CreateResourceHandlerForDownload(
716 net::URLRequest* request, 648 net::URLRequest* request,
717 bool is_content_initiated, 649 bool is_content_initiated,
718 bool must_download) { 650 bool must_download) {
719 std::unique_ptr<ResourceHandler> handler( 651 if (!create_download_handler_intercept_.is_null()) {
720 new DownloadResourceHandler(request)); 652 return create_download_handler_intercept_.Run(request, is_content_initiated,
721 if (delegate_) { 653 must_download);
722 const ResourceRequestInfoImpl* request_info(
723 ResourceRequestInfoImpl::ForRequest(request));
724
725 ScopedVector<ResourceThrottle> throttles;
726 delegate_->DownloadStarting(
727 request, request_info->GetContext(), request_info->GetChildID(),
728 request_info->GetRouteID(), is_content_initiated, must_download,
729 &throttles);
730 if (!throttles.empty()) {
731 handler.reset(new ThrottlingResourceHandler(std::move(handler), request,
732 std::move(throttles)));
733 }
734 } 654 }
735 return handler; 655 return std::unique_ptr<ResourceHandler>();
736 } 656 }
737 657
738 std::unique_ptr<ResourceHandler> 658 std::unique_ptr<ResourceHandler>
739 ResourceDispatcherHostImpl::MaybeInterceptAsStream( 659 ResourceDispatcherHostImpl::MaybeInterceptAsStream(
740 const base::FilePath& plugin_path, 660 const base::FilePath& plugin_path,
741 net::URLRequest* request, 661 net::URLRequest* request,
742 ResourceResponse* response, 662 ResourceResponse* response,
743 std::string* payload) { 663 std::string* payload) {
744 payload->clear(); 664 payload->clear();
745 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request); 665 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request);
(...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after
1585 new_request.get(), filter_->appcache_service(), child_id, 1505 new_request.get(), filter_->appcache_service(), child_id,
1586 request_data.appcache_host_id, request_data.resource_type, 1506 request_data.appcache_host_id, request_data.resource_type,
1587 request_data.should_reset_appcache); 1507 request_data.should_reset_appcache);
1588 1508
1589 std::unique_ptr<ResourceHandler> handler(CreateResourceHandler( 1509 std::unique_ptr<ResourceHandler> handler(CreateResourceHandler(
1590 new_request.get(), request_data, sync_result, route_id, process_type, 1510 new_request.get(), request_data, sync_result, route_id, process_type,
1591 child_id, resource_context, std::move(mojo_request), 1511 child_id, resource_context, std::move(mojo_request),
1592 std::move(url_loader_client))); 1512 std::move(url_loader_client)));
1593 1513
1594 if (handler) 1514 if (handler)
1595 BeginRequestInternal(std::move(new_request), std::move(handler)); 1515 BeginURLRequest(std::move(new_request), std::move(handler));
1596 } 1516 }
1597 1517
1598 std::unique_ptr<ResourceHandler> 1518 std::unique_ptr<ResourceHandler>
1599 ResourceDispatcherHostImpl::CreateResourceHandler( 1519 ResourceDispatcherHostImpl::CreateResourceHandler(
1600 net::URLRequest* request, 1520 net::URLRequest* request,
1601 const ResourceRequest& request_data, 1521 const ResourceRequest& request_data,
1602 IPC::Message* sync_result, 1522 IPC::Message* sync_result,
1603 int route_id, 1523 int route_id,
1604 int process_type, 1524 int process_type,
1605 int child_id, 1525 int child_id,
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1822 1742
1823 loader->CancelRequest(true); 1743 loader->CancelRequest(true);
1824 } 1744 }
1825 1745
1826 ResourceRequestInfoImpl* ResourceDispatcherHostImpl::CreateRequestInfo( 1746 ResourceRequestInfoImpl* ResourceDispatcherHostImpl::CreateRequestInfo(
1827 int child_id, 1747 int child_id,
1828 int render_view_route_id, 1748 int render_view_route_id,
1829 int render_frame_route_id, 1749 int render_frame_route_id,
1830 bool download, 1750 bool download,
1831 ResourceContext* context) { 1751 ResourceContext* context) {
1752 request_id_--;
1832 return new ResourceRequestInfoImpl( 1753 return new ResourceRequestInfoImpl(
1833 PROCESS_TYPE_RENDERER, 1754 PROCESS_TYPE_RENDERER,
1834 child_id, 1755 child_id,
1835 render_view_route_id, 1756 render_view_route_id,
1836 -1, // frame_tree_node_id 1757 -1, // frame_tree_node_id
1837 0, 1758 0,
1838 request_id_, 1759 request_id_,
1839 render_frame_route_id, 1760 render_frame_route_id,
1840 false, // is_main_frame 1761 false, // is_main_frame
1841 false, // parent_is_main_frame 1762 false, // parent_is_main_frame
(...skipping 12 matching lines...) Expand all
1854 context, 1775 context,
1855 base::WeakPtr<ResourceMessageFilter>(), // filter 1776 base::WeakPtr<ResourceMessageFilter>(), // filter
1856 false, // report_raw_headers 1777 false, // report_raw_headers
1857 true, // is_async 1778 true, // is_async
1858 false, // is_using_lofi 1779 false, // is_using_lofi
1859 std::string(), // original_headers 1780 std::string(), // original_headers
1860 nullptr, // body 1781 nullptr, // body
1861 false); // initiated_in_secure_context 1782 false); // initiated_in_secure_context
1862 } 1783 }
1863 1784
1785 void ResourceDispatcherHostImpl::RegisterCreateDownloadHandlerInterceptor(
1786 CreateDownloadHandlerIntercept intercept) {
1787 create_download_handler_intercept_ = intercept;
1788 }
1789
1864 void ResourceDispatcherHostImpl::OnRenderViewHostCreated(int child_id, 1790 void ResourceDispatcherHostImpl::OnRenderViewHostCreated(int child_id,
1865 int route_id) { 1791 int route_id) {
1866 scheduler_->OnClientCreated(child_id, route_id); 1792 scheduler_->OnClientCreated(child_id, route_id);
1867 } 1793 }
1868 1794
1869 void ResourceDispatcherHostImpl::OnRenderViewHostDeleted(int child_id, 1795 void ResourceDispatcherHostImpl::OnRenderViewHostDeleted(int child_id,
1870 int route_id) { 1796 int route_id) {
1871 scheduler_->OnClientDeleted(child_id, route_id); 1797 scheduler_->OnClientDeleted(child_id, route_id);
1872 } 1798 }
1873 1799
1874 void ResourceDispatcherHostImpl::OnRenderViewHostSetIsLoading(int child_id, 1800 void ResourceDispatcherHostImpl::OnRenderViewHostSetIsLoading(int child_id,
1875 int route_id, 1801 int route_id,
1876 bool is_loading) { 1802 bool is_loading) {
1877 scheduler_->OnLoadingStateChanged(child_id, route_id, !is_loading); 1803 scheduler_->OnLoadingStateChanged(child_id, route_id, !is_loading);
1878 } 1804 }
1879 1805
1880 // This function is only used for saving feature.
1881 void ResourceDispatcherHostImpl::BeginSaveFile(const GURL& url,
1882 const Referrer& referrer,
1883 SaveItemId save_item_id,
1884 SavePackageId save_package_id,
1885 int child_id,
1886 int render_view_route_id,
1887 int render_frame_route_id,
1888 ResourceContext* context) {
1889 if (is_shutdown_)
1890 return;
1891
1892 request_id_--;
1893
1894 const net::URLRequestContext* request_context = context->GetRequestContext();
1895 bool known_proto =
1896 request_context->job_factory()->IsHandledURL(url);
1897 if (!known_proto) {
1898 // Since any URLs which have non-standard scheme have been filtered
1899 // by save manager(see GURL::SchemeIsStandard). This situation
1900 // should not happen.
1901 NOTREACHED();
1902 return;
1903 }
1904
1905 std::unique_ptr<net::URLRequest> request(
1906 request_context->CreateRequest(url, net::DEFAULT_PRIORITY, NULL));
1907 request->set_method("GET");
1908 SetReferrerForRequest(request.get(), referrer);
1909
1910 // So far, for saving page, we need fetch content from cache, in the
1911 // future, maybe we can use a configuration to configure this behavior.
1912 request->SetLoadFlags(net::LOAD_PREFERRING_CACHE);
1913
1914 // Since we're just saving some resources we need, disallow downloading.
1915 ResourceRequestInfoImpl* extra_info =
1916 CreateRequestInfo(child_id, render_view_route_id,
1917 render_frame_route_id, false, context);
1918 extra_info->AssociateWithRequest(request.get()); // Request takes ownership.
1919
1920 // Check if the renderer is permitted to request the requested URL.
1921 using AuthorizationState = SaveFileResourceHandler::AuthorizationState;
1922 AuthorizationState authorization_state = AuthorizationState::AUTHORIZED;
1923 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(child_id,
1924 url)) {
1925 DVLOG(1) << "Denying unauthorized save of " << url.possibly_invalid_spec();
1926 authorization_state = AuthorizationState::NOT_AUTHORIZED;
1927 // No need to return here (i.e. okay to begin processing the request below),
1928 // because NOT_AUTHORIZED will cause the request to be cancelled. See also
1929 // doc comments for AuthorizationState enum.
1930 }
1931
1932 std::unique_ptr<SaveFileResourceHandler> handler(new SaveFileResourceHandler(
1933 request.get(), save_item_id, save_package_id, child_id,
1934 render_frame_route_id, url, authorization_state));
1935
1936 BeginRequestInternal(std::move(request), std::move(handler));
1937 }
1938
1939 void ResourceDispatcherHostImpl::MarkAsTransferredNavigation( 1806 void ResourceDispatcherHostImpl::MarkAsTransferredNavigation(
1940 const GlobalRequestID& id, 1807 const GlobalRequestID& id,
1941 const scoped_refptr<ResourceResponse>& response) { 1808 const scoped_refptr<ResourceResponse>& response) {
1942 GetLoader(id)->MarkAsTransferring(response); 1809 GetLoader(id)->MarkAsTransferring(response);
1943 } 1810 }
1944 1811
1945 void ResourceDispatcherHostImpl::CancelTransferringNavigation( 1812 void ResourceDispatcherHostImpl::CancelTransferringNavigation(
1946 const GlobalRequestID& id) { 1813 const GlobalRequestID& id) {
1947 // Request should still exist and be in the middle of a transfer. 1814 // Request should still exist and be in the middle of a transfer.
1948 DCHECK(IsTransferredNavigation(id)); 1815 DCHECK(IsTransferredNavigation(id));
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
2303 // dependency on child_id/route_id. Those are used by the ResourceScheduler; 2170 // dependency on child_id/route_id. Those are used by the ResourceScheduler;
2304 // currently it's a no-op. 2171 // currently it's a no-op.
2305 handler = 2172 handler =
2306 AddStandardHandlers(new_request.get(), resource_type, resource_context, 2173 AddStandardHandlers(new_request.get(), resource_type, resource_context,
2307 info.begin_params.request_context_type, 2174 info.begin_params.request_context_type,
2308 nullptr, // appcache_service 2175 nullptr, // appcache_service
2309 -1, // child_id 2176 -1, // child_id
2310 -1, // route_id 2177 -1, // route_id
2311 std::move(handler)); 2178 std::move(handler));
2312 2179
2313 BeginRequestInternal(std::move(new_request), std::move(handler)); 2180 BeginURLRequest(std::move(new_request), std::move(handler));
2314 } 2181 }
2315 2182
2316 void ResourceDispatcherHostImpl::EnableStaleWhileRevalidateForTesting() { 2183 void ResourceDispatcherHostImpl::EnableStaleWhileRevalidateForTesting() {
2317 if (!async_revalidation_manager_) 2184 if (!async_revalidation_manager_)
2318 async_revalidation_manager_.reset(new AsyncRevalidationManager); 2185 async_revalidation_manager_.reset(new AsyncRevalidationManager);
2319 } 2186 }
2320 2187
2321 void ResourceDispatcherHostImpl::SetLoaderDelegate( 2188 void ResourceDispatcherHostImpl::SetLoaderDelegate(
2322 LoaderDelegate* loader_delegate) { 2189 LoaderDelegate* loader_delegate) {
2323 loader_delegate_ = loader_delegate; 2190 loader_delegate_ = loader_delegate;
(...skipping 27 matching lines...) Expand all
2351 int strings_cost = request->extra_request_headers().ToString().size() + 2218 int strings_cost = request->extra_request_headers().ToString().size() +
2352 request->original_url().spec().size() + 2219 request->original_url().spec().size() +
2353 request->referrer().size() + 2220 request->referrer().size() +
2354 request->method().size(); 2221 request->method().size();
2355 2222
2356 // Note that this expression will typically be dominated by: 2223 // Note that this expression will typically be dominated by:
2357 // |kAvgBytesPerOutstandingRequest|. 2224 // |kAvgBytesPerOutstandingRequest|.
2358 return kAvgBytesPerOutstandingRequest + strings_cost; 2225 return kAvgBytesPerOutstandingRequest + strings_cost;
2359 } 2226 }
2360 2227
2361 void ResourceDispatcherHostImpl::BeginRequestInternal( 2228 void ResourceDispatcherHostImpl::BeginURLRequest(
2362 std::unique_ptr<net::URLRequest> request, 2229 std::unique_ptr<net::URLRequest> request,
2363 std::unique_ptr<ResourceHandler> handler) { 2230 std::unique_ptr<ResourceHandler> handler) {
2364 DCHECK(!request->is_pending()); 2231 DCHECK(!request->is_pending());
2365 ResourceRequestInfoImpl* info = 2232 ResourceRequestInfoImpl* info =
2366 ResourceRequestInfoImpl::ForRequest(request.get()); 2233 ResourceRequestInfoImpl::ForRequest(request.get());
2367 2234
2368 if ((TimeTicks::Now() - last_user_gesture_time_) < 2235 if ((TimeTicks::Now() - last_user_gesture_time_) <
2369 TimeDelta::FromMilliseconds(kUserGestureWindowMs)) { 2236 TimeDelta::FromMilliseconds(kUserGestureWindowMs)) {
2370 request->SetLoadFlags( 2237 request->SetLoadFlags(
2371 request->load_flags() | net::LOAD_MAYBE_USER_GESTURE); 2238 request->load_flags() | net::LOAD_MAYBE_USER_GESTURE);
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
2719 << iter->filesystem_url().spec(); 2586 << iter->filesystem_url().spec();
2720 return false; 2587 return false;
2721 } 2588 }
2722 } 2589 }
2723 } 2590 }
2724 } 2591 }
2725 return true; 2592 return true;
2726 } 2593 }
2727 2594
2728 } // namespace content 2595 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698