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. 4 5 #include "chrome/renderer/net/prescient_networking_dispatcher.h" 6 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" 11 12 using blink::WebPrescientNetworking; 13 14 const char kMouseEventPreconnectFieldTrialName[] = "MouseEventPreconnect"; 15 const char kMouseEventPreconnectFieldTrialMouseDownGroup[] = "MouseDown"; 16 const char kMouseEventPreconnectFieldTrialMouseOverGroup[] = "MouseOver"; 17 const char kMouseEventPreconnectFieldTrialTapUnconfirmedGroup[] = 18 "TapUnconfirmed"; 19 const char kMouseEventPreconnectFieldTrialTapDownGroup[] = "TapDown"; 20 21 namespace { 22 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 blink::WebPreconnectMotivation motivation) { 28 std::string group = 29 base::FieldTrialList::FindFullName(kMouseEventPreconnectFieldTrialName); 30 31 switch (motivation) { 32 case blink::WebPreconnectMotivationLinkMouseDown: 33 return group == kMouseEventPreconnectFieldTrialMouseDownGroup; 34 case blink::WebPreconnectMotivationLinkMouseOver: 35 return group == kMouseEventPreconnectFieldTrialMouseOverGroup; 36 case blink::WebPreconnectMotivationLinkTapUnconfirmed: 37 return group == kMouseEventPreconnectFieldTrialTapUnconfirmedGroup; 38 case blink::WebPreconnectMotivationLinkTapDown: 39 return group == kMouseEventPreconnectFieldTrialTapDownGroup; 40 default: 41 return false; 42 } 43 } 44 45 } // namespace 46 47 PrescientNetworkingDispatcher::PrescientNetworkingDispatcher() { 48 } 49 50 PrescientNetworkingDispatcher::~PrescientNetworkingDispatcher() { 51 } 52 53 void PrescientNetworkingDispatcher::prefetchDNS( 54 const blink::WebString& hostname) { 55 if (hostname.isEmpty()) 56 return; 57 58 std::string hostname_utf8 = base::UTF16ToUTF8(hostname); 59 net_predictor_.Resolve(hostname_utf8.data(), hostname_utf8.length()); 60 } 61 62 void PrescientNetworkingDispatcher::preconnect( 63 const blink::WebURL& url, 64 blink::WebPreconnectMotivation motivation) { 65 if (isPreconnectEnabledForMotivation(motivation)) 66 content::RenderThread::Get()->Send(new ChromeViewHostMsg_Preconnect(url)); 67 } 68 69