1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SIMPLE_METHOD_STATE_H_
18 #define SIMPLE_METHOD_STATE_H_
19 
20 #include <C2.h>
21 
22 namespace android {
23 
24 /**
25  * State for a simple method which returns c2_status_t and takes no parameters.
26  */
27 class SimpleMethodState {
28 public:
29     enum Mode {
30         // Execute the normal operation
31         EXECUTE,
32         // Don't do anything
33         NO_OP,
34         // Hang; never return
35         HANG,
36     };
37 
38     /**
39      * Injecter class that modifies the internal states of this class.
40      */
41     class Injecter {
42     public:
43         explicit Injecter(SimpleMethodState *thiz);
44 
45         /**
46          * Hang the operation.
47          */
48         void hang();
49 
50         /**
51          * Fail the operation with given params.
52          *
53          * \param err       error code to replace the actual return value
54          * \param execute   whether the wrapper should execute the operation
55          */
56         void fail(c2_status_t err, bool execute = false);
57 
58     private:
59         SimpleMethodState *const mThiz;
60     };
61 
62     SimpleMethodState();
63 
64     /**
65      * Get execution mode.
66      */
67     Mode getMode() const;
68 
69     /**
70      * Override result from running the operation if configured so.
71      */
72     bool overrideResult(c2_status_t *result) const;
73 
74 private:
75     Mode mMode;
76     bool mOverride;
77     c2_status_t mResultOverride;
78 };
79 
80 }  // namespace android
81 
82 #endif // SIMPLE_METHOD_STATE_H_
83