Skip to content

Commit

Permalink
Adding network configuration handler class
Browse files Browse the repository at this point in the history
that will eventually replace the NetworkLibrary method
of configuring networks.

BUG=none
TEST=none


Review URL: https://chromiumcodereview.appspot.com/11260047

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168338 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
gspencer@chromium.org committed Nov 17, 2012
1 parent 573950e commit 24e4732
Show file tree
Hide file tree
Showing 4 changed files with 712 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chromeos/chromeos.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
'network/device_state.h',
'network/managed_state.cc',
'network/managed_state.h',
'network/network_configuration_handler.cc',
'network/network_configuration_handler.h',
'network/network_sms_handler.cc',
'network/network_sms_handler.h',
'network/network_state.cc',
Expand Down Expand Up @@ -286,6 +288,7 @@
'dbus/introspectable_client_unittest.cc',
'dbus/modem_messaging_client_unittest.cc',
'disks/disk_mount_manager_unittest.cc',
'network/network_configuration_handler_unittest.cc',
'network/network_sms_handler_unittest.cc',
'network/network_state_handler_unittest.cc',
'network/shill_property_handler_unittest.cc',
Expand Down
221 changes: 221 additions & 0 deletions chromeos/network/network_configuration_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// Copyright (c) 2012 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 "chromeos/network/network_configuration_handler.h"

#include <string>
#include <vector>

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/shill_manager_client.h"
#include "chromeos/dbus/shill_service_client.h"
#include "dbus/object_path.h"

namespace chromeos {

namespace {

// None of these error messages are user-facing: they should only appear in
// logs.
const char kErrorsListTag[] = "errors";
const char kClearPropertiesFailedError[] = "Error.ClearPropertiesFailed";
const char kClearPropertiesFailedErrorMessage[] = "Clear properties failed";
const char kDBusFailedError[] = "Error.DBusFailed";
const char kDBusFailedErrorMessage[] = "DBus call failed.";

// These are names of fields in the error data returned through the error
// callbacks.
const char kErrorName[] = "errorName";
const char kErrorMessage[] = "errorMessage";
const char kServicePath[] = "servicePath";

base::DictionaryValue* CreateErrorData(const std::string& service_path,
const std::string& error_name,
const std::string& error_message) {
scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
error_data->SetString(kErrorName, error_name);
error_data->SetString(kErrorMessage, error_message);
if (!service_path.empty())
error_data->SetString(kServicePath, service_path);
LOG(ERROR) << "NetworkConfigurationHandler Received an error("
<< error_name << ") for service path '" << service_path << "':"
<< error_message;
return error_data.release();
}

void ClearPropertiesCallback(
const std::vector<std::string>& names,
const std::string& service_path,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback,
const base::ListValue& result) {
bool some_failed = false;
for (size_t i = 0; i < result.GetSize(); ++i) {
bool success;
if (result.GetBoolean(i, &success)) {
if (!success) {
some_failed = true;
break;
}
} else {
NOTREACHED() << "Result garbled from ClearProperties";
}
}

if (some_failed) {
DCHECK(names.size() == result.GetSize())
<< "Result wrong size from ClearProperties.";
scoped_ptr<base::DictionaryValue> error_data(
CreateErrorData(service_path,
kClearPropertiesFailedError,
kClearPropertiesFailedErrorMessage));
error_data->Set("errors", result.DeepCopy());
scoped_ptr<base::ListValue> name_list(new base::ListValue);
name_list->AppendStrings(names);
error_data->Set("names", name_list.release());
error_callback.Run(kClearPropertiesFailedError, error_data.Pass());
} else {
callback.Run();
}
}

void ClearPropertiesErrorCallback(
const std::string& service_path,
const NetworkHandlerErrorCallback& error_callback,
const std::string& error_name,
const std::string& error_message) {
// Add a new error.
scoped_ptr<base::DictionaryValue> error_data(
CreateErrorData(service_path, error_name, error_message));
error_callback.Run(kClearPropertiesFailedError, error_data.Pass());
}

// Used to translate the dbus dictionary callback into one that calls
// the error callback if we have a failure.
void RunCallbackWithDictionaryValue(
const NetworkHandlerDictionaryResultCallback& callback,
const NetworkHandlerErrorCallback& error_callback,
const std::string& service_path,
DBusMethodCallStatus call_status,
const base::DictionaryValue& value) {
if (call_status != DBUS_METHOD_CALL_SUCCESS) {
scoped_ptr<base::DictionaryValue> error_data(
CreateErrorData(service_path,
kDBusFailedError,
kDBusFailedErrorMessage));
error_callback.Run(kDBusFailedError, error_data.Pass());
} else {
callback.Run(service_path, value);
}
}

void RunErrorCallback(const std::string& service_path,
const NetworkHandlerErrorCallback& error_callback,
const std::string& error_name,
const std::string& error_message) {
scoped_ptr<base::DictionaryValue> error_dict(
CreateErrorData(service_path, error_name, error_message));
error_callback.Run(error_name, error_dict.Pass());
}

void RunCreateNetworkCallback(
const NetworkHandlerStringResultCallback& callback,
const dbus::ObjectPath& service_path) {
callback.Run(service_path.value());
}

} // namespace

NetworkConfigurationHandler::NetworkConfigurationHandler() {
}

NetworkConfigurationHandler::~NetworkConfigurationHandler() {
}

void NetworkConfigurationHandler::GetProperties(
const std::string& service_path,
const NetworkHandlerDictionaryResultCallback& callback,
const NetworkHandlerErrorCallback& error_callback) const {
DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
dbus::ObjectPath(service_path),
base::Bind(&RunCallbackWithDictionaryValue,
callback,
error_callback,
service_path));
}

