More cleanup: Get rid of ServiceConnectorRegistry.
Just move its stuff (which isn't very complicated) directly into
ServiceProviderImpl.
Also simplify ServiceConnector a bit.
R=vardhan@google.com
Review URL: https://codereview.chromium.org/1984463002 .
diff --git a/mojo/public/cpp/application/BUILD.gn b/mojo/public/cpp/application/BUILD.gn
index cbf41ac..b2d0620 100644
--- a/mojo/public/cpp/application/BUILD.gn
+++ b/mojo/public/cpp/application/BUILD.gn
@@ -13,8 +13,6 @@
"connection_context.h",
"lib/application_delegate.cc",
"lib/application_impl.cc",
- "lib/service_connector_registry.cc",
- "lib/service_connector_registry.h",
"lib/service_provider_impl.cc",
"service_connector.h",
"service_provider_impl.h",
diff --git a/mojo/public/cpp/application/lib/service_connector_registry.cc b/mojo/public/cpp/application/lib/service_connector_registry.cc
deleted file mode 100644
index 2698b71..0000000
--- a/mojo/public/cpp/application/lib/service_connector_registry.cc
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright 2015 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 "mojo/public/cpp/application/lib/service_connector_registry.h"
-
-#include <utility>
-
-#include "mojo/public/cpp/application/connection_context.h"
-#include "mojo/public/cpp/application/service_connector.h"
-
-namespace mojo {
-namespace internal {
-
-ServiceConnectorRegistry::ServiceConnectorRegistry() {}
-
-ServiceConnectorRegistry::~ServiceConnectorRegistry() {}
-
-void ServiceConnectorRegistry::SetServiceConnectorForName(
- std::unique_ptr<ServiceConnector> service_connector,
- const std::string& interface_name) {
- name_to_service_connector_[interface_name] = std::move(service_connector);
-}
-
-void ServiceConnectorRegistry::RemoveServiceConnectorForName(
- const std::string& interface_name) {
- NameToServiceConnectorMap::iterator it =
- name_to_service_connector_.find(interface_name);
- if (it == name_to_service_connector_.end())
- return;
- name_to_service_connector_.erase(it);
-}
-
-bool ServiceConnectorRegistry::ConnectToService(
- const ConnectionContext& connection_context,
- const std::string& interface_name,
- ScopedMessagePipeHandle* client_handle) {
- auto iter = name_to_service_connector_.find(interface_name);
- if (iter != name_to_service_connector_.end()) {
- iter->second->ConnectToService(connection_context, interface_name,
- client_handle->Pass());
- return true;
- }
- return false;
-}
-
-} // namespace internal
-} // namespace mojo
diff --git a/mojo/public/cpp/application/lib/service_connector_registry.h b/mojo/public/cpp/application/lib/service_connector_registry.h
deleted file mode 100644
index 235d34d..0000000
--- a/mojo/public/cpp/application/lib/service_connector_registry.h
+++ /dev/null
@@ -1,59 +0,0 @@
-// Copyright 2015 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 MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_REGISTRY_H_
-#define MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_REGISTRY_H_
-
-#include <map>
-#include <memory>
-#include <string>
-
-#include "mojo/public/cpp/system/message_pipe.h"
-
-namespace mojo {
-
-struct ConnectionContext;
-class ServiceConnector;
-
-namespace internal {
-
-// ServiceConnectorRegistry maintains a default ServiceConnector as well as at
-// most one ServiceConnector per interface name. When ConnectToService() is
-// invoked the ServiceConnector registered by name is given the request.
-class ServiceConnectorRegistry {
- public:
- ServiceConnectorRegistry();
- ~ServiceConnectorRegistry();
-
- // Returns true if non ServiceConnectors have been registered by name.
- bool empty() const { return name_to_service_connector_.empty(); }
-
- // Sets a ServiceConnector by name. This deletes any existing ServiceConnector
- // of the same name.
- void SetServiceConnectorForName(
- std::unique_ptr<ServiceConnector> service_connector,
- const std::string& interface_name);
- void RemoveServiceConnectorForName(const std::string& interface_name);
-
- // ConnectToService returns true if this registery has an entry for
- // |interface_name|. In that case, the |client_handle| is passed along
- // to the |ServiceConnector|. Otherwise, this function returns false and
- // |client_handle| is untouched.
- bool ConnectToService(const ConnectionContext& connection_context,
- const std::string& interface_name,
- ScopedMessagePipeHandle* client_handle);
-
- private:
- using NameToServiceConnectorMap =
- std::map<std::string, std::unique_ptr<ServiceConnector>>;
-
- NameToServiceConnectorMap name_to_service_connector_;
-
- MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorRegistry);
-};
-
-} // namespace internal
-} // namespace mojo
-
-#endif // MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_REGISTRY_H_
diff --git a/mojo/public/cpp/application/lib/service_provider_impl.cc b/mojo/public/cpp/application/lib/service_provider_impl.cc
index a3245b4..b65b77a 100644
--- a/mojo/public/cpp/application/lib/service_provider_impl.cc
+++ b/mojo/public/cpp/application/lib/service_provider_impl.cc
@@ -4,8 +4,7 @@
#include "mojo/public/cpp/application/service_provider_impl.h"
-#include "mojo/public/cpp/application/service_connector.h"
-#include "mojo/public/cpp/environment/logging.h"
+#include <utility>
namespace mojo {
@@ -37,12 +36,26 @@
}
}
+void ServiceProviderImpl::AddServiceForName(
+ std::unique_ptr<ServiceConnector> service_connector,
+ const std::string& service_name) {
+ name_to_service_connector_[service_name] = std::move(service_connector);
+}
+
+void ServiceProviderImpl::RemoveServiceForName(
+ const std::string& service_name) {
+ auto it = name_to_service_connector_.find(service_name);
+ if (it != name_to_service_connector_.end())
+ name_to_service_connector_.erase(it);
+}
+
void ServiceProviderImpl::ConnectToService(
const String& service_name,
ScopedMessagePipeHandle client_handle) {
- bool service_found = service_connector_registry_.ConnectToService(
- connection_context_, service_name, &client_handle);
- if (!service_found && fallback_service_provider_) {
+ auto it = name_to_service_connector_.find(service_name);
+ if (it != name_to_service_connector_.end()) {
+ it->second->ConnectToService(connection_context_, client_handle.Pass());
+ } else if (fallback_service_provider_) {
fallback_service_provider_->ConnectToService(service_name,
client_handle.Pass());
}
diff --git a/mojo/public/cpp/application/service_connector.h b/mojo/public/cpp/application/service_connector.h
index 378be9e..8b919b3 100644
--- a/mojo/public/cpp/application/service_connector.h
+++ b/mojo/public/cpp/application/service_connector.h
@@ -13,15 +13,16 @@
struct ConnectionContext;
+// |ServiceConnector| is the generic, type-unsafe interface for objects used by
+// |ServiceProviderImpl| to connect generic "interface requests" (i.e., just
+// message pipes) specified by service name to service implementations.
class ServiceConnector {
public:
virtual ~ServiceConnector() {}
- // Asks the ServiceConnector to connect to the specified service. If the
- // ServiceConnector connects to the service it should take ownership of
- // the handle in |handle|.
+ // Connects to the |ServiceConnector|'s service (if the |ServiceConnector|
+ // wishes to refuse the connection, it should simply drop |handle|).
virtual void ConnectToService(const ConnectionContext& connection_context,
- const std::string& interface_name,
ScopedMessagePipeHandle handle) = 0;
};
diff --git a/mojo/public/cpp/application/service_provider_impl.h b/mojo/public/cpp/application/service_provider_impl.h
index 77a7644..a44d036 100644
--- a/mojo/public/cpp/application/service_provider_impl.h
+++ b/mojo/public/cpp/application/service_provider_impl.h
@@ -6,11 +6,11 @@
#define MOJO_PUBLIC_APPLICATION_SERVICE_PROVIDER_IMPL_H_
#include <functional>
+#include <map>
#include <string>
#include <utility>
#include "mojo/public/cpp/application/connection_context.h"
-#include "mojo/public/cpp/application/lib/service_connector_registry.h"
#include "mojo/public/cpp/application/service_connector.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/interfaces/application/service_provider.mojom.h"
@@ -53,15 +53,12 @@
// unbound state). This may be called even if this object is already unbound.
void Close();
- // Adds a supported service with the given |name|, using the given
+ // Adds a supported service with the given |service_name|, using the given
// |service_connector|.
void AddServiceForName(std::unique_ptr<ServiceConnector> service_connector,
- const std::string& interface_name) {
- service_connector_registry_.SetServiceConnectorForName(
- std::move(service_connector), interface_name);
- }
+ const std::string& service_name);
- // Adds a supported service with the given |name|, using the given
+ // Adds a supported service with the given |service_name|, using the given
// |interface_request_handler| (see above for information about
// |InterfaceRequestHandler<Interface>|). |interface_request_handler| should
// remain valid for the lifetime of this object.
@@ -76,24 +73,23 @@
// });
template <typename Interface>
void AddService(InterfaceRequestHandler<Interface> interface_request_handler,
- const std::string& name = Interface::Name_) {
+ const std::string& service_name = Interface::Name_) {
AddServiceForName(
std::unique_ptr<ServiceConnector>(new ServiceConnectorImpl<Interface>(
std::move(interface_request_handler))),
- name);
+ service_name);
}
- // Removes support for the service with the given |name|.
- void RemoveServiceForName(const std::string& name) {
- service_connector_registry_.RemoveServiceConnectorForName(name);
- }
+ // Removes support for the service with the given |service_name|.
+ void RemoveServiceForName(const std::string& service_name);
// Like |RemoveServiceForName()| (above), but designed so that it can be used
- // like |RemoveService<Interface>()| or even |RemoveService<Interface>(name)|
- // (to parallel |AddService<Interface>()|).
+ // like |RemoveService<Interface>()| or even
+ // |RemoveService<Interface>(service_name)| (to parallel
+ // |AddService<Interface>()|).
template <typename Interface>
- void RemoveService(const std::string& name = Interface::Name_) {
- RemoveServiceForName(name);
+ void RemoveService(const std::string& service_name = Interface::Name_) {
+ RemoveServiceForName(service_name);
}
// This uses the provided |fallback_service_provider| for connection requests
@@ -124,7 +120,6 @@
~ServiceConnectorImpl() override {}
void ConnectToService(const mojo::ConnectionContext& connection_context,
- const std::string& interface_name,
ScopedMessagePipeHandle client_handle) override {
interface_request_handler_(
connection_context,
@@ -144,7 +139,9 @@
ConnectionContext connection_context_;
Binding<ServiceProvider> binding_;
- internal::ServiceConnectorRegistry service_connector_registry_;
+ std::map<std::string, std::unique_ptr<ServiceConnector>>
+ name_to_service_connector_;
+
ServiceProvider* fallback_service_provider_;
MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);