Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Side by Side Diff: mojo/edk/system/request_context.cc

Issue 1811433002: [mojo-edk] Expose notification source to MojoWatch callbacks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/edk/system/request_context.h" 5 #include "mojo/edk/system/request_context.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/threading/thread_local.h" 9 #include "base/threading/thread_local.h"
10 10
11 namespace mojo { 11 namespace mojo {
12 namespace edk { 12 namespace edk {
13 13
14 namespace { 14 namespace {
15 15
16 base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky 16 base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky
17 g_current_context = LAZY_INSTANCE_INITIALIZER; 17 g_current_context = LAZY_INSTANCE_INITIALIZER;
18 18
19 } // namespace 19 } // namespace
20 20
21 RequestContext::RequestContext() : tls_context_(g_current_context.Pointer()){ 21 RequestContext::RequestContext(Source source)
22 : source_(source), tls_context_(g_current_context.Pointer()) {
22 // We allow nested RequestContexts to exist as long as they aren't actually 23 // We allow nested RequestContexts to exist as long as they aren't actually
23 // used for anything. 24 // used for anything.
24 if (!tls_context_->Get()) 25 if (!tls_context_->Get())
yzshen1 2016/03/16 21:53:52 nit: it seems the two tls_context_->Get() (line 25
Ken Rockot(use gerrit already) 2016/03/17 00:15:33 Done
25 tls_context_->Set(this); 26 tls_context_->Set(this);
27
28 RequestContext* current_context = tls_context_->Get();
Anand Mistry (off Chromium) 2016/03/16 23:04:14 There's something very fishy about this code. Why
Ken Rockot(use gerrit already) 2016/03/17 00:15:33 I don't really know how I convinced myself that #4
29 outer_source_ = current_context->source_;
30 if (source != outer_source_) {
31 // This should only be possible if the outer context is from an external
32 // request and the inner context is from an API call.
33 CHECK(source == Source::LOCAL_API_CALL &&
34 outer_source_ == Source::EXTERNAL_PROCESS);
35 current_context->source_ = source;
36 }
26 } 37 }
27 38
28 RequestContext::~RequestContext() { 39 RequestContext::~RequestContext() {
29 // NOTE: Callbacks invoked by this destructor are allowed to initiate new 40 // NOTE: Callbacks invoked by this destructor are allowed to initiate new
30 // EDK requests on this thread, so we need to reset the thread-local context 41 // EDK requests on this thread, so we need to reset the thread-local context
31 // pointer before calling them. 42 // pointer before calling them.
43 RequestContext::Source notification_source = outer_source_;
32 if (IsCurrent()) 44 if (IsCurrent())
33 tls_context_->Set(nullptr); 45 tls_context_->Set(nullptr);
46 else
47 tls_context_->Get()->source_ = outer_source_;
48
49 MojoWatchNotificationFlags flags = MOJO_WATCH_NOTIFICATION_FLAG_NONE;
50 if (notification_source == Source::EXTERNAL_PROCESS)
51 flags |= MOJO_WATCH_NOTIFICATION_FLAG_EXTERNAL_PROCESS;
34 52
35 for (const WatchNotifyFinalizer& watch : 53 for (const WatchNotifyFinalizer& watch :
36 watch_notify_finalizers_.container()) { 54 watch_notify_finalizers_.container()) {
37 // Establish a new request context for the extent of each callback to ensure 55 // Establish a new request context for the extent of each callback to ensure
38 // that they don't themselves invoke callbacks while holding a watcher lock. 56 // that they don't themselves invoke callbacks while holding a watcher lock.
39 RequestContext request_context; 57 RequestContext request_context(notification_source);
40 watch.watcher->MaybeInvokeCallback(watch.result, watch.state); 58 watch.watcher->MaybeInvokeCallback(watch.result, watch.state, flags);
41 } 59 }
42 60
43 for (const scoped_refptr<Watcher>& watcher : 61 for (const scoped_refptr<Watcher>& watcher :
44 watch_cancel_finalizers_.container()) 62 watch_cancel_finalizers_.container())
45 watcher->Cancel(); 63 watcher->Cancel();
46 } 64 }
47 65
48 // static 66 // static
49 RequestContext* RequestContext::current() { 67 RequestContext* RequestContext::current() {
50 DCHECK(g_current_context.Pointer()->Get()); 68 DCHECK(g_current_context.Pointer()->Get());
(...skipping 22 matching lines...) Expand all
73 scoped_refptr<Watcher> watcher, 91 scoped_refptr<Watcher> watcher,
74 MojoResult result, 92 MojoResult result,
75 const HandleSignalsState& state) 93 const HandleSignalsState& state)
76 : watcher(watcher), result(result), state(state) { 94 : watcher(watcher), result(result), state(state) {
77 } 95 }
78 96
79 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} 97 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {}
80 98
81 } // namespace edk 99 } // namespace edk
82 } // namespace mojo 100 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698