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 <stddef.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
14namespace mojo {
15
16namespace {
17
18const MojoAsyncWaiter* g_default_async_waiter = NULL;
19const MojoLogger* g_default_logger = NULL;
20
21void Init(const MojoAsyncWaiter* default_async_waiter,
22          const MojoLogger* default_logger) {
23  g_default_async_waiter =
24      default_async_waiter ? default_async_waiter :
25                             &internal::kDefaultAsyncWaiter;
26  g_default_logger = default_logger ? default_logger :
27                                      &internal::kDefaultLogger;
28
29  RunLoop::SetUp();
30}
31
32}  // namespace
33
34Environment::Environment() {
35  Init(NULL, NULL);
36}
37
38Environment::Environment(const MojoAsyncWaiter* default_async_waiter,
39                         const MojoLogger* default_logger) {
40  Init(default_async_waiter, default_logger);
41}
42
43Environment::~Environment() {
44  RunLoop::TearDown();
45
46  // TODO(vtl): Maybe we should allow nesting, and restore previous default
47  // async waiters and loggers?
48}
49
50// static
51const MojoAsyncWaiter* Environment::GetDefaultAsyncWaiter() {
52  return g_default_async_waiter;
53}
54
55// static
56const MojoLogger* Environment::GetDefaultLogger() {
57  return g_default_logger;
58}
59
60}  // namespace mojo
61