summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/environment/lib/environment.cc
blob: 0b0addd015a73a63342a008046a46ce749fcfda0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/public/cpp/environment/environment.h"

#include <assert.h>

#include "mojo/public/c/environment/logger.h"
#include "mojo/public/cpp/environment/lib/default_async_waiter.h"
#include "mojo/public/cpp/environment/lib/default_logger.h"
#include "mojo/public/cpp/environment/lib/default_task_tracker.h"
#include "mojo/public/cpp/utility/run_loop.h"

namespace mojo {

namespace {

const MojoAsyncWaiter* g_default_async_waiter = nullptr;
const MojoLogger* g_default_logger = nullptr;
const TaskTracker* g_default_task_tracker = nullptr;

void Init(const MojoAsyncWaiter* default_async_waiter,
          const MojoLogger* default_logger,
          const TaskTracker* default_task_tracker) {
  g_default_async_waiter = default_async_waiter
                               ? default_async_waiter
                               : &internal::kDefaultAsyncWaiter;
  g_default_logger =
      default_logger ? default_logger : &internal::kDefaultLogger;

  g_default_task_tracker = default_task_tracker
                               ? default_task_tracker
                               : &internal::kDefaultTaskTracker;

  RunLoop::SetUp();
}

}  // namespace

Environment::Environment() {
  Init(nullptr, nullptr, nullptr);
}

Environment::Environment(const MojoAsyncWaiter* default_async_waiter,
                         const MojoLogger* default_logger,
                         const TaskTracker* default_task_tracker) {
  Init(default_async_waiter, default_logger, default_task_tracker);
}

Environment::~Environment() {
  RunLoop::TearDown();

  // TODO(vtl): Maybe we should allow nesting, and restore previous default
  // async waiters and loggers?
  g_default_async_waiter = nullptr;
  g_default_logger = nullptr;
}

// static
const MojoAsyncWaiter* Environment::GetDefaultAsyncWaiter() {
  assert(g_default_async_waiter);  // Fails if not "inside" |Environment|.
  return g_default_async_waiter;
}

// static
const MojoLogger* Environment::GetDefaultLogger() {
  assert(g_default_logger);  // Fails if not "inside" |Environment|.
  return g_default_logger;
}

// static
const TaskTracker* Environment::GetDefaultTaskTracker() {
  return g_default_task_tracker;
}

// static
void Environment::InstantiateDefaultRunLoop() {
  assert(!RunLoop::current());
  // Not leaked: accessible from |RunLoop::current()|.
  RunLoop* run_loop = new RunLoop();
  ALLOW_UNUSED_LOCAL(run_loop);
  assert(run_loop == RunLoop::current());
}

// static
void Environment::DestroyDefaultRunLoop() {
  assert(RunLoop::current());
  delete RunLoop::current();
  assert(!RunLoop::current());
}

}  // namespace mojo