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

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: move to mojo::edk namespace in preparation for runtim flag Created 5 years, 2 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 edk {
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 Core* g_core = nullptr;
51
52 base::TaskRunner* g_delegate_thread_task_runner;
53 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 // Reset g_wait_for_no_more_channels for unit tests which initialize and
66 // tear down multiple times in a process.
67 g_wait_for_no_more_channels = false;
68 ShutdownIPCSupportHelper(false);
69 }
70 }
71
72 } // namespace internal
73
74 void SetMaxMessageSize(size_t bytes) {
75 GetMutableConfiguration()->max_message_num_bytes = bytes;
76 }
77
78 void Init(scoped_ptr<PlatformSupport> platform_support) {
79 DCHECK(platform_support);
80
81 DCHECK(!internal::g_platform_support);
82 internal::g_platform_support = platform_support.release();
83
84 DCHECK(!internal::g_core);
85 internal::g_core = new Core(internal::g_platform_support);
86 }
87
88 MojoResult AsyncWait(MojoHandle handle,
89 MojoHandleSignals signals,
90 const base::Callback<void(MojoResult)>& callback) {
91 return internal::g_core->AsyncWait(handle, signals, callback);
92 }
93
94 MojoResult CreatePlatformHandleWrapper(
95 ScopedPlatformHandle platform_handle,
96 MojoHandle* platform_handle_wrapper_handle) {
97 DCHECK(platform_handle_wrapper_handle);
98
99 scoped_refptr<Dispatcher> dispatcher =
100 PlatformHandleDispatcher::Create(platform_handle.Pass());
101
102 DCHECK(internal::g_core);
103 MojoHandle h = internal::g_core->AddDispatcher(dispatcher);
104 if (h == MOJO_HANDLE_INVALID) {
105 LOG(ERROR) << "Handle table full";
106 dispatcher->Close();
107 return MOJO_RESULT_RESOURCE_EXHAUSTED;
108 }
109
110 *platform_handle_wrapper_handle = h;
111 return MOJO_RESULT_OK;
112 }
113
114 MojoResult PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle,
115 ScopedPlatformHandle* platform_handle) {
116 DCHECK(platform_handle);
117
118 DCHECK(internal::g_core);
119 scoped_refptr<Dispatcher> dispatcher(
120 internal::g_core->GetDispatcher(platform_handle_wrapper_handle));
121 if (!dispatcher)
122 return MOJO_RESULT_INVALID_ARGUMENT;
123
124 if (dispatcher->GetType() != Dispatcher::Type::PLATFORM_HANDLE)
125 return MOJO_RESULT_INVALID_ARGUMENT;
126
127 *platform_handle =
128 static_cast<PlatformHandleDispatcher*>(dispatcher.get())
129 ->PassPlatformHandle()
130 .Pass();
131 return MOJO_RESULT_OK;
132 }
133
134 void InitIPCSupport(scoped_refptr<base::TaskRunner> delegate_thread_task_runner,
135 ProcessDelegate* process_delegate,
136 scoped_refptr<base::TaskRunner> io_thread_task_runner,
137 ScopedPlatformHandle platform_handle) {
138 // |Init()| must have already been called.
139 DCHECK(internal::g_core);
140 internal::g_delegate_thread_task_runner = delegate_thread_task_runner.get();
141 internal::g_process_delegate = process_delegate;
142 internal::g_io_thread_task_runner = io_thread_task_runner.get();
143 }
144
145 void ShutdownIPCSupportOnIOThread() {
146 }
147
148 void ShutdownIPCSupport() {
149 internal::g_io_thread_task_runner->PostTask(
150 FROM_HERE, base::Bind(&ShutdownIPCSupportHelper, false));
151 }
152
153 void ShutdownIPCSupportAndWaitForNoChannels() {
154 internal::g_io_thread_task_runner->PostTask(
155 FROM_HERE, base::Bind(&ShutdownIPCSupportHelper, true));
156 }
157
158 ScopedMessagePipeHandle CreateMessagePipe(
159 ScopedPlatformHandle platform_handle) {
160 scoped_refptr<MessagePipeDispatcher> dispatcher =
161 MessagePipeDispatcher::Create(
162 MessagePipeDispatcher::kDefaultCreateOptions);
163
164 ScopedMessagePipeHandle rv(
165 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher)));
166 CHECK(rv.is_valid());
167 dispatcher->Init(platform_handle.Pass());
168 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it
169 // once that's fixed.
170 return rv.Pass();
171 }
172
173 } // namespace edk
174 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698