This source file includes following definitions.
- listening_
- Shutdown
- OnListenerAdded
- OnListenerRemoved
- StartOrStopListeningForNetworkChanges
- NetworkListChanged
- NetworkPropertiesUpdated
- Create
#include "chrome/browser/extensions/api/networking_private/networking_private_event_router.h"
#include "base/json/json_writer.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/networking_private/networking_private_api.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/networking_private.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_state_handler_observer.h"
#include "chromeos/network/onc/onc_signature.h"
#include "chromeos/network/onc/onc_translator.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/onc/onc_constants.h"
#include "extensions/browser/extension_system.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using chromeos::NetworkHandler;
using chromeos::NetworkState;
using chromeos::NetworkStateHandler;
namespace extensions {
class NetworkingPrivateEventRouterImpl
: public NetworkingPrivateEventRouter,
public chromeos::NetworkStateHandlerObserver {
public:
explicit NetworkingPrivateEventRouterImpl(Profile* profile);
virtual ~NetworkingPrivateEventRouterImpl();
protected:
virtual void Shutdown() OVERRIDE;
virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
virtual void NetworkListChanged() OVERRIDE;
virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE;
private:
void StartOrStopListeningForNetworkChanges();
Profile* profile_;
bool listening_;
DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateEventRouterImpl);
};
NetworkingPrivateEventRouterImpl::NetworkingPrivateEventRouterImpl(
Profile* profile)
: profile_(profile), listening_(false) {
EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
if (event_router) {
event_router->RegisterObserver(
this, api::networking_private::OnNetworksChanged::kEventName);
event_router->RegisterObserver(
this, api::networking_private::OnNetworkListChanged::kEventName);
StartOrStopListeningForNetworkChanges();
}
}
NetworkingPrivateEventRouterImpl::~NetworkingPrivateEventRouterImpl() {
DCHECK(!listening_);
}
void NetworkingPrivateEventRouterImpl::Shutdown() {
EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
if (event_router)
event_router->UnregisterObserver(this);
if (listening_) {
NetworkHandler::Get()->network_state_handler()->RemoveObserver(
this, FROM_HERE);
}
listening_ = false;
}
void NetworkingPrivateEventRouterImpl::OnListenerAdded(
const EventListenerInfo& details) {
StartOrStopListeningForNetworkChanges();
}
void NetworkingPrivateEventRouterImpl::OnListenerRemoved(
const EventListenerInfo& details) {
StartOrStopListeningForNetworkChanges();
}
void NetworkingPrivateEventRouterImpl::StartOrStopListeningForNetworkChanges() {
EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
bool should_listen =
event_router->HasEventListener(
api::networking_private::OnNetworksChanged::kEventName) ||
event_router->HasEventListener(
api::networking_private::OnNetworkListChanged::kEventName);
if (should_listen && !listening_) {
NetworkHandler::Get()->network_state_handler()->AddObserver(
this, FROM_HERE);
} else if (!should_listen && listening_) {
NetworkHandler::Get()->network_state_handler()->RemoveObserver(
this, FROM_HERE);
}
listening_ = should_listen;
}
void NetworkingPrivateEventRouterImpl::NetworkListChanged() {
EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
NetworkStateHandler::NetworkStateList networks;
NetworkHandler::Get()->network_state_handler()->GetNetworkList(&networks);
if (!event_router->HasEventListener(
api::networking_private::OnNetworkListChanged::kEventName)) {
NET_LOG_EVENT("NetworkingPrivate.NetworkListChanged: No Listeners", "");
return;
}
NET_LOG_EVENT("NetworkingPrivate.NetworkListChanged", "");
std::vector<std::string> changes;
for (NetworkStateHandler::NetworkStateList::const_iterator iter =
networks.begin();
iter != networks.end();
++iter) {
changes.push_back((*iter)->path());
}
scoped_ptr<base::ListValue> args(
api::networking_private::OnNetworkListChanged::Create(changes));
scoped_ptr<Event> extension_event(new Event(
api::networking_private::OnNetworkListChanged::kEventName, args.Pass()));
event_router->BroadcastEvent(extension_event.Pass());
}
void NetworkingPrivateEventRouterImpl::NetworkPropertiesUpdated(
const NetworkState* network) {
EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
if (!event_router->HasEventListener(
api::networking_private::OnNetworksChanged::kEventName)) {
NET_LOG_EVENT("NetworkingPrivate.NetworkPropertiesUpdated: No Listeners",
network->path());
return;
}
NET_LOG_EVENT("NetworkingPrivate.NetworkPropertiesUpdated",
network->path());
scoped_ptr<base::ListValue> args(
api::networking_private::OnNetworksChanged::Create(
std::vector<std::string>(1, network->path())));
scoped_ptr<Event> extension_event(new Event(
api::networking_private::OnNetworksChanged::kEventName, args.Pass()));
event_router->BroadcastEvent(extension_event.Pass());
}
NetworkingPrivateEventRouter* NetworkingPrivateEventRouter::Create(
Profile* profile) {
return new NetworkingPrivateEventRouterImpl(profile);
}
}