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

Side by Side Diff: mojo/edk/embedder/embedder.cc

Issue 1350023003: Add a Mojo EDK for Chrome that uses one OS pipe per message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 #include "mojo/edk/embedder/embedder.h"
6
7 #include "base/atomicops.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/task_runner.h"
14 #include "mojo/edk/embedder/embedder_internal.h"
15 #include "mojo/edk/embedder/platform_support.h"
16 #include "mojo/edk/embedder/process_delegate.h"
17 #include "mojo/edk/system/configuration.h"
18 #include "mojo/edk/system/core.h"
19 #include "mojo/edk/system/message_pipe_dispatcher.h"
20 #include "mojo/edk/system/platform_handle_dispatcher.h"
21
22 namespace mojo {
23 namespace embedder {
24
25 // TODO(jam): move into annonymous namespace. Keep outside for debugging in VS
26 // temporarily.
27 int g_channel_count = 0;
28 bool g_wait_for_no_more_channels = false;
29
30 namespace {
31
32 // Note: Called on the I/O thread.
33 void ShutdownIPCSupportHelper(bool wait_for_no_more_channels) {
34 if (wait_for_no_more_channels && g_channel_count) {
35 g_wait_for_no_more_channels = true;
36 return;
37 }
38
39 internal::g_delegate_thread_task_runner->PostTask(
40 FROM_HERE, base::Bind(&ProcessDelegate::OnShutdownComplete,
41 base::Unretained(internal::g_process_delegate)));
42 }
43
44 } // namespace
45
46 namespace internal {
47
48 // Declared in embedder_internal.h.
49 PlatformSupport* g_platform_support = nullptr;
50 system::Core* g_core = nullptr;
51
52 base::TaskRunner* g_delegate_thread_task_runner;
53 embedder::ProcessDelegate* g_process_delegate;
54 base::TaskRunner* g_io_thread_task_runner = nullptr;
55
56 void ChannelStarted() {
57 DCHECK(g_io_thread_task_runner->RunsTasksOnCurrentThread());
58 g_channel_count++;
59 }
60
61 void ChannelShutdown() {
62 DCHECK(g_io_thread_task_runner->RunsTasksOnCurrentThread());
63 g_channel_count--;
64 if (!g_channel_count && g_wait_for_no_more_channels) {
65 ShutdownIPCSupportHelper(false);
66 }
67 }
68
69 } // namespace internal
70
71 Configuration* GetConfiguration() {
72 return system::GetMutableConfiguration();
73 }
74
75 void Init(scoped_ptr<PlatformSupport> platform_support) {
76 DCHECK(platform_support);
77
78 DCHECK(!internal::g_platform_support);
79 internal::g_platform_support = platform_support.release();
80
81 DCHECK(!internal::g_core);
82 internal::g_core = new system::Core(internal::g_platform_support);
83 }
84
85 MojoResult AsyncWait(MojoHandle handle,
86 MojoHandleSignals signals,
87 const base::Callback<void(MojoResult)>& callback) {
88 return internal::g_core->AsyncWait(handle, signals, callback);
89 }
90
91 MojoResult CreatePlatformHandleWrapper(
92 ScopedPlatformHandle platform_handle,
93 MojoHandle* platform_handle_wrapper_handle) {
94 DCHECK(platform_handle_wrapper_handle);
95
96 scoped_refptr<system::Dispatcher> dispatcher =
97 system::PlatformHandleDispatcher::Create(platform_handle.Pass());
98
99 DCHECK(internal::g_core);
100 MojoHandle h = internal::g_core->AddDispatcher(dispatcher);
101 if (h == MOJO_HANDLE_INVALID) {
102 LOG(ERROR) << "Handle table full";
103 dispatcher->Close();
104 return MOJO_RESULT_RESOURCE_EXHAUSTED;
105 }
106
107 *platform_handle_wrapper_handle = h;
108 return MOJO_RESULT_OK;
109 }
110
111 MojoResult PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle,
112 ScopedPlatformHandle* platform_handle) {
113 DCHECK(platform_handle);
114
115 DCHECK(internal::g_core);
116 scoped_refptr<system::Dispatcher> dispatcher(
117 internal::g_core->GetDispatcher(platform_handle_wrapper_handle));
118 if (!dispatcher)
119 return MOJO_RESULT_INVALID_ARGUMENT;
120
121 if (dispatcher->GetType() != system::Dispatcher::Type::PLATFORM_HANDLE)
122 return MOJO_RESULT_INVALID_ARGUMENT;
123
124 *platform_handle =
125 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get())
126 ->PassPlatformHandle()
127 .Pass();
128 return MOJO_RESULT_OK;
129 }
130
131 void InitIPCSupport(ProcessType process_type,
132 scoped_refptr<base::TaskRunner> delegate_thread_task_runner,
133 ProcessDelegate* process_delegate,
134 scoped_refptr<base::TaskRunner> io_thread_task_runner,
135 ScopedPlatformHandle platform_handle) {
136 // |Init()| must have already been called.
137 DCHECK(internal::g_core);
138 internal::g_delegate_thread_task_runner = delegate_thread_task_runner.get();
139 internal::g_process_delegate = process_delegate;
140 internal::g_io_thread_task_runner = io_thread_task_runner.get();
141 }
142
143 void ShutdownIPCSupportOnIOThread() {
144 }
145
146 void ShutdownIPCSupport() {
147 internal::g_io_thread_task_runner->PostTask(
148 FROM_HERE, base::Bind(&ShutdownIPCSupportHelper, false));
149 }
150
151 void ShutdownIPCSupportAndWaitForNoChannels() {
152 internal::g_io_thread_task_runner->PostTask(
153 FROM_HERE, base::Bind(&ShutdownIPCSupportHelper, true));
154 }
155
156 ScopedMessagePipeHandle CreateMessagePipe(
157 ScopedPlatformHandle platform_handle) {
158 scoped_refptr<system::MessagePipeDispatcher> dispatcher =
159 system::MessagePipeDispatcher::Create(
160 system::MessagePipeDispatcher::kDefaultCreateOptions);
161
162 ScopedMessagePipeHandle rv(
163 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher)));
164 CHECK(rv.is_valid());
165 dispatcher->Init(platform_handle.Pass());
166 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it
167 // once that's fixed.
168 return rv.Pass();
169 }
170
171 } // namespace embedder
172 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698