Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "chrome/browser/chromeos/net/connectivity_state_helper.h" | 5 #include "chrome/browser/chromeos/net/connectivity_state_helper.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/chromeos/cros/cros_library.h" | 8 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 9 #include "chrome/browser/chromeos/cros/network_library.h" | 9 #include "chrome/browser/chromeos/cros/network_library.h" |
| 10 #include "chromeos/chromeos_switches.h" | 10 #include "chromeos/chromeos_switches.h" |
| 11 #include "chromeos/network/network_state.h" | 11 #include "chromeos/network/network_state.h" |
| 12 #include "chromeos/network/network_state_handler.h" | 12 #include "chromeos/network/network_state_handler.h" |
| 13 #include "chromeos/network/network_state_handler_observer.h" | |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 | 15 |
| 15 namespace chromeos { | 16 namespace chromeos { |
| 16 | 17 |
| 17 static ConnectivityStateHelper* g_connectivity_state_helper = NULL; | 18 static ConnectivityStateHelper* g_connectivity_state_helper = NULL; |
| 18 | 19 |
| 19 // Implementation of the connectivity state helper that uses the network | 20 // Implementation of the connectivity state helper that uses the network |
| 20 // state handler for fetching connectivity state. | 21 // state handler for fetching connectivity state. |
| 21 class ConnectivityStateHelperImpl | 22 class ConnectivityStateHelperImpl |
| 22 : public ConnectivityStateHelper { | 23 : public ConnectivityStateHelper, |
| 24 public NetworkStateHandlerObserver { | |
| 23 public: | 25 public: |
| 24 ConnectivityStateHelperImpl(); | 26 ConnectivityStateHelperImpl(); |
| 25 virtual ~ConnectivityStateHelperImpl(); | 27 virtual ~ConnectivityStateHelperImpl(); |
| 26 | 28 |
| 29 // NetworkStateHandler overrides. | |
| 27 virtual bool IsConnected() OVERRIDE; | 30 virtual bool IsConnected() OVERRIDE; |
| 28 virtual bool IsConnectedType(const std::string& type) OVERRIDE; | 31 virtual bool IsConnectedType(const std::string& type) OVERRIDE; |
| 29 virtual bool IsConnectingType(const std::string& type) OVERRIDE; | 32 virtual bool IsConnectingType(const std::string& type) OVERRIDE; |
| 30 virtual std::string NetworkNameForType(const std::string& type) OVERRIDE; | 33 virtual std::string NetworkNameForType(const std::string& type) OVERRIDE; |
| 31 virtual std::string DefaultNetworkName() OVERRIDE; | 34 virtual std::string DefaultNetworkName() OVERRIDE; |
| 32 virtual bool DefaultNetworkOnline() OVERRIDE; | 35 virtual bool DefaultNetworkOnline() OVERRIDE; |
| 33 | 36 |
| 37 // NetworkStateHandlerObserver overrides. | |
| 38 virtual void NetworkManagerChanged() OVERRIDE; | |
| 39 | |
| 34 private: | 40 private: |
| 35 NetworkStateHandler* network_state_handler_; | 41 NetworkStateHandler* network_state_handler_; |
| 36 }; | 42 }; |
| 37 | 43 |
| 38 // Implementation of the connectivity state helper that uses the network | 44 // Implementation of the connectivity state helper that uses the network |
| 39 // library for fetching connectivity state. | 45 // library for fetching connectivity state. |
| 40 class ConnectivityStateHelperNetworkLibrary | 46 class ConnectivityStateHelperNetworkLibrary |
| 41 : public ConnectivityStateHelper { | 47 : public ConnectivityStateHelper, |
| 48 public NetworkLibrary::NetworkManagerObserver { | |
| 42 public: | 49 public: |
| 43 ConnectivityStateHelperNetworkLibrary(); | 50 ConnectivityStateHelperNetworkLibrary(); |
| 44 virtual ~ConnectivityStateHelperNetworkLibrary(); | 51 virtual ~ConnectivityStateHelperNetworkLibrary(); |
| 45 | 52 |
| 53 // ConnectivityStateHelper overrides. | |
| 46 virtual bool IsConnected() OVERRIDE; | 54 virtual bool IsConnected() OVERRIDE; |
| 47 virtual bool IsConnectedType(const std::string& type) OVERRIDE; | 55 virtual bool IsConnectedType(const std::string& type) OVERRIDE; |
| 48 virtual bool IsConnectingType(const std::string& type) OVERRIDE; | 56 virtual bool IsConnectingType(const std::string& type) OVERRIDE; |
| 49 virtual std::string NetworkNameForType(const std::string& type) OVERRIDE; | 57 virtual std::string NetworkNameForType(const std::string& type) OVERRIDE; |
| 50 virtual std::string DefaultNetworkName() OVERRIDE; | 58 virtual std::string DefaultNetworkName() OVERRIDE; |
| 51 virtual bool DefaultNetworkOnline() OVERRIDE; | 59 virtual bool DefaultNetworkOnline() OVERRIDE; |
| 52 | 60 |
| 61 // NetworkLibrary::NetworkManagerObserver overrides. | |
| 62 virtual void OnNetworkManagerChanged(NetworkLibrary* network_library); | |
| 63 | |
| 53 private: | 64 private: |
| 54 NetworkLibrary* network_library_; | 65 NetworkLibrary* network_library_; |
| 55 }; | 66 }; |
| 56 | 67 |
| 57 // static | 68 // static |
| 58 void ConnectivityStateHelper::Initialize() { | 69 void ConnectivityStateHelper::Initialize() { |
| 59 CHECK(!g_connectivity_state_helper); | 70 CHECK(!g_connectivity_state_helper); |
| 60 if (CommandLine::ForCurrentProcess()->HasSwitch( | 71 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 61 chromeos::switches::kEnableNewNetworkChangeNotifier)) { | 72 chromeos::switches::kEnableNewNetworkChangeNotifier)) { |
| 62 g_connectivity_state_helper = new ConnectivityStateHelperImpl(); | 73 g_connectivity_state_helper = new ConnectivityStateHelperImpl(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 81 g_connectivity_state_helper = NULL; | 92 g_connectivity_state_helper = NULL; |
| 82 } | 93 } |
| 83 | 94 |
| 84 // static | 95 // static |
| 85 ConnectivityStateHelper* ConnectivityStateHelper::Get() { | 96 ConnectivityStateHelper* ConnectivityStateHelper::Get() { |
| 86 CHECK(g_connectivity_state_helper) | 97 CHECK(g_connectivity_state_helper) |
| 87 << "ConnectivityStateHelper: Get() called before Initialize()"; | 98 << "ConnectivityStateHelper: Get() called before Initialize()"; |
| 88 return g_connectivity_state_helper; | 99 return g_connectivity_state_helper; |
| 89 } | 100 } |
| 90 | 101 |
| 102 void ConnectivityStateHelper::AddNetworkManagerObserver( | |
| 103 ConnectivityStateHelperObserver* observer) { | |
| 104 network_manager_observers_.AddObserver(observer); | |
| 105 } | |
| 106 | |
| 107 void ConnectivityStateHelper::RemoveNetworkManagerObserver( | |
| 108 ConnectivityStateHelperObserver* observer) { | |
| 109 network_manager_observers_.RemoveObserver(observer); | |
| 110 } | |
| 111 | |
| 91 ConnectivityStateHelperImpl::ConnectivityStateHelperImpl() { | 112 ConnectivityStateHelperImpl::ConnectivityStateHelperImpl() { |
| 92 network_state_handler_ = NetworkStateHandler::Get(); | 113 network_state_handler_ = NetworkStateHandler::Get(); |
| 114 network_state_handler_->AddObserver(this); | |
| 93 } | 115 } |
| 94 | 116 |
| 95 ConnectivityStateHelperImpl::~ConnectivityStateHelperImpl() {} | 117 ConnectivityStateHelperImpl::~ConnectivityStateHelperImpl() { |
| 118 network_state_handler_->RemoveObserver(this); | |
|
stevenjb
2013/03/11 23:11:05
nit: Use NetworkStateHandler::Get() here; if we ac
gauravsh
2013/03/12 00:30:25
Good point. Done.
| |
| 119 } | |
| 96 | 120 |
| 97 bool ConnectivityStateHelperImpl::IsConnected() { | 121 bool ConnectivityStateHelperImpl::IsConnected() { |
| 98 return network_state_handler_->ConnectedNetworkByType( | 122 return network_state_handler_->ConnectedNetworkByType( |
| 99 NetworkStateHandler::kMatchTypeDefault) != NULL; | 123 NetworkStateHandler::kMatchTypeDefault) != NULL; |
| 100 } | 124 } |
| 101 | 125 |
| 102 bool ConnectivityStateHelperImpl::IsConnectedType( | 126 bool ConnectivityStateHelperImpl::IsConnectedType( |
| 103 const std::string& type) { | 127 const std::string& type) { |
| 104 return network_state_handler_->ConnectedNetworkByType(type) != NULL; | 128 return network_state_handler_->ConnectedNetworkByType(type) != NULL; |
| 105 } | 129 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 128 const NetworkState* network = network_state_handler_->DefaultNetwork(); | 152 const NetworkState* network = network_state_handler_->DefaultNetwork(); |
| 129 if (!network) | 153 if (!network) |
| 130 return false; | 154 return false; |
| 131 if (!network->IsConnectedState()) | 155 if (!network->IsConnectedState()) |
| 132 return false; | 156 return false; |
| 133 if (network->connection_state() == flimflam::kStatePortal) | 157 if (network->connection_state() == flimflam::kStatePortal) |
| 134 return false; | 158 return false; |
| 135 return true; | 159 return true; |
| 136 } | 160 } |
| 137 | 161 |
| 162 void ConnectivityStateHelperImpl::NetworkManagerChanged() { | |
| 163 FOR_EACH_OBSERVER(ConnectivityStateHelperObserver, network_manager_observers_, | |
| 164 NetworkManagerChanged()); | |
| 165 } | |
| 166 | |
| 138 //////////////////////////////////////////////////////////////////////////////// | 167 //////////////////////////////////////////////////////////////////////////////// |
| 139 // NetworkLibrary implementation. | 168 // NetworkLibrary implementation. |
| 140 // | 169 // |
| 141 | 170 |
| 142 ConnectivityStateHelperNetworkLibrary::ConnectivityStateHelperNetworkLibrary() { | 171 ConnectivityStateHelperNetworkLibrary::ConnectivityStateHelperNetworkLibrary() { |
| 143 network_library_ = CrosLibrary::Get()->GetNetworkLibrary(); | 172 network_library_ = CrosLibrary::Get()->GetNetworkLibrary(); |
| 173 network_library_->AddNetworkManagerObserver(this); | |
| 144 } | 174 } |
| 145 | 175 |
| 146 ConnectivityStateHelperNetworkLibrary::~ConnectivityStateHelperNetworkLibrary() | 176 ConnectivityStateHelperNetworkLibrary::~ConnectivityStateHelperNetworkLibrary() |
| 147 {} | 177 { |
| 178 network_library_->RemoveNetworkManagerObserver(this); | |
| 179 } | |
| 148 | 180 |
| 149 bool ConnectivityStateHelperNetworkLibrary::IsConnected() { | 181 bool ConnectivityStateHelperNetworkLibrary::IsConnected() { |
| 150 return network_library_->Connected(); | 182 return network_library_->Connected(); |
| 151 } | 183 } |
| 152 | 184 |
| 153 bool ConnectivityStateHelperNetworkLibrary::IsConnectedType( | 185 bool ConnectivityStateHelperNetworkLibrary::IsConnectedType( |
| 154 const std::string& type) { | 186 const std::string& type) { |
| 155 if (type == flimflam::kTypeEthernet) | 187 if (type == flimflam::kTypeEthernet) |
| 156 return network_library_->ethernet_connected(); | 188 return network_library_->ethernet_connected(); |
| 157 if (type == flimflam::kTypeWifi) | 189 if (type == flimflam::kTypeWifi) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 const Network* active_network = network_library_->active_network(); | 231 const Network* active_network = network_library_->active_network(); |
| 200 if (!active_network) | 232 if (!active_network) |
| 201 return false; | 233 return false; |
| 202 if (!active_network->connected()) | 234 if (!active_network->connected()) |
| 203 return false; | 235 return false; |
| 204 if (active_network->restricted_pool()) | 236 if (active_network->restricted_pool()) |
| 205 return false; | 237 return false; |
| 206 return true; | 238 return true; |
| 207 } | 239 } |
| 208 | 240 |
| 241 void ConnectivityStateHelperNetworkLibrary::OnNetworkManagerChanged( | |
| 242 NetworkLibrary* network_library) { | |
| 243 FOR_EACH_OBSERVER(ConnectivityStateHelperObserver, network_manager_observers_, | |
| 244 NetworkManagerChanged()); | |
| 245 } | |
| 246 | |
| 209 } // namespace chromeos | 247 } // namespace chromeos |
| OLD | NEW |