Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(982)

Unified Diff: content/browser/utility_process_mojo_client_impl.cc

Issue 2049303002: Add the UtilityProcessMojoClient class and convert SafeJsonParser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/utility_process_mojo_client_impl.cc
diff --git a/content/browser/utility_process_mojo_client_impl.cc b/content/browser/utility_process_mojo_client_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e6a69a47527c082340a6235b77d7f996f74e9d43
--- /dev/null
+++ b/content/browser/utility_process_mojo_client_impl.cc
@@ -0,0 +1,99 @@
+// 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.
+
+#include "content/public/browser/utility_process_mojo_client_impl.h"
+
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/utility_process_host.h"
+#include "content/public/browser/utility_process_host_client.h"
+#include "content/public/common/service_registry.h"
+
+namespace content {
+
+// Takes care of starting and stopping the utility process and connecting
+// the Mojo InterfaceRequest.
+class UtilityProcessMojoClientImpl::Holder
+ : public base::RefCountedThreadSafe<Holder> {
+ public:
+ explicit Holder(const base::string16& process_name)
+ : process_name_(process_name) {}
+
+ // Starts the utility process on the IO thread.
+ void Start(const std::string& mojo_interface_name,
+ mojo::ScopedMessagePipeHandle interface_pipe) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&Holder::StartOnIOThread, this, mojo_interface_name,
+ base::Passed(&interface_pipe)));
+ }
+
+ // Shuts down the utility process.
+ void Shutdown() {
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&Holder::ShutdownOnIOThread, this));
+ }
+
+ void set_disable_sandbox() { disable_sandbox_ = true; }
+
+ private:
+ friend class base::RefCountedThreadSafe<Holder>;
+
+ ~Holder() = default;
+
+ // Starts the utility process and connects to the remote Mojo service.
+ void StartOnIOThread(const std::string& mojo_interface_name,
+ mojo::ScopedMessagePipeHandle interface_pipe) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr();
+ utility_host_->SetName(process_name_);
+ if (disable_sandbox_)
+ utility_host_->DisableSandbox();
+
+ ServiceRegistry* service_registry = utility_host_->GetServiceRegistry();
+ service_registry->ConnectToRemoteService(mojo_interface_name,
+ std::move(interface_pipe));
+ utility_host_->Start();
Anand Mistry (off Chromium) 2016/06/10 11:13:54 Ordering doesn't matter, but I think it's more log
Patrick Monette 2016/06/10 18:13:23 Done.
+ }
+
+ void ShutdownOnIOThread() {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ delete utility_host_.get();
+ }
+
+ // Properties of the utility process.
+ base::string16 process_name_;
+ bool disable_sandbox_ = false;
+
+ // Checks that this class is always accessed from the same thread.
+ base::ThreadChecker thread_checker_;
Anand Mistry (off Chromium) 2016/06/10 11:13:54 This isn't particularly useful. The thread is only
Patrick Monette 2016/06/10 18:13:23 Removed.
+
+ // Must only be accessed on the IO thread.
+ base::WeakPtr<UtilityProcessHost> utility_host_;
+
+ DISALLOW_COPY_AND_ASSIGN(Holder);
+};
+
+UtilityProcessMojoClientImpl::UtilityProcessMojoClientImpl(
+ const base::string16& process_name) {
+ holder_ = new Holder(process_name);
+}
+
+UtilityProcessMojoClientImpl::~UtilityProcessMojoClientImpl() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ holder_->Shutdown();
+}
+
+void UtilityProcessMojoClientImpl::set_disable_sandbox() {
+ holder_->set_disable_sandbox();
+}
+
+void UtilityProcessMojoClientImpl::StartImpl(
+ const std::string& mojo_interface_name,
+ mojo::ScopedMessagePipeHandle interface_pipe) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ holder_->Start(mojo_interface_name, std::move(interface_pipe));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698