diff options
author | gspencer@chromium.org <gspencer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-15 18:49:45 +0000 |
---|---|---|
committer | gspencer@chromium.org <gspencer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-15 18:49:45 +0000 |
commit | a4e3ffdc80eac306d03ef1fed084b9a4bcbbef3f (patch) | |
tree | d90190e8a190898151a36018fc358d28f3d2ccdb | |
parent | e22e8fab7f024100c4a5b5ceae7c78b6a8724311 (diff) | |
download | chromium_src-a4e3ffdc80eac306d03ef1fed084b9a4bcbbef3f.zip chromium_src-a4e3ffdc80eac306d03ef1fed084b9a4bcbbef3f.tar.gz chromium_src-a4e3ffdc80eac306d03ef1fed084b9a4bcbbef3f.tar.bz2 |
Merge 181892
> This adds the onNetworkChanged event to networkingPrivate
> extension API.
>
> Trying to land this again after clang problem. Original review was
> here: https://chromiumcodereview.appspot.com/12188027/
>
> (TBR'ing trivial change to profile_dependency_manager.cc)
>
> TBR=davemoore@chromium.org,kalman@chromium.org,pneubeck@chromium.org
> BUG=chromium:168713
>
>
> Review URL: https://chromiumcodereview.appspot.com/12225087
TBR=gspencer@chromium.org
Review URL: https://codereview.chromium.org/12279007
git-svn-id: svn://svn.chromium.org/chrome/branches/1410/src@182764 0039d316-1c4b-4281-b951-d872f2087c98
9 files changed, 334 insertions, 1 deletions
diff --git a/chrome/browser/chromeos/extensions/networking_private_event_router.cc b/chrome/browser/chromeos/extensions/networking_private_event_router.cc new file mode 100644 index 0000000..2f884f8 --- /dev/null +++ b/chrome/browser/chromeos/extensions/networking_private_event_router.cc @@ -0,0 +1,145 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/chromeos/extensions/networking_private_event_router.h" + +#include "base/json/json_writer.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/chromeos/extensions/networking_private_api.h" +#include "chrome/browser/extensions/event_names.h" +#include "chrome/browser/extensions/event_router_forwarder.h" +#include "chrome/browser/extensions/extension_system.h" +#include "chrome/browser/extensions/extension_system_factory.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_dependency_manager.h" +#include "chrome/common/extensions/api/networking_private.h" +#include "chromeos/network/network_state.h" +#include "chromeos/network/network_state_handler.h" +#include "chromeos/network/onc/onc_constants.h" +#include "chromeos/network/onc/onc_signature.h" +#include "chromeos/network/onc/onc_translator.h" +#include "third_party/cros_system_api/dbus/service_constants.h" + +using extensions::ExtensionSystem; +namespace api = extensions::api::networking_private; + +namespace chromeos { + +namespace { + +// Translates the current connection state of the network into the ONC +// equivalent. +std::string GetConnectionState(const NetworkState* state) { + if (state->IsConnectedState()) + return onc::connection_state::kConnected; + else if (state->IsConnectingState()) + return onc::connection_state::kConnecting; + else + return onc::connection_state::kNotConnected; +} + +// Translate from the Shill network type to the ONC network type. +std::string GetConnectionType(const std::string& shill_type) { + base::DictionaryValue shill_type_dict; + shill_type_dict.SetStringWithoutPathExpansion(flimflam::kTypeProperty, + shill_type); + scoped_ptr<base::DictionaryValue> onc_type_dict = + onc::TranslateShillServiceToONCPart( + shill_type_dict, + &onc::kNetworkConfigurationSignature); + std::string onc_type; + if (onc_type_dict->GetString(onc::network_config::kType, &onc_type)) + return onc_type; + return std::string(); +} + +} // namespace + +NetworkingPrivateEventRouter::NetworkingPrivateEventRouter(Profile* profile) + : profile_(profile), listening_(false) { + // Register with the event router so we know when renderers are listening to + // our events. We first check and see if there *is* an event router, because + // some unit tests try to create all profile services, but don't initialize + // the event router first. + extensions::EventRouter* event_router = + ExtensionSystem::Get(profile_)->event_router(); + if (event_router) { + event_router->RegisterObserver( + this, extensions::event_names::kOnNetworkChanged); + StartOrStopListeningForNetworkChanges(); + } +} + +NetworkingPrivateEventRouter::~NetworkingPrivateEventRouter() { +} + +void NetworkingPrivateEventRouter::Shutdown() { + // Unregister with the event router. We first check and see if there *is* an + // event router, because some unit tests try to shutdown all profile services, + // but didn't initialize the event router first. + extensions::EventRouter* event_router = + ExtensionSystem::Get(profile_)->event_router(); + if (event_router) + event_router->UnregisterObserver(this); + + if (listening_) + NetworkStateHandler::Get()->RemoveObserver(this); + listening_ = false; +} + +void NetworkingPrivateEventRouter::OnListenerAdded( + const extensions::EventListenerInfo& details) { + // Start listening to events from the network state handler. + StartOrStopListeningForNetworkChanges(); +} + +void NetworkingPrivateEventRouter::OnListenerRemoved( + const extensions::EventListenerInfo& details) { + // Stop listening to events from the network state handler if there are no + // more listeners. + StartOrStopListeningForNetworkChanges(); +} + +void NetworkingPrivateEventRouter::StartOrStopListeningForNetworkChanges() { + bool should_listen = ExtensionSystem::Get(profile_)->event_router()-> + HasEventListener(extensions::event_names::kOnNetworkChanged); + + if (should_listen) { + if (!listening_) + NetworkStateHandler::Get()->AddObserver(this); + } else { + if (listening_) + NetworkStateHandler::Get()->RemoveObserver(this); + } + listening_ = should_listen; +} + +void NetworkingPrivateEventRouter::NetworkListChanged() { + NetworkStateList networks; + NetworkStateHandler::Get()->GetNetworkList(&networks); + std::vector<linked_ptr<api::NetworkProperties> > changes; + for (NetworkStateList::const_iterator iter = networks.begin(); + iter != networks.end(); ++iter) { + api::NetworkProperties* network_properties = new api::NetworkProperties; + network_properties->additional_properties.SetString( + onc::network_config::kName, (*iter)->name()); + network_properties->additional_properties.SetString( + onc::network_config::kGUID, (*iter)->path()); + network_properties->additional_properties.SetString( + onc::network_config::kType, + GetConnectionType((*iter)->type())); + network_properties->additional_properties.SetString( + onc::network_config::kConnectionState, + GetConnectionState(*iter)); + changes.push_back(make_linked_ptr(network_properties)); + } + + scoped_ptr<base::ListValue> args(api::OnNetworkChanged::Create(changes)); + scoped_ptr<extensions::Event> extension_event(new extensions::Event( + extensions::event_names::kOnNetworkChanged, args.Pass())); + ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent( + extension_event.Pass()); +} + +} // namespace chromeos diff --git a/chrome/browser/chromeos/extensions/networking_private_event_router.h b/chrome/browser/chromeos/extensions/networking_private_event_router.h new file mode 100644 index 0000000..29b8623 --- /dev/null +++ b/chrome/browser/chromeos/extensions/networking_private_event_router.h @@ -0,0 +1,54 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_NETWORKING_PRIVATE_EVENT_ROUTER_H_ +#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_NETWORKING_PRIVATE_EVENT_ROUTER_H_ + +#include "chrome/browser/extensions/event_router.h" +#include "chrome/browser/profiles/profile_keyed_service.h" +#include "chromeos/network/network_state_handler_observer.h" + +class Profile; + +namespace chromeos { + +// This is a factory class used by the ProfileDependencyManager to instantiate +// the event router that will forward events from the NetworkStateHandler to the +// JavaScript Networking API. +class NetworkingPrivateEventRouter : public ProfileKeyedService, + public extensions::EventRouter::Observer, + public NetworkStateHandlerObserver { + public: + explicit NetworkingPrivateEventRouter(Profile* profile); + virtual ~NetworkingPrivateEventRouter(); + + protected: + // ProfileKeyedService overrides: + virtual void Shutdown() OVERRIDE; + + // EventRouter::Observer overrides: + virtual void OnListenerAdded( + const extensions::EventListenerInfo& details) OVERRIDE; + virtual void OnListenerRemoved( + const extensions::EventListenerInfo& details) OVERRIDE; + + // NetworkStateHandlerObserver overrides: + virtual void NetworkListChanged() OVERRIDE; + + private: + // Decide if we should listen for network changes or not. If there are any + // JavaScript listeners registered for the onNetworkChanged event, then we + // want to register for change notification from the network state handler. + // Otherwise, we want to unregister and not be listening to network changes. + void StartOrStopListeningForNetworkChanges(); + + Profile* profile_; + bool listening_; + + DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateEventRouter); +}; + +} // namespace chromeos + +#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_NETWORKING_PRIVATE_EVENT_ROUTER_H_ diff --git a/chrome/browser/chromeos/extensions/networking_private_event_router_factory.cc b/chrome/browser/chromeos/extensions/networking_private_event_router_factory.cc new file mode 100644 index 0000000..dff2b2c --- /dev/null +++ b/chrome/browser/chromeos/extensions/networking_private_event_router_factory.cc @@ -0,0 +1,56 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/chromeos/extensions/networking_private_event_router_factory.h" + +#include "chrome/browser/chromeos/extensions/networking_private_event_router.h" +#include "chrome/browser/extensions/extension_system_factory.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_dependency_manager.h" + +namespace chromeos { + +// static +NetworkingPrivateEventRouter* +NetworkingPrivateEventRouterFactory::GetForProfile(Profile* profile) { + return static_cast<NetworkingPrivateEventRouter*>( + GetInstance()->GetServiceForProfile(profile, true)); +} + +// static +NetworkingPrivateEventRouterFactory* +NetworkingPrivateEventRouterFactory::GetInstance() { + return Singleton<NetworkingPrivateEventRouterFactory>::get(); +} + +NetworkingPrivateEventRouterFactory::NetworkingPrivateEventRouterFactory() + : ProfileKeyedServiceFactory( + "NetworkingPrivateEventRouter", + ProfileDependencyManager::GetInstance()) { + DependsOn(extensions::ExtensionSystemFactory::GetInstance()); +} + +NetworkingPrivateEventRouterFactory::~NetworkingPrivateEventRouterFactory() { +} + +ProfileKeyedService* +NetworkingPrivateEventRouterFactory::BuildServiceInstanceFor( + Profile* profile) const { + return new NetworkingPrivateEventRouter(profile); +} + +bool +NetworkingPrivateEventRouterFactory::ServiceHasOwnInstanceInIncognito() const { + return true; +} + +bool NetworkingPrivateEventRouterFactory::ServiceIsCreatedWithProfile() const { + return true; +} + +bool NetworkingPrivateEventRouterFactory::ServiceIsNULLWhileTesting() const { + return true; +} + +} // namespace chromeos diff --git a/chrome/browser/chromeos/extensions/networking_private_event_router_factory.h b/chrome/browser/chromeos/extensions/networking_private_event_router_factory.h new file mode 100644 index 0000000..5f7cfc6 --- /dev/null +++ b/chrome/browser/chromeos/extensions/networking_private_event_router_factory.h @@ -0,0 +1,50 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_NETWORKING_PRIVATE_EVENT_ROUTER_FACTORY_H_ +#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_NETWORKING_PRIVATE_EVENT_ROUTER_FACTORY_H_ + +#include "base/memory/singleton.h" +#include "chrome/browser/profiles/profile_keyed_service_factory.h" + +class Profile; + +namespace chromeos { + +class NetworkingPrivateEventRouter; + +// This is a factory class used by the ProfileDependencyManager to instantiate +// the networking event router per profile (since the extension event router is +// per profile). +class NetworkingPrivateEventRouterFactory : public ProfileKeyedServiceFactory { + public: + // Returns the NetworkingPrivateEventRouter for |profile|, creating it if + // it is not yet created. + static NetworkingPrivateEventRouter* GetForProfile(Profile* profile); + + // Returns the NetworkingPrivateEventRouterFactory instance. + static NetworkingPrivateEventRouterFactory* GetInstance(); + + protected: + // ProfileKeyedBaseFactory overrides: + virtual bool ServiceHasOwnInstanceInIncognito() const OVERRIDE; + virtual bool ServiceIsCreatedWithProfile() const OVERRIDE; + virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; + + private: + friend struct DefaultSingletonTraits<NetworkingPrivateEventRouterFactory>; + + NetworkingPrivateEventRouterFactory(); + virtual ~NetworkingPrivateEventRouterFactory(); + + // ProfileKeyedServiceFactory: + virtual ProfileKeyedService* BuildServiceInstanceFor( + Profile* profile) const OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateEventRouterFactory); +}; + +} // namespace chromeos + +#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_NETWORKING_PRIVATE_EVENT_ROUTER_FACTORY_H_ diff --git a/chrome/browser/extensions/event_names.cc b/chrome/browser/extensions/event_names.cc index 25f9315..e327563 100644 --- a/chrome/browser/extensions/event_names.cc +++ b/chrome/browser/extensions/event_names.cc @@ -95,6 +95,9 @@ const char kOnNotificationClicked[] = "experimental.notification.onClicked"; const char kOnNotificationButtonClicked[] = "experimental.notification.onButtonClicked"; +const char kOnNetworkChanged[] = + "networkingPrivate.onNetworkChanged"; + } // namespace event_names } // namespace extensions diff --git a/chrome/browser/extensions/event_names.h b/chrome/browser/extensions/event_names.h index 2d6535d..b065d7d 100644 --- a/chrome/browser/extensions/event_names.h +++ b/chrome/browser/extensions/event_names.h @@ -107,6 +107,9 @@ extern const char kOnNotificationClosed[]; extern const char kOnNotificationClicked[]; extern const char kOnNotificationButtonClicked[]; +// NetworkingPrivate +extern const char kOnNetworkChanged[]; + } // namespace event_names } // namespace extensions diff --git a/chrome/browser/profiles/profile_dependency_manager.cc b/chrome/browser/profiles/profile_dependency_manager.cc index 63f8bbf..5aa9fbc 100644 --- a/chrome/browser/profiles/profile_dependency_manager.cc +++ b/chrome/browser/profiles/profile_dependency_manager.cc @@ -99,6 +99,7 @@ #if defined(OS_CHROMEOS) #include "chrome/browser/chromeos/extensions/input_method_api.h" #include "chrome/browser/chromeos/extensions/media_player_api.h" +#include "chrome/browser/chromeos/extensions/networking_private_event_router_factory.h" #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" #if defined(FILE_MANAGER_EXTENSION) #include "chrome/browser/chromeos/extensions/file_browser_private_api_factory.h" @@ -232,7 +233,7 @@ ProfileDependencyManager::~ProfileDependencyManager() {} // profile creation time. // // TODO(erg): This needs to be something else. I don't think putting every -// FooServiceFactory here will scale or is desireable long term. +// FooServiceFactory here will scale or is desirable long term. void ProfileDependencyManager::AssertFactoriesBuilt() { if (built_factories_) return; @@ -317,6 +318,9 @@ void ProfileDependencyManager::AssertFactoriesBuilt() { notifier::ChromeNotifierServiceFactory::GetInstance(); #endif MediaGalleriesPreferencesFactory::GetInstance(); +#if defined(OS_CHROMEOS) + chromeos::NetworkingPrivateEventRouterFactory::GetInstance(); +#endif NTPResourceCacheFactory::GetInstance(); PasswordStoreFactory::GetInstance(); PersonalDataManagerFactory::GetInstance(); diff --git a/chrome/chrome_browser_chromeos.gypi b/chrome/chrome_browser_chromeos.gypi index 34b878d..104de0e 100644 --- a/chrome/chrome_browser_chromeos.gypi +++ b/chrome/chrome_browser_chromeos.gypi @@ -684,6 +684,10 @@ 'browser/chromeos/extensions/media_player_api.h', 'browser/chromeos/extensions/networking_private_api.cc', 'browser/chromeos/extensions/networking_private_api.h', + 'browser/chromeos/extensions/networking_private_event_router.cc', + 'browser/chromeos/extensions/networking_private_event_router.h', + 'browser/chromeos/extensions/networking_private_event_router_factory.cc', + 'browser/chromeos/extensions/networking_private_event_router_factory.h', 'browser/chromeos/extensions/wallpaper_manager_util.cc', 'browser/chromeos/extensions/wallpaper_manager_util.h', 'browser/chromeos/extensions/wallpaper_private_api.cc', diff --git a/chrome/common/extensions/api/networking_private.json b/chrome/common/extensions/api/networking_private.json index a567709..4a142e3 100644 --- a/chrome/common/extensions/api/networking_private.json +++ b/chrome/common/extensions/api/networking_private.json @@ -99,6 +99,20 @@ } ] } + ], + "events": [ + { + "name": "onNetworkChanged", + "type": "function", + "description": "Fired when the properties change on any of the networks. Only contains the networks that changed, but each item contains all the properties for the changed network.", + "parameters": [ + { + "name": "changes", + "type": "array", + "items": { "$ref": "NetworkProperties" } + } + ] + } ] } ] |