Chromium Code Reviews| Index: content/public/browser/utility_process_mojo_client.h |
| diff --git a/content/public/browser/utility_process_mojo_client.h b/content/public/browser/utility_process_mojo_client.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0661e900740aaaf64672b6ace730ac7e1c3c904a |
| --- /dev/null |
| +++ b/content/public/browser/utility_process_mojo_client.h |
| @@ -0,0 +1,72 @@ |
| +// 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 CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
| +#define CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
| + |
| +#include "base/callback.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/strings/string16.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "content/public/browser/utility_process_mojo_client_impl.h" |
| +#include "mojo/public/cpp/bindings/interface_ptr.h" |
| + |
| +namespace content { |
| + |
| +// Implements a client to a mojo service running on a utility process. Takes |
| +// care of starting the utility process and connecting to the remote mojo |
| +// service. The utility process is terminated in the destructor. |
| +// Note: This class is not thread-safe. It is bound to the |
| +// SingleThreadTaskRunner it is created on. |
| +template <class MojoInterface> |
| +class UtilityProcessMojoClient { |
| + public: |
| + explicit UtilityProcessMojoClient(const base::string16& process_name, |
|
grt (UTC plus 2)
2016/06/10 19:03:06
nit: omit "explicit"
Patrick Monette
2016/06/10 21:29:46
Done.
|
| + const base::Closure& on_error_callback) |
| + : impl_(process_name), on_error_callback_(on_error_callback) { |
| + DCHECK(!on_error_callback_.is_null()); |
| + } |
| + |
| + void set_disable_sandbox() { impl_.set_disable_sandbox(); } |
| + |
| + void Start() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(!start_called_); |
| + |
| + start_called_ = true; |
| + |
| + mojo::InterfaceRequest<MojoInterface> req = mojo::GetProxy(&service_); |
| + |
| + service_.set_connection_error_handler(on_error_callback_); |
| + impl_.Start(MojoInterface::Name_, req.PassMessagePipe()); |
| + } |
| + |
| + // Returns the Mojo service used to make calls to the utility process. |
| + MojoInterface* service() WARN_UNUSED_RESULT { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(start_called_); |
| + |
| + return service_.get(); |
| + } |
| + |
| + private: |
| + // Called when a connection error happens or if the process didn't start. |
| + base::Closure on_error_callback_; |
| + |
| + mojo::InterfacePtr<MojoInterface> service_; |
| + UtilityProcessMojoClientImpl impl_; |
| + |
| + // Enforce calling Start() before getting the service. |
| + bool start_called_ = false; |
| + |
| + // Checks that this class is always accessed from the same thread. |
| + base::ThreadChecker thread_checker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UtilityProcessMojoClient); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |