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/base/net_errors.h" | |
20 #include "net/log/net_log_event_type.h" | |
21 #include "net/url_request/url_request.h" | |
22 #include "net/url_request/url_request_context.h" | |
23 | |
24 namespace content { | |
25 namespace { | |
26 | |
27 std::unique_ptr<base::Value> NetLogCallbackForURL( | |
28 const GURL* url, | |
29 net::NetLogCaptureMode /* capture_mode */) { | |
30 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
31 dict->SetString("url", url->possibly_invalid_spec()); | |
32 return std::move(dict); | |
33 } | |
34 | |
35 class PreloadResourceHandler : public LayeredResourceHandler { | |
mmenke
2016/10/14 16:31:33
While this class is really simple (Which is good),
horo
2016/10/15 00:55:46
Done.
| |
36 public: | |
37 PreloadResourceHandler(net::URLRequest* request, | |
38 std::unique_ptr<ResourceHandler> next_handler) | |
39 : LayeredResourceHandler(request, std::move(next_handler)) { | |
40 request->net_log().BeginEvent( | |
41 net::NetLogEventType::SERVICE_WORKER_NAVIGATION_PRELOAD, | |
42 base::Bind(&NetLogCallbackForURL, &request->url())); | |
43 } | |
44 | |
45 ~PreloadResourceHandler() override { | |
46 request()->net_log().EndEventWithNetErrorCode( | |
47 net::NetLogEventType::SERVICE_WORKER_NAVIGATION_PRELOAD, net_error_); | |
48 } | |
49 | |
50 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
51 ResourceResponse* response, | |
52 bool* defer) override { | |
53 // NavigationPreload doesn't support redirect. | |
54 return false; | |
55 } | |
56 | |
57 void OnResponseCompleted(const net::URLRequestStatus& status, | |
58 bool* defer) override { | |
59 net_error_ = status.error(); | |
60 LayeredResourceHandler::OnResponseCompleted(status, defer); | |
61 } | |
62 | |
63 private: | |
64 int net_error_ = net::Error::ERR_ABORTED; | |
65 }; | |
66 | |
67 } // namespace | |
68 | |
69 // static | |
70 mojom::FetchEventPreloadHandlePtr ServiceWorkerNavigationPreloadFetcher::Start( | |
71 net::URLRequest* original_request) { | |
72 mojom::FetchEventPreloadHandlePtr preload_handle( | |
73 mojom::FetchEventPreloadHandle::New()); | |
74 const ResourceRequestInfoImpl* original_info = | |
75 ResourceRequestInfoImpl::ForRequest(original_request); | |
76 ResourceContext* resource_context = original_info->GetContext(); | |
77 std::unique_ptr<net::URLRequest> url_request( | |
78 original_request->context()->CreateRequest( | |
79 original_request->url(), original_request->priority(), nullptr)); | |
80 // TODO(horo): Set "Service-Worker-Navigation-Preload" header. | |
81 // See: https://github.com/w3c/ServiceWorker/issues/920#issuecomment-251150270 | |
82 ResourceDispatcherHostImpl::Get()->InitializeURLRequest( | |
83 url_request.get(), Referrer(GURL(original_request->referrer()), | |
84 original_info->GetReferrerPolicy()), | |
85 false /* download */, original_info->GetChildID(), | |
86 original_info->GetRouteID(), original_info->GetRenderFrameID(), | |
87 resource_context); | |
88 | |
89 mojom::URLLoaderClientPtr url_loader_client; | |
90 preload_handle->url_loader_client_request = GetProxy(&url_loader_client); | |
mmenke
2016/10/14 16:31:33
What's going on here? What class is driving the u
horo
2016/10/15 00:55:46
This preload_handle will be passed to the service
| |
91 | |
92 std::unique_ptr<ResourceHandler> handler(new MojoAsyncResourceHandler( | |
mmenke
2016/10/14 16:31:33
MojoAsyncResourceHandler isn't ready for prime tim
horo
2016/10/15 00:55:46
NavigationPreload is still an experimental feature
| |
93 url_request.get(), ResourceDispatcherHostImpl::Get(), | |
94 GetProxy(&preload_handle->url_loader), std::move(url_loader_client))); | |
95 handler.reset( | |
96 new PreloadResourceHandler(url_request.get(), std::move(handler))); | |
97 | |
98 ResourceDispatcherHostImpl::Get()->BeginURLRequest( | |
99 std::move(url_request), std::move(handler), false /* download */, | |
100 false /* is_content_initiated */, true /* do_not_prompt_for_login */, | |
101 resource_context); | |
102 return preload_handle; | |
103 } | |
104 | |
105 } // namespace content | |
OLD | NEW |