OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/public/cpp/environment/environment.h" | 5 #include "mojo/public/cpp/environment/environment.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 | 8 |
9 #include "mojo/public/c/environment/logger.h" | 9 #include "mojo/public/c/environment/logger.h" |
10 #include "mojo/public/cpp/environment/lib/default_async_waiter.h" | 10 #include "mojo/public/cpp/environment/lib/default_async_waiter.h" |
11 #include "mojo/public/cpp/environment/lib/default_logger.h" | 11 #include "mojo/public/cpp/environment/lib/default_logger.h" |
12 #include "mojo/public/cpp/utility/run_loop.h" | 12 #include "mojo/public/cpp/utility/run_loop.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 | 15 |
16 namespace { | 16 const MojoAsyncWaiter* g_default_async_waiter = &internal::kDefaultAsyncWaiter; |
17 | 17 const MojoLogger* g_default_logger = &internal::kDefaultLogger; |
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 | |
30 } // namespace | |
31 | |
32 Environment::Environment() { | |
33 Init(nullptr, nullptr); | |
34 } | |
35 | |
36 Environment::Environment(const MojoAsyncWaiter* default_async_waiter, | |
37 const MojoLogger* default_logger) { | |
38 Init(default_async_waiter, default_logger); | |
39 } | |
40 | |
41 Environment::~Environment() { | |
42 // TODO(vtl): Maybe we should allow nesting, and restore previous default | |
43 // async waiters and loggers? | |
44 g_default_async_waiter = nullptr; | |
45 g_default_logger = nullptr; | |
46 } | |
47 | 18 |
48 // static | 19 // static |
49 const MojoAsyncWaiter* Environment::GetDefaultAsyncWaiter() { | 20 const MojoAsyncWaiter* Environment::GetDefaultAsyncWaiter() { |
50 assert(g_default_async_waiter); // Fails if not "inside" |Environment|. | 21 assert(g_default_async_waiter); // Fails if not "inside" |Environment|. |
51 return g_default_async_waiter; | 22 return g_default_async_waiter; |
52 } | 23 } |
53 | 24 |
54 // static | 25 // static |
55 const MojoLogger* Environment::GetDefaultLogger() { | 26 const MojoLogger* Environment::GetDefaultLogger() { |
56 assert(g_default_logger); // Fails if not "inside" |Environment|. | 27 assert(g_default_logger); // Fails if not "inside" |Environment|. |
(...skipping 16 matching lines...) Expand all Loading... |
73 } | 44 } |
74 | 45 |
75 // static | 46 // static |
76 void Environment::DestroyDefaultRunLoop() { | 47 void Environment::DestroyDefaultRunLoop() { |
77 assert(RunLoop::current()); | 48 assert(RunLoop::current()); |
78 delete RunLoop::current(); | 49 delete RunLoop::current(); |
79 assert(!RunLoop::current()); | 50 assert(!RunLoop::current()); |
80 } | 51 } |
81 | 52 |
82 } // namespace mojo | 53 } // namespace mojo |
OLD | NEW |