Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/extensions/networking_private_event_router.h" | |
| 6 | |
| 7 #include "base/json/json_writer.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/chromeos/extensions/networking_private_api.h" | |
| 10 #include "chrome/browser/extensions/event_names.h" | |
| 11 #include "chrome/browser/extensions/event_router_forwarder.h" | |
| 12 #include "chrome/browser/extensions/extension_system.h" | |
| 13 #include "chrome/browser/extensions/extension_system_factory.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
| 16 #include "chrome/common/extensions/api/networking_private.h" | |
| 17 #include "chromeos/network/network_state.h" | |
| 18 #include "chromeos/network/network_state_handler.h" | |
| 19 #include "chromeos/network/onc/onc_constants.h" | |
| 20 #include "chromeos/network/onc/onc_signature.h" | |
| 21 #include "chromeos/network/onc/onc_translator.h" | |
| 22 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 23 | |
| 24 using extensions::ExtensionSystem; | |
| 25 namespace api = extensions::api::networking_private; | |
| 26 | |
| 27 namespace chromeos { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // Translates the current connection state of the network into the ONC | |
| 32 // equivalent. | |
| 33 std::string GetConnectionState(const NetworkState* state) { | |
| 34 if (state->IsConnectedState()) { | |
| 35 return onc::connection_state::kConnected; | |
| 36 } else if (state->IsConnectingState()) { | |
| 37 return onc::connection_state::kConnecting; | |
| 38 } else { | |
| 39 return onc::connection_state::kNotConnected; | |
| 40 } | |
|
not at google - send to devlin
2013/02/05 21:43:20
nit: no {}
Greg Spencer (Chromium)
2013/02/05 22:36:35
Done.
| |
| 41 } | |
| 42 | |
| 43 // Translate from the shill network type to the onc network type. | |
| 44 std::string GetConnectionType(const std::string& shill_type) { | |
| 45 base::DictionaryValue shill_type_dict; | |
| 46 shill_type_dict.SetStringWithoutPathExpansion(flimflam::kTypeProperty, | |
| 47 shill_type); | |
| 48 scoped_ptr<base::DictionaryValue> onc_type_dict = | |
| 49 onc::TranslateShillServiceToONCPart( | |
| 50 shill_type_dict, | |
| 51 &onc::kNetworkConfigurationSignature); | |
| 52 std::string onc_type; | |
| 53 if (onc_type_dict->GetString(onc::network_config::kType, &onc_type)) { | |
| 54 return onc_type; | |
| 55 } | |
|
not at google - send to devlin
2013/02/05 21:43:20
no {}
Greg Spencer (Chromium)
2013/02/05 22:36:35
Done.
| |
| 56 return std::string(); | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 NetworkingPrivateEventRouter::NetworkingPrivateEventRouter(Profile* profile) | |
| 62 : profile_(profile), listening_(false) { | |
| 63 // Register with the event router so we know when renderers are listening | |
| 64 // to our events. | |
| 65 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
| 66 this, extensions::event_names::kOnNetworkChanged); | |
| 67 StartOrStopListeningForNetworkChanges(); | |
| 68 } | |
| 69 | |
| 70 NetworkingPrivateEventRouter::~NetworkingPrivateEventRouter() { | |
| 71 } | |
| 72 | |
| 73 void NetworkingPrivateEventRouter::Shutdown() { | |
| 74 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | |
| 75 if (listening_) | |
| 76 NetworkStateHandler::Get()->RemoveObserver(this); | |
| 77 listening_ = false; | |
| 78 } | |
| 79 | |
| 80 void NetworkingPrivateEventRouter::OnListenerAdded( | |
| 81 const extensions::EventListenerInfo& details) { | |
| 82 // Start listening to events from the network state handler. | |
| 83 StartOrStopListeningForNetworkChanges(); | |
| 84 } | |
| 85 | |
| 86 void NetworkingPrivateEventRouter::OnListenerRemoved( | |
| 87 const extensions::EventListenerInfo& details) { | |
| 88 // Stop listening to events from the network state handler if there are no | |
| 89 // more listeners. | |
| 90 StartOrStopListeningForNetworkChanges(); | |
| 91 } | |
| 92 | |
| 93 // If there are any listeners for our event, then we want to register for | |
| 94 // change notification from the network state handler. Otherwise, we want to | |
| 95 // stop listening. | |
| 96 void NetworkingPrivateEventRouter::StartOrStopListeningForNetworkChanges() { | |
| 97 extensions::EventRouter* event_router = | |
| 98 ExtensionSystem::Get(profile_)->event_router(); | |
| 99 CHECK(event_router); | |
| 100 | |
| 101 bool should_listen = event_router->HasEventListener( | |
| 102 extensions::event_names::kOnNetworkChanged); | |
| 103 | |
| 104 if (should_listen && !listening_) { | |
| 105 NetworkStateHandler::Get()->AddObserver(this); | |
| 106 listening_ = true; | |
| 107 } else { | |
| 108 if (listening_) | |
|
not at google - send to devlin
2013/02/05 21:43:20
it can end up here if both should_listen and liste
Greg Spencer (Chromium)
2013/02/05 22:36:35
Good catch! Fixed.
| |
| 109 NetworkStateHandler::Get()->RemoveObserver(this); | |
| 110 listening_ = false; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void NetworkingPrivateEventRouter::NetworkListChanged( | |
| 115 const NetworkStateList& networks) { | |
| 116 extensions::EventRouter* event_router = | |
| 117 ExtensionSystem::Get(profile_)->event_router(); | |
| 118 CHECK(event_router); | |
| 119 | |
| 120 std::vector<linked_ptr<api::NetworkProperties> > changes; | |
| 121 for (NetworkStateList::const_iterator iter = networks.begin(); | |
| 122 iter != networks.end(); ++iter) { | |
| 123 api::NetworkProperties* network_properties = new api::NetworkProperties; | |
| 124 network_properties->additional_properties.SetString( | |
| 125 onc::network_config::kName, (*iter)->name()); | |
| 126 network_properties->additional_properties.SetString( | |
| 127 onc::network_config::kGUID, (*iter)->path()); | |
| 128 network_properties->additional_properties.SetString( | |
| 129 onc::network_config::kType, | |
| 130 GetConnectionType((*iter)->type())); | |
| 131 network_properties->additional_properties.SetString( | |
| 132 onc::network_config::kConnectionState, | |
| 133 GetConnectionState(*iter)); | |
| 134 changes.push_back(linked_ptr<api::NetworkProperties>(network_properties)); | |
|
not at google - send to devlin
2013/02/05 21:43:20
make_linked_ptr?
Greg Spencer (Chromium)
2013/02/05 22:36:35
Done.
| |
| 135 } | |
| 136 | |
| 137 scoped_ptr<base::ListValue> args(api::OnNetworkChanged::Create(changes)); | |
| 138 scoped_ptr<extensions::Event> extension_event(new extensions::Event( | |
| 139 extensions::event_names::kOnNetworkChanged, args.Pass())); | |
| 140 event_router->BroadcastEvent(extension_event.Pass()); | |
| 141 } | |
| 142 | |
| 143 } // namespace chromeos | |
| OLD | NEW |