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; |
| 36 virtual void RequestScan() const OVERRIDE; | |
| 37 | |
| 38 // NetworkStateHandlerObserver overrides. | |
| 39 virtual void NetworkManagerChanged() OVERRIDE; | |
| 33 | 40 |
| 34 private: | 41 private: |
| 35 NetworkStateHandler* network_state_handler_; | 42 NetworkStateHandler* network_state_handler_; |
| 36 }; | 43 }; |
| 37 | 44 |
| 38 // Implementation of the connectivity state helper that uses the network | 45 // Implementation of the connectivity state helper that uses the network |
| 39 // library for fetching connectivity state. | 46 // library for fetching connectivity state. |
| 40 class ConnectivityStateHelperNetworkLibrary | 47 class ConnectivityStateHelperNetworkLibrary |
| 41 : public ConnectivityStateHelper { | 48 : public ConnectivityStateHelper, |
| 49 public NetworkLibrary::NetworkManagerObserver { | |
| 42 public: | 50 public: |
| 43 ConnectivityStateHelperNetworkLibrary(); | 51 ConnectivityStateHelperNetworkLibrary(); |
| 44 virtual ~ConnectivityStateHelperNetworkLibrary(); | 52 virtual ~ConnectivityStateHelperNetworkLibrary(); |
| 45 | 53 |
| 54 // ConnectivityStateHelper overrides. | |
| 46 virtual bool IsConnected() OVERRIDE; | 55 virtual bool IsConnected() OVERRIDE; |
| 47 virtual bool IsConnectedType(const std::string& type) OVERRIDE; | 56 virtual bool IsConnectedType(const std::string& type) OVERRIDE; |
| 48 virtual bool IsConnectingType(const std::string& type) OVERRIDE; | 57 virtual bool IsConnectingType(const std::string& type) OVERRIDE; |
| 49 virtual std::string NetworkNameForType(const std::string& type) OVERRIDE; | 58 virtual std::string NetworkNameForType(const std::string& type) OVERRIDE; |
| 50 virtual std::string DefaultNetworkName() OVERRIDE; | 59 virtual std::string DefaultNetworkName() OVERRIDE; |
| 51 virtual bool DefaultNetworkOnline() OVERRIDE; | 60 virtual bool DefaultNetworkOnline() OVERRIDE; |
| 61 virtual void RequestScan() const OVERRIDE; | |
| 62 | |
| 63 // NetworkLibrary::NetworkManagerObserver overrides. | |
| 64 virtual void OnNetworkManagerChanged(NetworkLibrary* network_library); | |
| 52 | 65 |
| 53 private: | 66 private: |
| 54 NetworkLibrary* network_library_; | 67 NetworkLibrary* network_library_; |
| 55 }; | 68 }; |
| 56 | 69 |
| 70 ConnectivityStateHelper::ConnectivityStateHelper() {} | |
|
stevenjb
2013/03/14 00:10:47
nit: } on separate line when not in the class decl
gauravsh
2013/03/14 01:06:11
Done.
| |
| 71 | |
| 72 ConnectivityStateHelper::~ConnectivityStateHelper() {} | |
| 73 | |
| 57 // static | 74 // static |
| 58 void ConnectivityStateHelper::Initialize() { | 75 void ConnectivityStateHelper::Initialize() { |
| 59 CHECK(!g_connectivity_state_helper); | 76 CHECK(!g_connectivity_state_helper); |
| 60 if (CommandLine::ForCurrentProcess()->HasSwitch( | 77 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 61 chromeos::switches::kEnableNewNetworkChangeNotifier)) { | 78 chromeos::switches::kEnableNewNetworkChangeNotifier)) { |
| 62 g_connectivity_state_helper = new ConnectivityStateHelperImpl(); | 79 g_connectivity_state_helper = new ConnectivityStateHelperImpl(); |
| 63 } else { | 80 } else { |
| 64 g_connectivity_state_helper = | 81 g_connectivity_state_helper = |
| 65 new ConnectivityStateHelperNetworkLibrary(); | 82 new ConnectivityStateHelperNetworkLibrary(); |
| 66 } | 83 } |
| 67 } | 84 } |
| 68 | 85 |
| 69 // static | 86 // static |
| 70 void ConnectivityStateHelper::InitializeForTesting( | 87 void ConnectivityStateHelper::InitializeForTesting( |
| 71 ConnectivityStateHelper* connectivity_state_helper) { | 88 ConnectivityStateHelper* connectivity_state_helper) { |
| 72 CHECK(!g_connectivity_state_helper); | 89 CHECK(!g_connectivity_state_helper); |
| 73 CHECK(connectivity_state_helper); | 90 CHECK(connectivity_state_helper); |
| 74 g_connectivity_state_helper = connectivity_state_helper; | 91 g_connectivity_state_helper = connectivity_state_helper; |
| 75 } | 92 } |
| 76 | 93 |
| 77 // static | 94 // static |
| 95 bool ConnectivityStateHelper::IsInitialized() { | |
| 96 return g_connectivity_state_helper != NULL; | |
| 97 } | |
| 98 | |
| 99 // static | |
| 78 void ConnectivityStateHelper::Shutdown() { | 100 void ConnectivityStateHelper::Shutdown() { |
| 79 CHECK(g_connectivity_state_helper); | 101 CHECK(g_connectivity_state_helper); |
| 80 delete g_connectivity_state_helper; | 102 delete g_connectivity_state_helper; |
| 81 g_connectivity_state_helper = NULL; | 103 g_connectivity_state_helper = NULL; |
| 82 } | 104 } |
| 83 | 105 |
| 84 // static | 106 // static |
| 85 ConnectivityStateHelper* ConnectivityStateHelper::Get() { | 107 ConnectivityStateHelper* ConnectivityStateHelper::Get() { |
| 86 CHECK(g_connectivity_state_helper) | 108 CHECK(g_connectivity_state_helper) |
| 87 << "ConnectivityStateHelper: Get() called before Initialize()"; | 109 << "ConnectivityStateHelper: Get() called before Initialize()"; |
| 88 return g_connectivity_state_helper; | 110 return g_connectivity_state_helper; |
| 89 } | 111 } |
| 90 | 112 |
| 113 void ConnectivityStateHelper::AddNetworkManagerObserver( | |
| 114 ConnectivityStateHelperObserver* observer) { | |
| 115 network_manager_observers_.AddObserver(observer); | |
| 116 } | |
| 117 | |
| 118 void ConnectivityStateHelper::RemoveNetworkManagerObserver( | |
| 119 ConnectivityStateHelperObserver* observer) { | |
| 120 network_manager_observers_.RemoveObserver(observer); | |
| 121 } | |
| 122 | |
| 91 ConnectivityStateHelperImpl::ConnectivityStateHelperImpl() { | 123 ConnectivityStateHelperImpl::ConnectivityStateHelperImpl() { |
| 92 network_state_handler_ = NetworkStateHandler::Get(); | 124 network_state_handler_ = NetworkStateHandler::Get(); |
| 125 network_state_handler_->AddObserver(this); | |
| 93 } | 126 } |
| 94 | 127 |
| 95 ConnectivityStateHelperImpl::~ConnectivityStateHelperImpl() {} | 128 ConnectivityStateHelperImpl::~ConnectivityStateHelperImpl() { |
| 129 NetworkStateHandler::Get()->RemoveObserver(this); | |
| 130 } | |
| 96 | 131 |
| 97 bool ConnectivityStateHelperImpl::IsConnected() { | 132 bool ConnectivityStateHelperImpl::IsConnected() { |
| 98 return network_state_handler_->ConnectedNetworkByType( | 133 return network_state_handler_->ConnectedNetworkByType( |
| 99 NetworkStateHandler::kMatchTypeDefault) != NULL; | 134 NetworkStateHandler::kMatchTypeDefault) != NULL; |
| 100 } | 135 } |
| 101 | 136 |
| 102 bool ConnectivityStateHelperImpl::IsConnectedType( | 137 bool ConnectivityStateHelperImpl::IsConnectedType( |
| 103 const std::string& type) { | 138 const std::string& type) { |
| 104 return network_state_handler_->ConnectedNetworkByType(type) != NULL; | 139 return network_state_handler_->ConnectedNetworkByType(type) != NULL; |
| 105 } | 140 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 128 const NetworkState* network = network_state_handler_->DefaultNetwork(); | 163 const NetworkState* network = network_state_handler_->DefaultNetwork(); |
| 129 if (!network) | 164 if (!network) |
| 130 return false; | 165 return false; |
| 131 if (!network->IsConnectedState()) | 166 if (!network->IsConnectedState()) |
| 132 return false; | 167 return false; |
| 133 if (network->connection_state() == flimflam::kStatePortal) | 168 if (network->connection_state() == flimflam::kStatePortal) |
| 134 return false; | 169 return false; |
| 135 return true; | 170 return true; |
| 136 } | 171 } |
| 137 | 172 |
| 173 void ConnectivityStateHelperImpl::RequestScan() const { | |
| 174 network_state_handler_->RequestScan(); | |
| 175 } | |
| 176 | |
| 177 void ConnectivityStateHelperImpl::NetworkManagerChanged() { | |
| 178 FOR_EACH_OBSERVER(ConnectivityStateHelperObserver, network_manager_observers_, | |
| 179 NetworkManagerChanged()); | |
| 180 } | |
| 181 | |
| 138 //////////////////////////////////////////////////////////////////////////////// | 182 //////////////////////////////////////////////////////////////////////////////// |
| 139 // NetworkLibrary implementation. | 183 // NetworkLibrary implementation. |
| 140 // | 184 // |
| 141 | 185 |
| 142 ConnectivityStateHelperNetworkLibrary::ConnectivityStateHelperNetworkLibrary() { | 186 ConnectivityStateHelperNetworkLibrary::ConnectivityStateHelperNetworkLibrary() { |
| 143 network_library_ = CrosLibrary::Get()->GetNetworkLibrary(); | 187 network_library_ = CrosLibrary::Get()->GetNetworkLibrary(); |
| 188 network_library_->AddNetworkManagerObserver(this); | |
| 144 } | 189 } |
| 145 | 190 |
| 146 ConnectivityStateHelperNetworkLibrary::~ConnectivityStateHelperNetworkLibrary() | 191 ConnectivityStateHelperNetworkLibrary::~ConnectivityStateHelperNetworkLibrary() |
| 147 {} | 192 { |
| 193 network_library_->RemoveNetworkManagerObserver(this); | |
| 194 } | |
| 148 | 195 |
| 149 bool ConnectivityStateHelperNetworkLibrary::IsConnected() { | 196 bool ConnectivityStateHelperNetworkLibrary::IsConnected() { |
| 150 return network_library_->Connected(); | 197 return network_library_->Connected(); |
| 151 } | 198 } |
| 152 | 199 |
| 153 bool ConnectivityStateHelperNetworkLibrary::IsConnectedType( | 200 bool ConnectivityStateHelperNetworkLibrary::IsConnectedType( |
| 154 const std::string& type) { | 201 const std::string& type) { |
| 155 if (type == flimflam::kTypeEthernet) | 202 if (type == flimflam::kTypeEthernet) |
| 156 return network_library_->ethernet_connected(); | 203 return network_library_->ethernet_connected(); |
| 157 if (type == flimflam::kTypeWifi) | 204 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(); | 246 const Network* active_network = network_library_->active_network(); |
| 200 if (!active_network) | 247 if (!active_network) |
| 201 return false; | 248 return false; |
| 202 if (!active_network->connected()) | 249 if (!active_network->connected()) |
| 203 return false; | 250 return false; |
| 204 if (active_network->restricted_pool()) | 251 if (active_network->restricted_pool()) |
| 205 return false; | 252 return false; |
| 206 return true; | 253 return true; |
| 207 } | 254 } |
| 208 | 255 |
| 256 void ConnectivityStateHelperNetworkLibrary::RequestScan() const { | |
| 257 network_library_->RequestNetworkScan(); | |
| 258 } | |
| 259 | |
| 260 void ConnectivityStateHelperNetworkLibrary::OnNetworkManagerChanged( | |
| 261 NetworkLibrary* network_library) { | |
| 262 FOR_EACH_OBSERVER(ConnectivityStateHelperObserver, network_manager_observers_, | |
| 263 NetworkManagerChanged()); | |
| 264 } | |
| 265 | |
| 209 } // namespace chromeos | 266 } // namespace chromeos |
| OLD | NEW |