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. isPreconnectEnabledForMotivation(blink::WebPreconnectMotivation motivation)26bool 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 PrescientNetworkingDispatcher()47PrescientNetworkingDispatcher::PrescientNetworkingDispatcher() { 48 } 49 ~PrescientNetworkingDispatcher()50PrescientNetworkingDispatcher::~PrescientNetworkingDispatcher() { 51 } 52 prefetchDNS(const blink::WebString & hostname)53void PrescientNetworkingDispatcher::prefetchDNS( 54 const blink::WebString& hostname) { 55 if (hostname.isEmpty()) 56 return; 57 58 std::string hostname_utf8 = UTF16ToUTF8(hostname); 59 net_predictor_.Resolve(hostname_utf8.data(), hostname_utf8.length()); 60 } 61 preconnect(const blink::WebURL & url,blink::WebPreconnectMotivation motivation)62void 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