diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-28 20:37:58 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-28 20:37:58 +0000 |
commit | 9e41fef7be7762e0985fbf04506cdb02f86b3833 (patch) | |
tree | e2e8aed876112e18623ceea7c73d685a21c679a6 /apps | |
parent | ba85a60e1e866da7d4c8fcc2be26f2470ae0906b (diff) | |
download | chromium_src-9e41fef7be7762e0985fbf04506cdb02f86b3833.zip chromium_src-9e41fef7be7762e0985fbf04506cdb02f86b3833.tar.gz chromium_src-9e41fef7be7762e0985fbf04506cdb02f86b3833.tar.bz2 |
app_shell: Support specifying a preferred network.
Add an --app-shell-preferred-network flag, along with
renaming --app to --app-shell-app-path.
BUG=364297
TBR=oshima@chromium.org
Review URL: https://codereview.chromium.org/349533003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@280526 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'apps')
-rw-r--r-- | apps/shell/browser/default_shell_browser_main_delegate.cc | 8 | ||||
-rw-r--r-- | apps/shell/browser/shell_browser_main_parts.cc | 6 | ||||
-rw-r--r-- | apps/shell/browser/shell_desktop_controller.cc | 4 | ||||
-rw-r--r-- | apps/shell/browser/shell_network_controller_chromeos.cc | 134 | ||||
-rw-r--r-- | apps/shell/browser/shell_network_controller_chromeos.h | 39 | ||||
-rw-r--r-- | apps/shell/common/switches.cc | 11 | ||||
-rw-r--r-- | apps/shell/common/switches.h | 5 |
7 files changed, 139 insertions, 68 deletions
diff --git a/apps/shell/browser/default_shell_browser_main_delegate.cc b/apps/shell/browser/default_shell_browser_main_delegate.cc index 7b202ce..df2ff4d 100644 --- a/apps/shell/browser/default_shell_browser_main_delegate.cc +++ b/apps/shell/browser/default_shell_browser_main_delegate.cc @@ -23,8 +23,9 @@ DefaultShellBrowserMainDelegate::~DefaultShellBrowserMainDelegate() { void DefaultShellBrowserMainDelegate::Start( content::BrowserContext* browser_context) { base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); - if (command_line->HasSwitch(switches::kApp)) { - base::FilePath app_dir(command_line->GetSwitchValueNative(switches::kApp)); + if (command_line->HasSwitch(switches::kAppShellAppPath)) { + base::FilePath app_dir( + command_line->GetSwitchValueNative(switches::kAppShellAppPath)); base::FilePath app_absolute_dir = base::MakeAbsoluteFilePath(app_dir); extensions::ShellExtensionSystem* extension_system = @@ -34,7 +35,8 @@ void DefaultShellBrowserMainDelegate::Start( return; extension_system->LaunchApp(); } else { - LOG(ERROR) << "--" << switches::kApp << " unset; boredom is in your future"; + LOG(ERROR) << "--" << switches::kAppShellAppPath + << " unset; boredom is in your future"; } } diff --git a/apps/shell/browser/shell_browser_main_parts.cc b/apps/shell/browser/shell_browser_main_parts.cc index b438021..bafbf8e 100644 --- a/apps/shell/browser/shell_browser_main_parts.cc +++ b/apps/shell/browser/shell_browser_main_parts.cc @@ -12,6 +12,8 @@ #include "apps/shell/browser/shell_extensions_browser_client.h" #include "apps/shell/browser/shell_omaha_query_params_delegate.h" #include "apps/shell/common/shell_extensions_client.h" +#include "apps/shell/common/switches.h" +#include "base/command_line.h" #include "base/run_loop.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/omaha_query_params/omaha_query_params.h" @@ -66,7 +68,9 @@ void ShellBrowserMainParts::PreMainMessageLoopStart() { void ShellBrowserMainParts::PostMainMessageLoopStart() { #if defined(OS_CHROMEOS) chromeos::DBusThreadManager::Initialize(); - network_controller_.reset(new ShellNetworkController); + network_controller_.reset(new ShellNetworkController( + base::CommandLine::ForCurrentProcess()->GetSwitchValueNative( + switches::kAppShellPreferredNetwork))); #else // Non-Chrome OS platforms are for developer convenience, so use a test IME. ui::InitializeInputMethodForTesting(); diff --git a/apps/shell/browser/shell_desktop_controller.cc b/apps/shell/browser/shell_desktop_controller.cc index 71672a5..e9d11ab 100644 --- a/apps/shell/browser/shell_desktop_controller.cc +++ b/apps/shell/browser/shell_desktop_controller.cc @@ -229,9 +229,9 @@ void ShellDesktopController::CreateRootWindow() { // Set up basic pieces of ui::wm. gfx::Size size; base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); - if (command_line->HasSwitch(switches::kHostWindowBounds)) { + if (command_line->HasSwitch(switches::kAppShellHostWindowBounds)) { const std::string size_str = - command_line->GetSwitchValueASCII(switches::kHostWindowBounds); + command_line->GetSwitchValueASCII(switches::kAppShellHostWindowBounds); int width, height; CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height)); size = gfx::Size(width, height); diff --git a/apps/shell/browser/shell_network_controller_chromeos.cc b/apps/shell/browser/shell_network_controller_chromeos.cc index e1bf209..0b66604 100644 --- a/apps/shell/browser/shell_network_controller_chromeos.cc +++ b/apps/shell/browser/shell_network_controller_chromeos.cc @@ -20,7 +20,8 @@ namespace apps { namespace { -// Frequency at which networks should be scanned when not connected. +// Frequency at which networks should be scanned when not connected to a network +// or when connected to a non-preferred network. const int kScanIntervalSec = 10; void HandleEnableWifiError( @@ -29,10 +30,10 @@ void HandleEnableWifiError( LOG(WARNING) << "Unable to enable wifi: " << error_name; } -// Returns a human-readable name for the network described by |state|. -std::string GetNetworkName(const chromeos::NetworkState& state) { - return !state.name().empty() ? state.name() : - base::StringPrintf("[%s]", state.type().c_str()); +// Returns a human-readable name for the network described by |network|. +std::string GetNetworkName(const chromeos::NetworkState& network) { + return !network.name().empty() ? network.name() : + base::StringPrintf("[%s]", network.type().c_str()); } // Returns true if shill is either connected or connecting to a network. @@ -47,23 +48,22 @@ bool IsConnectedOrConnecting() { } // namespace -ShellNetworkController::ShellNetworkController() - : waiting_for_connection_result_(false), +ShellNetworkController::ShellNetworkController( + const std::string& preferred_network_name) + : state_(STATE_IDLE), + preferred_network_name_(preferred_network_name), + preferred_network_is_active_(false), weak_ptr_factory_(this) { chromeos::NetworkHandler::Initialize(); chromeos::NetworkStateHandler* state_handler = chromeos::NetworkHandler::Get()->network_state_handler(); - DCHECK(state_handler); state_handler->AddObserver(this, FROM_HERE); state_handler->SetTechnologyEnabled( chromeos::NetworkTypePattern::Primitive(shill::kTypeWifi), true, base::Bind(&HandleEnableWifiError)); - if (!IsConnectedOrConnecting()) { - RequestScan(); - SetScanningEnabled(true); - ConnectIfUnconnected(); - } + // If we're unconnected, trigger a connection attempt and start scanning. + NetworkConnectionStateChanged(NULL); } ShellNetworkController::~ShellNetworkController() { @@ -77,18 +77,26 @@ void ShellNetworkController::NetworkListChanged() { ConnectIfUnconnected(); } -void ShellNetworkController::DefaultNetworkChanged( - const chromeos::NetworkState* state) { - if (state) { - VLOG(1) << "Default network state changed:" - << " name=" << GetNetworkName(*state) - << " path=" << state->path() - << " state=" << state->connection_state(); +void ShellNetworkController::NetworkConnectionStateChanged( + const chromeos::NetworkState* network) { + if (network) { + VLOG(1) << "Network connection state changed:" + << " name=" << GetNetworkName(*network) + << " type=" << network->type() + << " path=" << network->path() + << " state=" << network->connection_state(); } else { - VLOG(1) << "Default network state changed: [no network]"; + VLOG(1) << "Network connection state changed: [none]"; } - if (IsConnectedOrConnecting()) { + const chromeos::NetworkState* wifi_network = GetActiveWiFiNetwork(); + preferred_network_is_active_ = + wifi_network && wifi_network->name() == preferred_network_name_; + VLOG(2) << "Active WiFi network is " + << (wifi_network ? wifi_network->name() : std::string("[none]")); + + if (preferred_network_is_active_ || + (preferred_network_name_.empty() && wifi_network)) { SetScanningEnabled(false); } else { SetScanningEnabled(true); @@ -96,6 +104,17 @@ void ShellNetworkController::DefaultNetworkChanged( } } +const chromeos::NetworkState* +ShellNetworkController::GetActiveWiFiNetwork() { + chromeos::NetworkStateHandler* state_handler = + chromeos::NetworkHandler::Get()->network_state_handler(); + const chromeos::NetworkState* network = state_handler->FirstNetworkByType( + chromeos::NetworkTypePattern::Primitive(shill::kTypeWifi)); + return network && + (network->IsConnectedState() || network->IsConnectingState()) ? + network : NULL; +} + void ShellNetworkController::SetScanningEnabled(bool enabled) { const bool currently_enabled = scan_timer_.IsRunning(); if (enabled == currently_enabled) @@ -103,6 +122,7 @@ void ShellNetworkController::SetScanningEnabled(bool enabled) { VLOG(1) << (enabled ? "Starting" : "Stopping") << " scanning"; if (enabled) { + RequestScan(); scan_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kScanIntervalSec), this, &ShellNetworkController::RequestScan); @@ -116,52 +136,72 @@ void ShellNetworkController::RequestScan() { chromeos::NetworkHandler::Get()->network_state_handler()->RequestScan(); } -// Attempts to connect to an available network if not already connecting or -// connected. void ShellNetworkController::ConnectIfUnconnected() { - chromeos::NetworkHandler* handler = chromeos::NetworkHandler::Get(); - DCHECK(handler); - if (waiting_for_connection_result_ || IsConnectedOrConnecting()) + // Don't do anything if the default network is already the preferred one or if + // we have a pending request to connect to it. + if (preferred_network_is_active_ || + state_ == STATE_WAITING_FOR_PREFERRED_RESULT) return; - chromeos::NetworkStateHandler::NetworkStateList state_list; + const chromeos::NetworkState* best_network = NULL; + bool can_connect_to_preferred_network = false; + + chromeos::NetworkHandler* handler = chromeos::NetworkHandler::Get(); + chromeos::NetworkStateHandler::NetworkStateList network_list; handler->network_state_handler()->GetVisibleNetworkListByType( - chromeos::NetworkTypePattern::WiFi(), &state_list); + chromeos::NetworkTypePattern::WiFi(), &network_list); for (chromeos::NetworkStateHandler::NetworkStateList::const_iterator it = - state_list.begin(); it != state_list.end(); ++it) { - const chromeos::NetworkState* state = *it; - DCHECK(state); - if (!state->connectable()) + network_list.begin(); it != network_list.end(); ++it) { + const chromeos::NetworkState* network = *it; + if (!network->connectable()) continue; - VLOG(1) << "Connecting to network " << GetNetworkName(*state) - << " with path " << state->path() << " and strength " - << state->signal_strength(); - waiting_for_connection_result_ = true; - handler->network_connection_handler()->ConnectToNetwork( - state->path(), - base::Bind(&ShellNetworkController::HandleConnectionSuccess, - weak_ptr_factory_.GetWeakPtr()), - base::Bind(&ShellNetworkController::HandleConnectionError, - weak_ptr_factory_.GetWeakPtr()), - false /* check_error_state */); + if (!preferred_network_name_.empty() && + network->name() == preferred_network_name_) { + best_network = network; + can_connect_to_preferred_network = true; + break; + } else if (!best_network) { + best_network = network; + } + } + + // Don't switch networks if we're already connecting/connected and wouldn't be + // switching to the preferred network. + if ((IsConnectedOrConnecting() || state_ != STATE_IDLE) && + !can_connect_to_preferred_network) + return; + if (!best_network) { + VLOG(1) << "Didn't find any connectable networks"; return; } - VLOG(1) << "Didn't find any connectable networks"; + VLOG(1) << "Connecting to network " << GetNetworkName(*best_network) + << " with path " << best_network->path() << " and strength " + << best_network->signal_strength(); + state_ = can_connect_to_preferred_network ? + STATE_WAITING_FOR_PREFERRED_RESULT : + STATE_WAITING_FOR_NON_PREFERRED_RESULT; + handler->network_connection_handler()->ConnectToNetwork( + best_network->path(), + base::Bind(&ShellNetworkController::HandleConnectionSuccess, + weak_ptr_factory_.GetWeakPtr()), + base::Bind(&ShellNetworkController::HandleConnectionError, + weak_ptr_factory_.GetWeakPtr()), + false /* check_error_state */); } void ShellNetworkController::HandleConnectionSuccess() { VLOG(1) << "Successfully connected to network"; - waiting_for_connection_result_ = false; + state_ = STATE_IDLE; } void ShellNetworkController::HandleConnectionError( const std::string& error_name, scoped_ptr<base::DictionaryValue> error_data) { LOG(WARNING) << "Unable to connect to network: " << error_name; - waiting_for_connection_result_ = false; + state_ = STATE_IDLE; } } // namespace apps diff --git a/apps/shell/browser/shell_network_controller_chromeos.h b/apps/shell/browser/shell_network_controller_chromeos.h index 41317e2..3e872da 100644 --- a/apps/shell/browser/shell_network_controller_chromeos.h +++ b/apps/shell/browser/shell_network_controller_chromeos.h @@ -22,23 +22,39 @@ class ShellNetworkController : public chromeos::NetworkStateHandlerObserver { public: // This class must be instantiated after chromeos::DBusThreadManager and // destroyed before it. - ShellNetworkController(); + explicit ShellNetworkController(const std::string& preferred_network_name); virtual ~ShellNetworkController(); // chromeos::NetworkStateHandlerObserver overrides: virtual void NetworkListChanged() OVERRIDE; - virtual void DefaultNetworkChanged( + virtual void NetworkConnectionStateChanged( const chromeos::NetworkState* state) OVERRIDE; private: + // State of communication with the connection manager. + enum State { + // No in-progress requests. + STATE_IDLE = 0, + // Waiting for the result of an attempt to connect to the preferred network. + STATE_WAITING_FOR_PREFERRED_RESULT, + // Waiting for the result of an attempt to connect to a non-preferred + // network. + STATE_WAITING_FOR_NON_PREFERRED_RESULT, + }; + + // Returns the connected or connecting WiFi network or NULL if no network + // matches that description. + const chromeos::NetworkState* GetActiveWiFiNetwork(); + // Controls whether scanning is performed periodically. void SetScanningEnabled(bool enabled); - // Asks shill to scan for networks. + // Asks the connection manager to scan for networks. void RequestScan(); // If not currently connected or connecting, chooses a wireless network and - // asks shill to connect to it. + // asks the connection manager to connect to it. Also switches to + // |preferred_network_name_| if possible. void ConnectIfUnconnected(); // Handles a successful or failed connection attempt. @@ -47,15 +63,20 @@ class ShellNetworkController : public chromeos::NetworkStateHandlerObserver { const std::string& error_name, scoped_ptr<base::DictionaryValue> error_data); - // True when ConnectIfUnconnected() has asked shill to connect but the attempt - // hasn't succeeded or failed yet. This is tracked to avoid sending duplicate - // requests before chromeos::NetworkStateHandler has acknowledged the initial - // connection attempt. - bool waiting_for_connection_result_; + // Current status of communication with the chromeos::NetworkStateHandler. + // This is tracked to avoid sending duplicate requests before the handler has + // acknowledged the initial connection attempt. + State state_; // Invokes RequestScan() periodically. base::RepeatingTimer<ShellNetworkController> scan_timer_; + // Optionally-supplied name of the preferred network. + std::string preferred_network_name_; + + // True if the preferred network is connected or connecting. + bool preferred_network_is_active_; + base::WeakPtrFactory<ShellNetworkController> weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(ShellNetworkController); diff --git a/apps/shell/common/switches.cc b/apps/shell/common/switches.cc index 8b91b63..df4b8ed 100644 --- a/apps/shell/common/switches.cc +++ b/apps/shell/common/switches.cc @@ -7,11 +7,14 @@ namespace apps { namespace switches { -// Specifies the path to an app to load at startup. -const char kApp[] = "app"; +// Path to an app to load at startup. +const char kAppShellAppPath[] = "app-shell-app-path"; -// Specifies the bounds for the host window to create (i.e. "800x600"). -const char kHostWindowBounds[] = "host-window-bounds"; +// Bounds for the host window to create (i.e. "800x600"). +const char kAppShellHostWindowBounds[] = "app-shell-host-window-bounds"; + +// SSID of the preferred WiFi network. +const char kAppShellPreferredNetwork[] = "app-shell-preferred-network"; } // namespace switches } // namespace apps diff --git a/apps/shell/common/switches.h b/apps/shell/common/switches.h index 2ea9f10..0ecf906 100644 --- a/apps/shell/common/switches.h +++ b/apps/shell/common/switches.h @@ -10,8 +10,9 @@ namespace switches { // All switches in alphabetical order. The switches should be documented // alongside the definition of their values in the .cc file. -extern const char kApp[]; -extern const char kHostWindowBounds[]; +extern const char kAppShellAppPath[]; +extern const char kAppShellHostWindowBounds[]; +extern const char kAppShellPreferredNetwork[]; } // namespace switches } // namespace apps |