Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/service_worker/service_worker_navigation_preload_fetch er.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "base/time/time.h" | |
| 10 #include "base/values.h" | |
| 11 #include "content/browser/loader/layered_resource_handler.h" | |
| 12 #include "content/browser/loader/mojo_async_resource_handler.h" | |
| 13 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 14 #include "content/browser/loader/resource_handler.h" | |
| 15 #include "content/browser/loader/resource_loader.h" | |
| 16 #include "content/browser/loader/resource_request_info_impl.h" | |
| 17 #include "content/common/url_loader.mojom.h" | |
| 18 #include "content/public/common/resource_response.h" | |
| 19 #include "net/log/net_log_event_type.h" | |
| 20 #include "net/url_request/url_request.h" | |
| 21 #include "net/url_request/url_request_context.h" | |
| 22 | |
| 23 namespace content { | |
| 24 namespace { | |
| 25 | |
| 26 std::unique_ptr<base::Value> NetLogCallbackForURL( | |
| 27 const GURL* url, | |
| 28 net::NetLogCaptureMode /* capture_mode */) { | |
| 29 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 30 dict->SetString("url", url->possibly_invalid_spec()); | |
| 31 return std::move(dict); | |
| 32 } | |
| 33 | |
| 34 class PreloadResourceHandler : public LayeredResourceHandler { | |
| 35 public: | |
| 36 PreloadResourceHandler(net::URLRequest* request, | |
| 37 std::unique_ptr<ResourceHandler> next_handler) | |
| 38 : LayeredResourceHandler(request, std::move(next_handler)) {} | |
| 39 ~PreloadResourceHandler() override {} | |
| 40 | |
| 41 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
| 42 ResourceResponse* response, | |
| 43 bool* defer) override { | |
| 44 return false; | |
| 45 } | |
| 46 bool OnWillStart(const GURL& url, bool* defer) override { | |
| 47 request()->net_log().BeginEvent( | |
| 48 net::NetLogEventType::SERVICE_WORKER_NAVIGATION_PRELOAD, | |
| 49 base::Bind(&NetLogCallbackForURL, &url)); | |
| 50 return LayeredResourceHandler::OnWillStart(url, defer); | |
| 51 } | |
| 52 void OnResponseCompleted(const net::URLRequestStatus& status, | |
| 53 bool* defer) override { | |
| 54 request()->net_log().EndEvent( | |
| 55 net::NetLogEventType::SERVICE_WORKER_NAVIGATION_PRELOAD); | |
|
falken
2016/10/14 07:53:12
Could be useful to log status.
horo
2016/10/14 16:09:05
Done.
| |
| 56 LayeredResourceHandler::OnResponseCompleted(status, defer); | |
|
falken
2016/10/14 07:53:12
Is OnResponseCompleted definitely called for every
horo
2016/10/14 16:09:05
Done.
Sounds good.
OnWillStart() is not called if
| |
| 57 } | |
| 58 }; | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 // static | |
| 63 mojom::FetchEventPreloadHandlePtr ServiceWorkerNavigationPreloadFetcher::Start( | |
| 64 net::URLRequest* original_request) { | |
| 65 mojom::FetchEventPreloadHandlePtr preload_handle( | |
| 66 mojom::FetchEventPreloadHandle::New()); | |
| 67 const ResourceRequestInfoImpl* original_info = | |
| 68 ResourceRequestInfoImpl::ForRequest(original_request); | |
| 69 ResourceContext* resource_context = original_info->GetContext(); | |
| 70 std::unique_ptr<net::URLRequest> url_request( | |
| 71 original_request->context()->CreateRequest( | |
| 72 original_request->url(), original_request->priority(), nullptr)); | |
| 73 ResourceDispatcherHostImpl::Get()->InitializeURLRequest( | |
| 74 url_request.get(), Referrer(GURL(original_request->referrer()), | |
| 75 original_info->GetReferrerPolicy()), | |
| 76 false /* download */, original_info->GetChildID(), | |
| 77 original_info->GetRouteID(), original_info->GetRenderFrameID(), | |
| 78 resource_context); | |
| 79 | |
| 80 mojom::URLLoaderClientPtr url_loader_client; | |
| 81 preload_handle->url_loader_client_request = GetProxy(&url_loader_client); | |
| 82 | |
| 83 std::unique_ptr<ResourceHandler> handler(new MojoAsyncResourceHandler( | |
| 84 url_request.get(), ResourceDispatcherHostImpl::Get(), | |
| 85 GetProxy(&preload_handle->url_loader), std::move(url_loader_client))); | |
| 86 handler.reset( | |
| 87 new PreloadResourceHandler(url_request.get(), std::move(handler))); | |
| 88 | |
| 89 ResourceDispatcherHostImpl::Get()->BeginURLRequest( | |
| 90 std::move(url_request), std::move(handler), false /* download */, | |
| 91 false /* is_content_initiated */, true /* do_not_prompt_for_login */, | |
| 92 resource_context); | |
| 93 return preload_handle; | |
| 94 } | |
| 95 | |
| 96 } // namespace content | |
| OLD | NEW |