1 // Copyright 2015 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 // See header file for description of RendererPreconnect class
7 #include "components/network_hints/renderer/renderer_preconnect.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "components/network_hints/common/network_hints_common.h"
14 #include "components/network_hints/common/network_hints_messages.h"
15 #include "content/public/renderer/render_thread.h"
17 using content::RenderThread
;
19 namespace network_hints
{
21 RendererPreconnect::RendererPreconnect()
22 : weak_factory_(this) {
25 RendererPreconnect::~RendererPreconnect() {
28 // Push URLs into the map quickly!
29 void RendererPreconnect::Preconnect(const GURL
&url
) {
33 // If this is the first entry in the map then a task needs to be scheduled.
34 bool needs_task
= url_request_count_map_
.empty();
36 // ints will initialize to 0 automatically so incrementing will initialize
37 // new URLs to 1 or increment existing URLs (exactly the desired behavior).
38 url_request_count_map_
[url
]++;
41 weak_factory_
.InvalidateWeakPtrs();
42 RenderThread::Get()->GetTaskRunner()->PostDelayedTask(
43 FROM_HERE
, base::Bind(&RendererPreconnect::SubmitPreconnect
,
44 weak_factory_
.GetWeakPtr()),
45 base::TimeDelta::FromMilliseconds(10));
49 // Extract data from the Map, and then send it off the the Browser process
50 // to be preconnected.
51 void RendererPreconnect::SubmitPreconnect() {
52 DCHECK(!url_request_count_map_
.empty());
53 for (const auto& entry
: url_request_count_map_
) {
54 RenderThread::Get()->Send(
55 new NetworkHintsMsg_Preconnect(entry
.first
, entry
.second
));
57 url_request_count_map_
.clear();
60 } // namespace network_hints