shill: wimax: Don't ask users to re-enter credentials if available.

shill now sets the PassphraseRequired to true only if credentials are
not available, or if a connect attempt to a live network failed. Also,
refuse connecting to a service that's marked as non-connectable, and
fix UpdateConnectable in WiMaxService::Start.

BUG=chrome-os-partner:10115
TEST=unit tests

Change-Id: I53989736a82e4062422c648c58ccc83abc045a86
Reviewed-on: https://gerrit.chromium.org/gerrit/24187
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Darin Petkov <petkov@chromium.org>
diff --git a/wimax_service.cc b/wimax_service.cc
index 06463f3..580b1bd 100644
--- a/wimax_service.cc
+++ b/wimax_service.cc
@@ -115,8 +115,8 @@
   SetStrength(signal_strength);
   proxy->set_signal_strength_changed_callback(
       Bind(&WiMaxService::OnSignalStrengthChanged, Unretained(this)));
-  UpdateConnectable();
   proxy_.reset(local_proxy.release());
+  UpdateConnectable();
   LOG(INFO) << "WiMAX service started: " << GetStorageIdentifier();
   return true;
 }
@@ -135,6 +135,13 @@
         error, Error::kAlreadyConnected, "Already connected.");
     return;
   }
+  if (!connectable()) {
+    LOG(ERROR) << "Can't connect. Service " << GetStorageIdentifier()
+               << " is not connectable.";
+    Error::PopulateAndLog(error, Error::kOperationFailed,
+                          Error::GetDefaultMessage(Error::kOperationFailed));
+    return;
+  }
   WiMaxRefPtr carrier = manager()->wimax_provider()->SelectCarrier(this);
   if (!carrier) {
     Error::PopulateAndLog(
@@ -179,19 +186,12 @@
 
 void WiMaxService::set_eap(const EapCredentials &eap) {
   Service::set_eap(eap);
+  need_passphrase_ = eap.identity.empty() || eap.password.empty();
   UpdateConnectable();
 }
 
 void WiMaxService::UpdateConnectable() {
-  // Don't use Service::Is8021xConnectable because we don't support the full set
-  // of authentication methods.
-  bool is_connectable = false;
-  if (IsStarted()) {
-    if (!eap().identity.empty()) {
-      is_connectable = !eap().password.empty();
-    }
-  }
-  set_connectable(is_connectable);
+  set_connectable(IsStarted() && !need_passphrase_);
 }
 
 void WiMaxService::OnSignalStrengthChanged(int strength) {
@@ -212,6 +212,7 @@
 bool WiMaxService::Unload() {
   // The base method also disconnects the service.
   Service::Unload();
+  ClearPassphrase();
   // Notify the WiMAX provider that this service has been unloaded. If the
   // provider releases ownership of this service, it needs to be deregistered.
   return manager()->wimax_provider()->OnServiceUnloaded(this);
@@ -245,4 +246,11 @@
   return storage_id;
 }
 
+void WiMaxService::ClearPassphrase() {
+  EapCredentials creds = eap();
+  creds.password.clear();
+  // Updates the service credentials and connectability status.
+  set_eap(creds);
+}
+
 }  // namespace shill