OLD | NEW |
| (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/public/cpp/environment/environment.h" | |
6 | |
7 #include <assert.h> | |
8 | |
9 #include "mojo/public/c/environment/logger.h" | |
10 #include "mojo/public/cpp/environment/lib/default_async_waiter.h" | |
11 #include "mojo/public/cpp/environment/lib/default_logger.h" | |
12 #include "mojo/public/cpp/utility/run_loop.h" | |
13 | |
14 namespace mojo { | |
15 | |
16 namespace { | |
17 | |
18 const MojoAsyncWaiter* g_default_async_waiter = nullptr; | |
19 const MojoLogger* g_default_logger = nullptr; | |
20 | |
21 void Init(const MojoAsyncWaiter* default_async_waiter, | |
22 const MojoLogger* default_logger) { | |
23 g_default_async_waiter = default_async_waiter | |
24 ? default_async_waiter | |
25 : &internal::kDefaultAsyncWaiter; | |
26 g_default_logger = | |
27 default_logger ? default_logger : &internal::kDefaultLogger; | |
28 | |
29 RunLoop::SetUp(); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 Environment::Environment() { | |
35 Init(nullptr, nullptr); | |
36 } | |
37 | |
38 Environment::Environment(const MojoAsyncWaiter* default_async_waiter, | |
39 const MojoLogger* default_logger) { | |
40 Init(default_async_waiter, default_logger); | |
41 } | |
42 | |
43 Environment::~Environment() { | |
44 RunLoop::TearDown(); | |
45 | |
46 // TODO(vtl): Maybe we should allow nesting, and restore previous default | |
47 // async waiters and loggers? | |
48 g_default_async_waiter = nullptr; | |
49 g_default_logger = nullptr; | |
50 } | |
51 | |
52 // static | |
53 const MojoAsyncWaiter* Environment::GetDefaultAsyncWaiter() { | |
54 assert(g_default_async_waiter); // Fails if not "inside" |Environment|. | |
55 return g_default_async_waiter; | |
56 } | |
57 | |
58 // static | |
59 const MojoLogger* Environment::GetDefaultLogger() { | |
60 assert(g_default_logger); // Fails if not "inside" |Environment|. | |
61 return g_default_logger; | |
62 } | |
63 | |
64 // static | |
65 void Environment::InstantiateDefaultRunLoop() { | |
66 assert(!RunLoop::current()); | |
67 // Not leaked: accessible from |RunLoop::current()|. | |
68 RunLoop* run_loop = new RunLoop(); | |
69 MOJO_ALLOW_UNUSED_LOCAL(run_loop); | |
70 assert(run_loop == RunLoop::current()); | |
71 } | |
72 | |
73 // static | |
74 void Environment::DestroyDefaultRunLoop() { | |
75 assert(RunLoop::current()); | |
76 delete RunLoop::current(); | |
77 assert(!RunLoop::current()); | |
78 } | |
79 | |
80 } // namespace mojo | |
OLD | NEW |