summaryrefslogtreecommitdiffstats
path: root/chromium/mojo/shell/keep_alive.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/mojo/shell/keep_alive.cc')
-rw-r--r--chromium/mojo/shell/keep_alive.cc34
1 files changed, 34 insertions, 0 deletions
diff --git a/chromium/mojo/shell/keep_alive.cc b/chromium/mojo/shell/keep_alive.cc
new file mode 100644
index 00000000000..387c6a790c3
--- /dev/null
+++ b/chromium/mojo/shell/keep_alive.cc
@@ -0,0 +1,34 @@
+// 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/shell/keep_alive.h"
+
+#include "base/bind.h"
+#include "mojo/shell/context.h"
+
+namespace mojo {
+namespace shell {
+
+KeepAlive::KeepAlive(Context* context) : context_(context) {
+ DCHECK(context_->task_runners()->ui_runner()->RunsTasksOnCurrentThread());
+ ++context_->keep_alive_counter()->count_;
+}
+
+KeepAlive::~KeepAlive() {
+ DCHECK(context_->task_runners()->ui_runner()->RunsTasksOnCurrentThread());
+ if (--context_->keep_alive_counter()->count_ == 0) {
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&KeepAlive::MaybeQuit, context_));
+ }
+}
+
+// static
+void KeepAlive::MaybeQuit(Context* context) {
+ if (context->keep_alive_counter()->count_ == 0)
+ base::MessageLoop::current()->Quit();
+}
+
+} // namespace shell
+} // namespace mojo