Index: mojo/edk/system/request_context.cc |
diff --git a/mojo/edk/system/request_context.cc b/mojo/edk/system/request_context.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..98f8912371451b75e452b6e86ef5396fc05d7f5c |
--- /dev/null |
+++ b/mojo/edk/system/request_context.cc |
@@ -0,0 +1,68 @@ |
+// Copyright 2016 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/edk/system/request_context.h" |
+ |
+#include "base/lazy_instance.h" |
Anand Mistry (off Chromium)
2016/03/02 01:50:36
#include "base/logging.h"
Ken Rockot(use gerrit already)
2016/03/02 02:09:39
done
|
+#include "base/threading/thread_local.h" |
+ |
+namespace mojo { |
+namespace edk { |
+ |
+namespace { |
+ |
+base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky |
+ g_current_context; |
+ |
+} // namespace |
+ |
+RequestContext::RequestContext() { |
+ DCHECK(!g_current_context.Pointer()->Get()); |
+ g_current_context.Pointer()->Set(this); |
+} |
+ |
+RequestContext::~RequestContext() { |
+ // NOTE: Callbacks invoked by this destructor are allowed to initiate new |
+ // EDK requests on this thread, so we need to reset the thread-local context |
+ // pointer before calling them. |
+ DCHECK(this == g_current_context.Pointer()->Get()); |
+ g_current_context.Pointer()->Set(nullptr); |
+ |
+ for (const WatchNotifyFinalizer& watch : watch_notify_finalizers_.container()) |
+ watch.watcher->MaybeInvokeCallback(watch.result, watch.state); |
+ |
+ for (const scoped_refptr<Watcher>& watcher : |
+ watch_cancel_finalizers_.container()) |
+ watcher->Cancel(); |
+} |
+ |
+// static |
+RequestContext* RequestContext::current() { |
+ DCHECK(g_current_context.Pointer()->Get()); |
+ return g_current_context.Pointer()->Get(); |
+} |
+ |
+void RequestContext::AddWatchNotifyFinalizer( |
+ scoped_refptr<Watcher> watcher, |
+ MojoResult result, |
+ const HandleSignalsState& state) { |
+ watch_notify_finalizers_->push_back( |
+ WatchNotifyFinalizer(watcher, result, state)); |
+} |
+ |
+void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) { |
+ watch_cancel_finalizers_->push_back(watcher); |
+} |
+ |
+RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( |
+ scoped_refptr<Watcher> watcher, |
+ MojoResult result, |
+ const HandleSignalsState& state) |
+ : watcher(watcher), result(result), state(state) { |
+} |
+ |
+RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} |
+ |
+} // namespace edk |
+} // namespace mojo |