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
3 // found in the LICENSE file.
5 #include "chrome/renderer/net/prescient_networking_dispatcher.h"
7 #include "base/metrics/field_trial.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/render_messages.h"
10 #include "content/public/renderer/render_thread.h"
12 using WebKit::WebPrescientNetworking
;
14 const char kMouseEventPreconnectFieldTrialName
[] = "MouseEventPreconnect";
15 const char kMouseEventPreconnectFieldTrialMouseDownGroup
[] = "MouseDown";
16 const char kMouseEventPreconnectFieldTrialMouseOverGroup
[] = "MouseOver";
17 const char kMouseEventPreconnectFieldTrialTapUnconfirmedGroup
[] =
19 const char kMouseEventPreconnectFieldTrialTapDownGroup
[] = "TapDown";
23 // Returns true if preconnect is enabled for given motivation.
24 // The preconnect via {mouse,gesture} event is enabled for limited userbase
25 // for Finch field trial.
26 bool isPreconnectEnabledForMotivation(
27 WebKit::WebPreconnectMotivation motivation
) {
29 base::FieldTrialList::FindFullName(kMouseEventPreconnectFieldTrialName
);
32 case WebKit::WebPreconnectMotivationLinkMouseDown
:
33 return group
== kMouseEventPreconnectFieldTrialMouseDownGroup
;
34 case WebKit::WebPreconnectMotivationLinkMouseOver
:
35 return group
== kMouseEventPreconnectFieldTrialMouseOverGroup
;
36 case WebKit::WebPreconnectMotivationLinkTapUnconfirmed
:
37 return group
== kMouseEventPreconnectFieldTrialTapUnconfirmedGroup
;
38 case WebKit::WebPreconnectMotivationLinkTapDown
:
39 return group
== kMouseEventPreconnectFieldTrialTapDownGroup
;
47 PrescientNetworkingDispatcher::PrescientNetworkingDispatcher() {
50 PrescientNetworkingDispatcher::~PrescientNetworkingDispatcher() {
53 void PrescientNetworkingDispatcher::prefetchDNS(
54 const WebKit::WebString
& hostname
) {
55 if (hostname
.isEmpty())
58 std::string hostname_utf8
= UTF16ToUTF8(hostname
);
59 net_predictor_
.Resolve(hostname_utf8
.data(), hostname_utf8
.length());
62 void PrescientNetworkingDispatcher::preconnect(
63 const WebKit::WebURL
& url
,
64 WebKit::WebPreconnectMotivation motivation
) {
65 if (isPreconnectEnabledForMotivation(motivation
))
66 content::RenderThread::Get()->Send(new ChromeViewHostMsg_Preconnect(url
));