| 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() { |
| 71 } |
| 72 |
| 73 ConnectivityStateHelper::~ConnectivityStateHelper() { |
| 74 } |
| 75 |
| 57 // static | 76 // static |
| 58 void ConnectivityStateHelper::Initialize() { | 77 void ConnectivityStateHelper::Initialize() { |
| 59 CHECK(!g_connectivity_state_helper); | 78 CHECK(!g_connectivity_state_helper); |
| 60 if (CommandLine::ForCurrentProcess()->HasSwitch( | 79 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 61 chromeos::switches::kEnableNewNetworkChangeNotifier)) { | 80 chromeos::switches::kEnableNewNetworkChangeNotifier)) { |
| 62 g_connectivity_state_helper = new ConnectivityStateHelperImpl(); | 81 g_connectivity_state_helper = new ConnectivityStateHelperImpl(); |
| 63 } else { | 82 } else { |
| 64 g_connectivity_state_helper = | 83 g_connectivity_state_helper = |
| 65 new ConnectivityStateHelperNetworkLibrary(); | 84 new ConnectivityStateHelperNetworkLibrary(); |
| 66 } | 85 } |
| 67 } | 86 } |
| 68 | 87 |
| 69 // static | 88 // static |
| 70 void ConnectivityStateHelper::InitializeForTesting( | 89 void ConnectivityStateHelper::InitializeForTesting( |
| 71 ConnectivityStateHelper* connectivity_state_helper) { | 90 ConnectivityStateHelper* connectivity_state_helper) { |
| 72 CHECK(!g_connectivity_state_helper); | 91 CHECK(!g_connectivity_state_helper); |
| 73 CHECK(connectivity_state_helper); | 92 CHECK(connectivity_state_helper); |
| 74 g_connectivity_state_helper = connectivity_state_helper; | 93 g_connectivity_state_helper = connectivity_state_helper; |
| 75 } | 94 } |
| 76 | 95 |
| 77 // static | 96 // static |
| 97 bool ConnectivityStateHelper::IsInitialized() { |
| 98 return g_connectivity_state_helper != NULL; |
| 99 } |
| 100 |
| 101 // static |
| 78 void ConnectivityStateHelper::Shutdown() { | 102 void ConnectivityStateHelper::Shutdown() { |
| 79 CHECK(g_connectivity_state_helper); | 103 CHECK(g_connectivity_state_helper); |
| 80 delete g_connectivity_state_helper; | 104 delete g_connectivity_state_helper; |
| 81 g_connectivity_state_helper = NULL; | 105 g_connectivity_state_helper = NULL; |
| 82 } | 106 } |
| 83 | 107 |
| 84 // static | 108 // static |
| 85 ConnectivityStateHelper* ConnectivityStateHelper::Get() { | 109 ConnectivityStateHelper* ConnectivityStateHelper::Get() { |
| 86 CHECK(g_connectivity_state_helper) | 110 CHECK(g_connectivity_state_helper) |
| 87 << "ConnectivityStateHelper: Get() called before Initialize()"; | 111 << "ConnectivityStateHelper: Get() called before Initialize()"; |
| 88 return g_connectivity_state_helper; | 112 return g_connectivity_state_helper; |
| 89 } | 113 } |
| 90 | 114 |
| 115 void ConnectivityStateHelper::AddNetworkManagerObserver( |
| 116 ConnectivityStateHelperObserver* observer) { |
| 117 network_manager_observers_.AddObserver(observer); |
| 118 } |
| 119 |
| 120 void ConnectivityStateHelper::RemoveNetworkManagerObserver( |
| 121 ConnectivityStateHelperObserver* observer) { |
| 122 network_manager_observers_.RemoveObserver(observer); |
| 123 } |
| 124 |
| 91 ConnectivityStateHelperImpl::ConnectivityStateHelperImpl() { | 125 ConnectivityStateHelperImpl::ConnectivityStateHelperImpl() { |
| 92 network_state_handler_ = NetworkStateHandler::Get(); | 126 network_state_handler_ = NetworkStateHandler::Get(); |
| 127 network_state_handler_->AddObserver(this); |
| 93 } | 128 } |
| 94 | 129 |
| 95 ConnectivityStateHelperImpl::~ConnectivityStateHelperImpl() {} | 130 ConnectivityStateHelperImpl::~ConnectivityStateHelperImpl() { |
| 131 NetworkStateHandler::Get()->RemoveObserver(this); |
| 132 } |
| 96 | 133 |
| 97 bool ConnectivityStateHelperImpl::IsConnected() { | 134 bool ConnectivityStateHelperImpl::IsConnected() { |
| 98 return network_state_handler_->ConnectedNetworkByType( | 135 return network_state_handler_->ConnectedNetworkByType( |
| 99 NetworkStateHandler::kMatchTypeDefault) != NULL; | 136 NetworkStateHandler::kMatchTypeDefault) != NULL; |
| 100 } | 137 } |
| 101 | 138 |
| 102 bool ConnectivityStateHelperImpl::IsConnectedType( | 139 bool ConnectivityStateHelperImpl::IsConnectedType( |
| 103 const std::string& type) { | 140 const std::string& type) { |
| 104 return network_state_handler_->ConnectedNetworkByType(type) != NULL; | 141 return network_state_handler_->ConnectedNetworkByType(type) != NULL; |
| 105 } | 142 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 128 const NetworkState* network = network_state_handler_->DefaultNetwork(); | 165 const NetworkState* network = network_state_handler_->DefaultNetwork(); |
| 129 if (!network) | 166 if (!network) |
| 130 return false; | 167 return false; |
| 131 if (!network->IsConnectedState()) | 168 if (!network->IsConnectedState()) |
| 132 return false; | 169 return false; |
| 133 if (network->connection_state() == flimflam::kStatePortal) | 170 if (network->connection_state() == flimflam::kStatePortal) |
| 134 return false; | 171 return false; |
| 135 return true; | 172 return true; |
| 136 } | 173 } |
| 137 | 174 |
| 175 void ConnectivityStateHelperImpl::RequestScan() const { |
| 176 network_state_handler_->RequestScan(); |
| 177 } |
| 178 |
| 179 void ConnectivityStateHelperImpl::NetworkManagerChanged() { |
| 180 FOR_EACH_OBSERVER(ConnectivityStateHelperObserver, network_manager_observers_, |
| 181 NetworkManagerChanged()); |
| 182 } |
| 183 |
| 138 //////////////////////////////////////////////////////////////////////////////// | 184 //////////////////////////////////////////////////////////////////////////////// |
| 139 // NetworkLibrary implementation. | 185 // NetworkLibrary implementation. |
| 140 // | 186 // |
| 141 | 187 |
| 142 ConnectivityStateHelperNetworkLibrary::ConnectivityStateHelperNetworkLibrary() { | 188 ConnectivityStateHelperNetworkLibrary::ConnectivityStateHelperNetworkLibrary() { |
| 143 network_library_ = CrosLibrary::Get()->GetNetworkLibrary(); | 189 network_library_ = CrosLibrary::Get()->GetNetworkLibrary(); |
| 190 network_library_->AddNetworkManagerObserver(this); |
| 144 } | 191 } |
| 145 | 192 |
| 146 ConnectivityStateHelperNetworkLibrary::~ConnectivityStateHelperNetworkLibrary() | 193 ConnectivityStateHelperNetworkLibrary::~ConnectivityStateHelperNetworkLibrary() |
| 147 {} | 194 { |
| 195 network_library_->RemoveNetworkManagerObserver(this); |
| 196 } |
| 148 | 197 |
| 149 bool ConnectivityStateHelperNetworkLibrary::IsConnected() { | 198 bool ConnectivityStateHelperNetworkLibrary::IsConnected() { |
| 150 return network_library_->Connected(); | 199 return network_library_->Connected(); |
| 151 } | 200 } |
| 152 | 201 |
| 153 bool ConnectivityStateHelperNetworkLibrary::IsConnectedType( | 202 bool ConnectivityStateHelperNetworkLibrary::IsConnectedType( |
| 154 const std::string& type) { | 203 const std::string& type) { |
| 155 if (type == flimflam::kTypeEthernet) | 204 if (type == flimflam::kTypeEthernet) |
| 156 return network_library_->ethernet_connected(); | 205 return network_library_->ethernet_connected(); |
| 157 if (type == flimflam::kTypeWifi) | 206 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(); | 248 const Network* active_network = network_library_->active_network(); |
| 200 if (!active_network) | 249 if (!active_network) |
| 201 return false; | 250 return false; |
| 202 if (!active_network->connected()) | 251 if (!active_network->connected()) |
| 203 return false; | 252 return false; |
| 204 if (active_network->restricted_pool()) | 253 if (active_network->restricted_pool()) |
| 205 return false; | 254 return false; |
| 206 return true; | 255 return true; |
| 207 } | 256 } |
| 208 | 257 |
| 258 void ConnectivityStateHelperNetworkLibrary::RequestScan() const { |
| 259 network_library_->RequestNetworkScan(); |
| 260 } |
| 261 |
| 262 void ConnectivityStateHelperNetworkLibrary::OnNetworkManagerChanged( |
| 263 NetworkLibrary* network_library) { |
| 264 FOR_EACH_OBSERVER(ConnectivityStateHelperObserver, network_manager_observers_, |
| 265 NetworkManagerChanged()); |
| 266 } |
| 267 |
| 209 } // namespace chromeos | 268 } // namespace chromeos |
| OLD | NEW |