OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 // See header file for description of class | |
6 | |
7 #include <ws2tcpip.h> | |
8 #include <Wspiapi.h> // Needed for win2k compatibility | |
9 | |
10 #include "chrome/browser/net/dns_slave.h" | |
11 | |
12 #include "base/logging.h" | |
13 #include "base/platform_thread.h" | |
14 #include "base/string_util.h" | |
15 #include "chrome/browser/net/dns_host_info.h" | |
16 #include "chrome/browser/net/dns_master.h" | |
17 | |
18 | |
19 namespace chrome_browser_net { | |
20 | |
21 //------------------------------------------------------------------------------ | |
22 // We supply a functions for stubbing network callbacks, so that offline testing | |
23 // can be performed. | |
24 //------------------------------------------------------------------------------ | |
25 static GetAddrInfoFunction get_addr = getaddrinfo; | |
26 static FreeAddrInfoFunction free_addr = freeaddrinfo; | |
27 void SetAddrinfoCallbacks(GetAddrInfoFunction getaddrinfo, | |
28 FreeAddrInfoFunction freeaddrinfo) { | |
29 get_addr = getaddrinfo; | |
30 free_addr = freeaddrinfo; | |
31 } | |
32 | |
33 GetAddrInfoFunction get_getaddrinfo() {return get_addr;} | |
34 FreeAddrInfoFunction get_freeaddrinfo() {return free_addr;} | |
35 | |
36 | |
37 //------------------------------------------------------------------------------ | |
38 // This is the entry method used by DnsMaster to start our thread. | |
39 //------------------------------------------------------------------------------ | |
40 | |
41 DWORD __stdcall DnsSlave::ThreadStart(void* pThis) { | |
42 DnsSlave* slave_instance = reinterpret_cast<DnsSlave*>(pThis); | |
43 return slave_instance->Run(); | |
44 } | |
45 | |
46 //------------------------------------------------------------------------------ | |
47 // The following are methods on the DnsPrefetch class. | |
48 //------------------------------------------------------------------------------ | |
49 | |
50 unsigned DnsSlave::Run() { | |
51 DCHECK(slave_index_ >= 0 && slave_index_ < DnsMaster::kSlaveCountMax); | |
52 | |
53 std::string name = StringPrintf( | |
54 "dns_prefetcher_%d_of_%d", slave_index_ + 1, DnsMaster::kSlaveCountMax); | |
55 DLOG(INFO) << "Now Running " << name; | |
56 PlatformThread::SetName(name.c_str()); | |
57 | |
58 while (master_->GetNextAssignment(&hostname_)) { | |
59 BlockingDnsLookup(); | |
60 } | |
61 // GetNextAssignment() fails when we are told to terminate. | |
62 master_->SetSlaveHasTerminated(slave_index_); | |
63 return 0; | |
64 } | |
65 | |
66 void DnsSlave::BlockingDnsLookup() { | |
67 const char* port = "80"; // I may need to get the real port | |
68 addrinfo* result = NULL; | |
69 | |
70 int error_code = get_addr(hostname_.c_str(), port, NULL, &result); | |
71 | |
72 // Note that since info has value semantics, I need to ask | |
73 // master_ to set the new states atomically in its map. | |
74 switch (error_code) { | |
75 case 0: | |
76 master_->SetFoundState(hostname_); | |
77 break; | |
78 | |
79 default: | |
80 DCHECK_EQ(0, error_code) << "surprising output" ; | |
81 // fall through | |
82 | |
83 case WSAHOST_NOT_FOUND: | |
84 master_->SetNoSuchNameState(hostname_); | |
85 break; | |
86 } | |
87 | |
88 // We don't save results, so lets free them... | |
89 if (result) { | |
90 free_addr(result); | |
91 result = NULL; | |
92 } | |
93 } | |
94 | |
95 } // namespace chrome_browser_net | |
96 | |
OLD | NEW |