Abstract out connection information into ConnectionContext. This is part of my plan to get rid of ApplicationConnection, which serves two roles: as the holder of connection information *and* as the ServiceProvider impl (via ServiceRegistry). R=vardhan@google.com Review URL: https://codereview.chromium.org/1971613002 .
diff --git a/mojo/public/cpp/application/BUILD.gn b/mojo/public/cpp/application/BUILD.gn index 819ca1c..9bd8474 100644 --- a/mojo/public/cpp/application/BUILD.gn +++ b/mojo/public/cpp/application/BUILD.gn
@@ -11,6 +11,7 @@ "application_delegate.h", "application_impl.h", "connect.h", + "connection_context.h", "interface_factory.h", "lib/application_connection.cc", "lib/application_delegate.cc",
diff --git a/mojo/public/cpp/application/connection_context.h b/mojo/public/cpp/application/connection_context.h new file mode 100644 index 0000000..59ce434 --- /dev/null +++ b/mojo/public/cpp/application/connection_context.h
@@ -0,0 +1,59 @@ +// Copyright 2016 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_APPLICATION_CONNECTION_CONTEXT_H_ +#define MOJO_PUBLIC_APPLICATION_CONNECTION_CONTEXT_H_ + +#include <string> + +namespace mojo { + +// A |ConnectionContext| is used to track context about a given message pipe +// connection. It is usually associated to the "impl" side of an interface. +// +// |Application| (see //mojo/public/interfaces/application/application.mojom) +// accepts a message that looks like: +// +// AcceptConnection(string requestor_url, +// string resolved_url, +// ServiceProvider& services); +// +// (TODO(vtl): I've jumped the gun: I've pre-removed |exposed_services|, made +// |services| non-nullable, and reordered the parameters.) +// +// Upon handling |AcceptConnection()|, the |ServiceProvider| implementation +// bound to |services| is given a |ConnectionContext| with |type = +// Type::INCOMING|, |remote_url = requestor_url|, and |connection_url = +// resolved_url|. +// +// The |ConnectionContext| is meant to be propagated: If the remote side uses +// its |ServiceProviderPtr| to request a |Foo| (from the local side), then the +// |Foo| implementation (again, on the local side) may be given (a copy of) the +// |ConnectionContext| described above. +struct ConnectionContext { + enum class Type { + UNKNOWN = 0, + INCOMING, + }; + + ConnectionContext() {} + ConnectionContext(Type type, + const std::string& remote_url, + const std::string& connection_url) + : type(type), remote_url(remote_url), connection_url(connection_url) {} + + Type type = Type::UNKNOWN; + + // The URL of the remote side of this connection (as provided by the |Shell|); + // if unknown/not applicable, this is empty. + std::string remote_url; + + // The URL used by the remote side to establish "this" connection (or a parent + // thereof); if unknown/not applicable, this is empty. + std::string connection_url; +}; + +} // namespace mojo + +#endif // MOJO_PUBLIC_APPLICATION_CONNECTION_CONTEXT_H_
diff --git a/mojo/public/cpp/application/lib/application_impl.cc b/mojo/public/cpp/application/lib/application_impl.cc index ea1c200..289d4b3 100644 --- a/mojo/public/cpp/application/lib/application_impl.cc +++ b/mojo/public/cpp/application/lib/application_impl.cc
@@ -7,6 +7,7 @@ #include <utility> #include "mojo/public/cpp/application/application_delegate.h" +#include "mojo/public/cpp/application/connection_context.h" #include "mojo/public/cpp/application/lib/service_registry.h" #include "mojo/public/cpp/bindings/interface_ptr.h" #include "mojo/public/cpp/bindings/interface_request.h" @@ -70,7 +71,10 @@ MOJO_LOG_IF(ERROR, exposed_services) << "DEPRECATED: exposed_services is going away"; std::unique_ptr<internal::ServiceRegistry> registry( - new internal::ServiceRegistry(url, requestor_url, services.Pass())); + new internal::ServiceRegistry( + ConnectionContext(ConnectionContext::Type::INCOMING, requestor_url, + url), + services.Pass())); if (!delegate_->ConfigureIncomingConnection(registry.get())) return; incoming_service_registries_.push_back(std::move(registry));
diff --git a/mojo/public/cpp/application/lib/service_registry.cc b/mojo/public/cpp/application/lib/service_registry.cc index 734dc1b..6be0e10 100644 --- a/mojo/public/cpp/application/lib/service_registry.cc +++ b/mojo/public/cpp/application/lib/service_registry.cc
@@ -13,12 +13,9 @@ ServiceRegistry::ServiceRegistry() : local_binding_(this) {} ServiceRegistry::ServiceRegistry( - const std::string& connection_url, - const std::string& remote_url, + const ConnectionContext& connection_context, InterfaceRequest<ServiceProvider> local_services) - : connection_url_(connection_url), - remote_url_(remote_url), - local_binding_(this) { + : connection_context_(connection_context), local_binding_(this) { if (local_services.is_pending()) local_binding_.Bind(local_services.Pass()); } @@ -38,11 +35,11 @@ } const std::string& ServiceRegistry::GetConnectionURL() { - return connection_url_; + return connection_context_.connection_url; } const std::string& ServiceRegistry::GetRemoteApplicationURL() { - return remote_url_; + return connection_context_.remote_url; } void ServiceRegistry::ConnectToService(const String& service_name,
diff --git a/mojo/public/cpp/application/lib/service_registry.h b/mojo/public/cpp/application/lib/service_registry.h index c8c47d3..50e5a43 100644 --- a/mojo/public/cpp/application/lib/service_registry.h +++ b/mojo/public/cpp/application/lib/service_registry.h
@@ -8,6 +8,7 @@ #include <string> #include "mojo/public/cpp/application/application_connection.h" +#include "mojo/public/cpp/application/connection_context.h" #include "mojo/public/cpp/application/lib/service_connector_registry.h" #include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/interfaces/application/service_provider.mojom.h" @@ -21,8 +22,7 @@ class ServiceRegistry : public ServiceProvider, public ApplicationConnection { public: ServiceRegistry(); - ServiceRegistry(const std::string& connection_url, - const std::string& remote_url, + ServiceRegistry(const ConnectionContext& connection_context, InterfaceRequest<ServiceProvider> local_services); ~ServiceRegistry() override; @@ -39,8 +39,7 @@ void ConnectToService(const String& service_name, ScopedMessagePipeHandle client_handle) override; - const std::string connection_url_; - const std::string remote_url_; + ConnectionContext connection_context_; Binding<ServiceProvider> local_binding_; ServiceConnectorRegistry service_connector_registry_;