| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
| 6 #define CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/strings/string16.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "content/public/browser/utility_process_mojo_client_impl.h" |
| 14 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 // Implements a client to a mojo service running on a utility process. Takes |
| 19 // care of starting the utility process and connecting to the remote mojo |
| 20 // service. The utility process is terminated in the destructor. |
| 21 // Note: This class is not thread-safe. It is bound to the |
| 22 // SingleThreadTaskRunner it is created on. |
| 23 template <class MojoInterface> |
| 24 class UtilityProcessMojoClient { |
| 25 public: |
| 26 UtilityProcessMojoClient(const base::string16& process_name, |
| 27 const base::Closure& on_error_callback) |
| 28 : impl_(process_name), on_error_callback_(on_error_callback) { |
| 29 DCHECK(!on_error_callback_.is_null()); |
| 30 } |
| 31 |
| 32 void set_disable_sandbox() { impl_.set_disable_sandbox(); } |
| 33 |
| 34 void Start() { |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); |
| 36 DCHECK(!start_called_); |
| 37 |
| 38 start_called_ = true; |
| 39 |
| 40 mojo::InterfaceRequest<MojoInterface> req = mojo::GetProxy(&service_); |
| 41 |
| 42 service_.set_connection_error_handler(on_error_callback_); |
| 43 impl_.Start(MojoInterface::Name_, req.PassMessagePipe()); |
| 44 } |
| 45 |
| 46 // Returns the Mojo service used to make calls to the utility process. |
| 47 MojoInterface* service() WARN_UNUSED_RESULT { |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); |
| 49 DCHECK(start_called_); |
| 50 |
| 51 return service_.get(); |
| 52 } |
| 53 |
| 54 private: |
| 55 // Called when a connection error happens or if the process didn't start. |
| 56 base::Closure on_error_callback_; |
| 57 |
| 58 mojo::InterfacePtr<MojoInterface> service_; |
| 59 UtilityProcessMojoClientImpl impl_; |
| 60 |
| 61 // Enforce calling Start() before getting the service. |
| 62 bool start_called_ = false; |
| 63 |
| 64 // Checks that this class is always accessed from the same thread. |
| 65 base::ThreadChecker thread_checker_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(UtilityProcessMojoClient); |
| 68 }; |
| 69 |
| 70 } // namespace content |
| 71 |
| 72 #endif // CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
| OLD | NEW |