void NetworkConfigurationHandler::SetProperties(
const std::string& service_path,
const base::DictionaryValue& properties,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const {
DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService(
properties,
callback,
base::Bind(&RunErrorCallback, service_path, error_callback));
}

void NetworkConfigurationHandler::ClearProperties(
const std::string& service_path,
const std::vector<std::string>& names,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) {
DBusThreadManager::Get()->GetShillServiceClient()->ClearProperties(
dbus::ObjectPath(service_path),
names,
base::Bind(&ClearPropertiesCallback,
names,
service_path,
callback,
error_callback),
base::Bind(&ClearPropertiesErrorCallback, service_path, error_callback));
}

void NetworkConfigurationHandler::Connect(
const std::string& service_path,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const {
DBusThreadManager::Get()->GetShillServiceClient()->Connect(
dbus::ObjectPath(service_path),
callback,
base::Bind(&RunErrorCallback, service_path, error_callback));
}

void NetworkConfigurationHandler::Disconnect(
const std::string& service_path,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const {
DBusThreadManager::Get()->GetShillServiceClient()->Disconnect(
dbus::ObjectPath(service_path),
callback,
base::Bind(&RunErrorCallback, service_path, error_callback));
}

void NetworkConfigurationHandler::CreateConfiguration(
const base::DictionaryValue& properties,
const NetworkHandlerStringResultCallback& callback,
const NetworkHandlerErrorCallback& error_callback) const {
DBusThreadManager::Get()->GetShillManagerClient()->GetService(
properties,
base::Bind(&RunCreateNetworkCallback, callback),
base::Bind(&RunErrorCallback, "", error_callback));
}

void NetworkConfigurationHandler::RemoveConfiguration(
const std::string& service_path,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const {
DBusThreadManager::Get()->GetShillServiceClient()->Remove(
dbus::ObjectPath(service_path),
callback,
base::Bind(&RunErrorCallback, service_path, error_callback));
}

} // namespace chromeos
124 changes: 124 additions & 0 deletions chromeos/network/network_configuration_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) 2012 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 CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_
#define CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/gtest_prod_util.h"
#include "chromeos/chromeos_export.h"

namespace base {
class DictionaryValue;
}

namespace chromeos {

// An error callback used by both the configuration handler and the state
// handler to receive error results from the API.
// TODO(gspencer): move to a common header.
typedef base::Callback<
void(const std::string& error_name,
const scoped_ptr<base::DictionaryValue> error_data)>
NetworkHandlerErrorCallback;

typedef base::Callback<
void(const std::string& service_path,
const base::DictionaryValue& dictionary)>
NetworkHandlerDictionaryResultCallback;

typedef base::Callback<void(const std::string& service_path)>
NetworkHandlerStringResultCallback;

// The NetworkConfigurationHandler class is used to create and configure
// networks in ChromeOS. It mostly calls through to the Shill service API, and
// most calls are asynchronous for that reason. No calls will block on DBus
// calls.
//
// This is owned and it's lifetime managed by aura::Shell.
//
// For accessing lists of remembered networks, and other state information, see
// the class NetworkStateHandler.
//
// Note on callbacks: Because all the functions here are meant to be
// asynchronous, they all take a |callback| of some type, and an
// |error_callback|. When the operation succeeds, |callback| will be called, and
// when it doesn't, |error_callback| will be called with information about the
// error, including a symbolic name for the error and often some error message
// that is suitable for logging. None of the error message text is meant for
// user consumption.

class CHROMEOS_EXPORT NetworkConfigurationHandler {
public:
NetworkConfigurationHandler();
~NetworkConfigurationHandler();

// Gets the properties of the network with id |service_path|. See note on
// |callback| and |error_callback|, in class description above.
void GetProperties(const std::string& service_path,
const NetworkHandlerDictionaryResultCallback& callback,
const NetworkHandlerErrorCallback& error_callback) const;

// Sets the properties of the network with id |service_path|. This means the
// given properties will be merged with the existing settings, and it won't
// clear any existing properties. See note on |callback| and |error_callback|,
// in class description above.
void SetProperties(const std::string& service_path,
const base::DictionaryValue& properties,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const;

// Removes the properties with the given property paths. If any of them are
// unable to be cleared, the |error_callback| will only be run once with
// accumulated information about all of the errors as a list attached to the
// "errors" key of the error data, and the |callback| will not be run, even
// though some of the properties may have been cleared. If there are no
// errors, |callback| will be run.
void ClearProperties(const std::string& service_path,
const std::vector<std::string>& property_paths,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback);

// Initiates a connection with network that has id |service_path|. See note on
// |callback| and |error_callback|, in class description above.
void Connect(const std::string& service_path,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const;

// Initiates a disconnect with the network at |service_path|. See note on
// |callback| and |error_callback|, in class description above.
void Disconnect(const std::string& service_path,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const;


// Creates a network with the given properties in the active Shill profile,
// and returns the properties to |callback| if successful, along with the new
// service_path. See note on |callback| and |error_callback|, in class
// description above.
void CreateConfiguration(
const base::DictionaryValue& properties,
const NetworkHandlerStringResultCallback& callback,
const NetworkHandlerErrorCallback& error_callback) const;

// Removes the network |service_path| from the remembered network list in the
// active Shill profile. The network may still show up in the visible networks
// after this, but no profile configuration will remain. See note on
// |callback| and |error_callback|, in class description above.
void RemoveConfiguration(
const std::string& service_path,
const base::Closure& callback,
const NetworkHandlerErrorCallback& error_callback) const;

private:
DISALLOW_COPY_AND_ASSIGN(NetworkConfigurationHandler);
};

} // namespace chromeos

#endif // CHROMEOS_NETWORK_NETWORK_CONFIGURATION_HANDLER_H_
Loading

0 comments on commit 24e4732

Please sign in to comment.