summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/platform/ScriptForbiddenScope.h
blob: 0c077ca37e4caf2b7f2343651836035e2f3269e7 (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
// 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.

#ifndef ScriptForbiddenScope_h
#define ScriptForbiddenScope_h

#include "platform/PlatformExport.h"
#include "wtf/Allocator.h"
#include "wtf/Optional.h"
#include "wtf/TemporaryChange.h"

namespace blink {

// Scoped disabling of script execution on the main thread,
// and only to be used by the main thread.
class PLATFORM_EXPORT ScriptForbiddenScope final {
    STACK_ALLOCATED();
    WTF_MAKE_NONCOPYABLE(ScriptForbiddenScope);
public:
    ScriptForbiddenScope();
    ~ScriptForbiddenScope();

    class PLATFORM_EXPORT AllowUserAgentScript final {
        STACK_ALLOCATED();
        WTF_MAKE_NONCOPYABLE(AllowUserAgentScript);
    public:
        AllowUserAgentScript();
        ~AllowUserAgentScript();
    private:
        Optional<TemporaryChange<unsigned>> m_change;
    };

    static void enter();
    static void exit();
    static bool isScriptForbidden();
};

// Scoped disabling of script execution on the main thread,
// if called on the main thread.
//
// No effect when used by from other threads -- simplifies
// call sites that might be used by multiple threads to have
// this scope object perform the is-main-thread check on
// its behalf.
class PLATFORM_EXPORT ScriptForbiddenIfMainThreadScope final {
    STACK_ALLOCATED();
    WTF_MAKE_NONCOPYABLE(ScriptForbiddenIfMainThreadScope);
public:
    ScriptForbiddenIfMainThreadScope();
    ~ScriptForbiddenIfMainThreadScope();
};

} // namespace blink

#endif // ScriptForbiddenScope_h