1 // Copyright 2014 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 "components/network_hints/renderer/web_prescient_networking_impl.h"
6 
7 #include "base/logging.h"
8 #include "content/public/renderer/render_frame.h"
9 #include "third_party/blink/public/common/browser_interface_broker_proxy.h"
10 
11 namespace network_hints {
12 namespace {
13 
ForwardToHandler(mojo::Remote<mojom::NetworkHintsHandler> * handler,const std::vector<std::string> & names)14 void ForwardToHandler(mojo::Remote<mojom::NetworkHintsHandler>* handler,
15                       const std::vector<std::string>& names) {
16   handler->get()->PrefetchDNS(names);
17 }
18 
19 }  // namespace
20 
WebPrescientNetworkingImpl(content::RenderFrame * render_frame)21 WebPrescientNetworkingImpl::WebPrescientNetworkingImpl(
22     content::RenderFrame* render_frame)
23     : dns_prefetch_(
24           base::BindRepeating(&ForwardToHandler, base::Unretained(&handler_))) {
25   render_frame->GetBrowserInterfaceBroker()->GetInterface(
26       handler_.BindNewPipeAndPassReceiver());
27 }
28 
~WebPrescientNetworkingImpl()29 WebPrescientNetworkingImpl::~WebPrescientNetworkingImpl() {}
30 
PrefetchDNS(const blink::WebString & hostname)31 void WebPrescientNetworkingImpl::PrefetchDNS(const blink::WebString& hostname) {
32   DVLOG(2) << "Prefetch DNS: " << hostname.Utf8();
33   if (hostname.IsEmpty())
34     return;
35 
36   std::string hostname_utf8 = hostname.Utf8();
37   dns_prefetch_.Resolve(hostname_utf8.data(), hostname_utf8.length());
38 }
39 
Preconnect(const blink::WebURL & url,bool allow_credentials)40 void WebPrescientNetworkingImpl::Preconnect(
41     const blink::WebURL& url,
42     bool allow_credentials) {
43   DVLOG(2) << "Preconnect: " << url.GetString().Utf8();
44   if (!url.IsValid())
45     return;
46 
47   handler_->Preconnect(url, allow_credentials);
48 }
49 
50 }  // namespace network_hints
51