forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_call_restrictions.h
135 lines (112 loc) · 4.49 KB
/
sync_call_restrictions.h
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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.
#ifndef MOJO_PUBLIC_CPP_BINDINGS_SYNC_CALL_RESTRICTIONS_H_
#define MOJO_PUBLIC_CPP_BINDINGS_SYNC_CALL_RESTRICTIONS_H_
#include "base/component_export.h"
#include "base/macros.h"
#include "base/threading/thread_restrictions.h"
#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
#define ENABLE_SYNC_CALL_RESTRICTIONS 1
#else
#define ENABLE_SYNC_CALL_RESTRICTIONS 0
#endif
namespace sync_preferences {
class PrefServiceSyncable;
}
namespace leveldb {
class LevelDBMojoProxy;
}
namespace prefs {
class PersistentPrefStoreClient;
}
namespace ui {
class HostContextFactoryPrivate;
} // namespace ui
namespace viz {
class HostFrameSinkManager;
}
namespace mojo {
class ScopedAllowSyncCallForTesting;
// In some processes, sync calls are disallowed. For example, in the browser
// process we don't want any sync calls to child processes for performance,
// security and stability reasons. SyncCallRestrictions helps to enforce such
// rules.
//
// Before processing a sync call, the bindings call
// SyncCallRestrictions::AssertSyncCallAllowed() to check whether sync calls are
// allowed. By default sync calls are allowed but they may be globally
// disallowed within a process by calling DisallowSyncCall().
//
// If globally disallowed but you but you have a very compelling reason to
// disregard that (which should be very very rare), you can override it by
// constructing a ScopedAllowSyncCall object which allows making sync calls on
// the current sequence during its lifetime.
class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) SyncCallRestrictions {
public:
#if ENABLE_SYNC_CALL_RESTRICTIONS
// Checks whether the current sequence is allowed to make sync calls, and
// causes a DCHECK if not.
static void AssertSyncCallAllowed();
// Disables sync calls within the calling process. Any caller who wishes to
// make sync calls once this has been invoked must do so within the extent of
// a ScopedAllowSyncCall or ScopedAllowSyncCallForTesting.
static void DisallowSyncCall();
#else
// Inline the empty definitions of functions so that they can be compiled out.
static void AssertSyncCallAllowed() {}
static void DisallowSyncCall() {}
#endif
private:
// DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to mojo/OWNERS first.
// BEGIN ALLOWED USAGE.
// SynchronousCompositorHost is used for Android webview.
friend class content::SynchronousCompositorHost;
// LevelDBMojoProxy makes same-process sync calls from the DB thread.
friend class leveldb::LevelDBMojoProxy;
// Pref service connection is sync at startup.
friend class prefs::PersistentPrefStoreClient;
// Incognito pref service instances are created synchronously.
friend class sync_preferences::PrefServiceSyncable;
friend class mojo::ScopedAllowSyncCallForTesting;
// For destroying the GL context/surface that draw to a platform window before
// the platform window is destroyed.
friend class viz::HostFrameSinkManager;
// For preventing frame swaps of wrong size during resize on Windows.
// (https://crbug.com/811945)
friend class ui::HostContextFactoryPrivate;
// END ALLOWED USAGE.
#if ENABLE_SYNC_CALL_RESTRICTIONS
static void IncreaseScopedAllowCount();
static void DecreaseScopedAllowCount();
#else
static void IncreaseScopedAllowCount() {}
static void DecreaseScopedAllowCount() {}
#endif
// If a process is configured to disallow sync calls in general, constructing
// a ScopedAllowSyncCall object temporarily allows making sync calls on the
// current sequence. Doing this is almost always incorrect, which is why we
// limit who can use this through friend. If you find yourself needing to use
// this, talk to mojo/OWNERS.
class ScopedAllowSyncCall {
public:
ScopedAllowSyncCall() { IncreaseScopedAllowCount(); }
~ScopedAllowSyncCall() { DecreaseScopedAllowCount(); }
private:
#if ENABLE_SYNC_CALL_RESTRICTIONS
base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_wait_;
#endif
DISALLOW_COPY_AND_ASSIGN(ScopedAllowSyncCall);
};
DISALLOW_IMPLICIT_CONSTRUCTORS(SyncCallRestrictions);
};
class ScopedAllowSyncCallForTesting {
public:
ScopedAllowSyncCallForTesting() {}
~ScopedAllowSyncCallForTesting() {}
private:
SyncCallRestrictions::ScopedAllowSyncCall scoped_allow_sync_call_;
DISALLOW_COPY_AND_ASSIGN(ScopedAllowSyncCallForTesting);
};
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BINDINGS_SYNC_CALL_RESTRICTIONS_H_