Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 | 22 |
| 23 namespace rtc { | 23 namespace rtc { |
| 24 | 24 |
| 25 const int kFakeIPv4NetworkPrefixLength = 24; | 25 const int kFakeIPv4NetworkPrefixLength = 24; |
| 26 const int kFakeIPv6NetworkPrefixLength = 64; | 26 const int kFakeIPv6NetworkPrefixLength = 64; |
| 27 | 27 |
| 28 // Fake network manager that allows us to manually specify the IPs to use. | 28 // Fake network manager that allows us to manually specify the IPs to use. |
| 29 class FakeNetworkManager : public NetworkManagerBase, | 29 class FakeNetworkManager : public NetworkManagerBase, |
| 30 public MessageHandler { | 30 public MessageHandler { |
| 31 public: | 31 public: |
| 32 FakeNetworkManager() | 32 FakeNetworkManager() : thread_(Thread::Current()) {} |
| 33 : thread_(Thread::Current()), | |
| 34 next_index_(0), | |
| 35 started_(false), | |
| 36 sent_first_update_(false) { | |
| 37 } | |
| 38 | 33 |
| 39 typedef std::vector<SocketAddress> IfaceList; | 34 typedef std::vector<SocketAddress> IfaceList; |
| 40 | 35 |
| 41 void AddInterface(const SocketAddress& iface) { | 36 void AddInterface(const SocketAddress& iface) { |
| 42 // ensure a unique name for the interface | 37 // ensure a unique name for the interface |
| 43 SocketAddress address("test" + rtc::ToString(next_index_++), 0); | 38 SocketAddress address("test" + rtc::ToString(next_index_++), 0); |
| 44 address.SetResolvedIP(iface.ipaddr()); | 39 address.SetResolvedIP(iface.ipaddr()); |
| 45 ifaces_.push_back(address); | 40 ifaces_.push_back(address); |
| 46 DoUpdateNetworks(); | 41 DoUpdateNetworks(); |
| 47 } | 42 } |
| 48 | 43 |
| 49 void RemoveInterface(const SocketAddress& iface) { | 44 void RemoveInterface(const SocketAddress& iface) { |
| 50 for (IfaceList::iterator it = ifaces_.begin(); | 45 for (IfaceList::iterator it = ifaces_.begin(); |
| 51 it != ifaces_.end(); ++it) { | 46 it != ifaces_.end(); ++it) { |
| 52 if (it->EqualIPs(iface)) { | 47 if (it->EqualIPs(iface)) { |
| 53 ifaces_.erase(it); | 48 ifaces_.erase(it); |
| 54 break; | 49 break; |
| 55 } | 50 } |
| 56 } | 51 } |
| 57 DoUpdateNetworks(); | 52 DoUpdateNetworks(); |
| 58 } | 53 } |
| 59 | 54 |
| 60 virtual void StartUpdating() { | 55 virtual void StartUpdating() { |
| 61 if (started_) { | 56 if (start_count_++ == 0) { |
|
pthatcher1
2015/08/31 22:01:35
That's a bit too subtle for me. How about this?
Taylor Brandstetter
2015/09/01 23:53:30
Done.
| |
| 57 sent_first_update_ = false; | |
| 58 thread_->Post(this); | |
| 59 } else { | |
| 62 if (sent_first_update_) | 60 if (sent_first_update_) |
| 63 SignalNetworksChanged(); | 61 SignalNetworksChanged(); |
| 64 return; | |
| 65 } | 62 } |
| 66 | |
| 67 started_ = true; | |
| 68 sent_first_update_ = false; | |
| 69 thread_->Post(this); | |
| 70 } | 63 } |
| 71 | 64 |
| 72 virtual void StopUpdating() { | 65 virtual void StopUpdating() { --start_count_; } |
| 73 started_ = false; | |
| 74 } | |
| 75 | 66 |
| 76 // MessageHandler interface. | 67 // MessageHandler interface. |
| 77 virtual void OnMessage(Message* msg) { | 68 virtual void OnMessage(Message* msg) { |
| 78 DoUpdateNetworks(); | 69 DoUpdateNetworks(); |
| 79 } | 70 } |
| 80 | 71 |
| 81 using NetworkManagerBase::set_enumeration_permission; | 72 using NetworkManagerBase::set_enumeration_permission; |
| 82 | 73 |
| 83 private: | 74 private: |
| 84 void DoUpdateNetworks() { | 75 void DoUpdateNetworks() { |
| 85 if (!started_) | 76 if (start_count_ == 0) |
| 86 return; | 77 return; |
| 87 std::vector<Network*> networks; | 78 std::vector<Network*> networks; |
| 88 for (IfaceList::iterator it = ifaces_.begin(); | 79 for (IfaceList::iterator it = ifaces_.begin(); |
| 89 it != ifaces_.end(); ++it) { | 80 it != ifaces_.end(); ++it) { |
| 90 int prefix_length = 0; | 81 int prefix_length = 0; |
| 91 if (it->ipaddr().family() == AF_INET) { | 82 if (it->ipaddr().family() == AF_INET) { |
| 92 prefix_length = kFakeIPv4NetworkPrefixLength; | 83 prefix_length = kFakeIPv4NetworkPrefixLength; |
| 93 } else if (it->ipaddr().family() == AF_INET6) { | 84 } else if (it->ipaddr().family() == AF_INET6) { |
| 94 prefix_length = kFakeIPv6NetworkPrefixLength; | 85 prefix_length = kFakeIPv6NetworkPrefixLength; |
| 95 } | 86 } |
| 96 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length); | 87 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length); |
| 97 scoped_ptr<Network> net(new Network(it->hostname(), | 88 scoped_ptr<Network> net(new Network(it->hostname(), |
| 98 it->hostname(), | 89 it->hostname(), |
| 99 prefix, | 90 prefix, |
| 100 prefix_length)); | 91 prefix_length)); |
| 101 net->AddIP(it->ipaddr()); | 92 net->AddIP(it->ipaddr()); |
| 102 networks.push_back(net.release()); | 93 networks.push_back(net.release()); |
| 103 } | 94 } |
| 104 bool changed; | 95 bool changed; |
| 105 MergeNetworkList(networks, &changed); | 96 MergeNetworkList(networks, &changed); |
| 106 if (changed || !sent_first_update_) { | 97 if (changed || !sent_first_update_) { |
| 107 SignalNetworksChanged(); | 98 SignalNetworksChanged(); |
| 108 sent_first_update_ = true; | 99 sent_first_update_ = true; |
| 109 } | 100 } |
| 110 } | 101 } |
| 111 | 102 |
| 112 Thread* thread_; | 103 Thread* thread_; |
| 113 IfaceList ifaces_; | 104 IfaceList ifaces_; |
| 114 int next_index_; | 105 int next_index_ = 0; |
| 115 bool started_; | 106 int start_count_ = 0; |
| 116 bool sent_first_update_; | 107 bool sent_first_update_ = false; |
| 117 }; | 108 }; |
| 118 | 109 |
| 119 } // namespace rtc | 110 } // namespace rtc |
| 120 | 111 |
| 121 #endif // WEBRTC_BASE_FAKENETWORK_H_ | 112 #endif // WEBRTC_BASE_FAKENETWORK_H_ |
| OLD | NEW |