OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // See header file for description of RendererPreconnect class | 5 // See header file for description of RendererPreconnect class |
6 | 6 |
7 #include "components/network_hints/renderer/renderer_preconnect.h" | 7 #include "components/network_hints/renderer/renderer_preconnect.h" |
8 | 8 |
9 #include "base/bind.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" | 9 #include "components/network_hints/common/network_hints_common.h" |
14 #include "components/network_hints/common/network_hints_messages.h" | 10 #include "components/network_hints/common/network_hints_messages.h" |
15 #include "content/public/renderer/render_thread.h" | 11 #include "content/public/renderer/render_thread.h" |
16 | 12 |
17 using content::RenderThread; | 13 using content::RenderThread; |
18 | 14 |
19 namespace network_hints { | 15 namespace network_hints { |
20 | 16 |
21 RendererPreconnect::RendererPreconnect() | 17 RendererPreconnect::RendererPreconnect() { |
22 : weak_factory_(this) { | |
23 } | 18 } |
24 | 19 |
25 RendererPreconnect::~RendererPreconnect() { | 20 RendererPreconnect::~RendererPreconnect() { |
26 } | 21 } |
27 | 22 |
28 // Push URLs into the map quickly! | |
29 void RendererPreconnect::Preconnect(const GURL &url) { | 23 void RendererPreconnect::Preconnect(const GURL &url) { |
30 if (!url.is_valid()) | 24 if (!url.is_valid()) |
31 return; | 25 return; |
32 | 26 |
33 // If this is the first entry in the map then a task needs to be scheduled. | 27 RenderThread::Get()->Send( |
34 bool needs_task = url_request_count_map_.empty(); | 28 new NetworkHintsMsg_Preconnect(url, 1)); |
35 | |
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]++; | |
39 | |
40 if (needs_task) { | |
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)); | |
46 } | |
47 } | |
48 | |
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)); | |
56 } | |
57 url_request_count_map_.clear(); | |
58 } | 29 } |
59 | 30 |
60 } // namespace network_hints | 31 } // namespace network_hints |
OLD | NEW |