Chromium Code Reviews| Index: third_party/WebKit/Source/platform/ServiceConnector.h |
| diff --git a/third_party/WebKit/Source/platform/ServiceConnector.h b/third_party/WebKit/Source/platform/ServiceConnector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6f9df0e8bc25e7d855d00195f14a4045d3062f00 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/ServiceConnector.h |
| @@ -0,0 +1,67 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
|
Ken Rockot(use gerrit already)
2016/12/05 20:54:18
nit: 2016
blundell
2016/12/15 17:04:52
Done. Hopefully this lands before it needs to be c
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef ServiceConnector_h |
| +#define ServiceConnector_h |
| + |
| +#include "mojo/public/cpp/bindings/binding.h" |
| +#include "public/platform/Platform.h" |
| +#include "services/service_manager/public/interfaces/connector.mojom-blink.h" |
| +#include "wtf/StdLibExtras.h" |
| + |
| +namespace blink { |
| + |
| +// ServiceConnector supports connecting to Mojo services by name. |
| +class ServiceConnector { |
| + public: |
| + static ServiceConnector& instance() { |
| + DEFINE_STATIC_LOCAL(ServiceConnector, instance, ()); |
| + return instance; |
| + } |
| + |
| + template <typename Interface> |
| + void ConnectToInterface(const char* serviceName, |
|
esprehn
2016/12/02 02:23:14
These should all be lowerCase() for now.
blundell
2016/12/15 17:04:52
Done.
|
| + mojo::InterfaceRequest<Interface> interfacePtr) { |
|
Noel Gordon
2016/12/07 07:09:26
|interfacePtr| ?? Isn't the argument a request?
blundell
2016/12/15 17:04:52
Great catch, thanks. Done.
|
| + InitializeIfNecessary(); |
| + |
| + if (!m_connector.is_bound()) |
| + return; |
| + |
| + service_manager::mojom::blink::IdentityPtr remoteIdentity( |
| + service_manager::mojom::blink::Identity::New()); |
| + remoteIdentity->name = serviceName; |
| + remoteIdentity->user_id = service_manager::mojom::blink::kInheritUserID; |
| + remoteIdentity->instance = ""; |
| + |
| + service_manager::mojom::blink::InterfaceProviderPtr remoteInterfaces; |
| + m_connector->Connect(std::move(remoteIdentity), GetProxy(&remoteInterfaces), |
| + nullptr, |
| + base::Bind(&ServiceConnector::OnConnectionCompleted)); |
| + |
| + remoteInterfaces->GetInterface(Interface::Name_, |
| + interfacePtr.PassMessagePipe()); |
| + } |
| + |
| + private: |
| + ServiceConnector() : m_initialized(false) {} |
|
Noel Gordon
2016/12/07 07:09:26
Is this needed? Could we init the class fully at c
blundell
2016/12/15 17:04:51
The recommended practice in Mojo is to set up all
|
| + |
| + void InitializeIfNecessary() { |
|
esprehn
2016/12/02 02:23:14
initializeIfNeeded()
blundell
2016/12/15 17:04:52
Done.
|
| + if (m_initialized) |
| + return; |
| + |
| + m_connector = Platform::current()->serviceConnector(); |
| + m_initialized = true; |
| + } |
| + |
| + static void OnConnectionCompleted( |
| + service_manager::mojom::ConnectResult result, |
| + const WTF::String& targetUserID) {} |
| + |
| + bool m_initialized; |
| + service_manager::mojom::blink::ConnectorPtr m_connector; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // ServiceConnector_h |